:root {
  --bg: #f5f1e8;
  --bg-subtle: #efe9dc;
  --paper: #fffdf8;
  --paper-soft: #faf6ee;
  --ink: #232a22;
  --muted: #6a7068;
  --faint: #9aa09a;
  --line: rgba(40,50,40,.10);
  --line-strong: rgba(40,50,40,.18);
  --green: #2a7a5e;
  --green-soft: rgba(42,122,94,.12);
  --gold: #9a7435;
  --gold-soft: rgba(154,116,53,.12);
  --blue: #3a6080;
  --blue-soft: rgba(58,96,128,.12);
  --coral: #c05040;
  --coral-soft: rgba(192,80,64,.12);
  --amber: #b87a20;
  --amber-soft: rgba(184,122,32,.12);
  --font-body: -apple-system, BlinkMacSystemFont, "Noto Sans TC", "PingFang TC", "Segoe UI", sans-serif;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-pill: 999px;
  --shadow-card: 0 1px 3px rgba(40,36,28,.06), 0 4px 12px rgba(40,36,28,.05);
  --shadow-elevated: 0 8px 30px rgba(40,36,28,.10);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #080a0d;
    --bg-subtle: #0d1118;
    --paper: #121923;
    --paper-soft: #17202b;
    --ink: #f6f0df;
    --muted: #a9b3c3;
    --faint: #5a6778;
    --line: rgba(255,255,255,.12);
    --line-strong: rgba(255,255,255,.20);
    --gold: #d8b46a;
    --gold-soft: rgba(216,180,106,.14);
    --shadow-card: 0 4px 20px rgba(0,0,0,.25);
    --shadow-elevated: 0 12px 34px rgba(0,0,0,.34);
  }
}

* {
  box-sizing: border-box;
}

html {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 14px;
  letter-spacing: 0;
  line-height: 1.72;
  min-height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  background: radial-gradient(900px 520px at 90% -10%, var(--gold-soft), transparent 60%), var(--bg);
  margin: 0;
  min-height: 100vh;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

button,
select {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  min-height: 100vh;
  padding-bottom: calc(20px + var(--safe-bottom));
}

.topbar {
  align-items: center;
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(30,40,50,.06);
  display: flex;
  gap: 16px;
  justify-content: space-between;
  min-height: calc(72px + var(--safe-top));
  padding: calc(10px + var(--safe-top)) 18px 12px;
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.eyebrow {
  color: var(--gold);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .18em;
  line-height: 1.2;
  margin: 0;
  text-transform: uppercase;
}

h1,
h2 {
  line-height: 1.2;
  margin: 0;
}

h1 {
  font-size: 24px;
}

h2 {
  font-size: 18px;
}

.status-pill {
  align-items: center;
  background: var(--paper-soft);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-pill);
  color: var(--ink);
  display: inline-flex;
  flex: 0 0 auto;
  font-size: 12px;
  font-weight: 700;
  gap: 7px;
  min-height: 32px;
  padding: 0 10px;
}

.status-dot {
  background: var(--amber);
  border-radius: var(--radius-pill);
  display: inline-block;
  height: 7px;
  width: 7px;
}

.status-pill.ready {
  background: var(--green-soft);
}

.status-pill.ready .status-dot {
  background: var(--green);
}

.status-pill.error {
  background: var(--coral-soft);
}

.status-pill.error .status-dot {
  background: var(--coral);
}

.main-grid {
  display: grid;
  gap: 14px;
  margin: 0 auto;
  padding: 16px 18px 0;
  width: min(980px, 100%);
}

.control-card,
.output-card,
.note-card,
.history-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  display: grid;
  gap: 14px;
  padding: 18px;
}

