:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --sidebar: #efeee8;
  --line: #e2e0d8;
  --text: #22251f;
  --muted: #6c6f66;
  --accent: #4a5d3a;
  --accent-hover: #3d4d30;
  --accent-soft: #e7ecdf;
  --accent-ink: #ffffff;
  --danger: #a3402f;
  --user-bubble: #e9ece3;
  --code-bg: #f1f0ea;
  --shadow: 0 1px 2px rgba(30, 32, 25, .05), 0 4px 16px rgba(30, 32, 25, .06);
  --radius: 14px;
  --serif: "IBM Plex Serif", Georgia, serif;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1c18;
    --surface: #22251f;
    --sidebar: #161814;
    --line: #33372f;
    --text: #e8e9e3;
    --muted: #9a9e93;
    --accent: #8fa676;
    --accent-hover: #a3b98a;
    --accent-soft: #2c3526;
    --accent-ink: #141712;
    --danger: #e07b67;
    --user-bubble: #2b3027;
    --code-bg: #2a2d26;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.6 var(--sans); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid transparent; border-radius: 10px; padding: 9px 14px; font-weight: 500; text-decoration: none; transition: background .15s, border-color .15s; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn.ghost:hover { background: var(--surface); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }
.link { background: none; border: 0; padding: 4px 6px; color: var(--muted); font-size: 13px; border-radius: 6px; }
.link:hover { color: var(--text); background: var(--line); }
.link.danger:hover { color: var(--danger); }
.icon-btn { background: none; border: 0; font-size: 18px; padding: 4px 8px; color: var(--muted); }

.brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-weight: 600; font-size: 18px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; display: block; flex-shrink: 0; }
.brand-sub { font-weight: 500; color: var(--accent); }

/* Auth */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 32px 28px; box-shadow: var(--shadow); }
.auth-card h1 { font-family: var(--serif); font-weight: 600; font-size: 24px; margin: 22px 0 6px; }
.auth-card > p { margin: 0 0 22px; font-size: 14px; }
.tabs { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; margin-bottom: 18px; }
.tab { flex: 1; border: 0; background: none; padding: 7px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
#auth-form label { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-bottom: 14px; }
#auth-form input { display: block; width: 100%; margin-top: 6px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); font-size: 15px; }
#auth-form input:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.error { color: var(--danger); font-size: 13px; margin: -4px 0 12px; }
.hint { font-size: 12.5px; color: var(--muted); margin: 14px 0 0; text-align: center; }

/* Layout */
.layout { display: flex; height: 100%; }
.sidebar { width: 272px; flex-shrink: 0; background: var(--sidebar); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 16px 12px; gap: 12px; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 8px; }
.nav-item { width: 100%; display: flex; align-items: center; justify-content: space-between; border: 0; background: none; padding: 8px 10px; border-radius: 8px; font-weight: 500; text-align: left; }
.nav-item:hover, .nav-item.active { background: var(--line); }
.badge { font-size: 12px; background: var(--accent-soft); color: var(--accent); padding: 1px 8px; border-radius: 999px; font-weight: 600; }
.section-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 6px 10px 0; font-weight: 600; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-list li button { width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; color: var(--text); }
.chat-list li button:hover { background: var(--line); }
.chat-list li button.active { background: var(--surface); box-shadow: var(--shadow); }
.chat-list .empty-list { color: var(--muted); font-size: 13px; padding: 6px 10px; }
.sidebar-foot { display: flex; align-items: center; justify-content: space-between; border-top: 1px solid var(--line); padding: 12px 6px 0; }
.user { display: flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 500; min-width: 0; }
.user span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 600; font-size: 13px; flex-shrink: 0; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 56px; display: flex; align-items: center; gap: 8px; padding: 0 20px; border-bottom: 1px solid var(--line); flex-shrink: 0; }
.topbar h2 { font-size: 15px; font-weight: 600; margin: 0; flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 4px; }

/* Chat */
.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.scroll { flex: 1; overflow-y: auto; }
.thread { max-width: 760px; margin: 0 auto; padding: 28px 20px 12px; }
.msg { margin-bottom: 26px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bubble); padding: 10px 16px; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg.assistant .md { word-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 22px; }
.md li { margin: 3px 0; }
.md h1, .md h2, .md h3 { font-family: var(--serif); font-weight: 600; line-height: 1.3; margin: 20px 0 8px; }
.md h1 { font-size: 21px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md strong { font-weight: 600; }
.md code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 13.5px; }
.md pre { background: var(--code-bg); padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; margin: 0 0 14px; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--code-bg); font-weight: 600; }
.md blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.status { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); margin-top: 6px; }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); 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%, 100% { opacity: .25; } 40% { opacity: 1; } }
.note-chip { display: flex; align-items: flex-start; gap: 10px; margin-top: 12px; background: var(--accent-soft); border-radius: 10px; padding: 10px 12px; font-size: 13.5px; }
.note-chip .check { color: var(--accent); font-weight: 700; }
.note-chip .kind { color: var(--muted); font-size: 12px; }
.msg-error { background: color-mix(in srgb, var(--danger) 10%, transparent); color: var(--danger); border-radius: 10px; padding: 10px 12px; font-size: 14px; }

