/* Cakap Thai: tokens and components from .bossku/DESIGN.md ("khram cotton"). */

:root {
  color-scheme: light dark;
  --bg: #F5F2EA;
  --bg-outer: #EBE7DD;
  --surface: #FFFFFF;
  --surface-2: #EAE6DC;
  --ink: #161A26;
  --ink-2: #545A69;
  --line: #D9D4C8;
  --line-strong: #6F7483;
  --accent: #2B3A8C;
  --accent-press: #22307A;
  --on-accent: #FFFFFF;
  --accent-soft: #E3E6F5;
  --danger: #A8261B;
  --danger-soft: #FBE9E6;
  --on-danger: #FFFFFF;
  --warn: #7A4F00;
  --warn-soft: #FBF0D9;
  --scrim: rgb(15 17 23 / 0.48);
  --shadow-sheet: 0 -8px 24px rgb(22 26 38 / 0.12);
  --shadow-float: 0 2px 8px rgb(22 26 38 / 0.16);

  --radius: 14px;
  --radius-in: 10px; /* nested box inside 4px padding */
  --gutter: 16px;
  --ease: cubic-bezier(0.2, 0, 0, 1);

  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Sukhumvit Set", Thonburi, "Noto Sans Thai", "Noto Sans Thai UI", "Leelawadee UI", Leelawadee, Tahoma, sans-serif;
  --font-th: "Sukhumvit Set", Thonburi, "Noto Sans Thai", "Noto Sans Thai UI", "Leelawadee UI", Leelawadee, Tahoma, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F1117;
    --bg-outer: #0A0C10;
    --surface: #181B24;
    --surface-2: #222632;
    --ink: #EDEAE2;
    --ink-2: #A6ABB8;
    --line: #2E3240;
    --line-strong: #767B8B;
    --accent: #9AA8F5;
    --accent-press: #B4BFF8;
    --on-accent: #0F1117;
    --accent-soft: #262C4D;
    --danger: #FFB4A8;
    --danger-soft: #3A1A17;
    --on-danger: #0F1117;
    --warn: #F2C46B;
    --warn-soft: #33280F;
    --shadow-sheet: none;
    --shadow-float: 0 2px 8px rgb(0 0 0 / 0.5);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 1rem/1.5 var(--font-ui);
}

