:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --text: #1b2430;
  --muted: #5b6675;
  --border: #e2e6ec;
  --accent: #0f766e;
  --accent-strong: #0b5d57;
  --accent-soft: #e6f4f2;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --ok-soft: #e7f6ec;
  --info-soft: #eaf2fd;
  --code-bg: #f1f3f6;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 10px;
  --topbar: 58px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0f1419;
    --surface: #161d24;
    --text: #e6e9ed;
    --muted: #98a3b0;
    --border: #273039;
    --accent: #2dd4bf;
    --accent-strong: #5eead4;
    --accent-soft: #10302d;
    --danger: #f97066;
    --danger-soft: #3a1714;
    --ok-soft: #12301d;
    --info-soft: #13263d;
    --code-bg: #1f2830;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #0f1419;
  --surface: #161d24;
  --text: #e6e9ed;
  --muted: #98a3b0;
  --border: #273039;
  --accent: #2dd4bf;
  --accent-strong: #5eead4;
  --accent-soft: #10302d;
  --danger: #f97066;
  --danger-soft: #3a1714;
  --ok-soft: #12301d;
  --info-soft: #13263d;
  --code-bg: #1f2830;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}
a { color: var(--accent); text-underline-offset: 2px; }
a:hover { color: var(--accent-strong); }
h1, h2, h3, h4 { line-height: 1.25; }
h1 { font-size: 1.85rem; margin: 0.2em 0 0.5em; letter-spacing: -0.01em; }
h2 { font-size: 1.3rem; margin: 1.8em 0 0.6em; }
h3 { font-size: 1.08rem; margin: 1.5em 0 0.5em; }
code { background: var(--code-bg); padding: 0.1em 0.35em; border-radius: 5px; font-size: 0.9em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
.danger { color: var(--danger); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar);
  display: flex; align-items: center; gap: 16px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 650; white-space: nowrap; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 800;
}
.brand.big { justify-content: center; font-size: 1.25rem; margin-bottom: 8px; }
.brand.big .brand-mark { width: 40px; height: 40px; font-size: 1.2rem; }
.topsearch { position: relative; flex: 1; max-width: 520px; margin-left: auto; }
.topsearch input {
  width: 100%; height: 38px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--bg); color: var(--text); font: inherit; font-size: 0.95rem;
}
.topsearch input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.live-results {
  position: absolute; top: 44px; left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15); overflow: hidden; max-height: 70vh; overflow-y: auto;
}
.live-results a { display: grid; gap: 2px; padding: 10px 14px; text-decoration: none; color: var(--text); border-bottom: 1px solid var(--border); }
.live-results a span { color: var(--muted); font-size: 0.78rem; }
.live-results a small { color: var(--muted); font-size: 0.85rem; line-height: 1.4; }
.live-results a:hover, .live-results a.active { background: var(--accent-soft); }
.live-results a.all { color: var(--accent); font-size: 0.9rem; }
.live-results .empty { padding: 12px 14px; color: var(--muted); }
.topnav { display: flex; align-items: center; gap: 14px; }
.ask-link {
  padding: 6px 14px; border-radius: 999px; background: var(--accent); color: #fff; text-decoration: none; font-weight: 600; font-size: 0.92rem;
}
.ask-link:hover { background: var(--accent-strong); color: #fff; }
.usermenu { position: relative; }
.usermenu summary { cursor: pointer; list-style: none; color: var(--muted); font-size: 0.92rem; white-space: nowrap; }
.usermenu summary::-webkit-details-marker { display: none; }
.usermenu summary::after { content: ' ▾'; }
.usermenu-panel {
  position: absolute; right: 0; top: 30px; min-width: 190px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15); padding: 6px; display: grid;
}
.usermenu-panel a, .usermenu-panel button {
  display: block; width: 100%; text-align: left; padding: 8px 10px; border-radius: 6px;
  color: var(--text); text-decoration: none; background: none; border: 0; font: inherit; font-size: 0.92rem; cursor: pointer;
}
.usermenu-panel a:hover, .usermenu-panel button:hover { background: var(--accent-soft); }
.menu-btn { display: none; }

