/* PicTalk styles — large touch targets, high contrast, calm colors.
   Designed for a child's finger on a cheap tablet: big tiles, clear labels,
   nothing tiny or fiddly on the Talk screen.

   Themes: light (default), dark, and high-contrast — switched by setting
   data-theme on <html>. Every color used by the app lives in a variable here
   so a theme is just an override block. */

:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --surface-3: #e8eef5;
  --border: #e2e8f0;
  --border-strong: #d4dce6;
  --ink: #1a2230;
  --muted: #5b6675;
  --label: #44505f;
  --brand: #1d6fb8;
  --brand-dark: #155488;
  --brand-soft: #e3f0fb;
  --brand-soft-border: #cfe2f5;
  --accent: #2bb673;
  --danger: #d2462f;
  --focus: #0b57a4;
  --shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
  --radius: 16px;
  /* Scan highlight — ≥3:1 contrast against --bg (WCAG 2.4.13). */
  --scan-row: #d97706;
  --scan-item: #c2410c;
  --scan-glow: rgba(194, 65, 12, 0.35);
  /* Modified Fitzgerald Key — soft tile tints + solid edge colors. */
  --fitz-yellow-bg: #fff7d6;  --fitz-yellow: #d9a400;
  --fitz-green-bg:  #e6f7e9;  --fitz-green:  #2e9e4f;
  --fitz-blue-bg:   #e3f0fb;  --fitz-blue:   #1d6fb8;
  --fitz-orange-bg: #fff0e0;  --fitz-orange: #e07b16;
  --fitz-purple-bg: #f3e8fb;  --fitz-purple: #8b46c8;
  --fitz-pink-bg:   #fde8f0;  --fitz-pink:   #d23f77;
  --fitz-red-bg:    #fdeae6;  --fitz-red:    #d2462f;
}

:root[data-theme="dark"] {
  --bg: #121821;
  --surface: #1d2633;
  --surface-2: #28323f;
  --surface-3: #2e3947;
  --border: #36414f;
  --border-strong: #46535f;
  --ink: #eef2f7;
  --muted: #a6b1bf;
  --label: #c2ccd8;
  --brand: #5aa2e0;
  --brand-dark: #8cc1ee;
  --brand-soft: #1f3a55;
  --brand-soft-border: #2c5076;
  --accent: #2bb673;
  --danger: #e0644e;
  --focus: #7cc0ff;
  --shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
  --scan-row: #fbbf24;
  --scan-item: #fb923c;
  --scan-glow: rgba(251, 146, 60, 0.4);
  --fitz-yellow-bg: #3a3320;  --fitz-yellow: #e8c14a;
  --fitz-green-bg:  #20362a;  --fitz-green:  #57c779;
  --fitz-blue-bg:   #1f3a55;  --fitz-blue:   #6fb1e8;
  --fitz-orange-bg: #3c2e1e;  --fitz-orange: #f0a050;
  --fitz-purple-bg: #322640;  --fitz-purple: #bb8ae0;
  --fitz-pink-bg:   #3c2430;  --fitz-pink:   #ea84ab;
  --fitz-red-bg:    #3c2521;  --fitz-red:    #f08a76;
}

:root[data-theme="contrast"] {
  --bg: #000000;
  --surface: #000000;
  --surface-2: #1a1a1a;
  --surface-3: #1a1a1a;
  --border: #ffffff;
  --border-strong: #ffffff;
  --ink: #ffffff;
  --muted: #e6e6e6;
  --label: #ffffff;
  --brand: #4db2ff;
  --brand-dark: #8ed0ff;
  --brand-soft: #00264d;
  --brand-soft-border: #4db2ff;
  --accent: #00d473;
  --danger: #ff6a4d;
  --focus: #ffd500;
  --shadow: none;
  --scan-row: #ffd500;
  --scan-item: #ffd500;
  --scan-glow: rgba(255, 213, 0, 0.45);
  --fitz-yellow-bg: #000;  --fitz-yellow: #ffd500;
  --fitz-green-bg:  #000;  --fitz-green:  #00e676;
  --fitz-blue-bg:   #000;  --fitz-blue:   #4db2ff;
  --fitz-orange-bg: #000;  --fitz-orange: #ffab40;
  --fitz-purple-bg: #000;  --fitz-purple: #d09aff;
  --fitz-pink-bg:   #000;  --fitz-pink:   #ff80ab;
  --fitz-red-bg:    #000;  --fitz-red:    #ff6a4d;
}

