:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-sidebar: #f4f4f5;
  --bg-card: #f7f7f8;
  --bg-input: #f0f0f1;
  --fg: #111114;
  --fg-dim: #6b6b73;
  --fg-faint: #9a9aa2;
  --border: rgba(0,0,0,0.08);
  --accent: #4d6bfe;
  --accent-fg: #ffffff;
  --danger: #ff3b30;
  --radius: 22px;
  --radius-sm: 14px;
  --radius-pill: 999px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191919;
    --bg-sidebar: #141414;
    --bg-card: #232323;
    --bg-input: #2a2a2a;
    --fg: #f5f5f5;
    --fg-dim: #9a9aa2;
    --fg-faint: #6b6b73;
    --border: rgba(255,255,255,0.09);
  }
}
html[data-theme="light"] { color-scheme: light; --bg:#fff; --bg-sidebar:#f4f4f5; --bg-card:#f7f7f8; --bg-input:#f0f0f1; --fg:#111114; --fg-dim:#6b6b73; --fg-faint:#9a9aa2; --border:rgba(0,0,0,0.08); }
html[data-theme="dark"]  { color-scheme: dark;  --bg:#191919; --bg-sidebar:#141414; --bg-card:#232323; --bg-input:#2a2a2a; --fg:#f5f5f5; --fg-dim:#9a9aa2; --fg-faint:#6b6b73; --border:rgba(255,255,255,0.09); }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}
body { overflow: hidden; overflow-x: hidden; }

/* ---- Layout ---- */
.app {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
  background: var(--bg);
}

.topbar {
  display: flex; align-items: center; gap: 6px;
  height: 52px; padding: 0 12px;
  background: var(--bg);
  flex-shrink: 0;
  position: relative;
}
.topbar-title {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; align-items: center;
}
#chatTitle { font-size: 15px; font-weight: 600; }
.topbar-model { font-size: 11px; color: var(--fg-dim); }

.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  background: transparent; border: 0; color: var(--fg);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background 0.15s;
}
.icon-btn:active { background: var(--bg-card); }

/* Tabs */
.tabs {
  display: flex; gap: 4px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab {
  flex: 0 0 auto;
  padding: 10px 4px 12px;
  margin-right: 16px;
  background: transparent; border: 0;
  color: var(--fg-dim);
  font-size: 14px; font-weight: 500;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.tab.active { color: var(--fg); border-bottom-color: var(--accent); }

/* Chat scroll — sits between topbar and fixed composer */
.chat-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 12px 16px 140px; /* bottom padding makes room for fixed composer */
  scroll-behavior: smooth;
}
.messages { display: flex; flex-direction: column; gap: 14px; max-width: 720px; margin: 0 auto; }

/* Message bubbles */
.msg { display: flex; flex-direction: column; gap: 6px; animation: fadeUp 0.25s ease; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg-user { align-items: flex-end; }
.msg-user .bubble {
  background: var(--bg-card);
  color: var(--fg);
  padding: 10px 14px;
  border-radius: 20px 20px 4px 20px;
  max-width: 85%;
  font-size: 15px; line-height: 1.45;
  white-space: pre-wrap; word-wrap: break-word;
}
.msg-assistant .bubble {
  background: transparent;
  color: var(--fg);
  padding: 2px 0;
  max-width: 100%;
  font-size: 15px; line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-assistant .bubble pre {
  background: var(--bg-card);
  padding: 12px; border-radius: 12px;
  overflow-x: auto;
  font-size: 13px;
}
.msg-assistant .bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}

/* Empty state */
.empty-state {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 0 24px 140px; text-align: center;
  color: var(--fg-dim);
  pointer-events: none;
}
.empty-logo { color: var(--accent); margin-bottom: 14px; }
.empty-state h1 { font-size: 24px; font-weight: 600; color: var(--fg); margin: 0 0 6px; }
.empty-state p { font-size: 14px; margin: 0; }

/* ---- Fixed composer at bottom ---- */
.composer-wrap {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 8px 12px calc(10px + var(--safe-bottom));
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  background: var(--bg);
  z-index: 10;
}
.composer {
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 26px;
  padding: 6px 6px 6px 12px;
  max-width: 720px; margin: 0 auto;
  transition: border-color 0.15s;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 50%, var(--border)); }
.composer textarea {
  flex: 1; resize: none;
  background: transparent; border: 0; outline: none;
  color: var(--fg); font-size: 16px;
  font-family: inherit;
  padding: 10px 4px;
  max-height: 140px; min-height: 24px;
  line-height: 1.35;
}
.composer-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: 0; display: grid; place-items: center;
  cursor: pointer; flex-shrink: 0;
  background: transparent; color: var(--fg-dim);
  transition: transform 0.1s, background 0.15s, color 0.15s;
}
.composer-btn:active { background: var(--bg-card); }
.composer-btn.active { color: var(--accent); }
.composer-send {
  background: var(--fg);
  color: var(--bg);
}
.composer-send:disabled { opacity: 0.3; }
.composer-send:active:not(:disabled) { transform: scale(0.92); }
.composer-stop {
  background: var(--fg); color: var(--bg);
}
.stop-square { width: 12px; height: 12px; border-radius: 3px; background: var(--bg); }

.composer-meta {
  display: flex; align-items: center; justify-content: center;
  gap: 6px; margin-top: 6px;
  font-size: 11.5px; color: var(--fg-dim);
  max-width: 720px; margin-left: auto; margin-right: auto;
}
.meta-chip {
  background: var(--bg-card); border: 1px solid var(--border);
  color: var(--fg-dim);
  padding: 3px 8px; border-radius: var(--radius-pill);
  font-size: 11.5px; cursor: pointer;
}
.meta-sep { opacity: 0.4; }

/* ---- Sidebar ---- */
.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: min(82vw, 320px);
  background: var(--bg-sidebar);
  color: var(--fg);
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 20;
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  border-right: 1px solid var(--border);
  will-change: transform;
}
.sidebar.open { transform: translateX(0); }
.scrim {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  opacity: 0; transition: opacity 0.25s;
  z-index: 15;
}
.scrim.show { opacity: 1; }

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px 4px;
}
.sidebar-header .brand { font-weight: 600; font-size: 16px; }
.sidebar-nav { padding: 8px; display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px;
  background: transparent; border: 0;
  color: var(--fg); font-size: 15px; text-align: left;
  border-radius: 12px; cursor: pointer;
  min-height: 44px;
  transition: background 0.15s;
  width: 100%;
}
.nav-item:active { background: var(--bg-card); }
.sidebar-section-title {
  font-size: 11px; color: var(--fg-faint);
  padding: 12px 12px 6px; text-transform: uppercase;
  letter-spacing: 0.5px;
}
.project-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 10px;
  color: var(--fg-dim); font-size: 14px;
  background: transparent; border: 0; cursor: pointer;
  text-align: left; width: 100%;
}
.project-row:active { background: var(--bg-card); }
.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }
.usage-mini { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--fg-dim); }
.usage-mini-row { display: flex; justify-content: space-between; }

