:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #f0f0ec;
  --border: #e2e2dc;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --accent: #0f766e;
  --accent-hover: #0d625b;
  --accent-text: #ffffff;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --user-bubble: #e6f2f0;
  --code-bg: #f3f3ef;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  --radius: 12px;
  --sidebar-w: 280px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161716;
    --surface: #1e1f1e;
    --surface-2: #262826;
    --border: #343634;
    --text: #ececea;
    --muted: #a0a29e;
    --accent: #2dd4bf;
    --accent-hover: #5eead4;
    --accent-text: #052e2b;
    --danger: #f87171;
    --danger-bg: #3a1d1b;
    --user-bubble: #1f3532;
    --code-bg: #111211;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 30%);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #161716;
  --surface: #1e1f1e;
  --surface-2: #262826;
  --border: #343634;
  --text: #ececea;
  --muted: #a0a29e;
  --accent: #2dd4bf;
  --accent-hover: #5eead4;
  --accent-text: #052e2b;
  --danger: #f87171;
  --danger-bg: #3a1d1b;
  --user-bubble: #1f3532;
  --code-bg: #111211;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 30%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: -9999px; }
.skip-link:focus { left: 8px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; border-radius: 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pad { padding: 8px 12px; }

/* Buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); background: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn-icon { width: 38px; padding: 0; }
.btn-block { width: 100%; }
input, select, textarea {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 8px 10px; min-height: 38px;
}
.select { max-width: 220px; }
label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 4px; }
.form-error { color: var(--danger); font-size: 14px; margin: 10px 0 0; }
.badge {
  display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: var(--user-bubble); color: var(--accent); font-weight: 600;
}
.badge-warn { background: var(--danger-bg); color: var(--danger); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }

/* Login */
.login-view { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(400px, 100%); padding: 28px; }
.login-card input { width: 100%; }
.login-card .btn { margin-top: 18px; }
.brand { font-weight: 700; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.brand-mark { color: var(--accent); }

/* App layout */
.app { display: flex; height: 100%; }
.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w); display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border);
}
.sidebar-head, .sidebar-foot { padding: 12px; }
.sidebar-foot { border-top: 1px solid var(--border); display: grid; gap: 8px; }
.user-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.conv-list-wrap { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-item { display: flex; align-items: center; border-radius: 8px; }
.conv-item:hover, .conv-item.active { background: var(--surface-2); }
.conv-item button.conv-open {
  flex: 1; text-align: left; background: none; border: 0; padding: 9px 10px; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; border-radius: 8px;
}
.conv-item button.conv-delete { visibility: hidden; background: none; border: 0; cursor: pointer; padding: 6px 8px; color: var(--muted); border-radius: 6px; }
.conv-item:hover button.conv-delete, .conv-item.active button.conv-delete, .conv-item button.conv-delete:focus-visible { visibility: visible; }
.conv-item button.conv-delete:hover { color: var(--danger); }
.sidebar-backdrop { display: none; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.conv-title { font-size: 16px; font-weight: 600; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
#toggle-sidebar { display: none; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px; scroll-behavior: smooth; }
.msg { max-width: 820px; margin: 0 auto 18px; display: flex; flex-direction: column; }
.msg-user { align-items: flex-end; }
.msg-user .msg-body {
  background: var(--user-bubble); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 85%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-assistant .msg-body { width: 100%; overflow-wrap: anywhere; }
.msg-meta { font-size: 12px; color: var(--muted); margin: 0 0 4px; display: flex; gap: 8px; align-items: center; }
.msg-actions { display: flex; gap: 4px; margin-top: 6px; }
.msg-actions .btn { min-height: 28px; font-size: 12px; padding: 0 8px; color: var(--muted); }
.msg-error { color: var(--danger); font-size: 14px; }
.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing span { animation: none; } .messages { scroll-behavior: auto; } }

/* Markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 1.1em 0 .5em; }
.md h1 { font-size: 1.4em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; }
.md p, .md ul, .md ol, .md blockquote, .md table { margin: .6em 0; }
.md ul, .md ol { padding-left: 1.4em; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--muted); margin-left: 0; }
.md code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: .1em .35em; border-radius: 5px; }
.md pre code { background: none; padding: 0; font-size: 13px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); }
.md hr { border: 0; border-top: 1px solid var(--border); }
.code-block { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin: .8em 0; background: var(--code-bg); }
.code-head {
  display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 4px 12px;
  font: 12px var(--mono); color: var(--muted); border-bottom: 1px solid var(--border); background: var(--surface-2);
}
.code-head .btn { min-height: 26px; font-size: 12px; padding: 0 8px; }
.code-block pre { margin: 0; padding: 12px; overflow-x: auto; font-family: var(--mono); }

/* Welcome */
.welcome { max-width: 680px; margin: 8vh auto 0; text-align: center; }
.welcome h2 { font-size: 26px; margin: 0 0 8px; }
.suggestions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 24px; }
.suggestion {
  text-align: left; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: 14px;
}
.suggestion:hover { background: var(--surface-2); }

/* Composer */
.error-banner {
  max-width: 820px; width: calc(100% - 32px); margin: 0 auto 8px; display: flex; gap: 8px; align-items: center;
  background: var(--danger-bg); color: var(--danger); padding: 8px 12px; border-radius: 10px; font-size: 14px;
}
.error-banner span { flex: 1; }
.composer {
  max-width: 820px; width: calc(100% - 32px); margin: 0 auto; display: flex; gap: 8px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 8px; box-shadow: var(--shadow);
}
.composer textarea { flex: 1; border: 0; resize: none; max-height: 240px; min-height: 40px; padding: 9px 8px; background: transparent; outline: none; }
.composer:focus-within { border-color: var(--accent); }
.footnote { text-align: center; margin: 6px 0 10px; }

/* Dialog & toast */
.dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 20px; width: min(440px, calc(100% - 32px)); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog h2 { margin: 0 0 8px; font-size: 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 999px; font-size: 14px; z-index: 50; box-shadow: var(--shadow);
}

/* Tablet & mobile */
@media (max-width: 900px) {
  #toggle-sidebar { display: inline-flex; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / 35%); z-index: 20; }
}
@media (max-width: 600px) {
  .topbar { padding: 8px; flex-wrap: wrap; }
  .topbar-actions { width: 100%; }
  .select { flex: 1; max-width: none; }
  .suggestions { grid-template-columns: 1fr; }
  .welcome { margin-top: 4vh; }
  .messages { padding: 16px 12px; }
  .composer, .error-banner { width: calc(100% - 16px); }
  .msg-user .msg-body { max-width: 92%; }
}

/* Admin */
.admin-wrap { max-width: 1200px; margin: 0 auto; padding: 16px; }
.admin-header { display: flex; align-items: center; gap: 12px; justify-content: space-between; flex-wrap: wrap; margin-bottom: 16px; }
.admin-header h1 { margin: 0; font-size: 22px; }
.grid-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { padding: 14px 16px; }
.stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .value.ok { color: var(--accent); }
.stat .value.bad { color: var(--danger); }
.panel { padding: 16px; margin-bottom: 16px; }
.panel h2 { font-size: 16px; margin: 0 0 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th, table.data td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
table.data th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 14px; }
.form-grid input, .form-grid select { width: 100%; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 6px; }
.checks label { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; margin: 0; font-size: 14px; }
.checks input { min-height: auto; }
.secret-box { font-family: var(--mono); background: var(--code-bg); padding: 10px; border-radius: 8px; word-break: break-all; border: 1px solid var(--border); user-select: all; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 800px) { .two-col { grid-template-columns: 1fr; } }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; font-size: 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.mt { margin-top: 14px; }