* { box-sizing: border-box; }

/* The hidden attribute must win over our explicit display rules below
   (display:grid/flex would otherwise override [hidden]'s display:none). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* Keyboard / switch users must always see where they are. */
:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 12px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition: none !important;
    animation: none !important;
  }
  .btn:active, .tile:active, .key:active, .pred:active, .ptile:active, .token:active {
    transform: none !important;
  }
}

/* ---- Top bar: sentence + controls ---- */
.topbar {
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 8px;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  padding-top: max(8px, env(safe-area-inset-top));
}

.sentence {
  flex: 1;
  min-height: 64px;
  /* Cap at ~2 lines and scroll inside — a long message must NEVER grow the
     top bar and squeeze the board below (it reached 9 lines on a phone). */
  max-height: 100px;
  display: flex;
  align-items: center;
  align-content: flex-start;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg);
  border-radius: var(--radius);
  font-size: 28px;
  overflow-y: auto;
}

/* Sentence words are tappable chips — tap one to hear just that word. */
.token {
  background: var(--brand);
  color: #fff;
  border: none;
  font: inherit;
  font-size: 24px;
  font-weight: 600;
  padding: 4px 14px;
  border-radius: 999px;
  white-space: nowrap;
  cursor: pointer;
  transition: transform 0.05s ease;
}
.token:active { transform: scale(0.94); }
:root[data-theme="contrast"] .token { background: var(--brand-soft); color: #fff; border: 2px solid var(--brand); }

.controls { display: flex; gap: 8px; }

.btn {
  border: none;
  border-radius: var(--radius);
  background: var(--surface-3);
  color: var(--ink);
  font-size: 20px;
  font-weight: 600;
  padding: 0 18px;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.96); }
:root[data-theme="contrast"] .btn { border: 2px solid var(--border); }

