:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #1c2321;
  --muted: #66716d;
  --border: #dde2df;
  --accent: #1f6f5c;
  --accent-text: #ffffff;
  --user: #e3f1ec;
  --code: #eef0ee;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131716;
    --surface: #1b211f;
    --text: #e6ebe8;
    --muted: #98a39e;
    --border: #2d3532;
    --accent: #3fae93;
    --accent-text: #08130f;
    --user: #1f3029;
    --code: #232b28;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.app { display: flex; height: 100dvh; }

.sidebar {
  width: 260px;
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-list li { display: flex; align-items: center; border-radius: 8px; }
.chat-list li.active { background: var(--user); }
.chat-list .open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: inherit; font: inherit; padding: 8px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-list .remove { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 8px; font-size: 16px; }

.sidebar-footer { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 14px; }
.sidebar-footer form { margin: 0; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }

.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.topbar h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#toggle-sidebar { display: none; }

.icon-button { background: none; border: 0; color: var(--text); font-size: 20px; cursor: pointer; padding: 6px 8px; border-radius: 8px; }
.icon-button:hover { background: var(--user); }

.settings { padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface); display: grid; gap: 12px; }
.settings[hidden] { display: none; }
.settings label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.settings-row { display: flex; gap: 24px; flex-wrap: wrap; }
.settings-row label { flex: 1; min-width: 180px; }
.hint { margin: 0; font-size: 13px; color: var(--muted); }

textarea, input[type="number"] {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }

.msg { max-width: min(760px, 100%); padding: 10px 14px; border-radius: 12px; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }
.msg.error { color: #b3261e; }
.msg pre { background: var(--code); border-radius: 8px; padding: 10px; overflow-x: auto; margin: 8px 0; }
.msg code { background: var(--code); border-radius: 4px; padding: 1px 4px; font-family: ui-monospace, Consolas, monospace; font-size: 14px; }
.msg pre code { background: none; padding: 0; }
.msg details { color: var(--muted); font-size: 14px; margin-bottom: 8px; }
.msg details summary { cursor: pointer; }
.msg .meta { margin-top: 6px; font-size: 12px; color: var(--muted); }

.composer { display: flex; gap: 8px; padding: 12px 16px 4px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 40dvh; }

.button {
  display: inline-block;
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: 8px;
  padding: 9px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.button.secondary { background: var(--border); color: var(--text); }
.button:disabled { opacity: 0.5; cursor: default; }
.button[hidden] { display: none; }

.link { background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 0; }

.status { margin: 0; padding: 2px 16px; font-size: 13px; color: var(--muted); }
#status { padding-bottom: 10px; min-height: 1.5em; }
.status .link { font-size: 13px; text-decoration: underline dotted; }
.status .link[hidden] { display: none; }
.inline-check { margin-left: 8px; cursor: pointer; }
.inline-check[hidden] { display: none; }

.msg.tool { align-self: flex-start; background: none; border: 1px dashed var(--border); font-size: 14px; color: var(--muted); }
.msg.tool summary { cursor: pointer; }
.msg.tool ol { margin: 8px 0 0; padding-left: 20px; }
.msg.tool li { margin-bottom: 6px; }
.msg.tool a { color: var(--accent); }
.msg.tool .snippet { font-size: 13px; }
.toolcall { margin-top: 6px; font-size: 13px; color: var(--muted); }

.ctx-panel { margin: 0 16px 6px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); font-size: 14px; }
.ctx-panel[hidden] { display: none; }
.ctx-head { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.ctx-bar { position: relative; display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: var(--code); }
.ctx-seg { display: block; height: 100%; }
.ctx-mark { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--text); opacity: 0.7; }
.ctx-legend { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 4px; }
.ctx-legend li { display: grid; grid-template-columns: 14px 1fr auto 56px; gap: 8px; align-items: center; }
.ctx-legend .num, .ctx-legend .pct { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.seg-system { background: #d98b2b; }
.seg-definition { background: #b5651d; }
.seg-summary { background: #8a6fd1; }
.seg-user { background: #3b82c4; }
.seg-assistant { background: #2fa67f; }
.seg-tool { background: #d1568a; }
.seg-reserve { background: repeating-linear-gradient(45deg, var(--border), var(--border) 4px, transparent 4px, transparent 8px); }
.seg-free { background: transparent; }

.msg.summary { align-self: stretch; max-width: none; background: none; border: 1px dashed var(--border); }
.settings label.check { display: flex; gap: 8px; align-items: center; }

.notice { max-width: 440px; margin: 15vh auto 0; padding: 0 16px; }
.notice h1 { font-size: 24px; }
.notice a { color: var(--accent); }
.notice a.button { color: var(--accent-text); }

@media (max-width: 760px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform 0.2s; box-shadow: 0 0 24px rgba(0, 0, 0, 0.25); }
  .app.sidebar-open .sidebar { transform: none; }
  #toggle-sidebar { display: inline-block; }
}
