:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --live: #e5484d;
  --radius: 14px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --border: #2a2f37;
    --accent: #4c8dff;
    --live: #ff6369;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* Top bar */
.bar {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px;
}
.bar > :first-child { justify-self: start; }


.bar-left { display: flex; align-items: center; gap: 2px; }
.text-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 10px;
  min-height: 40px;
  border-radius: 999px;
}
.text-btn:not(:disabled):active { background: var(--border); }
.text-btn:disabled { opacity: .3; cursor: default; }
#saved-btn { position: relative; }
.badge {
  position: absolute;
  top: 4px;
  right: 2px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: .65rem;
  line-height: 16px;
  text-align: center;
}
.bar-right { display: flex; align-items: center; gap: 4px; justify-self: end; }

/* Cloud / Offline switch */
.engine-pill {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 12px;
  min-height: 32px;
  font-size: .85rem;
}
.engine-pill[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.engine-pill:disabled { opacity: .5; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: transparent;
}
.icon-btn svg { width: 24px; height: 24px; fill: var(--muted); }
.icon-btn:not(:disabled):active { background: var(--border); }
.icon-btn:disabled { opacity: .3; cursor: default; }

/* The surface: the draft, big and readable */
.surface {
  flex: 1;
  overflow-y: auto;
  padding: 12px 24px 24px;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
}

.text {
  margin: 0;
  font-size: clamp(1.25rem, 1rem + 1.4vw, 1.6rem);
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  user-select: text;
}
.text.fresh { animation: fade-in .35s ease-out; }
/* Live on-device transcript replacing the draft while you speak. */
.text.live::after {
  content: '';
  display: inline-block;
  width: .5em;
  height: 1em;
  margin-left: .15em;
  vertical-align: -.12em;
  border-radius: 2px;
  background: var(--live);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }
.surface.busy .text { opacity: .55; transition: opacity .2s; }

/* Draft text hidden for focus */
.hidden-note {
  margin: 20vh 0 0;
  color: var(--muted);
  text-align: center;
  font-size: 1.05rem;
}
#eye-btn .icon-eye { display: none; }
#eye-btn[aria-pressed="true"] .icon-eye { display: block; }
#eye-btn[aria-pressed="true"] .icon-eye-off { display: none; }

.placeholder {
  margin: 20vh 0 0;
  color: var(--muted);
  text-align: center;
  font-size: 1.05rem;
}

@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } }

/* Bottom dock */
.dock {
  padding: 8px 16px calc(env(safe-area-inset-bottom) + 18px);
  display: grid;
  justify-items: center;
  gap: 6px;
}

.status {
  margin: 0;
  min-height: 1.5em;
  color: var(--muted);
  font-size: .9rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.status.error { color: var(--live); }

.dock-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 420px;
}
.dock-left { justify-self: end; grid-column: 1; display: flex; gap: 8px; }
.dock-right { justify-self: start; grid-column: 3; display: flex; gap: 8px; }
.mic { grid-column: 2; }

.side-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 8px 16px;
  min-height: 40px;
  color: var(--muted);
}
.side-btn:disabled { visibility: hidden; }
.dock-left .side-btn, .dock-right .side-btn { padding: 8px 12px; }
#save-btn.done { color: var(--accent); border-color: currentColor; }

