/* WhatsApp-like palette: green is the resting color, red only while capturing. */
:root {
  --bg: #f7f8f6;
  --fg: #111b21;
  --muted: #667781;
  --faint: #9aa6ad;
  --line: #e3e8ea;
  --surface: #ffffff;
  --green: #00a884;
  --green-press: #008f72;
  --green-soft: #25d366;
  --green-tint: rgba(37, 211, 102, 0.14);
  --rec: #f15c6d;
  --rec-press: #d94456;
  --rec-tint: rgba(241, 92, 109, 0.2);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --fg: #e9edef;
    --muted: #8696a0;
    --faint: #56656e;
    --line: #222d34;
    --surface: #111b21;
    --green-tint: rgba(37, 211, 102, 0.12);
    --rec-tint: rgba(241, 92, 109, 0.22);
  }
}

* { box-sizing: border-box; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  flex-direction: column;
  /* Pinned app height (set from JS) so the on-screen keyboard can't squeeze the layout. */
  height: var(--app-h, 100dvh);
  overflow: hidden;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}

/* One screen: brand, text card (grows), controls pinned below within thumb reach. */
main {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 12px 0 8px;
}

/* ---------- header: brand left, language right ---------- */

.top {
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 0 4px;
}

[data-state="loading"] .top { visibility: hidden; }

.brand {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.035em;
  color: var(--fg);
  white-space: nowrap;
}

.brand em {
  font-style: normal;
  color: var(--green);
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--green);
  color: #fff;
  flex-shrink: 0;
}

.brand-mark svg { width: 100%; height: 100%; }

.lang-pick {
  position: relative;
  flex-shrink: 0;
}

/* Fixed width (fits "Português"), so the pill doesn't jump when switching language. */
.lang-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 164px;
  min-height: 44px;
  padding: 0 12px 0 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  font-size: 15px;
  font-weight: 600;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.lang-name { flex: 1; text-align: left; }

@media (hover: hover) {
  .lang-btn:hover { border-color: var(--green-soft); }
}

.lang-btn:focus-visible, .lang-btn[aria-expanded="true"] {
  outline: none;
  border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-tint);
}

.lang-pick svg {
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.lang-pick .globe { width: 18px; height: 18px; stroke-width: 1.6; color: var(--green); }
.lang-pick .chev { width: 18px; height: 18px; stroke-width: 2; color: var(--muted); transition: transform 0.2s; }
.lang-btn[aria-expanded="true"] .chev { transform: rotate(180deg); }

.lang-menu {
  position: absolute;
  z-index: 10;
  top: calc(100% + 8px);
  right: 0;
  width: max-content;
  min-width: 100%;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 16px 40px -12px rgba(0, 0, 0, 0.28);
  animation: menu-in 0.14s ease-out;
  transform-origin: top right;
  outline: none;
}

.lang-menu[hidden] { display: none; }

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

.lang-menu li {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 46px;
  padding: 0 12px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  outline: none;
}

.lang-menu li:hover, .lang-menu li:focus-visible { background: var(--green-tint); }

.lang-menu .code {
  flex-shrink: 0;
  width: 26px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.lang-menu .name { flex: 1; padding-right: 12px; }

.lang-menu .check { flex-shrink: 0; width: 18px; height: 18px; stroke-width: 2.4; color: var(--green); visibility: hidden; }
.lang-menu [aria-selected="true"] { font-weight: 700; }
.lang-menu [aria-selected="true"] .check { visibility: visible; }

[data-state="recording"] .lang-pick, [data-state="transcribing"] .lang-pick { opacity: 0.4; pointer-events: none; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: 0;
  background: none;
  touch-action: manipulation;
}

/* ---------- state visibility ---------- */

.login, .card, .recorder, footer { display: none; }
[data-state="login"] .login { display: flex; }
body:not([data-state="login"]):not([data-state="loading"]) .card,
body:not([data-state="login"]):not([data-state="loading"]) .recorder,
body:not([data-state="login"]):not([data-state="loading"]) footer:not([hidden]) { display: flex; }
[data-state="recording"] footer, [data-state="transcribing"] footer { visibility: hidden; }

/* ---------- login ---------- */

.login {
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 300px;
  margin: auto 0;
}

.login input {
  font: inherit;
  font-size: 16px; /* prevents iOS zoom */
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--fg);
  outline: none;
}

.login input:focus { border-color: var(--green); }

.login button {
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--green);
  color: #fff;
  font-weight: 600;
}

.login button:active { background: var(--green-press); }

.error { margin: 0; color: var(--rec); font-size: 14px; text-align: center; }

/* ---------- text card ---------- */

.card {
  flex: 1 1 auto;
  min-height: 0;
  max-height: 520px;
  width: 100%;
  flex-direction: column;
  background: var(--surface);
  border: 2px solid var(--green-soft);
  border-radius: 18px;
  overflow: hidden;
  transition: border-color 0.2s;
}

[data-state="recording"] .card { border-color: var(--rec); }
.card:focus-within { box-shadow: 0 0 0 4px var(--green-tint); }

.text {
  flex: 1;
  min-height: 0;
  padding: 20px 22px;
  font-size: 19px;
  line-height: 1.55;
  overflow-y: auto;
  overscroll-behavior: contain;
  white-space: pre-wrap;
  word-wrap: break-word;
  user-select: text;
  -webkit-user-select: text;
  outline: none;
  cursor: text;
  caret-color: var(--green);
}