.btn.speak { background: var(--accent); color: #fff; font-size: 24px; padding: 0 26px; }
.btn.undo  { background: var(--surface-2); font-size: 26px; }
.btn.clear { background: var(--surface-2); font-size: 26px; position: relative; overflow: hidden; }
.btn.gear  { background: var(--surface-2); font-size: 24px; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.danger  { background: var(--danger); color: #fff; }
.btn.small   { font-size: 16px; padding: 8px 14px; }
:root[data-theme="contrast"] .btn.speak { color: #000; }

/* Hold-to-clear: a fill sweeps across while the button is held. */
.btn.clear::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--danger);
  opacity: 0.5;
  transform: scaleX(0);
  transform-origin: left;
}
.btn.clear.holding::after {
  transform: scaleX(1);
  transition: transform 0.6s linear;
}

/* ---- Transient "hold to clear" hint ---- */
.toast {
  position: fixed;
  left: 50%;
  top: 84px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  font-size: 16px;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.toast.show { opacity: 1; }

/* ---- Category bar ---- */
.categories {
  display: flex;
  gap: 8px;
  padding: 8px;
  overflow-x: auto;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
}

.cat {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 84px;
  padding: 8px 10px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.cat .cat-emoji { font-size: 26px; }
.cat.active {
  background: var(--brand-soft);
  border-color: var(--brand);
  color: var(--brand-dark);
}

/* ---- Tile grid ---- */
.grid {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}
body[data-tilesize="small"] .grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
body[data-tilesize="large"] .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }

.tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  aspect-ratio: 1 / 1;
  background: var(--surface);
  border: 2px solid var(--border);
  border-bottom-width: 5px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--ink);
  cursor: pointer;
  padding: 8px;
  transition: transform 0.05s ease, border-color 0.15s ease;
}
.tile:active { transform: scale(0.95); border-color: var(--brand); }

/* Modified Fitzgerald Key color coding (toggleable in settings).
   Soft background tint + solid bottom edge; label color stays --ink so
   text contrast never drops below AA. */
.tile.fitz-yellow { background: var(--fitz-yellow-bg); border-bottom-color: var(--fitz-yellow); }
.tile.fitz-green  { background: var(--fitz-green-bg);  border-bottom-color: var(--fitz-green); }
.tile.fitz-blue   { background: var(--fitz-blue-bg);   border-bottom-color: var(--fitz-blue); }
.tile.fitz-orange { background: var(--fitz-orange-bg); border-bottom-color: var(--fitz-orange); }
.tile.fitz-purple { background: var(--fitz-purple-bg); border-bottom-color: var(--fitz-purple); }
.tile.fitz-pink   { background: var(--fitz-pink-bg);   border-bottom-color: var(--fitz-pink); }
.tile.fitz-red    { background: var(--fitz-red-bg);    border-bottom-color: var(--fitz-red); }
:root[data-theme="contrast"] .tile[class*="fitz-"] { border-width: 2px 2px 6px; }

.tile-emoji { font-size: clamp(40px, 9vw, 64px); line-height: 1; }
.tile-photo {
  width: 70%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 12px;
}
.tile-label {
  font-size: clamp(16px, 3.5vw, 22px);
  font-weight: 700;
  text-align: center;
  line-height: 1.1;
}

.no-speech {
  margin: 0;
  padding: 10px 16px;
  background: #fff4d6;
  color: #6b5300;
  font-size: 15px;
  text-align: center;
}

/* ---- Parent panel (dialog) ---- */
.parent-panel {
  width: min(560px, 94vw);
  max-height: 90dvh;
  border: none;
  border-radius: 20px;
  padding: 0;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  overflow: auto;
  background: var(--surface);
  color: var(--ink);
}
.parent-panel::backdrop { background: rgba(10, 20, 35, 0.45); }
:root[data-theme="contrast"] .parent-panel { border: 3px solid var(--border); }

.parent-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--brand);
  color: #fff;
  z-index: 1;
}
.parent-head h2 { margin: 0; font-size: 22px; }
:root[data-theme="contrast"] .parent-head { color: #000; }

.parent-section { padding: 16px 20px; border-bottom: 1px solid var(--surface-2); }
.parent-section h3 { margin: 0 0 6px; font-size: 18px; }

.muted { color: var(--muted); font-size: 14px; margin: 0 0 10px; }

.add-form { display: flex; flex-direction: column; gap: 12px; }
.add-form label, .field-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--label);
}
.add-form input, .add-form select, .parent-section select, .parent-section input[type="range"] {
  font-size: 17px;
  padding: 10px;
  border: 2px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
}
.parent-section input[type="range"] { padding: 0; height: 36px; }

.personal-list { display: flex; flex-direction: column; gap: 8px; }
.personal-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  background: var(--surface-2);
  border-radius: 12px;
}
.thumb {
  width: 48px; height: 48px;
  object-fit: cover;
  border-radius: 10px;
  background: var(--surface-3);
  display: flex; align-items: center; justify-content: center;
  font-size: 24px;
}
.personal-meta { flex: 1; font-weight: 600; }

.privacy {
  margin: 0;
  padding: 16px 20px;
  font-size: 14px;
  color: #2f6b4a;
  background: #eafaf1;
}
:root[data-theme="dark"] .privacy, :root[data-theme="contrast"] .privacy {
  color: #7fe0ad; background: #11271b;
}

#voice-select { width: 100%; }