/* ---------- layout ---------- */
.shell { display: block; }
.shell.with-nav { display: grid; grid-template-columns: 280px minmax(0, 1fr); }
.sidebar {
  position: sticky; top: var(--topbar); height: calc(100vh - var(--topbar)); overflow-y: auto;
  padding: 18px 10px 40px 16px; border-right: 1px solid var(--border); font-size: 0.92rem;
}
.nav-section summary { cursor: pointer; padding: 6px 6px 6px 22px; border-radius: 6px; font-weight: 600; list-style: none; position: relative; }
.nav-section summary::-webkit-details-marker { display: none; }
.nav-section summary::before { content: ''; position: absolute; left: 8px; top: 14px; border: 4px solid transparent; border-left: 5px solid var(--muted); transition: transform 0.15s; transform-origin: 2px 4px; }
.nav-section[open] > summary::before { transform: rotate(90deg); }
.nav-section summary a { color: var(--text); text-decoration: none; }
.nav-section summary:hover { background: var(--accent-soft); }
.nav-section ul { list-style: none; margin: 2px 0 8px; padding: 0 0 0 12px; border-left: 1px solid var(--border); margin-left: 10px; }
.nav-section li a { display: block; padding: 4px 8px; color: var(--muted); text-decoration: none; border-radius: 6px; line-height: 1.35; }
.nav-section li a:hover { color: var(--text); background: var(--accent-soft); }
.nav-section li a[aria-current='page'] { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
main { min-width: 0; padding: 28px 40px 80px; }
.doc { max-width: 780px; margin: 0 auto; }
.doc.wide { max-width: 1100px; }
.doc.narrow { max-width: 460px; }

/* ---------- article ---------- */
.crumbs { margin: 0; font-size: 0.85rem; color: var(--muted); }
.crumbs a { color: var(--muted); }
.roles { display: flex; flex-wrap: wrap; gap: 6px; margin: -4px 0 18px; }
.roles span { font-size: 0.78rem; padding: 2px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-strong); }
.doc-body ol, .doc-body ul { padding-left: 1.4em; }
.doc-body li { margin: 0.3em 0; }
.doc-body li > p { margin: 0.2em 0; }
.doc-body img { max-width: 100%; }
.anchor { float: left; margin-left: -1em; padding-right: 0.25em; opacity: 0; text-decoration: none; color: var(--muted); }
h2:hover .anchor, h3:hover .anchor { opacity: 1; }
blockquote { margin: 1.2em 0; padding: 10px 16px; border-left: 4px solid var(--border); background: var(--surface); border-radius: 0 var(--radius) var(--radius) 0; }
blockquote p { margin: 0.3em 0; }
.callout-note, .callout-tip { border-left-color: var(--accent); background: var(--accent-soft); }
.callout-important, .callout-warning { border-left-color: #d97706; background: color-mix(in srgb, #d97706 12%, var(--surface)); }
.table-wrap { overflow-x: auto; margin: 1em 0; }
table { border-collapse: collapse; width: 100%; font-size: 0.93rem; background: var(--surface); }
th, td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: top; }
th { background: var(--bg); font-weight: 600; }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 48px; }
.pager a { display: grid; padding: 12px 16px; border: 1px solid var(--border); border-radius: var(--radius); text-decoration: none; background: var(--surface); }
.pager a small { color: var(--muted); }
.pager .next { text-align: right; }
.ask-more { margin-top: 24px; color: var(--muted); font-size: 0.92rem; }

