:root {
  --bg: #fff8f3; --panel: #ffffff; --ink: #3b3341; --muted: #8a7f90; --line: #f3dfe6;
  --accent: #ff8fab; --accent-ink: #ffffff; --soft: #fff0f4; --user: #e6f7e9; --danger: #d9534f;
  --mint: #cdeedd; --peach: #ffdcc2; --pink: #ffd3df; --lemon: #fff3c4;
  --radius: 18px; --sidebar: 300px; --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #2a2430; --panel: #342c3b; --ink: #f6eef2; --muted: #b7a9bd; --line: #4a3f52;
    --accent: #ff9fb8; --accent-ink: #3a1f2a; --soft: #3f3347; --user: #2f4238;
    --mint: #3c5a4a; --peach: #5a4436; --pink: #5a3a46; --lemon: #5a5236;
  }
}
:root[data-theme="dark"] {
  --bg: #2a2430; --panel: #342c3b; --ink: #f6eef2; --muted: #b7a9bd; --line: #4a3f52;
  --accent: #ff9fb8; --accent-ink: #3a1f2a; --soft: #3f3347; --user: #2f4238;
  --mint: #3c5a4a; --peach: #5a4436; --pink: #5a3a46; --lemon: #5a5236;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg) radial-gradient(circle at 12% 8%, var(--pink) 0, transparent 28rem) no-repeat, radial-gradient(circle at 92% 95%, var(--mint) 0, transparent 26rem) no-repeat; background-color: var(--bg); color: var(--ink); font: 16px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.app { display: grid; grid-template-columns: var(--sidebar) 1fr; height: 100dvh; }

/* ---------- sidebar ---------- */
.sidebar { background: linear-gradient(180deg, var(--pink) 0%, var(--panel) 28%); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 16px; gap: 12px; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.brand { font-weight: 800; font-size: 20px; letter-spacing: -0.2px; display: flex; gap: 6px; align-items: center; }
.search input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); outline: none; }
.search input:focus { border-color: var(--accent); }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -6px; }
.chat-item { text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 10px; cursor: pointer; display: block; width: 100%; }
.chat-item:hover { background: var(--soft); }
.chat-item.active { background: var(--soft); font-weight: 600; }
.chat-item .t { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .s { display: block; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .s b { color: var(--ink); font-weight: 600; }
.empty { color: var(--muted); font-size: 14px; padding: 8px 10px; }
.visited { border-top: 1px solid var(--line); padding-top: 10px; font-size: 14px; }
.visited summary { cursor: pointer; color: var(--muted); font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
.visited summary::-webkit-details-marker { display: none; }
.visited .count { background: var(--soft); border-radius: 999px; padding: 0 8px; font-weight: 500; }
.visited-list { max-height: 30vh; overflow-y: auto; margin-top: 6px; }
.visited-list a { display: block; padding: 6px 4px; color: inherit; text-decoration: none; border-radius: 8px; }
.visited-list a:hover { background: var(--soft); }
.visited-list .t { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13.5px; }
.visited-list .u { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.src.visited { border-style: dashed; }
.src .seen { font-size: 11px; color: var(--muted); font-weight: 500; }
.sidebar-foot { border-top: 1px solid var(--line); padding-top: 12px; font-size: 14px; }
.account { display: flex; align-items: center; gap: 10px; }
.account img { width: 32px; height: 32px; border-radius: 50%; }
.account .who { flex: 1; min-width: 0; }
.account .who span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account .who .e { color: var(--muted); font-size: 12.5px; }

/* ---------- main ---------- */
.main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.topbar-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quota { font-size: 13px; color: var(--ink); background: var(--lemon); padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.quota.warn { color: var(--danger); }
.thread { flex: 1; overflow-y: auto; padding: 24px 16px; }
.thread-inner, .welcome, .composer, .fineprint { width: 100%; max-width: 760px; margin: 0 auto; }
.welcome { text-align: center; padding-top: 10vh; }
.welcome h1 { font-size: 30px; margin: 0 0 8px; font-weight: 800; }
.welcome p { color: var(--muted); margin: 0 0 24px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip { background: var(--lemon); border: 1px solid transparent; border-radius: 999px; padding: 8px 14px; cursor: pointer; font-size: 14px; }
.chip:hover { background: var(--peach); }
.chips .chip:nth-child(2n) { background: var(--pink); } .chips .chip:nth-child(3n) { background: var(--mint); }

.msg { display: flex; flex-direction: column; margin-bottom: 20px; }
.msg.user { align-items: flex-end; }
.bubble { max-width: 92%; padding: 12px 16px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line); overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--mint); border-color: transparent; }
.bubble p { margin: 0 0 10px; } .bubble p:last-child { margin: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin-bottom: 4px; }
.cite { display: inline-block; font-size: 11.5px; font-weight: 600; background: var(--soft); color: var(--accent); border-radius: 6px; padding: 0 5px; margin-left: 2px; text-decoration: none; vertical-align: 1px; }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

.sources { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin-top: 10px; max-width: 92%; }
.src { display: flex; gap: 10px; text-decoration: none; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px; min-width: 0; }
.src:hover { border-color: var(--accent); }
.src .thumb { width: 56px; height: 56px; border-radius: 12px; object-fit: cover; background: var(--peach); flex: none; display: grid; place-items: center; font-size: 22px; }
.src .meta { min-width: 0; }
.src .n { font-size: 11px; color: var(--accent); font-weight: 700; }
.src .title { font-size: 13.5px; font-weight: 600; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.src .pub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.notice { background: var(--soft); border-radius: var(--radius); padding: 14px 16px; font-size: 14.5px; }
.notice.err { color: var(--danger); }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 16px 0; }
.composer textarea { flex: 1; resize: none; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); outline: none; max-height: 160px; }
.composer textarea:focus { border-color: var(--accent); }
.send { width: 44px; height: 44px; border-radius: 12px; font-size: 20px; flex: none; }
.fineprint { font-size: 12px; color: var(--muted); text-align: center; margin: 6px auto 0; padding: 0 16px calc(10px + env(safe-area-inset-bottom)); }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 14px; cursor: pointer; }
.btn.primary { background: linear-gradient(135deg, var(--accent), #ffb27a); color: var(--accent-ink); border-color: transparent; font-weight: 700; box-shadow: 0 4px 14px rgba(255,143,171,.35); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.icon-btn { border: 0; background: none; font-size: 20px; cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--soft); }
.hide-desktop { display: none; }
.backdrop { display: none; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(85vw, 320px); z-index: 20; transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 40px rgba(0,0,0,.25); }
  .sidebar.open { transform: none; }
  .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; opacity: 0; pointer-events: none; transition: opacity .2s; }
  .backdrop.show { opacity: 1; pointer-events: auto; }
  .hide-desktop { display: inline-block; }
  .thread { padding: 16px 12px; }
  .bubble, .sources { max-width: 100%; }
  .welcome { padding-top: 4vh; } .welcome h1 { font-size: 23px; }
  .composer { padding: 8px 12px 0; }
}
