:root {
  --bg: #0f0d0a;
  --bg-2: #181510;
  --bg-3: #221d15;
  --text: #ede4d3;
  --text-dim: #9a8f7a;
  --text-faint: #5a5145;
  --accent: #e89d5a;
  --accent-deep: #c44f38;
  --border: rgba(237,228,211,0.08);
  --border-strong: rgba(237,228,211,0.16);
  --serif: "Newsreader", "Iowan Old Style", Georgia, serif;
  --display: "Fraunces", "Hoefler Text", Georgia, serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.55;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
body::before {
  content: "";
  position: fixed; inset: 0;
  pointer-events: none; opacity: 0.04; z-index: 100;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}

.app {
  max-width: 640px; margin: 0 auto;
  /* fall back chain: 100vh → 100dvh → JS-set --vh (most reliable on mobile) */
  height: 100vh;
  height: 100dvh;
  height: var(--vh, 100dvh);
  display: flex; flex-direction: column;
  padding:
    calc(14px + env(safe-area-inset-top, 0px))
    calc(22px + env(safe-area-inset-right, 0px))
    calc(18px + env(safe-area-inset-bottom, 0px))
    calc(22px + env(safe-area-inset-left, 0px));
  gap: 14px;
}

/* --- Nav --- */
nav.tabs {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}
.brand {
  font-family: var(--display);
  font-variation-settings: "opsz" 72, "SOFT" 50;
  font-weight: 400;
  font-size: 22px;
  letter-spacing: -0.02em;
  color: var(--text);
}
.brand em {
  font-style: italic;
  font-variation-settings: "opsz" 144, "SOFT" 100;
  color: var(--accent);
}
.tab-links {
  display: flex; gap: 16px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.tab-links a {
  color: var(--text-faint);
  text-decoration: none;
  padding: 4px 0;
  border-bottom: 1px solid transparent;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.tab-links a.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.nav-right {
  display: flex; align-items: center; gap: 14px;
}
.gear-btn {
  background: transparent; border: 0; padding: 4px;
  color: var(--text-faint);
  cursor: pointer;
  line-height: 0;
  transition: color 0.15s ease;
}
.gear-btn:active, .gear-btn:hover { color: var(--accent); }
.gear-btn svg { display: block; width: 16px; height: 16px; }

/* --- Settings modal --- */
.modal {
  position: fixed; inset: 0;
  background: rgba(15,13,10,0.78);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 60;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease;
}
.modal.visible { opacity: 1; pointer-events: auto; }
.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 20px 22px;
  display: flex; flex-direction: column; gap: 16px;
  /* Pro-controls modal can grow tall on phones — let it scroll inside the
     overlay rather than push the Done button off-screen. */
  max-height: calc(100vh - 48px);
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.modal-title {
  font-family: var(--display);
  font-variation-settings: "opsz" 144, "SOFT" 100;
  font-weight: 400;
  font-style: italic;
  font-size: 22px;
  color: var(--accent);
  letter-spacing: -0.01em;
}
.setting {
  display: flex; flex-direction: column; gap: 8px;
}
.setting-label {
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.setting-help {
  font-family: var(--serif);
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.45;
  margin-top: -2px;
}
.seg-control {
  display: flex; flex-wrap: wrap; gap: 6px;
}
.seg-control button {
  flex: 1 1 auto;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  min-width: 52px;
}
.seg-control button.active {
  background: var(--accent);
  color: #1a0f05;
  border-color: var(--accent);
}

/* --- Native select styled as a field (matches Speak's voice picker visual) --- */
.select-wrap {
  position: relative;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 30px 10px 12px;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text);
}
.select-wrap select {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
}
.select-wrap::after {
  content: "";
  position: absolute;
  right: 14px;
  top: 50%;
  width: 6px;
  height: 6px;
  border-right: 1.5px solid var(--text-dim);
  border-bottom: 1.5px solid var(--text-dim);
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
}

/* --- Slider with adjacent value readout --- */
.slider-row {
  display: flex; align-items: center; gap: 12px;
}
.slider-row input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  border-radius: 2px;
  background: var(--border-strong);
  outline: none;
  margin: 0;
  /* The thumb is bigger than the track — give it elbow room so it isn't
     clipped at the ends. */
  padding: 6px 0;
  background-clip: content-box;
}
.slider-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 0;
}
.slider-row input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 0;
}
.slider-value {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  min-width: 64px;
  text-align: right;
  flex-shrink: 0;
}

