/* Qwen3 Chat — styles (dark + light, mobile friendly) */
:root {
  color-scheme: dark;
  --bg: #111110;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --surface-3: #2c2c29;
  --border: #33332f;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --accent: #3987e5;
  --accent-ink: #ffffff;
  --series-1: #3987e5;
  --user-bubble: #23324a;
  --good: #3fae5a;
  --critical: #e66767;
  --code-bg: #0d0d0c;
  --radius: 12px;
  --sidebar-w: 280px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
@media (prefers-color-scheme: light) {
  :root:where(:not([data-theme="dark"])) {
    color-scheme: light;
    --bg: #f5f5f3; --surface-1: #fcfcfb; --surface-2: #f0efec; --surface-3: #e7e6e1; --border: #e0dfda;
    --text-primary: #0b0b0b; --text-secondary: #52514e; --text-muted: #77766f;
    --accent: #2a78d6; --series-1: #2a78d6; --user-bubble: #e3edfa;
    --good: #1f8a3b; --critical: #c93b3a; --code-bg: #f3f2ee;
  }
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f5f3; --surface-1: #fcfcfb; --surface-2: #f0efec; --surface-3: #e7e6e1; --border: #e0dfda;
  --text-primary: #0b0b0b; --text-secondary: #52514e; --text-muted: #77766f;
  --accent: #2a78d6; --series-1: #2a78d6; --user-bubble: #e3edfa;
  --good: #1f8a3b; --critical: #c93b3a; --code-bg: #f3f2ee;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text-primary); font: 15px/1.6 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------- layout */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; height: 100dvh; }
.main { min-width: 0; display: flex; flex-direction: column; height: 100dvh; }
.view { display: none; flex-direction: column; height: 100%; min-height: 0; }
.view.active { display: flex; }

/* ---------------------------------------------------------- sidebar */
.sidebar { background: var(--surface-1); border-right: 1px solid var(--border); display: flex; flex-direction: column; gap: 12px; padding: 14px; min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 4px 6px; font-weight: 650; font-size: 16px; }
.brand img { border-radius: 8px; }
.nav { display: grid; gap: 2px; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 0; border-radius: 8px; background: transparent; color: var(--text-secondary); cursor: pointer; text-align: left; }
.nav-item:hover { background: var(--surface-2); color: var(--text-primary); }
.nav-item.active { background: var(--surface-3); color: var(--text-primary); }
.conv-head { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.conv-head span { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); padding: 0 4px; }
.conv-head input { width: 100%; padding: 7px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); outline: none; }
.conv-head input:focus { border-color: var(--accent); }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; display: grid; align-content: start; gap: 1px; }
.conv { display: flex; align-items: center; border-radius: 8px; }
.conv:hover, .conv.active { background: var(--surface-2); }
.conv.active { background: var(--surface-3); }
.conv-open { flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 8px 10px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-secondary); }
.conv.active .conv-open { color: var(--text-primary); }
.conv-del { border: 0; background: transparent; color: var(--text-muted); padding: 6px 8px; cursor: pointer; opacity: 0; border-radius: 6px; }
.conv:hover .conv-del, .conv-del:focus-visible { opacity: 1; }
.conv-del:hover { color: var(--critical); }
.conv-del svg { width: 16px; height: 16px; }
.conv-empty { padding: 8px 10px; font-size: 13px; }
.sidebar-foot { display: flex; align-items: center; justify-content: space-between; border-top: 1px solid var(--border); padding-top: 10px; }
.foot-actions { display: flex; gap: 4px; }
.status { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-secondary); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); display: inline-block; }
.status.ok .status-dot, .status-dot.ok { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }
.status.bad .status-dot, .status-dot.bad { background: var(--critical); }
.scrim { display: none; }