:lang(th) { font-family: var(--font-th); line-height: 1.6; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

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

.icon {
  width: 24px; height: 24px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- Shell ---------- */

.app {
  display: grid;
  grid-template-rows: auto auto auto minmax(30dvh, 1fr) auto; /* top bar, direction tabs, banner, feed, dock */
  height: calc(100dvh - var(--keyboard, 0px));
  max-width: 560px;
  margin-inline: auto;
  background: var(--bg);
}

@media (min-width: 600px) {
  body { background: var(--bg-outer); }
  .app { border-inline: 1px solid var(--line); }
}

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: calc(56px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(var(--gutter), env(safe-area-inset-right)) 0 max(var(--gutter), env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 1.125rem; line-height: 1.5rem; font-weight: 700; }
.brand img { border-radius: 6px; }

/* ---------- Buttons ---------- */

.icon-btn {
  display: inline-grid; place-items: center;
  width: 48px; height: 48px; flex: none;
  border: 0; border-radius: var(--radius); background: transparent; color: var(--ink);
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.accent { background: var(--accent); color: var(--on-accent); }
.icon-btn.accent:hover { background: var(--accent-press); }
.icon-btn[aria-disabled="true"], .icon-btn:disabled { color: var(--ink-2); background: transparent; cursor: not-allowed; }
.icon-btn.accent:disabled { background: var(--surface-2); color: var(--ink-2); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 16px;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); font-weight: 600;
  transition: background-color 150ms var(--ease), color 150ms var(--ease), border-color 150ms var(--ease);
}
.btn:hover { background: var(--surface-2); }
.btn:disabled, .btn[aria-disabled="true"] { color: var(--ink-2); border-color: var(--line); cursor: not-allowed; background: var(--surface); }
.btn.danger-outline { color: var(--danger); border-color: var(--danger); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: var(--on-danger); }

.text-btn {
  min-height: 44px; padding: 0 12px;
  border: 0; border-radius: var(--radius); background: transparent;
  color: var(--ink); font-weight: 600; font-size: 0.9375rem;
  text-decoration: underline; text-underline-offset: 3px;
}
.text-btn:hover { background: var(--surface-2); }

.ghost-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 12px; justify-self: center;
  border: 0; border-radius: var(--radius); background: transparent; color: var(--ink); font-weight: 600;
}
.ghost-btn:hover { background: var(--surface-2); }

/* ---------- Banner ---------- */

.banner {
  display: grid; grid-template-columns: auto 1fr auto; gap: 4px 12px; align-items: start;
  padding: 12px var(--gutter);
  border-inline-start: 4px solid var(--line-strong);
  background: var(--surface-2);
  font-size: 0.9375rem; line-height: 1.375rem;
  animation: fade 150ms var(--ease);
}
.banner .icon { margin-top: 1px; }
.banner p { margin: 0; }
.banner .banner-title { font-weight: 600; }
.banner .banner-more { color: var(--ink); margin-top: 6px; }
.banner .banner-actions { grid-column: 2; display: flex; flex-wrap: wrap; gap: 4px; margin-inline-start: -12px; }
.banner .icon-btn { width: 44px; height: 44px; margin-top: -10px; }
.banner[data-tone="warn"] { background: var(--warn-soft); border-color: var(--warn); }
.banner[data-tone="warn"] > .icon { color: var(--warn); }
.banner[data-tone="danger"] { background: var(--danger-soft); border-color: var(--danger); }
.banner[data-tone="danger"] > .icon { color: var(--danger); }

/* ---------- Feed ---------- */

.feed-wrap { position: relative; min-height: 0; display: grid; }
.feed {
  overflow-y: auto; overscroll-behavior: contain;
  padding: var(--gutter) max(var(--gutter), env(safe-area-inset-right)) var(--gutter) max(var(--gutter), env(safe-area-inset-left));
  scroll-padding-block: var(--gutter);
}

.turns { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }

/* Clean card: no border or label row; heard text small, translation big, slim action column. */
.card {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 4px; align-items: start;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 4px 12px 16px;
  animation: fade 120ms var(--ease);
}
.card.th-en { background: var(--surface-2); }
.card.speaking { box-shadow: inset 3px 0 0 var(--accent); }
.card.error { box-shadow: inset 0 0 0 1px var(--danger); }
.card:focus { outline: none; }
.card:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.card-main { min-width: 0; padding-block: 2px; }

.empty {
  display: grid; align-content: center; gap: 6px; min-height: 100%;
  padding: 24px 16px; text-align: center; color: var(--ink-2);
}
.empty p { margin: 0; }
.empty-lead { color: var(--ink); font-weight: 600; font-size: 1.0625rem; }

.heard { margin: 0 0 4px; color: var(--ink-2); font-size: 0.9375rem; line-height: 1.375rem; overflow-wrap: break-word; }
.heard:lang(th) { line-height: 1.5rem; }

.trans { margin: 0; font-weight: 500; overflow-wrap: break-word; cursor: pointer; }
.trans:lang(th) { font-size: 1.625rem; line-height: 2.5rem; }
.trans:lang(en) { font-size: 1.375rem; line-height: 1.875rem; }
/* Earlier turns step back so the newest one stands out. */
.turns > li:not(:last-child) .trans:lang(th) { font-size: 1.25rem; line-height: 2rem; }
.turns > li:not(:last-child) .trans:lang(en) { font-size: 1.125rem; line-height: 1.625rem; }

.say { margin: 6px 0 0; font-size: 1rem; line-height: 1.5rem; color: var(--ink); overflow-wrap: break-word; }
.say-label { color: var(--ink-2); font-size: 0.875rem; font-weight: 600; }
.turns > li:not(:last-child) .say { font-size: 0.9375rem; }

.card-side { display: flex; flex-direction: column; margin-top: -6px; }
.card-side .icon-btn { width: 44px; height: 44px; color: var(--ink-2); }
.card-side .icon-btn:hover { color: var(--ink); }
.card-side .icon { width: 20px; height: 20px; }
.trans.streaming::after {
  content: ""; display: inline-block; width: 2px; height: 1em;
  margin-inline-start: 3px; vertical-align: -0.15em; background: var(--accent);
}

.skeleton { width: 60%; height: 2.5rem; border-radius: var(--radius-in); background: var(--surface-2); }
.card.th-en .skeleton { background: var(--line); }

.card-msg { margin: 0; font-weight: 600; }
.card.error .card-msg { color: var(--danger); }
.card-detail { margin: 4px 0 0; color: var(--ink-2); font-size: 0.9375rem; }
.card-actions { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0 0 -12px; }

.jump {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%);
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 48px; padding: 0 16px;
  border: 0; border-radius: var(--radius);
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
  box-shadow: var(--shadow-float);
  z-index: 20;
}

/* ---------- Dock ---------- */

.dock {
  display: grid; gap: 12px;
  padding: 12px max(var(--gutter), env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(var(--gutter), env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: var(--bg);
  z-index: 10;
}

.live-strip { min-height: 3.25rem; display: flex; align-items: flex-end; }
.live-strip p { margin: 0; color: var(--ink-2); overflow-wrap: break-word; }

.status {
  display: flex; align-items: center; gap: 8px; min-height: 44px;
  color: var(--ink-2); font-size: 0.9375rem; line-height: 1.375rem; font-weight: 600;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); flex: none; }
.status.live .dot { background: var(--accent); }
.status #cancel-btn { margin-inline-start: auto; }

.dir-bar {
  padding: 8px max(var(--gutter), env(safe-area-inset-right)) 8px max(var(--gutter), env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
}

.mics { display: grid; grid-template-columns: 1fr; }
.mic {
  display: grid; justify-items: center; align-content: center; gap: 2px;
  min-height: 88px; min-width: 0; padding: 10px 8px;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface-2); color: var(--ink);
  transition: background-color 150ms var(--ease), color 150ms var(--ease), border-color 150ms var(--ease);
}
.mic:hover { border-color: var(--ink); }
.mic-l1 { font-size: 1.125rem; line-height: 1.5rem; font-weight: 700; }
.mic-l1:lang(th) { line-height: 1.75rem; }
.mic-l2 { font-size: 0.875rem; line-height: 1.25rem; font-weight: 600; text-align: center; }
.mic[data-state="listening"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.mic[data-state="listening"]:hover { background: var(--accent-press); }
.mic[data-state="paused"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.mic[data-state="disabled"] { background: var(--surface-2); color: var(--ink-2); border-color: transparent; cursor: not-allowed; }
.mic[data-state="listening"]:focus-visible { outline-offset: 3px; }

/* ---------- Type bar ---------- */

.typebar { display: grid; gap: 8px; }
.segmented {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius);
}
.segmented label { position: relative; display: grid; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented label > span {
  display: flex; align-items: center; justify-content: center; gap: 0.3em;
  min-height: 44px; border-radius: var(--radius-in);
  font-weight: 600; color: var(--ink-2);
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}
.segmented input:checked + span { background: var(--accent-soft); color: var(--accent); }
.segmented input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }

.type-row { display: flex; align-items: flex-end; gap: 8px; }
.type-row textarea {
  flex: 1; min-width: 0; min-height: 48px; max-height: calc(4 * 1.6em + 26px);
  padding: 11px 12px; resize: none;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface); line-height: 1.6;
}
.type-row textarea::placeholder { color: var(--ink-2); opacity: 1; }
.type-note { margin: 0; color: var(--ink-2); font-size: 0.875rem; }
#type-close { justify-self: center; }

/* ---------- Dialogs ---------- */

dialog { color: var(--ink); padding: 0; border: 0; }
dialog::backdrop { background: var(--scrim); }

.sheet, .dialog {
  background: var(--surface);
  box-shadow: var(--shadow-sheet);
  border-top: 1px solid var(--line);
}
.sheet[open], .dialog[open] { animation: rise 240ms var(--ease); }

.sheet {
  width: 100%; max-width: 100%; max-height: 90dvh;
  margin: auto 0 0; border-radius: var(--radius) var(--radius) 0 0;
}
.dialog { width: min(420px, calc(100% - 32px)); border-radius: var(--radius); border: 1px solid var(--line); }
@media (min-width: 600px) {
  .sheet { width: 480px; margin: auto; border-radius: var(--radius); border: 1px solid var(--line); }
}

.sheet-inner { padding: 20px var(--gutter) max(20px, env(safe-area-inset-bottom)); display: grid; gap: 4px; }
.sheet h2, .dialog h2 { margin: 0; font-size: 1.125rem; line-height: 1.5rem; font-weight: 700; }
.sheet h2:focus { outline: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-bottom: 8px; }
.sheet p { margin: 8px 0; }

.row { margin: 0; padding: 12px 0; border: 0; border-top: 1px solid var(--line); display: grid; gap: 8px; min-width: 0; }
.row legend { padding: 0; font-weight: 600; float: left; width: 100%; }
.row > label, .row-inline > label, .row-inline > span, .row-title { font-weight: 600; }
label.row-inline { cursor: pointer; }
.helper-more summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; font-weight: 600; font-size: 0.875rem; text-decoration: underline; text-underline-offset: 3px; }
.helper-more p, .helper > p { margin: 0; }
.row-inline { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.helper { margin: 0; color: var(--ink-2); font-size: 0.875rem; line-height: 1.25rem; }
.sheet p.helper { margin: 0; }
.field-error { margin: 0; color: var(--danger); font-size: 0.875rem; font-weight: 600; }

.radio { display: flex; align-items: center; gap: 12px; min-height: 48px; }
.radio input { width: 22px; height: 22px; margin: 0; accent-color: var(--accent); }

.switch {
  appearance: none; -webkit-appearance: none; flex: none;
  width: 52px; height: 32px; margin: 0; position: relative;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius-in);
  background: var(--surface-2);
  transition: background-color 150ms var(--ease), border-color 150ms var(--ease);
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 23px; height: 23px;
  border-radius: 6px; background: var(--line-strong);
  transition: transform 150ms var(--ease), background-color 150ms var(--ease);
}
.switch:checked { background: var(--accent); border-color: var(--accent); }
.switch:checked::after { transform: translateX(20px); background: var(--on-accent); }

input[type="range"] { width: 100%; min-height: 44px; margin: 0; accent-color: var(--accent); }

.select-row { display: flex; gap: 8px; }
.select-row select, .select-row input {
  flex: 1; min-width: 0; min-height: 48px; padding: 0 12px;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface); font-size: 1rem;
}
.select-row select:disabled { color: var(--ink-2); border-color: var(--line); }
input[aria-invalid="true"] { border-color: var(--danger); }

