:root {
  --bg: #ffffff;
  --bg-side: #f5f6f3;
  --ink: #1d231f;
  --muted: #68726b;
  --line: #e2e5df;
  --bubble: #eceee9;
  --hover: rgba(29, 35, 31, 0.06);
  --accent: #2f7d5b;
  --accent-ink: #ffffff;
  --accent-tint: rgba(47, 125, 91, 0.12);
  --danger: #b3382c;
  --code-bg: #f2f3ef;
  --shadow: 0 6px 24px rgba(20, 30, 24, 0.12);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1d1b;
    --bg-side: #141715;
    --ink: #e7ebe6;
    --muted: #98a29a;
    --line: #2a2f2c;
    --bubble: #262b28;
    --hover: rgba(231, 235, 230, 0.08);
    --accent: #4fb286;
    --accent-ink: #0e1a14;
    --accent-tint: rgba(79, 178, 134, 0.16);
    --danger: #ef7a6c;
    --code-bg: #212522;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.is-hidden { display: none !important; }
svg { width: 18px; height: 18px; flex: none; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.main { display: flex; flex-direction: column; min-width: 0; height: 100%; }
.icon-btn.only-mobile { display: none; }
.scrim { display: none; }

.sidebar {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--bg-side); border-right: 1px solid var(--line);
}
.sidebar-top { display: flex; gap: 8px; padding: 12px 12px 8px; }
.btn-new {
  flex: 1; display: flex; align-items: center; gap: 8px; padding: 9px 12px;
  border-radius: 10px; font-weight: 600;
}
.btn-new:hover { background: var(--hover); }
.search { padding: 4px 12px 8px; }
.search input {
  width: 100%; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg);
}
.history { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.sidebar-foot { padding: 8px 12px 12px; border-top: 1px solid var(--line); }
.btn-quiet {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 8px 10px; border-radius: 10px; color: var(--muted);
}
.btn-quiet:hover { background: var(--hover); color: var(--ink); }

.group-label { padding: 14px 10px 6px; font-size: 12.5px; color: var(--muted); }
.conv {
  position: relative; display: flex; align-items: center; gap: 4px;
  border-radius: 10px;
}
.conv:hover, .conv.is-active { background: var(--hover); }
.conv-open {
  flex: 1; min-width: 0; text-align: left; padding: 8px 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-tools { display: none; gap: 2px; padding-right: 4px; }
.conv:hover .conv-tools, .conv:focus-within .conv-tools { display: flex; }
@media (hover: none) { .conv-tools { display: flex; } }
.conv-rename {
  flex: 1; margin: 4px; padding: 5px 8px; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--bg);
}
.empty-note { padding: 16px 10px; color: var(--muted); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 8px; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--ink); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px; min-height: 52px;
  padding: 8px 14px; padding-top: max(8px, env(safe-area-inset-top));
}
.controls { display: flex; gap: 8px; flex-wrap: wrap; }
.controls select {
  padding: 6px 28px 6px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); max-width: 46vw;
}
.controls select:disabled { opacity: 0.45; }

