/* Chat: calm, compact, light and dark. No external fonts or assets. */
:root {
  --bg: #faf9f6;
  --side: #f2f0eb;
  --surface: #ffffff;
  --surface-2: #f5f3ef;
  --text: #2a2926;
  --muted: #6f6b64;
  --border: #e3e0d8;
  --accent: #4b6b8f;
  --accent-text: #ffffff;
  --accent-soft: #e6ecf3;
  --user-bubble: #ece9e2;
  --code-bg: #f4f2ee;
  --danger: #b3412f;
  --ok: #3f7a4f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .05);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", "Times New Roman", serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1e1c; --side: #191816; --surface: #2a2926; --surface-2: #252421; --text: #ebe8e1; --muted: #a29e95;
    --border: #393731; --accent: #8fb0d6; --accent-text: #14181d; --accent-soft: #2a3542; --user-bubble: #34322d;
    --code-bg: #171614; --danger: #e07a66; --ok: #7fbf8e; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1f1e1c; --side: #191816; --surface: #2a2926; --surface-2: #252421; --text: #ebe8e1; --muted: #a29e95;
  --border: #393731; --accent: #8fb0d6; --accent-text: #14181d; --accent-soft: #2a3542; --user-bubble: #34322d;
  --code-bg: #171614; --danger: #e07a66; --ok: #7fbf8e; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  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; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .55; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 18px; letter-spacing: .2px; }
.brand-mark { width: 18px; height: 18px; border-radius: 6px; background: linear-gradient(135deg, var(--accent), #8a9fb5); display: inline-block; }

.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 7px 14px; }
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.06); }
.btn-sm { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.link-btn { background: none; border: 0; padding: 2px 4px; color: var(--muted); text-decoration: none; font-size: 13px; }
.link-btn:hover { color: var(--text); }
.icon-btn { background: none; border: 0; width: 30px; height: 30px; border-radius: 8px; color: var(--muted); font-size: 18px; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.form-error { color: var(--danger); font-size: 14px; margin: 4px 0; }

/* ---------- auth pages ---------- */
.auth-page { display: grid; place-items: center; padding: 24px 16px; min-height: 100%; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 28px; box-shadow: var(--shadow); }
.auth-card h1 { font-family: var(--serif); font-weight: 500; font-size: 22px; margin: 18px 0 14px; }
.auth-card form { display: grid; gap: 12px; }
.auth-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-card input { border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 9px 11px; font-size: 15px; color: var(--text); }
.auth-card input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.auth-card .btn-primary { margin-top: 4px; padding: 9px; }
.auth-tools { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }

/* ---------- app layout ---------- */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar { width: 264px; flex: none; background: var(--side); border-right: 1px solid var(--border); display: flex; flex-direction: column;
  padding: 14px 10px 10px; }
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 12px; }
.btn-new { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; background: transparent; border-color: transparent; }
.btn-new:hover { background: var(--surface-2); }
.btn-new .plus { width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-size: 15px; }
.conv-head { padding: 16px 8px 6px; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.conv-item { display: flex; align-items: center; border-radius: 8px; }
.conv-item:hover, .conv-item.active { background: var(--surface-2); }
.conv-item.active { background: var(--user-bubble); }
.conv-link { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 6px 8px; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-act { visibility: hidden; background: none; border: 0; color: var(--muted); padding: 2px 6px; font-size: 14px; }
.conv-item:hover .conv-act, .conv-item.active .conv-act { visibility: visible; }
.conv-act:hover { color: var(--text); }
.sidebar-foot { border-top: 1px solid var(--border); padding: 10px 6px 0; display: grid; gap: 6px; }
.user-line { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-size: 13px; font-weight: 600; }
.foot-tools { display: flex; flex-wrap: wrap; gap: 2px 6px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 18px; min-height: 48px; border-bottom: 1px solid transparent; }
.conv-title { flex: 1; min-width: 0; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-total { white-space: nowrap; }

.thread { flex: 1; overflow-y: auto; padding: 8px 16px 24px; position: relative; }
.thread-inner { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.empty { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
.empty h2 { font-family: var(--serif); font-weight: 400; font-size: 26px; color: var(--text); margin: 0 0 12vh; }

.msg-user { display: flex; justify-content: flex-end; }
.msg-user .bubble { background: var(--user-bubble); padding: 9px 14px; border-radius: 16px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-assistant { padding: 0 2px; }
.msg-note { align-self: center; }
.msg-note .cost-line { margin: 0; }
.msg-actions { margin-top: 2px; }
.cost-line { margin-top: 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.notice { margin-top: 8px; font-size: 13px; color: var(--muted); }
.notice-error { color: var(--danger); }

/* markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md table, .md pre { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { font-family: var(--serif); font-weight: 600; line-height: 1.3; margin: 18px 0 8px; }
.md h1 { font-size: 21px; } .md h2 { font-size: 19px; } .md h3 { font-size: 17px; } .md h4 { font-size: 15px; }
.md ul, .md ol { padding-left: 22px; }
.md li { margin: 3px 0; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--muted); }
.md code { font-family: var(--mono); font-size: 13px; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.md pre code { display: block; padding: 10px 12px; overflow-x: auto; background: none; font-size: 13px; line-height: 1.5; }
.code-bar { display: flex; justify-content: space-between; align-items: center; padding: 4px 8px 4px 12px; border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted); font-family: var(--font); }
.code-copy { background: none; border: 0; color: var(--muted); font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.code-copy:hover { background: var(--surface-2); color: var(--text); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.typing { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: pulse 1s infinite ease-in-out; }
@keyframes pulse { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }

/* highlight.js tokens */
.hljs-comment, .hljs-quote { color: var(--muted); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-built_in { color: #8a4baf; }
.hljs-string, .hljs-regexp, .hljs-addition { color: #3f7a4f; }
.hljs-number, .hljs-symbol, .hljs-bullet { color: #b5651d; }
.hljs-title, .hljs-section, .hljs-function .hljs-title { color: #2f62a8; }
.hljs-attr, .hljs-attribute, .hljs-variable, .hljs-template-variable, .hljs-type { color: #a0522d; }
.hljs-meta, .hljs-deletion { color: #b3412f; }
:root[data-theme="dark"] .hljs-keyword, :root[data-theme="dark"] .hljs-built_in, :root[data-theme="dark"] .hljs-literal { color: #c79be8; }
:root[data-theme="dark"] .hljs-string { color: #93c99f; }
:root[data-theme="dark"] .hljs-number { color: #e5a46a; }
:root[data-theme="dark"] .hljs-title { color: #8fb7ec; }
:root[data-theme="dark"] .hljs-attr, :root[data-theme="dark"] .hljs-type { color: #e0a17d; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .hljs-keyword, :root:not([data-theme="light"]) .hljs-built_in, :root:not([data-theme="light"]) .hljs-literal { color: #c79be8; }
  :root:not([data-theme="light"]) .hljs-string { color: #93c99f; }
  :root:not([data-theme="light"]) .hljs-number { color: #e5a46a; }
  :root:not([data-theme="light"]) .hljs-title { color: #8fb7ec; }
  :root:not([data-theme="light"]) .hljs-attr, :root:not([data-theme="light"]) .hljs-type { color: #e0a17d; }
}

/* composer */
.composer-wrap { padding: 0 16px 10px; }
.composer, .panel, .hint { max-width: 760px; margin-left: auto; margin-right: auto; }
.composer { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow); padding: 10px 10px 8px 14px; }
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.composer textarea { width: 100%; border: 0; outline: 0; resize: none; background: transparent; padding: 4px 2px; max-height: 260px; line-height: 1.5; }
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 4px; flex-wrap: wrap; }
.composer-left, .composer-right { display: flex; align-items: center; gap: 6px; }
.chip { border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px; padding: 3px 11px; font-size: 13px; color: var(--muted); }
.chip:hover { color: var(--text); border-color: var(--muted); }
.chip.busy { opacity: .6; pointer-events: none; }
.model-select { border: 1px solid transparent; background: transparent; border-radius: 8px; padding: 4px 6px; font-size: 13px; color: var(--muted); }
.model-select:hover { background: var(--surface-2); color: var(--text); }
.send-btn { width: 32px; height: 32px; border-radius: 10px; border: 0; background: var(--accent); color: var(--accent-text); font-size: 17px; line-height: 1; }
.send-btn.busy { opacity: .45; }
.hint { text-align: center; margin: 6px auto 0; font-size: 12px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 12px 12px;
  margin-bottom: 8px; max-height: 46vh; overflow-y: auto; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.panel-title { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.versions { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 8px; }
.version { border: 1px solid var(--border); border-radius: 12px; padding: 10px; background: var(--bg); display: flex; flex-direction: column; gap: 6px; }
.version-label { font-size: 12px; font-weight: 600; color: var(--accent); }
.version-text { font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; flex: 1; max-height: 220px; overflow-y: auto; }
.version-actions { display: flex; gap: 6px; }
.decisions { margin-top: 10px; }
.decisions ul { margin: 4px 0 0; padding-left: 20px; font-size: 14px; }

/* admin */
.admin-page { padding: 0 16px 40px; }
.admin-top { max-width: 1100px; margin: 0 auto; display: flex; justify-content: space-between; align-items: center; padding: 16px 0; gap: 12px; flex-wrap: wrap; }
.admin-main { max-width: 1100px; margin: 0 auto; display: grid; gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.card h2 { font-family: var(--serif); font-weight: 500; font-size: 18px; margin: 0 0 12px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form input { flex: 1; min-width: 200px; border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 7px 10px; }
.new-link { margin-top: 12px; }
.link-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.link-row code { font-family: var(--mono); font-size: 12px; background: var(--code-bg); padding: 6px 8px; border-radius: 8px; overflow-wrap: anywhere; flex: 1; min-width: 0; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .3px; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge-active, .badge-open { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge-disabled, .badge-revoked { background: color-mix(in srgb, var(--danger) 13%, transparent); color: var(--danger); }

/* mobile */
.only-mobile { display: none; }
.scrim { display: none; }
@media (max-width: 760px) {
  .only-mobile { display: inline-grid; place-items: center; }
  .sidebar { position: fixed; z-index: 20; inset: 0 auto 0 0; transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
    padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
  .sidebar-open .sidebar { transform: none; }
  .sidebar-open .scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, .3); }
  .topbar { padding: 8px 10px; }
  .composer-wrap { padding: 0 10px calc(8px + env(safe-area-inset-bottom, 0px)); }
  .msg-user .bubble { max-width: 92%; }
}