/* ---------------------------------------------------------- buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; font-weight: 550; }
.btn:hover { background: var(--surface-3); }
.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn-ghost { background: transparent; }
.btn-danger { background: transparent; color: var(--critical); border-color: color-mix(in srgb, var(--critical) 40%, transparent); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent; color: var(--text-secondary); cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); color: var(--text-primary); }
.link-btn { border: 0; background: none; padding: 2px 6px; border-radius: 6px; color: var(--text-muted); cursor: pointer; font-size: 12.5px; }
.link-btn:hover { color: var(--text-primary); background: var(--surface-2); }

/* ---------------------------------------------------------- topbar */
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 18px; border-bottom: 1px solid var(--border); min-height: 56px; }
.menu-btn { display: none; }
.chat-title { font-size: 15px; font-weight: 600; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-controls { display: flex; align-items: center; gap: 12px; }
.select-wrap select { appearance: none; padding: 7px 32px 7px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238f8e86' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center / 14px; cursor: pointer; font-weight: 550; }
.toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; color: var(--text-secondary); font-size: 14px; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track { width: 34px; height: 20px; border-radius: 99px; background: var(--surface-3); position: relative; transition: background .15s; }
.toggle-thumb { position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; border-radius: 50%; background: var(--text-secondary); transition: transform .15s, background .15s; }
.toggle input:checked + .toggle-track { background: var(--accent); }
.toggle input:checked + .toggle-track .toggle-thumb { transform: translateX(14px); background: #fff; }
.toggle input:focus-visible + .toggle-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------- messages */
.messages { flex: 1; overflow-y: auto; padding: 24px 18px 8px; scroll-behavior: auto; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; padding: 8vh 0 0; }
.empty img { border-radius: 14px; }
.empty h2 { margin: 16px 0 4px; font-size: 24px; font-weight: 650; }
.chips { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin: 28px auto 0; max-width: 640px; }
.chip { text-align: left; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-secondary); cursor: pointer; font-size: 14px; line-height: 1.4; }
.chip:hover { background: var(--surface-2); color: var(--text-primary); }

.msg { display: flex; margin-bottom: 22px; }
.msg-user { justify-content: flex-end; }
.msg-user .bubble { background: var(--user-bubble); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 80%; }
.user-text { white-space: pre-wrap; word-wrap: break-word; }
.msg-assistant .bubble { width: 100%; min-width: 0; }
.msg-meta { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; }
.msg-actions { display: flex; gap: 2px; }
.msg-error { margin-top: 8px; padding: 8px 12px; border-radius: 8px; color: var(--critical); background: color-mix(in srgb, var(--critical) 12%, transparent); font-size: 14px; }
.msg-error::before { content: "⚠ "; }

.thinking { margin-bottom: 10px; border-left: 2px solid var(--border); padding-left: 12px; }
.thinking summary { cursor: pointer; color: var(--text-muted); font-size: 13px; list-style: none; }
.thinking summary::before { content: "▸ "; }
.thinking[open] summary::before { content: "▾ "; }
.thinking-body { white-space: pre-wrap; color: var(--text-muted); font-size: 13.5px; margin-top: 6px; max-height: 320px; overflow: auto; }

.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; opacity: .6; } }

/* markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h2, .md h3, .md h4 { margin: 20px 0 8px; line-height: 1.3; }
.md h2 { font-size: 20px; } .md h3 { font-size: 17px; } .md h4, .md h5, .md h6 { font-size: 15px; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 24px; }
.md li { margin: 3px 0; }
.md a { color: var(--accent); }
.md code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); padding: 2px 5px; border-radius: 5px; }
.md blockquote { margin: 0 0 12px; padding: 4px 14px; border-left: 3px solid var(--border); color: var(--text-secondary); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
.code-block { margin: 0 0 14px; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--code-bg); }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; font-size: 12px; color: var(--text-muted); background: var(--surface-2); border-bottom: 1px solid var(--border); }
.copy-code { border: 0; background: none; color: var(--text-muted); cursor: pointer; font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.copy-code:hover { color: var(--text-primary); background: var(--surface-3); }
.code-block pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code-block code { background: none; padding: 0; font-size: 13.5px; line-height: 1.55; white-space: pre; }
.md-table { overflow-x: auto; margin: 0 0 14px; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); font-weight: 600; }

/* ---------------------------------------------------------- composer */
.composer { padding: 8px 18px 14px; }
.composer-box { max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; padding: 8px 8px 8px 16px; border: 1px solid var(--border); border-radius: 16px; background: var(--surface-1); }
.composer-box:focus-within { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: transparent; padding: 7px 0; max-height: 220px; line-height: 1.5; }
.send-btn { width: 38px; height: 38px; border-radius: 11px; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; }
.send-btn .i-stop { display: none; fill: currentColor; stroke: none; }
.send-btn.stop { background: var(--text-primary); color: var(--bg); }
.send-btn.stop .i-send { display: none; } .send-btn.stop .i-stop { display: block; }
.composer-hint { text-align: center; font-size: 12px; margin: 8px 0 0; }