/* Saved drafts */
.sheet textarea {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--bg);
  font: inherit;
  color: inherit;
}
.sheet-head { display: flex; align-items: center; gap: 8px; }
.sheet-head > h2 { flex: 1; }
.sheet-head h2 { margin-right: auto; }
#clear-notes-btn { color: var(--live); margin-right: auto; }
.sheet-head .copy-all { min-height: 36px; padding: 4px 10px; white-space: nowrap; }
#saved .actions { flex-wrap: wrap; }
#saved .actions .btn { white-space: nowrap; }
.saved-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.saved-list li.playing { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
#play-all-btn[data-active] { color: var(--accent); border-color: currentColor; }
.saved-list li {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--bg);
}
.saved-list .saved-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.saved-list time { display: block; font-size: .8rem; color: var(--muted); margin-top: 4px; }
.saved-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.saved-actions .btn { min-height: 34px; padding: 4px 12px; font-size: .9rem; }
.saved-actions .danger { color: var(--live); margin-left: auto; }
.saved-actions .icon { width: 34px; padding: 0; display: grid; place-items: center; }
.saved-actions .icon svg { width: 18px; height: 18px; fill: currentColor; pointer-events: none; }
#listen-btn[data-state="loading"] { opacity: .6; }
#listen-btn[data-state="playing"] { color: var(--accent); border-color: currentColor; }
#retry-btn { color: var(--live); border-color: currentColor; }

/* Mic button: idle -> recording -> busy */
.mic {
  --level: 0;
  position: relative;
  width: 84px; height: 84px;
  border: 0; border-radius: 50%;
  background: var(--accent);
  display: grid; place-items: center;
  box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 35%, transparent);
  transition: background .2s, transform .1s;
}
.mic:active { transform: scale(.95); }
.mic svg { width: 38px; height: 38px; fill: var(--accent-text); position: relative; grid-area: 1 / 1; }
.mic .icon-stop, .mic .spinner { display: none; }

.mic .ring {
  position: absolute; inset: 0;
  border-radius: 50%;
  background: var(--live);
  opacity: 0;
  transform: scale(calc(1 + var(--level) * .5));
  transition: transform 80ms linear, opacity .2s;
}

.mic[aria-pressed="true"] { background: var(--live); box-shadow: 0 6px 24px color-mix(in srgb, var(--live) 40%, transparent); }
.mic[aria-pressed="true"] .ring { opacity: .3; }
.mic[aria-pressed="true"] .icon-mic { display: none; }
.mic[aria-pressed="true"] .icon-stop { display: block; }

.mic.busy { background: var(--muted); box-shadow: none; }
.mic.busy .icon-mic { display: none; }
.mic.busy .spinner {
  display: block;
  grid-area: 1 / 1;
  width: 34px; height: 34px;
  border: 3px solid color-mix(in srgb, var(--accent-text) 35%, transparent);
  border-top-color: var(--accent-text);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Settings */
.sheet { max-height: calc(100dvh - 32px); overflow-y: auto; }
.sheet select {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--bg);
  width: 100%;
}
.provider-fields { display: grid; gap: 12px; }
.sheet label.check { display: flex; align-items: center; gap: 10px; }
.sheet input[type=range] { width: 100%; padding: 0; border: 0; background: none; accent-color: var(--accent); }
.sheet input[type=checkbox] { width: 20px; height: 20px; padding: 0; accent-color: var(--accent); flex: none; }
.provider-fields[hidden] { display: none; }
.offline {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px 12px;
  margin: 0;
  display: grid;
  gap: 10px;
}
.offline legend { padding: 0 6px; font-size: .95rem; }
.model-row { display: flex; align-items: center; gap: 10px; justify-content: space-between; }
.model-row .btn { flex: none; }
.offline progress { width: 100%; accent-color: var(--accent); }
.sheet {
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  width: min(520px, calc(100vw - 32px));
  padding: 18px;
}
.sheet::backdrop { background: rgb(0 0 0 / .45); }
.sheet h2 { margin: 0; font-size: 1.1rem; }
.sheet form { display: grid; gap: 12px; }
.sheet label { display: grid; gap: 4px; font-size: .95rem; }
.sheet input {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--bg);
  width: 100%;
}
.sheet input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.actions { display: flex; gap: 8px; justify-content: flex-end; }
.btn {
  border-radius: 10px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  background: transparent;
  min-height: 40px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.muted { color: var(--muted); font-weight: normal; }
.small { font-size: .85rem; margin: 0; }

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 60px);
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 10px;
  max-width: calc(100vw - 32px);
  z-index: 10;
}