.option {
  display: grid; justify-items: start; gap: 2px;
  min-height: 64px; width: 100%; margin-top: 8px; padding: 10px 16px;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface); text-align: start;
}
.option:hover { background: var(--accent-soft); border-color: var(--accent); }
.option-l1 { font-size: 1.125rem; font-weight: 700; line-height: 1.75rem; }
.option-l2 { color: var(--ink-2); font-size: 0.9375rem; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

/* Show view: the translation, big enough to read across a counter. */
.show-view {
  width: 100vw; max-width: none; height: 100dvh; max-height: none; margin: 0;
  background: var(--surface); z-index: 60;
}
.show-view[open] { animation: fade 160ms var(--ease); }
.show-inner {
  display: grid; grid-template-rows: auto 1fr auto; height: 100%; gap: 16px;
  padding: max(12px, env(safe-area-inset-top)) max(var(--gutter), env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(var(--gutter), env(safe-area-inset-left));
}
.show-top { display: flex; justify-content: flex-end; }
.close-btn {
  display: inline-flex; align-items: center; gap: 8px; min-height: 56px; min-width: 56px; padding: 0 16px;
  border: 1.5px solid var(--line-strong); border-radius: var(--radius); background: var(--surface); font-weight: 600;
}
.show-text {
  margin: 0; align-self: center; overflow-y: auto; max-height: 100%;
  font-size: clamp(2.5rem, 6vw + 1rem, 5.5rem); font-weight: 500; line-height: 1.3; overflow-wrap: break-word;
}
.show-text:lang(th) { line-height: 1.5; }
.show-text.long { font-size: min(clamp(2.5rem, 6vw + 1rem, 5.5rem), 3.5rem); }
.show-text.longer { font-size: 2.5rem; }
.wide-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 56px; width: 100%;
  border: 0; border-radius: var(--radius); background: var(--accent); color: var(--on-accent); font-weight: 700;
}
.wide-btn:hover { background: var(--accent-press); }
.wide-btn:disabled { background: var(--surface-2); color: var(--ink-2); cursor: not-allowed; }

/* ---------- Motion ---------- */

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  .sheet[open], .dialog[open] { animation: fade 120ms var(--ease); }
  .switch::after { transition: none; }
}

/* ---------- Small and short screens ---------- */

@media (max-width: 340px) {
  :root { --gutter: 12px; }
  .trans:lang(th) { font-size: 1.5rem; line-height: 2.375rem; }
}

@media (max-height: 500px) and (orientation: landscape) {
  .topbar { min-height: calc(48px + env(safe-area-inset-top)); }
  .mic { min-height: 64px; }
  .live-strip { min-height: 1.75rem; }
}