/* ---------- home ---------- */
.hero { max-width: 760px; margin: 20px auto 36px; text-align: center; }
.hero h1 { font-size: 2.3rem; }
.lead { font-size: 1.1rem; color: var(--muted); margin: 0 0 22px; }
.hero-ask { display: flex; gap: 8px; }
.hero-ask input {
  flex: 1; min-width: 0; height: 50px; padding: 0 18px; font: inherit;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.hero-ask input:focus { outline: 2px solid var(--accent); }
.cards { max-width: 1100px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.card {
  display: flex; flex-direction: column; padding: 18px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); text-decoration: none; box-shadow: var(--shadow); transition: border-color 0.15s;
}
.card:hover { border-color: var(--accent); color: var(--text); }
.card h3 { margin: 0 0 6px; }
.card p { margin: 0 0 10px; color: var(--muted); font-size: 0.92rem; line-height: 1.45; flex: 1; }
.card .count { font-size: 0.8rem; color: var(--accent); font-weight: 600; }

.article-list { list-style: none; padding: 0; margin: 16px 0; }
.article-list li { display: grid; gap: 2px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.article-list li a { font-weight: 600; text-decoration: none; }
.article-list li span { color: var(--muted); font-size: 0.92rem; }
.article-list li .where { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; }

/* ---------- forms ---------- */
input, select, textarea { font: inherit; color: var(--text); }
.stack { display: grid; gap: 14px; }
.stack label, .grid-form label { display: grid; gap: 4px; font-size: 0.9rem; font-weight: 600; }
.stack input, .grid-form input, .grid-form select, .inline-form input {
  height: 42px; padding: 0 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); font-weight: 400;
}
.grid-form { display: grid; grid-template-columns: 1.3fr 1.3fr 1fr auto; gap: 10px; align-items: end; }
.inline-form { display: flex; gap: 8px; margin-bottom: 8px; }
.inline-form input { flex: 1; min-width: 0; }
button { font: inherit; cursor: pointer; border-radius: 8px; }
button.primary {
  height: 42px; padding: 0 20px; border: 0; background: var(--accent); color: #fff; font-weight: 600;
}
[data-theme] button.primary, button.primary { color: #fff; }
@media (prefers-color-scheme: dark) { :root:not([data-theme='light']) button.primary, :root:not([data-theme='light']) .ask-link, :root:not([data-theme='light']) .brand-mark { color: #0f1419; } }
button.primary:hover { background: var(--accent-strong); }
button.primary:disabled { opacity: 0.55; cursor: default; }
button.ghost { padding: 6px 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
button.ghost:hover { border-color: var(--accent); }
button.small { padding: 4px 10px; font-size: 0.82rem; }
button.danger { color: var(--danger); }
.alert { padding: 10px 14px; border-radius: 8px; background: var(--danger-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); }
.alert.ok { background: var(--ok-soft); border-color: color-mix(in srgb, #16a34a 35%, transparent); }
.alert.info { background: var(--info-soft); border-color: color-mix(in srgb, #2563eb 35%, transparent); }
.otp { font-size: 1.1rem; padding: 3px 8px; user-select: all; }

/* ---------- sign-in ---------- */
body.auth { display: grid; min-height: 100vh; place-items: center; padding: 16px; }
body.auth main { padding: 0; width: 100%; }
.auth-card {
  width: 100%; max-width: 400px; margin: 0 auto; padding: 32px 28px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); text-align: center;
}
.auth-card form { text-align: left; margin-top: 18px; }
.auth-card .primary { width: 100%; }

/* ---------- admin ---------- */
table.users td.actions { display: flex; flex-wrap: wrap; gap: 6px; border-width: 0 0 1px 0; }
table.users tr.disabled td { opacity: 0.6; }
.stats { font-weight: 600; }

/* ---------- assistant ---------- */
.ask-form { position: sticky; bottom: 0; padding: 12px 0 16px; background: linear-gradient(transparent, var(--bg) 18px); }
.ask-form textarea {
  width: 100%; padding: 12px 14px; resize: vertical; min-height: 56px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow);
}
.ask-form textarea:focus { outline: 2px solid var(--accent); }
.ask-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.turn { margin: 22px 0; }
.turn .q {
  margin: 0 0 10px auto; width: fit-content; max-width: 85%;
  padding: 10px 14px; border-radius: 14px 14px 4px 14px; background: var(--accent-soft); white-space: pre-wrap;
}
.turn .a { padding: 4px 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; }
.turn .a.failed { border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.turn .a ol, .turn .a ul { padding-left: 1.3em; }
.thinking { color: var(--muted); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
.sources { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 10px; font-size: 0.85rem; color: var(--muted); }
.sources a { padding: 2px 10px; border: 1px solid var(--border); border-radius: 999px; text-decoration: none; background: var(--surface); }
.feedback { margin-top: 8px; font-size: 0.85rem; color: var(--muted); }
.feedback button { margin-left: 6px; padding: 2px 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 0.82rem; }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  .shell.with-nav { display: block; }
  .menu-btn {
    display: grid; gap: 4px; width: 36px; height: 36px; padding: 9px 7px; border: 1px solid var(--border); background: var(--surface);
  }
  .menu-btn span { display: block; height: 2px; background: var(--text); border-radius: 2px; }
  .sidebar {
    position: fixed; top: var(--topbar); left: 0; bottom: 0; width: min(320px, 86vw); height: auto; z-index: 15;
    background: var(--surface); transform: translateX(-100%); transition: transform 0.2s; box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
  }
  body.nav-open .sidebar { transform: none; }
  main { padding: 20px 16px 60px; }
  .topbar { gap: 10px; padding: 0 12px; }
  .brand-name { display: none; }
  .usermenu summary { max-width: 90px; overflow: hidden; text-overflow: ellipsis; }
  .grid-form { grid-template-columns: 1fr; }
  .hero h1 { font-size: 1.8rem; }
  .hero-ask { flex-direction: column; }
}
@media (max-width: 520px) {
  .topnav .ask-link { padding: 6px 10px; }
  .pager { grid-template-columns: 1fr; }
}