/* ---- Sheets ---- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  max-height: 82vh;
  background: var(--bg-card);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  z-index: 30;
  padding: 8px 16px calc(20px + var(--safe-bottom));
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-shadow: 0 -10px 40px rgba(0,0,0,0.4);
}
.sheet.open { transform: translateY(0); }
.sheet-handle {
  width: 40px; height: 5px; border-radius: 3px;
  background: var(--fg-faint); opacity: 0.5;
  margin: 4px auto 10px;
}
.sheet-header { display: flex; align-items: center; justify-content: space-between; }
.sheet-header h2 { font-size: 18px; margin: 0; }

/* Range tabs */
.range-tabs {
  display: flex; gap: 4px; overflow-x: auto;
  padding: 12px 0 8px;
  scrollbar-width: none;
}
.range-tabs::-webkit-scrollbar { display: none; }
.range {
  flex: 0 0 auto;
  padding: 8px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--fg-dim);
  border-radius: var(--radius-pill);
  font-size: 13px; cursor: pointer;
  white-space: nowrap;
}
.range.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

.usage-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 8px 0; }
.usage-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px; display: flex; flex-direction: column; gap: 4px;
}
.usage-card .label { font-size: 12px; color: var(--fg-dim); }
.usage-card .value { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.usage-card.full { grid-column: 1 / -1; }
.usage-note { font-size: 12px; color: var(--fg-faint); padding: 8px 0; }

/* Settings */
.settings-body { display: flex; flex-direction: column; gap: 14px; padding: 12px 0; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--fg-dim); }
.field input, .field textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 12px;
  padding: 12px;
  font-size: 15px; font-family: inherit;
  outline: none; min-height: 44px;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field.toggle { flex-direction: row; align-items: center; justify-content: space-between; }
.field.toggle input { width: auto; min-height: auto; }
.btn-primary {
  background: var(--accent); color: var(--accent-fg);
  border: 0; border-radius: 14px;
  padding: 14px; font-size: 16px; font-weight: 600;
  cursor: pointer; min-height: 50px;
}
.btn-primary:active { transform: scale(0.98); }
.settings-hint { font-size: 12px; color: var(--fg-faint); line-height: 1.5; }

/* Voice overlay */
.voice-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  z-index: 50;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 20px; color: white;
  animation: fadeIn 0.2s;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.voice-pulse {
  width: 100px; height: 100px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.5s infinite;
}
@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.15); opacity: 0.7; }
}
.voice-text { font-size: 16px; max-width: 80%; text-align: center; }
.voice-hint { font-size: 13px; opacity: 0.6; }

/* File chips inside composer */
.attach-chips {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 4px 6px;
  max-width: 720px; margin: 0 auto;
}
.attach-chip {
  display: flex; align-items: center; gap: 6px;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  padding: 4px 10px; font-size: 12px;
  color: var(--fg-dim);
}
.attach-chip button { background: transparent; border: 0; color: var(--fg-dim); cursor: pointer; font-size: 14px; padding: 0; }

/* Project picker */
.project-picker {
  display: flex; flex-direction: column; gap: 8px;
  padding: 8px 0;
}
.project-pick-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; border-radius: 12px;
  background: var(--bg); border: 1px solid var(--border);
  color: var(--fg); cursor: pointer; font-size: 15px;
}
.project-pick-row:active { background: var(--bg-card); }
.project-pick-row.selected { border-color: var(--accent); }

/* Landscape */
@media (orientation: landscape) and (max-height: 500px) {
  .topbar { height: 44px; }
}
.settings-divider {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--fg-faint);
  padding: 16px 0 4px;
  border-top: 1px solid var(--border);
  margin-top: 8px;
}
.btn-secondary {
  background: var(--bg-input);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px;
}
.btn-secondary:active { transform: scale(0.98); }
#daemonStatus.ok { color: #34c759; }
#daemonStatus.err { color: var(--danger); }
#daemonChip.connected { color: #34c759; }

/* Tool call rendering */
.tool-call {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  margin: 6px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--fg-dim);
  white-space: pre-wrap;
  word-break: break-all;
}
.tool-call .tool-name { color: var(--accent); font-weight: 600; }
.tool-call .tool-result { color: var(--fg); margin-top: 4px; }
.tool-call.error { border-color: var(--danger); }
