:root {
  --bg: #f6f6f4;
  --panel: #ffffff;
  --panel-2: #efefec;
  --text: #17181a;
  --muted: #6b6f76;
  --border: #e2e2de;
  --accent: #e8571c;
  --accent-text: #ffffff;
  --user-bg: #17181a;
  --user-text: #ffffff;
  --code-bg: #f0efeb;
  --ok: #2f9e44;
  --err: #d6336c;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #16191f;
    --panel-2: #1d2128;
    --text: #e8e9eb;
    --muted: #8b919b;
    --border: #262a32;
    --accent: #ff6a2b;
    --user-bg: #2a2f38;
    --user-text: #f2f3f5;
    --code-bg: #1d2128;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--err); min-height: 1.5em; margin: 0; font-size: 14px; }
.small { font-size: 13px; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-card h1 { margin: 0; font-size: 28px; letter-spacing: -0.02em; }
.login-card p { margin: 0 0 8px; }
.logo { color: var(--accent); font-size: 40px; line-height: 1; }
.login-card input, .login-card button {
  height: 48px; border-radius: 12px; border: 1px solid var(--border); padding: 0 16px; font-size: 16px;
}
.login-card input { background: var(--panel); color: var(--text); outline: none; }
.login-card input:focus { border-color: var(--accent); }
.login-card button, .btn-primary { background: var(--accent); color: var(--accent-text); border: 0; font-weight: 600; }

/* Layout */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: 280px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.sidebar-head { padding: 12px; }
.btn-primary { width: 100%; height: 40px; border-radius: 10px; }
.btn-ghost { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv {
  display: flex; align-items: center; gap: 6px; width: 100%; text-align: left; background: none; border: 0;
  border-radius: 8px; padding: 9px 10px; color: var(--text); font-size: 14px;
}
.conv span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--panel-2); font-weight: 600; }
.conv .del { opacity: 0; background: none; border: 0; color: var(--muted); padding: 0 4px; }
.conv:hover .del, .conv.active .del { opacity: 1; }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.status { font-size: 12px; line-height: 1.4; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border);
  background: var(--bg); padding-top: max(8px, env(safe-area-inset-top));
}
.icon-btn { display: none; background: none; border: 0; font-size: 20px; width: 36px; height: 36px; border-radius: 8px; }
.tabs { display: flex; gap: 4px; background: var(--panel-2); border-radius: 10px; padding: 3px; }
.tab { background: none; border: 0; border-radius: 8px; padding: 5px 14px; font-size: 14px; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* Chat */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; scroll-behavior: smooth; }
.msg { max-width: 760px; margin: 0 auto 18px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bg); color: var(--user-text); padding: 10px 14px; border-radius: 18px 18px 4px 18px;
  max-width: 85%; white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .bubble { overflow-wrap: anywhere; }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble table { border-collapse: collapse; display: block; overflow-x: auto; margin: 10px 0; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; vertical-align: top; }
.bubble th { background: var(--panel-2); }
.bubble code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 0.9em; }
.bubble pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.bubble pre code { background: none; padding: 0; }
.bubble h1, .bubble h2, .bubble h3 { font-size: 1.05em; margin: 1em 0 .4em; }
.bubble ul, .bubble ol { padding-left: 1.3em; }
.tools { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.tool-chip {
  font-size: 12px; color: var(--muted); background: var(--panel-2); border-radius: 999px; padding: 2px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.tool-chip.running::before { content: "⟳ "; display: inline-block; animation: spin 1s linear infinite; }
.tool-chip.fail { color: var(--err); }
@keyframes spin { to { transform: rotate(360deg); } }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
.msg-error { color: var(--err); font-size: 14px; margin-top: 6px; }

.empty { max-width: 760px; margin: 12vh auto 0; text-align: center; }
.empty h2 { font-size: 22px; margin: 0 0 16px; letter-spacing: -0.01em; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip { background: var(--panel); border: 1px solid var(--border); border-radius: 999px; padding: 8px 14px; font-size: 14px; }
.chip:hover { border-color: var(--accent); }

.composer {
  display: flex; gap: 8px; align-items: flex-end; max-width: 792px; width: 100%; margin: 0 auto;
  padding: 8px 16px max(12px, env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; max-height: 40dvh; min-height: 46px; padding: 12px 14px; border-radius: 14px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); font: inherit; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.composer button {
  width: 46px; height: 46px; flex-shrink: 0; border-radius: 50%; border: 0; background: var(--accent);
  color: var(--accent-text); font-size: 20px; font-weight: 700;
}
.composer button:disabled { opacity: .4; cursor: default; }

/* Plans */
.split { flex: 1; min-height: 0; display: flex; }
.plan-list { width: 300px; flex-shrink: 0; overflow-y: auto; border-right: 1px solid var(--border); padding: 12px 8px; }
.plan-group { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 12px 10px 4px; }
.plan-item { display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 8px; padding: 8px 10px; }
.plan-item:hover, .plan-item.active { background: var(--panel-2); }
.plan-item b { display: block; font-size: 14px; font-weight: 600; }
.plan-item small { color: var(--muted); font-size: 12px; }
.plan-view { flex: 1; overflow-y: auto; padding: 20px 24px; }
.plan-view .bubble { max-width: 820px; }
.plan-meta { font-size: 13px; color: var(--muted); margin-bottom: 14px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.plan-meta select { background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; font-size: 13px; }
.back { display: none; margin-bottom: 12px; }

/* Memory */
.page { flex: 1; overflow-y: auto; padding: 20px 16px; max-width: 820px; width: 100%; margin: 0 auto; }
.mem { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; }
.mem b { display: block; margin-bottom: 4px; }
.mem small { color: var(--muted); }
.actions { font-size: 13px; }
.action { padding: 6px 0; border-bottom: 1px solid var(--border); display: flex; gap: 10px; }
.action code { font-size: 12px; }

/* Mobile */
@media (max-width: 760px) {
  .icon-btn { display: inline-block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s ease;
    width: 84%; max-width: 320px; padding-top: env(safe-area-inset-top);
  }
  .sidebar.open { transform: none; }
  .scrim.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 10; }
  .split { flex-direction: column; }
  .plan-list { width: 100%; border-right: 0; }
  .split.reading .plan-list { display: none; }
  .split:not(.reading) .plan-view { display: none; }
  .plan-view { padding: 16px; }
  .back { display: inline-block; }
  .msg.user .bubble { max-width: 92%; }
}