.section-head,
.panel-meta {
  align-items: start;
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.section-head.compact {
  align-items: center;
}

.field-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.select-control {
  background: var(--paper-soft);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
  min-height: 44px;
  outline: none;
  padding: 10px 12px;
  width: 100%;
}

.select-control:focus {
  border-color: var(--green);
}

.quick-actions {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.turn-readout {
  background: var(--green-soft);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  display: grid;
  gap: 2px;
  min-height: 74px;
  padding: 12px;
}

.turn-readout span,
.turn-readout small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.turn-readout strong {
  color: var(--green);
  font-size: 20px;
  line-height: 1.25;
}

.session-readout {
  align-items: center;
  background: var(--paper-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  display: flex;
  justify-content: space-between;
  min-height: 44px;
  padding: 8px 12px;
}

.session-readout span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.session-readout strong,
.language-tag {
  color: var(--green);
  font-weight: 700;
}

.start-button {
  align-items: center;
  background: var(--ink);
  border: 1px solid var(--ink);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-elevated);
  color: var(--paper);
  display: grid;
  gap: 9px;
  justify-items: center;
  min-height: 132px;
  padding: 18px;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
  user-select: none;
  transition: background-color .14s ease, border-color .14s ease, transform .08s ease, filter .14s ease;
  width: 100%;
}

.start-button:active,
.start-button.pressing {
  filter: brightness(1.08);
  transform: scale(.985);
}

.start-button.active {
  background: var(--green);
  border-color: var(--green);
}

.start-button.recording {
  background: var(--coral);
  border-color: var(--coral);
}

.start-button.connecting {
  background: var(--amber);
  border-color: var(--amber);
}

.button-icon {
  align-items: center;
  background: color-mix(in srgb, var(--paper) 16%, transparent);
  border: 1px solid color-mix(in srgb, var(--paper) 28%, transparent);
  border-radius: var(--radius-pill);
  display: inline-flex;
  height: 54px;
  justify-content: center;
  width: 54px;
}

.button-icon svg {
  height: 28px;
  width: 28px;
}

#startLabel {
  font-size: 18px;
  font-weight: 700;
}

.secondary-button,
.icon-button {
  align-items: center;
  border-radius: var(--radius-pill);
  display: inline-flex;
  justify-content: center;
}

.secondary-button {
  background: color-mix(in srgb, var(--paper) 82%, transparent);
  border: 1px solid var(--line-strong);
  color: var(--ink);
  gap: 7px;
  min-height: 42px;
  padding: 0 12px;
}

.secondary-button:disabled {
  color: var(--faint);
}

.select-control:disabled,
.icon-button:disabled {
  cursor: not-allowed;
  opacity: .72;
}

.secondary-button svg,
.icon-button svg {
  height: 18px;
  width: 18px;
}

.icon-button {
  height: 38px;
  min-width: 38px;
  padding: 0;
}

.icon-button.secondary {
  background: color-mix(in srgb, var(--paper) 82%, transparent);
  border: 1px solid var(--line-strong);
  color: var(--ink);
}

.icon-button.ghost {
  background: transparent;
  border: 0;
  color: var(--muted);
}

.output-grid {
  display: grid;
  gap: 14px;
}

.language-tag {
  font-size: 12px;
  margin: 0;
}

.text-output {
  color: var(--ink);
  font-size: clamp(21px, 6.2vw, 34px);
  font-weight: 700;
  letter-spacing: 0;
  line-height: 1.32;
  min-height: 96px;
  overflow-wrap: anywhere;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.source-text {
  color: var(--muted);
  font-size: clamp(18px, 5vw, 26px);
  font-weight: 600;
}

.result-card {
  border-color: var(--line-strong);
}

.result-text.playable {
  cursor: pointer;
}

.result-text.playable:active {
  color: var(--green);
}

.hint-list {
  display: grid;
  gap: 6px;
}

.hint-list p,
.empty-note {
  color: var(--muted);
  margin: 0;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-item {
  background: var(--paper-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  display: grid;
  gap: 4px;
  padding: 10px 12px;
}

.history-item small {
  color: var(--muted);
  font-size: 12px;
}

.history-item strong {
  font-size: 15px;
  line-height: 1.48;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.toast {
  background: var(--ink);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-pill);
  bottom: calc(16px + var(--safe-bottom));
  color: var(--paper);
  left: 50%;
  max-width: min(420px, calc(100% - 36px));
  padding: 10px 14px;
  position: fixed;
  transform: translateX(-50%);
  z-index: 10;
}

@media (min-width: 760px) {
  .main-grid {
    grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  }

  .output-grid,
  .history-card {
    grid-column: 2;
  }

  .control-card {
    grid-row: span 2;
    position: sticky;
    top: 92px;
  }
}

@media (max-width: 390px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .status-pill {
    align-self: flex-start;
  }

  .quick-actions {
    grid-template-columns: 1fr;
  }
}