.voice-row { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.voice-row label { flex: 1; }

/* ---- Mode switch (settings) ---- */
.mode-switch { display: flex; gap: 10px; flex-wrap: wrap; }
.mode-opt {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border: 2px solid var(--border-strong);
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.mode-opt:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }

/* ---- Keyboard board ---- */
.keyboard {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  overflow-y: auto;
}

.pred-row {
  display: flex;
  gap: 8px;
  min-height: 52px;
}
.pred {
  flex: 1;
  border: none;
  border-radius: 12px;
  background: var(--brand-soft);
  color: var(--brand-dark);
  font-size: 20px;
  font-weight: 700;
  padding: 10px 6px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pred:active { transform: scale(0.97); }
:root[data-theme="contrast"] .pred { border: 2px solid var(--brand); }

.keys { display: flex; flex-direction: column; gap: 8px; }
.key-row { display: flex; gap: 6px; justify-content: center; }

.key {
  flex: 1;
  min-width: 0;
  max-width: 64px;
  height: 58px;
  border: none;
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  font-size: 22px;
  font-weight: 600;
  cursor: pointer;
}
.key:active { transform: scale(0.95); background: var(--surface-2); }
.key-fn { background: var(--surface-3); max-width: 84px; font-size: 19px; }
.key-space { max-width: none; flex: 5; background: var(--surface); }
.key-active { background: var(--brand); color: #fff; }
:root[data-theme="contrast"] .key { border: 2px solid var(--border); }

/* ---- Phrase board (keyboard mode) ---- */
.ptabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  flex-shrink: 0;
}
.ptab {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 16px;
  font-weight: 700;
  padding: 10px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.ptab.on {
  background: var(--brand-soft);
  border-color: var(--brand);
  color: var(--brand-dark);
}
:root[data-theme="contrast"] .ptab { border-color: var(--border); }

.phrase-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
  align-content: start;
  overflow-y: auto;
  flex: 1;
  padding-bottom: max(4px, env(safe-area-inset-bottom));
}
.ptile {
  border: 2px solid var(--border);
  border-bottom-width: 5px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  font-size: 18px;
  font-weight: 700;
  line-height: 1.3;
  padding: 18px 12px;
  min-height: 76px;
  cursor: pointer;
  transition: transform 0.05s ease;
}
.ptile:active { transform: scale(0.96); }
.ptile.fitz-red    { background: var(--fitz-red-bg);    border-bottom-color: var(--fitz-red); }
.ptile.fitz-orange { background: var(--fitz-orange-bg); border-bottom-color: var(--fitz-orange); }
.ptile.fitz-pink   { background: var(--fitz-pink-bg);   border-bottom-color: var(--fitz-pink); }
.ptile.fitz-blue   { background: var(--fitz-blue-bg);   border-bottom-color: var(--fitz-blue); }
.ptile.fitz-purple { background: var(--fitz-purple-bg); border-bottom-color: var(--fitz-purple); }
.ptile.builder {
  border-style: dashed;
  border-bottom-style: solid;
  color: var(--brand-dark);
}
.phrase-empty {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 16px;
  text-align: center;
  padding: 28px 8px;
}

/* Typing view wrapper (predictions + keys) — anchored to the BOTTOM of the
   board like a native phone keyboard, so thumbs reach the keys. */
.typing {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 10px;
  flex: 1;
  min-height: 0;
}

/* ---- Switch scanning highlights ---- */
/* A whole row being scanned (row phase). */
.scan-hl-row {
  outline: 4px solid var(--scan-row) !important;
  outline-offset: 2px;
  border-radius: 12px;
}
/* The single item being scanned (column phase) — stronger, with a glow. */
.scan-hl {
  outline: 5px solid var(--scan-item) !important;
  outline-offset: 2px;
  box-shadow: 0 0 0 5px var(--scan-glow) !important;
  border-radius: 12px;
  position: relative;
  z-index: 2;
}

.check-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 600;
  color: var(--label);
  margin-bottom: 10px;
}
.check-row input { width: 22px; height: 22px; }

/* ---- First-run hint / iOS install hint ---- */
.hint {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--brand-soft); border-bottom: 2px solid var(--brand-soft-border);
  padding: 10px 16px; font-size: 15px; color: var(--ink);
}
.hint b { color: var(--brand-dark); }
.hint .btn { flex-shrink: 0; }

/* ---- Help link in settings ---- */
.help-link {
  display: inline-block; font-size: 16px; font-weight: 700; color: var(--brand);
  text-decoration: none; padding: 10px 14px;
  border: 2px solid var(--border-strong); border-radius: 12px;
}
.help-link:hover { border-color: var(--brand); }