/* Inline help-style notice that appears when a setting won't take effect
   immediately (e.g. model change while a session is running). */
.setting-note {
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  margin-top: 4px;
}

/* --- Advanced disclosure --- */
.advanced {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  margin-top: 2px;
}
.advanced summary {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-dim);
  cursor: pointer;
  list-style: none;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.advanced summary::-webkit-details-marker { display: none; }
.advanced summary::before {
  content: "";
  width: 6px; height: 6px;
  border-right: 1.5px solid var(--text-dim);
  border-bottom: 1.5px solid var(--text-dim);
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
}
.advanced[open] summary::before { transform: rotate(45deg); }
.advanced[open] summary { color: var(--text); margin-bottom: 14px; }
.advanced .setting + .setting { margin-top: 12px; }
.advanced-actions {
  margin-top: 14px;
  display: flex; justify-content: flex-end;
}
.reset-btn {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 8px 14px;
  border-radius: 7px;
  cursor: pointer;
}
.reset-btn:active { background: var(--bg-3); color: var(--text); }

.modal-actions {
  display: flex; justify-content: flex-end; gap: 10px;
  margin-top: 4px;
}
.modal-actions button {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
}
.modal-actions button.primary {
  background: var(--accent);
  color: #1a0f05;
  border-color: var(--accent);
}
.modal-actions button.primary:active { filter: brightness(0.92); }
.modal-body {
  font-family: var(--serif);
  font-size: 14px;
  color: var(--text-dim);
  line-height: 1.5;
  margin: 0;
}

/* --- Status bar --- */
.status-row {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: -2px;
}
.intro {
  font-family: var(--display);
  font-weight: 300;
  font-variation-settings: "opsz" 144, "SOFT" 80;
  font-size: 14px;
  font-style: italic;
  color: var(--text-dim);
  line-height: 1.5;
  flex: 1;
  margin-right: 14px;
}
.intro em { color: var(--accent); font-style: italic; }
.status {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-dim);
  display: flex; align-items: center; gap: 8px;
  white-space: nowrap;
}
.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-faint);
  transition: all 0.3s ease;
}
.status[data-state="loading"] .status-dot { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.status[data-state="ready"] .status-dot { background: #6ecb7d; }
.status[data-state="recording"] .status-dot { background: var(--accent-deep); animation: pulse 1.4s ease-in-out infinite; }
.status[data-state="error"] .status-dot { background: var(--accent-deep); }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(196,79,56,0.4); }
  50% { box-shadow: 0 0 0 8px rgba(196,79,56,0); }
}

/* --- Visualizer --- */
.viz {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  height: 72px;
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}
.viz canvas {
  width: 100%; height: 100%;
  display: block;
}
.viz-meta {
  position: absolute;
  left: 14px; top: 10px;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-faint);
  pointer-events: none;
}
.viz-meta .active { color: var(--accent); }

/* --- Transcript --- */
.transcript-wrap {
  flex: 1;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.transcript-header {
  padding: 10px 16px 8px;
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
  flex-wrap: nowrap;
  min-height: 30px;
}
.transcript-header .meta {
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 1;
}
.transcript-header .actions {
  display: flex;
  gap: 0;
  flex-shrink: 0;
  white-space: nowrap;
}
.transcript-header button {
  background: transparent; border: 0; padding: 0;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-dim);
  cursor: pointer;
}
.transcript-header button:active { color: var(--accent); }
.transcript-header .sep { color: var(--text-faint); margin: 0 10px; }