/* ---------- thread ---------- */
.thread { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.thread-inner { max-width: 760px; margin: 0 auto; padding: 12px 18px 28px; }

.empty-state { text-align: center; padding: 18vh 12px 0; color: var(--muted); }
.empty-state h2 { margin: 0 0 6px; font-size: 22px; font-weight: 600; color: var(--ink); }
.empty-state p { margin: 0; }

.msg { margin: 18px 0; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble {
  max-width: 85%; padding: 9px 14px; border-radius: 18px; background: var(--bubble);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant { align-items: stretch; }
.msg-actions { display: flex; gap: 2px; margin-top: 4px; opacity: 0; transition: opacity 0.15s; }
.msg:hover .msg-actions, .msg:focus-within .msg-actions { opacity: 1; }
@media (hover: none) { .msg-actions { opacity: 1; } }

.msg-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 6px; justify-content: flex-end; }
.msg.assistant .msg-images { justify-content: flex-start; }
.msg-images img {
  max-width: min(100%, 420px); max-height: 420px; border-radius: 14px;
  border: 1px solid var(--line); display: block; background: var(--bubble);
}
.msg-images.is-thumbs img { max-width: 140px; max-height: 140px; object-fit: cover; }

.note-cut { margin-top: 6px; font-size: 13px; color: var(--muted); }
.note-error { margin-top: 6px; font-size: 14px; color: var(--danger); }

.thinking { display: inline-flex; gap: 5px; padding: 8px 2px; }
.thinking i {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: pulse 1.2s infinite ease-in-out;
}
.thinking i:nth-child(2) { animation-delay: 0.15s; }
.thinking i:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.skeleton {
  width: min(100%, 320px); aspect-ratio: 1 / 1; border-radius: 14px;
  background: linear-gradient(100deg, var(--bubble) 30%, var(--hover) 50%, var(--bubble) 70%);
  background-size: 200% 100%; animation: shimmer 1.4s infinite linear;
}
@keyframes shimmer { to { background-position: -200% 0; } }
.skeleton-label { margin-top: 6px; color: var(--muted); font-size: 13px; }

.reasoning { margin-bottom: 8px; color: var(--muted); font-size: 14px; }
.reasoning summary { cursor: pointer; width: fit-content; }
.reasoning div { margin-top: 6px; padding-left: 12px; border-left: 2px solid var(--line); white-space: pre-wrap; }

/* ---------- markdown ---------- */
.prose { overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p, .prose ul, .prose ol, .prose pre, .prose blockquote, .prose table { margin: 0 0 12px; }
.prose ul, .prose ol { padding-left: 22px; }
.prose li + li { margin-top: 3px; }
.prose h1, .prose h2, .prose h3, .prose h4 { margin: 18px 0 8px; line-height: 1.3; }
.prose h1 { font-size: 1.35em; } .prose h2 { font-size: 1.2em; } .prose h3, .prose h4 { font-size: 1.05em; }
.prose a { color: var(--accent); }
.prose code { font-family: var(--mono); font-size: 0.9em; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.prose blockquote { padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.prose table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.prose th, .prose td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.codeblock { position: relative; margin: 0 0 12px; border-radius: 12px; background: var(--code-bg); border: 1px solid var(--line); }
.codeblock pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.codeblock pre code { background: none; padding: 0; font-size: 13.5px; }
.codeblock .copy-code {
  position: absolute; top: 6px; right: 6px; padding: 3px 8px; border-radius: 8px;
  font-size: 12.5px; color: var(--muted); background: var(--bg);
  border: 1px solid var(--line);
}
.codeblock .copy-code:hover { color: var(--ink); }

/* ---------- composer ---------- */
.composer-wrap { padding: 6px 14px calc(12px + env(safe-area-inset-bottom)); }
.composer-inner { max-width: 760px; margin: 0 auto; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.attachments:empty { display: none; }
.thumb { position: relative; width: 58px; height: 58px; }
.thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.thumb.is-loading img { opacity: 0.4; }
.thumb button {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--ink); color: var(--bg); display: flex; align-items: center; justify-content: center;
}
.thumb button svg { width: 12px; height: 12px; }

.composer {
  border: 1px solid var(--line); border-radius: 22px; background: var(--bg);
  padding: 10px 10px 8px 16px; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  display: block; width: 100%; resize: none; border: 0; outline: 0; background: transparent;
  max-height: 220px; line-height: 1.5; padding: 2px 0;
}
.composer-row { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.composer-left { display: flex; align-items: center; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 11px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--muted); font-size: 13.5px;
}
.chip:hover { color: var(--ink); background: var(--hover); }
.chip[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); background: var(--accent-tint); }
.send {
  width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-ink);
}
.send:disabled { opacity: 0.35; cursor: default; }

/* ---------- toast ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast {
  max-width: 320px; padding: 10px 14px; border-radius: 12px; background: var(--ink); color: var(--bg);
  box-shadow: var(--shadow); font-size: 14px;
}
.toast.is-error { background: var(--danger); color: #fff; }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; background: var(--bg-side); }
.login-card {
  width: min(360px, 92vw); display: flex; flex-direction: column; gap: 10px;
  padding: 28px; border-radius: 18px; background: var(--bg); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 8px; font-size: 22px; }
.login-card input { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
.btn-primary { margin-top: 6px; padding: 10px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.form-error { margin: 0; color: var(--danger); font-size: 14px; }

/* ---------- mobile ---------- */
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .icon-btn.only-mobile { display: inline-flex; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(300px, 86vw); z-index: 30;
    transform: translateX(-100%); transition: transform 0.2s ease;
    padding-top: env(safe-area-inset-top);
  }
  .sidebar.is-open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); z-index: 20; display: none; }
  .scrim.is-open { display: block; }
  .bubble { max-width: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  .thinking i, .skeleton { animation: none; }
  .sidebar { transition: none; }
}