.empty { max-width: 760px; margin: 0 auto; padding: 9vh 20px 20px; }
.empty h1 { font-family: var(--serif); font-weight: 600; font-size: 30px; margin: 0 0 6px; }
.empty > p { margin: 0 0 28px; }
.suggest-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.suggest-col { display: flex; flex-direction: column; gap: 8px; }
.suggest-label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; margin-bottom: 2px; }
.suggest { text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 11px 14px; font-size: 14px; transition: border-color .15s, transform .15s; }
.suggest:hover { border-color: var(--accent); }

.composer { padding: 8px 20px 16px; flex-shrink: 0; }
.composer-box { max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 8px 8px 16px; box-shadow: var(--shadow); }
.composer-box:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; resize: none; background: none; outline: none; padding: 6px 0; max-height: 220px; line-height: 1.5; font-size: 15px; }
.send { width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; flex-shrink: 0; }
.send:disabled { opacity: .35; cursor: default; }
.composer-hint { max-width: 760px; margin: 6px auto 0; font-size: 12px; color: var(--muted); text-align: center; }

/* Notes */
.notes-view { flex: 1; overflow-y: auto; }
.notes-wrap { max-width: 860px; margin: 0 auto; padding: 24px 20px 40px; }
.notes-toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.seg { display: flex; background: var(--sidebar); border-radius: 10px; padding: 3px; }
.seg-btn { border: 0; background: none; padding: 6px 14px; border-radius: 8px; color: var(--muted); font-weight: 500; font-size: 14px; }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
#notes-kind { padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 14px; }
#notes-export { margin-left: auto; font-size: 14px; }
.notes-list { display: flex; flex-direction: column; gap: 12px; }
.note-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.note-card header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 10px; margin-bottom: 6px; }
.note-card h3 { font-size: 15.5px; font-weight: 600; margin: 0; flex: 1; }
.note-card .body { white-space: pre-wrap; font-size: 14.5px; }
.note-meta { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; font-size: 12.5px; color: var(--muted); margin-top: 10px; }
.pill { font-size: 12px; padding: 2px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 500; white-space: nowrap; }
.pill.problem { background: color-mix(in srgb, var(--danger) 13%, transparent); color: var(--danger); }
.pill.remark_legacy, .pill.process, .pill.other { background: var(--code-bg); color: var(--muted); }
.notes-empty { color: var(--muted); text-align: center; padding: 60px 0; }

.only-mobile { display: none; }
@media (max-width: 820px) {
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .topbar { padding: 0 12px; }
  .thread, .empty, .notes-wrap { padding-left: 16px; padding-right: 16px; }
  .composer { padding: 8px 12px 12px; }
  .suggest-grid { grid-template-columns: 1fr; }
  .composer-hint { display: none; }
  .composer-hint.show { display: block; }
  #rename-chat { display: none; }
}

/* Chat list states */
.chat-list li .chat-link { display: flex; align-items: center; gap: 8px; }
.chat-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-state { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.chat-state.unread { background: var(--accent); }
.chat-state.running { border: 2px solid var(--accent); border-right-color: transparent; width: 12px; height: 12px; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Title rename */
.topbar h2.editable { cursor: text; border-radius: 6px; padding: 2px 6px; margin-left: -6px; }
.topbar h2.editable:hover { background: var(--line); }
.title-input { flex: 1; font-size: 15px; font-weight: 600; padding: 4px 8px; border: 1px solid var(--accent); border-radius: 8px; background: var(--surface); outline: none; min-width: 0; }

/* Message extras */
.msg-note { color: var(--muted); font-size: 13.5px; margin-top: 8px; }
.msg-note .link, .msg-error .link { font-size: 13.5px; color: var(--accent); font-weight: 500; }
.msg-error .link { color: var(--danger); text-decoration: underline; }
.msg-tools { margin-top: 6px; opacity: 0; transition: opacity .15s; }
.msg.assistant:hover .msg-tools { opacity: 1; }
.msg-tools .link { padding: 2px 6px; margin-left: -6px; font-size: 12.5px; }
.status .elapsed { opacity: .8; }
.send.stop { background: var(--text); color: var(--bg); }
.send:focus-visible, .link:focus-visible, .btn:focus-visible, .suggest:focus-visible, .chat-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Overlay & toast */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 15; }
.toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); z-index: 30; max-width: calc(100% - 32px); }
@media (hover: none) { .msg-tools { opacity: 1; } }

/* Voice */
.icon-round { width: 36px; height: 36px; border-radius: 10px; border: 0; background: none; color: var(--muted); display: grid; place-items: center; flex-shrink: 0; }
.icon-round:hover:not(:disabled) { background: var(--bg); color: var(--accent); }
.icon-round:disabled { opacity: .35; cursor: default; }
.recorder { flex: 1; display: flex; align-items: center; gap: 10px; min-height: 36px; min-width: 0; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: blink 1.2s infinite; flex-shrink: 0; }
.rec-time { font-variant-numeric: tabular-nums; font-size: 14px; min-width: 38px; }
.rec-wave { flex: 1; min-width: 0; height: 28px; width: 100%; }
.msg.voice .bubble { display: flex; flex-direction: column; gap: 6px; min-width: 260px; }
.msg.voice audio { width: 100%; height: 36px; }
.voice-text { white-space: pre-wrap; }
.voice-text.muted { font-style: italic; }
@media (max-width: 820px) { .msg.voice .bubble { min-width: 0; width: 85%; } }
