:root {
  color-scheme: dark;
  --bg: #0b0b0d;
  --surface: #16171a;
  --surface-2: #1d1f24;
  --border: #2a2d33;
  --text: #e8eaee;
  --text-dim: #8b8f98;
  --accent: #ff9f43;
  --user: #2a4365;
  --assistant: #2b2b2b;
  --danger: #e06662;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  /* --app-h is set by viewport.ts (measured visualViewport.height). We fall
   * back to 100dvh for the brief interval before the script runs. */
  height: var(--app-h, 100dvh);
  overflow: hidden;
  box-sizing: border-box;
}

body.chat .app,
body.voice .voice-app {
  display: flex;
  flex-direction: column;
  height: 100%;
}
body.chat .app > *, body.voice .voice-app > * { flex-shrink: 0; }
body.chat .log, body.voice .voice-stage { flex-shrink: 1; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  gap: 8px;
}
.brand {
  font-weight: 600;
  letter-spacing: 0.02em;
  font-size: 16px;
}
.topbar a {
  color: var(--text);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--surface-2);
}
.topbar a:hover { background: var(--border); }
.spacer { width: 36px; }

/* Chat */
.log {
  flex: 1;
  min-height: 0;        /* allow the scroll container to shrink inside flex */
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}
.msg {
  max-width: 86%;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant, .msg.system { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--assistant);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.4;
  font-size: 15px;
}
.msg.user .bubble {
  background: var(--user);
}
.msg.system .bubble {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  font-size: 13px;
}
.meta {
  font-size: 11px;
  color: var(--text-dim);
  padding: 0 6px;
}