/* ---------------------------------------------------------- dashboard */
.dash { flex: 1; overflow-y: auto; padding: 20px 18px 28px; display: grid; gap: 16px; align-content: start; }
.dash > * { width: 100%; max-width: 1100px; margin: 0 auto; }
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 2px; }
.tile-label { font-size: 13px; color: var(--text-secondary); }
.tile-value { font-size: 28px; font-weight: 650; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.tile-sub { font-size: 12.5px; color: var(--text-muted); }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); gap: 16px; }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; position: relative; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head.mt { margin-top: 18px; }
.card h3 { margin: 0; font-size: 14px; font-weight: 600; }
.chart { width: 100%; }
.bar-svg { width: 100%; height: auto; display: block; overflow: visible; fill: none; stroke: none; stroke-width: 1; }
.bar-svg .bar { fill: var(--series-1); }
.bar-svg .grid { stroke: var(--border); stroke-width: 1; stroke-dasharray: 2 4; }
.bar-svg .axis { stroke: var(--border); stroke-width: 1; }
.bar-svg .tick { fill: var(--text-muted); font-size: 11px; font-family: var(--font); }
.bar-svg .hit { fill: transparent; cursor: default; outline: none; }
.bar-svg .hit.on { fill: color-mix(in srgb, var(--text-primary) 6%, transparent); }
.chart-tooltip { position: absolute; z-index: 5; pointer-events: none; background: var(--surface-3); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; display: grid; font-size: 12.5px; min-width: 140px; box-shadow: 0 6px 20px rgba(0,0,0,.25); }
.chart-tooltip strong { font-size: 14px; color: var(--text-primary); }
.chart-tooltip span { color: var(--text-secondary); }
.kv { margin: 0; display: grid; gap: 8px; }
.kv div { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.kv dt { color: var(--text-secondary); } .kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.model-bars { display: grid; gap: 12px; }
.mbar-head { display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; margin-bottom: 4px; }
.mbar-track { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.mbar-fill { height: 100%; border-radius: 4px; background: var(--series-1); }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th { text-align: left; font-weight: 550; color: var(--text-secondary); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; font-variant-numeric: tabular-nums; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; }
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 8px; border-radius: 99px; font-weight: 550; }
.badge.ok { color: var(--good); background: color-mix(in srgb, var(--good) 14%, transparent); }
.badge.neutral { color: var(--text-secondary); background: var(--surface-2); }
.badge.bad { color: var(--critical); background: color-mix(in srgb, var(--critical) 14%, transparent); cursor: help; }

/* ---------------------------------------------------------- dialogs */
.dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--surface-1); color: var(--text-primary); padding: 0; width: min(460px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dialog-body { padding: 22px; display: grid; gap: 16px; }
.dialog h2 { margin: 0; font-size: 18px; }
.dialog p { margin: 0; }
.field { display: grid; gap: 6px; font-size: 14px; }
.field > span { display: flex; justify-content: space-between; font-weight: 550; }
.field input[type="password"], .field textarea { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); outline: none; resize: vertical; }
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field input[type="range"] { accent-color: var(--accent); }
.dialog-actions { display: flex; align-items: center; gap: 8px; }
.spacer { flex: 1; }
.error-text { color: var(--critical); font-size: 14px; }

.toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--text-primary); color: var(--bg); padding: 8px 16px; border-radius: 10px; font-size: 14px; z-index: 50; box-shadow: 0 8px 24px rgba(0,0,0,.3); max-width: calc(100vw - 32px); }

/* ---------------------------------------------------------- responsive */
@media (max-width: 960px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(86vw, var(--sidebar-w)); z-index: 30; transform: translateX(-100%); transition: transform .2s ease; }
  .sidebar.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 8px 10px; gap: 8px; }
  .toggle-label { display: none; }
  .chips { grid-template-columns: 1fr; }
  .messages { padding: 16px 12px 8px; }
  .composer { padding: 6px 10px calc(10px + env(safe-area-inset-bottom)); }
  .msg-user .bubble { max-width: 90%; }
  .conv-del { opacity: 1; }
}