/* ---- Free+ section ---- */
.fp-free {
  font-size: 12px; font-weight: 800; color: #1f6b47; background: #eafaf1;
  border-radius: 999px; padding: 2px 9px; vertical-align: middle;
}
:root[data-theme="dark"] .fp-free, :root[data-theme="contrast"] .fp-free {
  color: #7fe0ad; background: #11271b;
}
.fp-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
#fp-status { font-weight: 600; color: var(--ink); }

/* ---- First-visit welcome ---- */
.welcome {
  width: min(440px, 92vw); border: none; border-radius: 20px; padding: 0;
  box-shadow: 0 14px 50px rgba(10, 20, 35, 0.35);
  background: var(--surface); color: var(--ink);
}
.welcome::backdrop { background: rgba(10, 20, 35, 0.5); }
.welcome-inner { padding: 28px 26px 24px; text-align: center; }
.welcome-logo { width: 72px; height: 72px; border-radius: 16px; }
.welcome-inner h2 { font-size: 26px; color: var(--ink); margin: 12px 0 6px; }
.welcome-lead { font-size: 17px; color: var(--ink); }
.welcome-sub { font-size: 15px; color: var(--muted); line-height: 1.5; margin: 10px 0 16px; }
#welcome-form { display: flex; flex-direction: column; gap: 10px; }
#welcome-email {
  font-size: 17px; padding: 12px; border: 2px solid var(--border-strong);
  border-radius: 12px; background: var(--surface); color: var(--ink);
}
.welcome-cta { font-size: 18px; padding: 14px; width: 100%; }
.welcome-skip {
  background: none; border: none; color: var(--brand); font-weight: 700; font-size: 15px;
  margin-top: 14px; cursor: pointer; padding: 8px;
}
.welcome-skip:hover { text-decoration: underline; }
.welcome-fine { font-size: 13px; color: #2f6b4a; margin-top: 12px; }
:root[data-theme="dark"] .welcome-fine, :root[data-theme="contrast"] .welcome-fine { color: #7fe0ad; }

/* ---- Small screens (phones) ----
   Desktop sizes crowd a 390px-wide phone: the top-bar buttons squeeze the
   message bar and the board paddings waste space. Scale the chrome down;
   touch targets stay >=44px. */
@media (max-width: 600px) {
  .topbar { gap: 6px; padding: 6px; padding-top: max(6px, env(safe-area-inset-top)); }
  .sentence { font-size: 20px; min-height: 52px; max-height: 80px; padding: 6px 10px; }
  .token { font-size: 18px; padding: 3px 10px; }
  .btn { font-size: 17px; padding: 0 12px; }
  .btn.speak { font-size: 18px; padding: 0 14px; }
  .btn.undo, .btn.clear { font-size: 20px; }
  .btn.gear { font-size: 19px; }
  .ptabs { gap: 6px; }
  .ptab { font-size: 14px; padding: 8px 10px; gap: 4px; }
  .phrase-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
  .ptile { font-size: 17px; padding: 16px 10px; min-height: 92px; }
  .keyboard { padding: 8px; gap: 6px; }
  .key { height: 50px; font-size: 19px; border-radius: 8px; }
  .key-fn { font-size: 16px; }
  .pred-row { min-height: 44px; gap: 6px; }
  .pred { font-size: 17px; padding: 8px 4px; }
  .keys { gap: 6px; }
  .key-row { gap: 4px; }
  .cat { font-size: 13px; padding: 6px 8px; }
  .cat .cat-emoji { font-size: 22px; }
}

/* ---- Short screens (landscape phones) ---- */
@media (max-height: 500px) {
  .topbar { padding: 4px 8px; }
  .sentence { min-height: 44px; max-height: 56px; font-size: 18px; }
  .keyboard { gap: 4px; padding: 6px 8px; }
  .keys { gap: 4px; }
  .key { height: 38px; font-size: 17px; }
  .pred-row { min-height: 36px; }
  .pred { font-size: 15px; padding: 4px; }
  .ptab { padding: 6px 10px; font-size: 14px; }
  .ptile { min-height: 52px; padding: 10px 8px; font-size: 15px; }
}