/* Composer */
.composer {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  align-items: flex-end;
  /* The composer is not the bottommost element on chat (nav sits below),
   * but when the nav is hidden (none today, kept for safety) this still
   * sits inside the safe area. */
}
.composer textarea { font-size: 16px; } /* Prevent iOS zoom-on-focus */
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius);
  font-size: 15px;
  max-height: 160px;
  font-family: inherit;
  line-height: 1.4;
}
.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.composer button {
  border: none;
  background: var(--accent);
  color: #1a1a1a;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.composer button:hover { filter: brightness(1.05); }
.composer button:disabled { opacity: 0.6; cursor: not-allowed; }

/* Voice */
.voice-stage {
  flex: 1;
  min-height: 0;        /* same flex fix as .log */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 24px;
  overflow-y: auto;
}
.record {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 150ms, background 150ms, border-color 150ms;
}
.record:hover { background: var(--surface-2); }
.record:active { transform: scale(0.97); }
.record[data-state="recording"] {
  border-color: var(--danger);
  background: rgba(224, 102, 98, 0.1);
  animation: pulse 1.4s ease-in-out infinite;
}
.record[data-state="uploading"] {
  border-color: var(--accent);
  cursor: progress;
}
.record[data-state="delay"] {
  border-color: var(--accent);
  background: rgba(255, 159, 67, 0.1);
}
.record-dot {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--danger);
  transition: border-radius 150ms, transform 150ms, opacity 150ms;
}
.record-count {
  position: absolute;
  font-size: 48px;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.record { position: relative; }
.record[data-state="idle"] .record-dot,
.record[data-state="done"] .record-dot,
.record[data-state="error"] .record-dot {
  background: var(--danger);
}
.record[data-state="recording"] .record-dot {
  border-radius: 8px;
  background: var(--danger);
}
.record[data-state="uploading"] .record-dot {
  background: var(--accent);
  animation: spin 1s linear infinite;
}
.record[data-state="delay"] .record-dot {
  opacity: 0;
}

.status { color: var(--text-dim); font-size: 14px; text-align: center; padding: 0 16px; }

/* Voice mode switch */
.mode-switch {
  display: flex;
  gap: 6px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
}
.mode-chip {
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 120ms, color 120ms;
}
.mode-chip.active {
  background: var(--accent);
  color: #1a1a1a;
}
.mode-chip:not(.active):hover {
  background: var(--surface-2);
  color: var(--text);
}

/* Confirm + delay panels */
.confirm-panel {
  width: min(520px, 92vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.confirm-panel textarea {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.4;
  resize: vertical;
  min-height: 96px;
}
.confirm-panel textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.btn-primary, .btn-ghost {
  padding: 9px 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary {
  background: var(--accent);
  color: #1a1a1a;
}
.btn-primary:hover { filter: brightness(1.05); }
.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border-color: var(--border);
}
.btn-ghost:hover {
  color: var(--text);
  background: var(--surface-2);
}


/* Bottom navigation (shared across chat / voice / history). */
/* Nav is pinned to the bottom of the *visible* viewport (height comes from
 * JS via --app-h), with its own safe-area padding so it never hides under
 * the Android gesture bar. Layouts below reserve space via --nav-h. */
.bottom-nav {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding-bottom: var(--safe-bottom, 16px);
  position: relative;
}
.bottom-nav .nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 0 8px;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 11px;
  letter-spacing: 0.02em;
  font-weight: 500;
}
.bottom-nav .nav-item.active { color: var(--accent); }
.bottom-nav .nav-item:active { background: var(--surface-2); }

/* Wrapper for the center capture button + floating discard. */
.capture-wrap {
  position: relative;
  display: inline-flex;
  align-items: flex-end;
  justify-content: center;
}
.quick-discard {
  position: absolute;
  bottom: calc(100% + 14px);           /* clear of the capture button */
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--danger);
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  z-index: 300;                        /* above the toast */
}
/* [hidden] wins by !important so our display value below doesn't leak. */
.quick-discard[hidden] { display: none !important; }
.quick-discard:not([hidden]) { display: inline-flex; }
.quick-discard:hover { background: var(--surface-2); }

/* Center capture button */
.capture-btn {
  border: 0;
  background: var(--accent);
  color: #1a1a1a;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin: -16px 6px 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(255, 159, 67, 0.35);
  cursor: pointer;
  transition: transform 120ms, background 120ms, box-shadow 120ms;
  touch-action: none;
}
.capture-btn:active { transform: scale(0.96); }
.capture-btn[data-state="recording"] {
  background: var(--danger);
  box-shadow: 0 4px 18px rgba(224, 102, 98, 0.55);
  animation: capture-pulse 1.4s ease-in-out infinite;
}
.capture-btn[data-state="uploading"] {
  background: var(--text-dim);
}
@keyframes capture-pulse {
  0%, 100% { box-shadow: 0 4px 18px rgba(224, 102, 98, 0.35); }
  50%      { box-shadow: 0 4px 24px rgba(224, 102, 98, 0.9); }
}

/* Quick-record status toast — pinned to the top of the viewport so it
 * never covers the capture button or the discard pill. */
.quick-toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  top: calc(env(safe-area-inset-top, 0) + 10px);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms, transform 120ms;
  z-index: 200;
  max-width: 90vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.quick-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.quick-toast[data-state="recording"] { border-color: var(--danger); }
.quick-toast[data-state="error"] { border-color: var(--danger); color: var(--danger); }
.quick-toast[data-state="done"] { border-color: var(--accent); color: var(--accent); }

/* Voice modal — bottom sheet. Native <dialog> centers by default, so we
 * pin to the bottom explicitly. Closed state stays `display: none` (scoped
 * to [open]) or the sheet would be visible on page load. */
.voice-modal {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: auto;
  margin: 0 auto;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 18px 18px 0 0;
  padding: 0;
  width: 100%;
  max-width: 640px;
  max-height: 92vh;
  overflow: hidden;
  transition: transform 180ms ease-out;
}
.voice-modal[open] {
  display: flex;
  flex-direction: column;
}
.voice-modal.dragging { transition: none; }
.voice-modal::backdrop { background: rgba(0, 0, 0, 0.55); }
.voice-modal-handle {
  width: 44px;
  height: 4px;
  border-radius: 999px;
  background: var(--border);
  margin: 8px auto 0;
  flex-shrink: 0;
  touch-action: none;          /* let JS handle the drag */
  cursor: grab;
}
.voice-modal-handle-hit {
  /* Large tap target so the thin grab line is easy to hit with a thumb. */
  width: 100%;
  padding: 12px 0 6px;
  display: flex;
  justify-content: center;
  touch-action: none;
  cursor: grab;
}
.voice-modal-handle-hit:active { cursor: grabbing; }
.voice-modal .voice-stage {
  padding: 18px 24px 8px;
  flex: 1;
  overflow-y: auto;
}
.voice-modal-foot {
  padding: 8px 16px calc(12px + env(safe-area-inset-bottom, 0));
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.btn-wide {
  width: 100%;
  padding: 14px;
  font-size: 15px;
}

/* Icon-only buttons in topbars (reload, etc.) */
.icon-btn {
  background: var(--surface-2);
  border: 0;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:hover { background: var(--border); }

.topbar-actions { display: inline-flex; gap: 6px; }
.btn-close-chevron {
  width: 100%;
  padding: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.mode-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 36px;
  padding: 0;
}
.mode-chip svg { display: block; }

.btn-primary.icon-only, .btn-ghost.icon-only {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 14px;
  min-width: 54px;
}
.day-divider {
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
  padding: 8px 0 4px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* History list as collapsible sessions. */
.history-log {
  display: block;              /* override chat's flex column */
  padding: 10px;
}
.empty-state {
  color: var(--text-dim);
  text-align: center;
  padding: 40px 0;
}
.session {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 8px;
  overflow: hidden;
}
.session[open] { background: var(--surface-2); }
.session-summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.session-summary::-webkit-details-marker { display: none; }
.session-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}
.session-count { color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: 12px; }
.session-preview {
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.session-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 14px 14px;
  border-top: 1px solid var(--border);
}

/* Queue badge + panel on voice-app */
.queue-badge {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: #1a1a1a;
  border: 0;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.queue-badge:hover { filter: brightness(1.05); }

.queue-panel {
  width: min(520px, 92vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
}
.queue-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  color: var(--text-dim);
  font-size: 14px;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224, 102, 98, 0.5); }
  50%      { box-shadow: 0 0 0 16px rgba(224, 102, 98, 0); }
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

/* Token dialog */
.token-dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 320px;
}
.token-dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.token-dialog h2 { margin: 0 0 6px; font-size: 18px; }
.token-dialog .hint { color: var(--text-dim); font-size: 13px; margin: 0 0 12px; }
.token-dialog input {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  font-family: monospace;
}
.token-dialog .actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 12px;
}
.token-dialog button {
  padding: 8px 16px;
  background: var(--accent);
  color: #1a1a1a;
  border: none;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: white;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 1000;
}
