/* CST Local LLM Gateway web application. Light theme by default, dark via
   prefers-color-scheme or the explicit data-theme attribute. */

:root {
  --bg: #f4f3ef;
  --panel: #ffffff;
  --side: #e9e7e0;
  --text: #1d2125;
  --muted: #6b7076;
  --line: #d7d4cb;
  --accent: #1f6f63;
  --accent-text: #ffffff;
  --accent-soft: #d6ebe6;
  --danger: #b4382f;
  --user-bubble: #e1ece9;
  --code-bg: #f0eee8;
  --think-bg: #f7f5ee;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181b; --panel: #1c2024; --side: #111417; --text: #e3e6e8; --muted: #8d949a;
    --line: #2c3238; --accent: #3fae9c; --accent-text: #0d1513; --accent-soft: #1e3a35;
    --danger: #e0645a; --user-bubble: #22312e; --code-bg: #111417; --think-bg: #191d21;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15181b; --panel: #1c2024; --side: #111417; --text: #e3e6e8; --muted: #8d949a;
  --line: #2c3238; --accent: #3fae9c; --accent-text: #0d1513; --accent-soft: #1e3a35;
  --danger: #e0645a; --user-bubble: #22312e; --code-bg: #111417; --think-bg: #191d21;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.pad { padding: 8px 12px; }
.brand { font-weight: 700; letter-spacing: .02em; }
.link { color: var(--accent); }

/* ----- buttons & inputs ----- */
.btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
  border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.tiny { padding: 2px 8px; font-size: 12px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ----- login ----- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }

/* ----- layout ----- */
.layout { display: grid; grid-template-columns: 270px 1fr; height: 100%; }
.sidebar {
  background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 12px; }
.chat-list { flex: 1; overflow-y: auto; padding: 4px 6px; }
.chat-item { display: flex; align-items: center; border-radius: 8px; }
.chat-item:hover, .chat-item.active { background: var(--accent-soft); }
.chat-open {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: inherit; font: inherit;
  padding: 8px 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer;
}
.icon-btn {
  background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px; visibility: hidden;
}
.chat-item:hover .icon-btn, .chat-item.active .icon-btn { visibility: visible; }
.icon-btn:hover { color: var(--text); }
.sidebar-foot {
  border-top: 1px solid var(--line); padding: 10px 12px; display: flex; flex-wrap: wrap;
  align-items: center; gap: 8px; font-size: 13px;
}

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 18px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.chat-title { flex: 1; margin: 0; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; }
.model-pick { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.only-mobile { display: none; }

/* ----- messages ----- */
.messages { flex: 1; overflow-y: auto; padding: 20px max(18px, calc((100% - 820px) / 2)); }
.empty-hint { text-align: center; margin-top: 12vh; }
.msg { margin: 0 0 18px; }
.msg-role { font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.msg.user .msg-body {
  background: var(--user-bubble); border-radius: var(--radius); padding: 10px 14px; display: inline-block; max-width: 100%;
}
.msg.user { text-align: right; }
.msg.user .msg-body { text-align: left; }
.plain { white-space: pre-wrap; word-wrap: break-word; }
.msg-body p { margin: 0 0 .7em; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { margin: 0 0 .7em; padding-left: 1.4em; }
.msg-body blockquote { margin: 0 0 .7em; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.msg-body a { color: var(--accent); }
.md-h { margin: .6em 0 .4em; line-height: 1.3; }
code.inline { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
.code-block { border: 1px solid var(--line); border-radius: 8px; margin: 0 0 .8em; overflow: hidden; }
.code-bar {
  display: flex; justify-content: space-between; align-items: center; padding: 3px 8px;
  background: var(--side); font-size: 12px; color: var(--muted);
}
.code-block pre { margin: 0; padding: 10px 12px; overflow-x: auto; background: var(--code-bg); }
.code-block code { font-family: var(--mono); font-size: 13px; }
.msg-meta { font-size: 12px; color: var(--muted); margin-top: 6px; }

.reasoning {
  background: var(--think-bg); border: 1px dashed var(--line); border-radius: 8px;
  margin: 0 0 8px; font-size: 13px; color: var(--muted);
}
.reasoning summary { cursor: pointer; padding: 6px 10px; user-select: none; }
.reasoning-text { white-space: pre-wrap; padding: 0 12px 10px; max-height: 320px; overflow-y: auto; }

.waiting { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }
.wait-clock { font-variant-numeric: tabular-nums; }
.spinner {
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line);
  border-top-color: var(--accent); animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ----- composer ----- */
.composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 12px max(18px, calc((100% - 820px) / 2)) 18px; border-top: 1px solid var(--line); background: var(--panel);
}
.composer textarea { flex: 1; resize: none; min-height: 42px; max-height: 240px; }

/* ----- admin ----- */
.admin { max-width: 1000px; margin: 0 auto; padding: 20px 16px 40px; }
.admin header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.admin h1 { font-size: 20px; margin: 0; }
.admin h2 { font-size: 16px; margin: 24px 0 10px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.card .num { font-size: 22px; font-weight: 700; }
.card .lbl { font-size: 12px; color: var(--muted); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); font-size: 14px; white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
td.detail { white-space: normal; word-break: break-word; }

/* ----- admin: gateway card ----- */
.gw { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.gw .lbl { font-size: 12px; color: var(--muted); }
.gw-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; }
.gw-model { font-size: 20px; font-weight: 700; }
.gw-state-wrap { display: flex; align-items: center; gap: 8px; }
.gw-state { border-radius: 999px; padding: 3px 12px; font-size: 13px; font-weight: 600; background: var(--side); }
.gw-state.st-READY { background: var(--accent-soft); color: var(--accent); }
.gw-state.st-SWITCHING, .gw-state.st-DRAINING { background: var(--think-bg); color: var(--text); border: 1px dashed var(--accent); }
.gw-state.st-FAILED { background: var(--danger); color: #fff; }
.gw-facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px 16px; margin: 0 0 12px; }
.gw-facts div { min-width: 0; }
.gw-facts dt { font-size: 12px; color: var(--muted); }
.gw-facts dd { margin: 0; font-size: 14px; word-break: break-word; }
.gw-hbm { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px 16px; margin: 4px 0 12px; }
.gw-bar-row { font-size: 13px; }
.gw-bar-top { display: flex; justify-content: space-between; gap: 8px; font-variant-numeric: tabular-nums; }
.gw-bar { height: 8px; border-radius: 4px; background: var(--side); border: 1px solid var(--line); overflow: hidden; margin-top: 3px; }
.gw-bar-fill { height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.gw-bar-fill.high { background: var(--danger); }
.gw-alert {
  border: 1px solid var(--danger); border-left-width: 4px; border-radius: 8px; padding: 10px 12px;
  margin-bottom: 12px; color: var(--danger); font-size: 14px; white-space: pre-wrap; word-break: break-word;
}
.gw-alert strong { display: block; margin-bottom: 4px; }
.gw-actions { margin-top: 4px; }
.gw-note { font-size: 12px; margin: 8px 0 0; }
.gw-note:empty { display: none; }

/* ----- narrow screens ----- */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 25% 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s;
  }
  .sidebar.open { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35); }
  .only-mobile { display: inline-block; }
  .model-pick span { display: none; }
  .messages, .composer { padding-left: 16px; padding-right: 16px; }
  .icon-btn { visibility: visible; }
}
