:root {
  --bg: #faf9f6;
  --panel: #f1efe9;
  --surface: #ffffff;
  --text: #1f1e1c;
  --muted: #6b6760;
  --border: #e0ddd5;
  --accent: #c2603b;
  --accent-text: #ffffff;
  --user-bubble: #ece8df;
  --code-bg: #f4f2ed;
  --danger: #b3261e;
  --focus: #2f6fdf;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SFMono-Regular", Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1c1a;
    --panel: #161514;
    --surface: #262522;
    --text: #ecebe7;
    --muted: #a19d94;
    --border: #3a3834;
    --accent: #d9774f;
    --user-bubble: #2f2d29;
    --code-bg: #141412;
    --danger: #f2827a;
    --focus: #7aa7ff;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.splash { display: grid; place-items: center; height: 100%; color: var(--muted); }

/* ---- layout ---- */
.app { display: grid; grid-template-columns: 272px 1fr; height: 100%; }
.sidebar { display: flex; flex-direction: column; gap: 10px; padding: 12px; background: var(--panel); border-right: 1px solid var(--border); min-height: 0; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

/* ---- buttons ---- */
.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 14px; cursor: pointer; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.subtle { background: transparent; }
.btn.small { padding: 4px 10px; }
.btn.danger-btn { background: var(--danger); border-color: var(--danger); color: #fff; }
.icon-btn { border: 0; background: transparent; padding: 6px 9px; border-radius: 8px; cursor: pointer; font-size: 18px; line-height: 1; color: var(--muted); }
.icon-btn:hover { background: var(--border); color: var(--text); }

/* ---- sidebar ---- */
.sidebar-head { display: flex; gap: 6px; }
.sidebar-head .btn { flex: 1; }
#close-sidebar { display: none; }
.search { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.conv-nav { flex: 1; overflow-y: auto; min-height: 0; margin: 0 -4px; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-link { display: block; padding: 7px 10px; margin: 1px 4px; border-radius: 8px; color: inherit; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-link:hover { background: var(--border); }
.conv-link.active { background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); font-weight: 550; }
#load-more { width: calc(100% - 8px); margin: 6px 4px; }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 10px; display: flex; flex-direction: column; gap: 4px; align-items: flex-start; }
.sidebar-foot div { overflow-wrap: anywhere; }

/* ---- header ---- */
.conv-header { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.menu-btn { display: none; }
.conv-title { flex: 1 1 200px; font-size: 16px; font-weight: 600; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.control { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.control select { padding: 5px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); max-width: 190px; }
.control select:disabled { opacity: .55; }
.settings-note { margin: 0; padding: 6px 16px; font-size: 13px; color: var(--muted); background: var(--panel); border-bottom: 1px solid var(--border); }

.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; display: flex; flex-direction: column; min-width: 160px; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 8px 24px rgb(0 0 0 / .12); }
.menu-items button { text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu-items button:hover { background: var(--panel); }
.menu-items .danger { color: var(--danger); }

/* ---- messages ---- */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; min-height: 0; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 18vh; }
.empty h2 { font-weight: 500; font-size: 26px; margin: 0 0 8px; }
.msg { margin-bottom: 20px; }
.msg.user { display: flex; justify-content: flex-end; }
.user-text { background: var(--user-bubble); border-radius: 14px; padding: 9px 14px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-tag { font-size: 12px; color: var(--muted); margin-top: 4px; }
.error { color: var(--danger); margin: 8px 0; }

.thought { margin-bottom: 8px; font-size: 13.5px; color: var(--muted); }
.thought summary { cursor: pointer; user-select: none; width: max-content; }
.thought.empty-thought summary { cursor: default; list-style: none; }
.thought.empty-thought summary::-webkit-details-marker { display: none; }
.thought-body { white-space: pre-wrap; border-left: 2px solid var(--border); padding: 4px 0 4px 12px; margin-top: 6px; max-height: 320px; overflow-y: auto; }

.cursor { display: inline-block; width: 8px; height: 1em; vertical-align: text-bottom; background: var(--accent); animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---- markdown ---- */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote, .md table { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 18px 0 8px; }
.md h1 { font-size: 1.4em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; }
.md a { color: var(--accent); }
.md code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre { position: relative; background: var(--code-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; overflow-x: auto; }
.md pre code { background: none; padding: 0; font-size: 13px; line-height: 1.5; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.md img { max-width: 100%; }
.copy-code { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 2px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); cursor: pointer; opacity: 0; transition: opacity .15s; }
.md pre:hover .copy-code, .copy-code:focus-visible { opacity: 1; }

/* ---- composer ---- */
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px 4px; max-width: 852px; width: 100%; margin: 0 auto; }
.composer textarea { flex: 1; resize: none; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); max-height: 40vh; min-height: 44px; }
.composer .btn { height: 44px; }
.notice { text-align: center; font-size: 11.5px; color: var(--muted); padding: 4px 16px 10px; }

/* ---- dialogs ---- */
.dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 20px; width: min(420px, calc(100vw - 32px)); }
.dialog.wide { width: min(680px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgb(0 0 0 / .35); }
.dialog h2 { margin: 0 0 10px; font-size: 17px; }
.dialog input, .dialog textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.dialog textarea { resize: vertical; font-family: var(--mono); font-size: 13px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

.scrim { display: none; }

/* ---- small screens ---- */
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 85vw); z-index: 30; transform: translateX(-100%); transition: transform .2s ease; }
  body.sidebar-open .sidebar { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; z-index: 25; background: rgb(0 0 0 / .35); }
  #close-sidebar, .menu-btn { display: inline-block; }
  .conv-header { padding: 8px 10px; gap: 6px; }
  .controls { width: 100%; gap: 6px; }
  .control span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .control select { max-width: 42vw; }
  .messages { padding: 16px 12px 8px; }
  .composer { padding: 8px 10px 4px; }
}