.text:empty {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px 32px;
}

.text:empty::before {
  content: attr(data-placeholder);
  color: var(--faint);
  font-size: 17px;
  max-width: 300px;
}

[data-state="recording"] .text:empty::before { content: attr(data-listening); }

/* Live preview is provisional: dimmer until the final pass lands. */
.text.live { color: var(--muted); }
.text.live::after {
  content: "";
  display: inline-block;
  width: 2px;
  height: 1.1em;
  margin-left: 3px;
  vertical-align: -0.18em;
  background: var(--rec);
  animation: blink 1s steps(1) infinite;
}

@keyframes blink { 50% { opacity: 0; } }

.card-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 52px;
  padding: 8px 10px 8px 22px;
  border-top: 1px solid var(--line);
}

.card-bar:has(.copy[hidden]):has(.copied:empty) { display: none; }

.copied {
  font-size: 15px;
  font-weight: 600;
  color: var(--green);
}

.copied.pending { color: var(--muted); font-weight: 400; }
.copied.flash { animation: pop 0.3s ease-out; }

@keyframes pop { from { transform: scale(0.85); opacity: 0; } }

.copy {
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--green);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  transition: transform 0.08s ease, background-color 0.15s;
}

.copy:active { transform: scale(0.95); background: var(--green-press); }

/* ---------- controls: timer on top, REC in the middle, Clear bottom-right ---------- */

.recorder {
  width: 100%;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.timer {
  margin: 0;
  height: 26px;
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--rec);
  visibility: hidden;
}

[data-state="recording"] .timer { visibility: visible; }

.timer::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 8px;
  vertical-align: 0.12em;
  border-radius: 50%;
  background: var(--rec);
  animation: blink 1.2s steps(1) infinite;
}

.controls {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

/* The REC button: flat green disc with a white mic; flat red with a stop square while recording. */
.mic {
  --size: clamp(96px, 26vw, 112px);
  position: relative;
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  display: grid;
  place-items: center;
  transform: scale(calc((1 + var(--v, 0) * 0.07) * var(--hold, 1)));
  transition: transform 0.12s ease-out, background-color 0.2s;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* holding to talk: the button swells under the thumb, like WhatsApp */
.mic.holding { --hold: 1.14; }

.mic.pressed:not(.holding) { transform: scale(0.92); }
.mic:focus-visible { outline: 3px solid var(--green-soft); outline-offset: 5px; }

@media (hover: hover) {
  [data-state="idle"] .mic:hover, [data-state="error"] .mic:hover { background: var(--green-press); }
}

.icon {
  position: relative;
  grid-area: 1 / 1;
  width: 64%;
  height: 64%;
}

.icon-stop { width: 34%; height: 34%; display: none; }

[data-state="recording"] .mic { background: var(--rec); }
[data-state="recording"] .mic:active { background: var(--rec-press); }
[data-state="recording"] .icon-mic { display: none; }
[data-state="recording"] .icon-stop { display: block; }

[data-state="transcribing"] .mic { background: var(--surface); color: var(--faint); box-shadow: inset 0 0 0 2px var(--line); }

/* Flat voice halo: grows with the voice level (--v, 0..1) set from JS. */
.level {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--rec-tint);
  transform: scale(calc(1 + var(--v, 0) * 0.3));
  opacity: 0;
  transition: transform 0.07s linear, opacity 0.2s;
}

[data-state="recording"] .level { opacity: 1; }

.spinner {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: var(--green);
  opacity: 0;
}

[data-state="transcribing"] .spinner { opacity: 1; animation: spin 0.8s linear infinite; }

@keyframes spin { to { transform: rotate(360deg); } }

/* Clear: a quiet pill matching the language picker; turns red only on intent. */
.clear {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px 0 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  color: var(--fg);
  font-size: 15px;
  font-weight: 600;
  animation: fade-in 0.2s ease-out;
  transition: border-color 0.15s, color 0.15s, background-color 0.15s, transform 0.08s;
}

.clear svg { width: 20px; height: 20px; color: var(--muted); transition: color 0.15s; }
.clear:active { transform: scale(0.95); }
.clear:focus-visible { outline: none; border-color: var(--rec); box-shadow: 0 0 0 3px var(--rec-tint); }

@media (hover: hover) {
  .clear:hover { border-color: color-mix(in srgb, var(--rec) 55%, var(--line)); }
  .clear:hover svg { color: var(--rec); }
}

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

.clear[hidden] { display: none; }
[data-state="recording"] .clear, [data-state="transcribing"] .clear { visibility: hidden; }

.status {
  margin: 0;
  min-height: 1.5em;
  font-size: 15px;
  color: var(--muted);
  text-align: center;
}

[data-state="error"] .status { color: var(--rec); }

.retry {
  font-size: 14px;
  color: var(--fg);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 4px 8px;
}

/* ---------- footer: language (and engine) below a hairline ---------- */

footer {
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  justify-content: center;
  gap: 18px;
  padding: 10px 0 14px;
  border-top: 1px solid var(--line);
}

.pick {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}

.pick[hidden] { display: none; }

.pick svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
}

.pick select {
  font: inherit;
  font-weight: 500;
  color: var(--fg);
  background: transparent;
  border: 0;
  padding: 6px 2px;
  cursor: pointer;
  outline: none;
}

.pick select:focus-visible { outline: 2px solid var(--green-soft); border-radius: 6px; }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
