:root {
  --bg: #0d1014;
  --surface: #14181f;
  --raise: #1a1f28;
  --border: #232935;
  --border-soft: #1c222c;
  --text: #dde2ea;
  --text-dim: #8a93a3;
  --text-faint: #626b7a;
  --accent: #4f7cff;
  --accent-soft: #1c2a4a;
  --danger: #e05561;
  --ok: #3fb950;
  --warn: #d6a13c;
  --radius: 6px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto,
          "Helvetica Neue", Arial, sans-serif;
}
:root[data-theme="light"] {
  --bg: #ffffff;
  --surface: #f7f8fa;
  --raise: #eef0f4;
  --border: #dfe3e9;
  --border-soft: #e9ecf1;
  --text: #1a1d23;
  --text-dim: #5c6472;
  --text-faint: #858d9b;
  --accent: #2f5cd8;
  --accent-soft: #e4eaff;
  --danger: #c4353f;
  --ok: #1a7f37;
  --warn: #9a6700;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 400 13.5px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
.hidden { display: none !important; }
.muted { color: var(--text-dim); }

/* controls */
button { font: inherit; cursor: pointer; border-radius: var(--radius); border: 1px solid transparent; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
button.primary {
  background: var(--accent); color: #fff; padding: 6px 12px; font-weight: 500;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button.primary:hover { filter: brightness(1.08); }
button.ghost {
  background: transparent; color: var(--text-dim); padding: 5px 10px;
  border-color: var(--border);
}
button.ghost:hover { background: var(--raise); color: var(--text); }
button.danger { background: var(--danger); color: #fff; padding: 6px 12px; font-weight: 500; }
button.danger:hover { filter: brightness(1.08); }
button.block { width: 100%; }
button.nav {
  display: flex; align-items: center; gap: 9px; width: 100%; text-align: left;
  background: transparent; color: var(--text-dim); padding: 6px 9px; font-size: 13px;
}
button.nav:hover { background: var(--raise); color: var(--text); }
button.nav svg { flex: none; opacity: .75; }
.icon-btn {
  background: transparent; color: var(--text-dim); border: 1px solid transparent;
  width: 28px; height: 28px; display: inline-grid; place-items: center; flex: none;
}
.icon-btn:hover { background: var(--raise); color: var(--text); }
svg { display: block; }

input, textarea, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 6px 9px; width: 100%;
}
select { cursor: pointer; }
.error { color: var(--danger); min-height: 1.1em; margin: 4px 0 0; font-size: 12.5px; }

/* login */
.login-screen { height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 26px 24px; width: min(330px, 100%); display: grid; gap: 10px;
}
.login-card h1 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: -.01em; }
.login-card p { margin: -4px 0 6px; font-size: 12.5px; }

/* layout */
.app { display: flex; height: 100%; }
.sidebar {
  width: 232px; flex: none; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.sidebar-head { padding: 10px; border-bottom: 1px solid var(--border-soft); }
.conversations {
  flex: 1; overflow-y: auto; padding: 6px; display: grid; gap: 1px; align-content: start;
}
.conv {
  display: block; width: 100%; text-align: left; background: transparent;
  color: var(--text-dim); border: none; padding: 6px 9px; border-radius: var(--radius);
  font-size: 13px; line-height: 1.35;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv:hover { background: var(--raise); color: var(--text); }
.conv.active { background: var(--accent-soft); color: var(--text); }
.conv small { display: block; color: var(--text-faint); font-size: 11px; margin-top: 1px; }
.sidebar-foot { border-top: 1px solid var(--border-soft); padding: 6px; display: grid; gap: 1px; }
.sidebar-sep { height: 1px; background: var(--border-soft); margin: 5px 4px; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.topbar h2 {
  font-size: 13.5px; font-weight: 600; margin: 0; flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-actions { display: flex; gap: 2px; align-items: center; }
.pill {
  font-size: 11px; color: var(--text-faint); font-family: var(--mono);
  padding: 2px 7px; border: 1px solid var(--border); border-radius: 3px; margin-right: 4px;
}

/* messages */
.messages { flex: 1; overflow-y: auto; padding: 18px 16px 4px; }
.msg { max-width: 800px; margin: 0 auto 18px; }
.msg-head {
  display: flex; align-items: baseline; gap: 8px; margin-bottom: 5px;
}
.msg-role {
  font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-faint);
}
.msg.user .msg-role { color: var(--accent); }
.msg-meta { font-size: 11px; color: var(--text-faint); font-family: var(--mono); }
.msg-meta .warn { color: var(--warn); }
.msg .tools {
  font-size: 12px; color: var(--text-dim); font-family: var(--mono);
  margin-bottom: 6px; padding-left: 9px; border-left: 2px solid var(--border);
}
.msg .actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0; transition: opacity .12s; }
.msg:hover .actions { opacity: 1; }
.msg .actions button {
  background: transparent; border: 1px solid var(--border); color: var(--text-faint);
  font-size: 11.5px; padding: 2px 8px; display: inline-flex; align-items: center; gap: 5px;
}
.msg .actions button:hover { color: var(--text); background: var(--raise); }
.status-line { color: var(--text-dim); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.status-line.error { color: var(--danger); }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 3px; height: 3px; border-radius: 50%; background: currentColor; opacity: .3;
  animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: .9; } }
.empty { color: var(--text-faint); text-align: center; margin-top: 16vh; font-size: 13px; }

/* markdown */
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 0 0 9px; white-space: pre-wrap; }
.md h1, .md h2, .md h3, .md h4 { margin: 15px 0 7px; line-height: 1.3; font-weight: 600; }
.md h1 { font-size: 16px; } .md h2 { font-size: 14.5px; }
.md h3 { font-size: 13.5px; } .md h4 { font-size: 13px; }
.md ul, .md ol { margin: 0 0 9px; padding-left: 20px; }
.md li { margin: 2px 0; }
.md code {
  font-family: var(--mono); font-size: 12px; background: var(--raise);
  padding: 1px 4px; border-radius: 3px;
}
.md pre {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; overflow-x: auto; margin: 0 0 10px; position: relative;
}
.md pre code { background: none; padding: 0; font-size: 12px; line-height: 1.5; }
.md pre .copy {
  position: absolute; top: 6px; right: 6px; font-size: 11px; padding: 2px 7px;
  background: var(--bg); border: 1px solid var(--border); color: var(--text-faint);
  opacity: 0; transition: opacity .12s;
}
.md pre:hover .copy { opacity: 1; }
.md blockquote {
  margin: 0 0 9px; padding-left: 11px; border-left: 2px solid var(--border);
  color: var(--text-dim);
}
.md a { color: var(--accent); text-underline-offset: 2px; }
.md table { border-collapse: collapse; margin: 0 0 10px; display: block; overflow-x: auto; font-size: 12.5px; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.md th { background: var(--surface); font-weight: 600; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

/* composer */
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 10px 16px 14px; }
.composer-row { max-width: 800px; margin: 0 auto; display: flex; gap: 6px; align-items: flex-end; }
#input { resize: none; max-height: 200px; min-height: 34px; line-height: 1.45; padding: 7px 9px; }
.attachments { max-width: 800px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  background: var(--raise); border: 1px solid var(--border); border-radius: 4px;
  padding: 3px 8px; font-size: 12px; display: flex; gap: 6px; align-items: center;
  color: var(--text-dim);
}
.chip button { background: none; border: none; color: var(--text-faint); padding: 0; line-height: 1; }
.chip button:hover { color: var(--danger); }
.composer.dragover { outline: 1px dashed var(--accent); outline-offset: -5px; }

/* panels */
.panel {
  position: fixed; top: 0; right: 0; height: 100%; width: min(600px, 100vw);
  background: var(--surface); border-left: 1px solid var(--border); z-index: 20;
  display: flex; flex-direction: column;
}
.panel-head {
  display: flex; align-items: center; gap: 8px; padding: 11px 14px;
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { margin: 0; font-size: 13.5px; font-weight: 600; flex: 1; }
.panel-body { flex: 1; overflow-y: auto; padding: 14px; }
.backdrop { position: fixed; inset: 0; background: #0007; z-index: 15; }
.card {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; margin-bottom: 10px;
}
.card h3 { margin: 0 0 3px; font-size: 12.5px; font-weight: 600; }
.card p.hint { margin: 0 0 9px; color: var(--text-dim); font-size: 12px; line-height: 1.45; }
.row { display: flex; gap: 7px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.switch { display: flex; gap: 8px; align-items: center; cursor: pointer; font-size: 12.5px; }
.switch input { width: auto; accent-color: var(--accent); }
.field-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--text-faint); margin: 10px 0 4px; font-weight: 600;
}
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(132px, 1fr)); gap: 8px; }
.stat { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 9px 11px; }
.stat b { display: block; font-size: 17px; font-weight: 600; letter-spacing: -.01em; }
.stat span { color: var(--text-faint); font-size: 11px; }
.badge {
  font-size: 11px; padding: 1px 7px; border-radius: 3px; background: var(--raise);
  color: var(--text-dim); border: 1px solid var(--border);
}
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; flex: none; background: var(--text-faint); }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }
.log-line {
  font-family: var(--mono); font-size: 11.5px; padding: 5px 8px; border-radius: 3px;
  border-left: 2px solid var(--border); margin-bottom: 3px; background: var(--bg);
  white-space: pre-wrap; word-break: break-word; line-height: 1.45;
}
.log-line.status { border-color: var(--ok); }
.log-line.debug { border-color: var(--accent); }
.log-line.error { border-color: var(--danger); }
.log-line time { color: var(--text-faint); margin-right: 8px; }
.log-line pre { margin: 5px 0 0; font-size: 11px; color: var(--text-faint); overflow-x: auto; }
.aspect-head { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; }
.aspect-head input[type="text"] { flex: 1; }
textarea.aspect { min-height: 92px; font-family: var(--mono); font-size: 12px; line-height: 1.5; }
pre.preview {
  white-space: pre-wrap; font-family: var(--mono); font-size: 11.5px;
  color: var(--text-dim); margin: 0; line-height: 1.5; max-height: 220px; overflow-y: auto;
}

/* toast + confirm */
.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--raise); border: 1px solid var(--border); color: var(--text);
  padding: 7px 14px; border-radius: var(--radius); z-index: 50; font-size: 12.5px;
  box-shadow: 0 4px 16px #0004;
}
.confirm {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 18px; max-width: 420px; font-size: 13px; line-height: 1.5;
}
.confirm p { margin: 0; }
.confirm::backdrop { background: #0008; }
.confirm-actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 14px; }

/* scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 820px) {
  .sidebar {
    position: fixed; z-index: 30; height: 100%; transform: translateX(-100%);
    transition: transform .18s;
  }
  .sidebar.open { transform: none; }
  .messages { padding: 14px 10px 4px; }
  .composer { padding: 8px 10px 12px; }
}
@media (min-width: 821px) { #menu-toggle { display: none; } }