.transcript {
  flex: 1;
  overflow-y: auto;
  padding: 18px 18px 24px;
  font-family: var(--serif);
  font-variation-settings: "opsz" 16;
  font-size: 18px;
  line-height: 1.6;
  color: var(--text);
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}
.transcript:empty::before {
  content: "Waiting for speech…";
  color: var(--text-faint);
  font-style: italic;
}
.transcript .seg {
  animation: fadeIn 0.35s ease;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(2px); }
  to { opacity: 1; transform: translateY(0); }
}
.transcript .pending {
  color: var(--text-faint);
  letter-spacing: 0.3em;
  animation: pendingBlink 1.2s ease-in-out infinite;
}
@keyframes pendingBlink {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 0.9; }
}
.transcript .live-cursor {
  display: inline-block;
  width: 2px; height: 1em;
  background: var(--accent);
  vertical-align: text-bottom;
  margin-left: 2px;
  animation: cursorBlink 0.8s ease-in-out infinite;
}
@keyframes cursorBlink {
  0%, 45% { opacity: 1; }
  50%, 95% { opacity: 0.15; }
}

/* --- Controls --- */
.controls {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: stretch;
  flex-shrink: 0;
}
.icon-btn.accent-link {
  color: var(--accent);
  border-color: rgba(232,157,90,0.25);
}
.icon-btn.accent-link:disabled {
  color: var(--text-faint);
  border-color: var(--border);
}
.icon-btn {
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 0 16px;
  min-height: 54px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.icon-btn:active { background: var(--bg-3); }
.icon-btn:disabled { color: var(--text-faint); cursor: not-allowed; }

.stop {
  background: var(--accent-deep);
  color: #fff1e8;
  border: 0;
  border-radius: 12px;
  padding: 0 22px;
  min-height: 54px;
  font-family: var(--display);
  font-variation-settings: "opsz" 144, "SOFT" 50;
  font-weight: 500;
  font-size: 17px;
  letter-spacing: -0.005em;
  cursor: pointer;
  transition: transform 0.1s ease;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.stop:active { transform: scale(0.98); }
.stop.start {
  background: var(--accent);
  color: #1a0f05;
}
.stop .dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: currentColor;
}
.stop.start .dot {
  border-radius: 2px;
}

/* --- Setup overlay --- */
.setup {
  position: fixed; inset: 0;
  background: rgba(15,13,10,0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.3s ease;
}
.setup.visible { opacity: 1; pointer-events: auto; }
.setup-inner { max-width: 360px; text-align: center; }
.setup-title {
  font-family: var(--display);
  font-variation-settings: "opsz" 144, "SOFT" 100;
  font-weight: 400;
  font-style: italic;
  font-size: 30px;
  color: var(--accent);
  margin-bottom: 14px;
  letter-spacing: -0.01em;
}
.setup-body {
  font-family: var(--serif);
  font-size: 15.5px;
  line-height: 1.55;
  color: var(--text-dim);
  margin-bottom: 22px;
}
.setup-progress {
  width: 100%; height: 2px;
  background: var(--border); border-radius: 2px;
  overflow: hidden; margin-bottom: 10px;
}
.setup-progress-fill {
  height: 100%; background: var(--accent);
  width: 0%; transition: width 0.3s ease;
}
.setup-meta {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-faint);
  min-height: 14px;
}
.setup-btn {
  margin-top: 18px;
  background: var(--accent);
  border: 0;
  color: #1a0f05;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 14px 26px;
  border-radius: 9px;
  cursor: pointer;
  font-weight: 500;
}

/* toast */
.toast {
  position: fixed; bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-3);
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-family: var(--mono);
  font-size: 11px;
  padding: 10px 16px;
  border-radius: 8px;
  opacity: 0; pointer-events: none;
  transition: all 0.25s ease;
  z-index: 200;
  letter-spacing: 0.06em;
}
.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}
.toast-action {
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 11px;
  padding: 4px 10px;
  margin-left: 12px;
  border-radius: 6px;
  cursor: pointer;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.toast-action:hover { background: rgba(232, 157, 90, 0.12); }
