/* SPDX-License-Identifier: GPL-3.0-or-later */

/* ============================================================
   Theming
   ------------------------------------------------------------
   Every color in this file is a custom property, defined once here
   for light mode (the default look) and again for dark mode. That's
   what makes three things possible without touching any rule below:

   1. The Appearance select in Voice Settings (Light/Dark/System) sets
      `data-theme="light"` or `"dark"` on <html>, or removes it
      entirely for System (falls through to prefers-color-scheme).
   2. A deployer can re-skin the app via js/theme.config.js — logo,
      title, and a palette object — with no UI and no build step. It's
      applied by injecting a small <style> block that redefines
      whichever of these custom properties it wants to override,
      in the same cascade order as the block below, so it composes
      correctly with the light/dark switch instead of fighting it.
   3. Any future color need is just another custom property here,
      never a hardcoded literal in a rule further down.
   ============================================================ */

:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --input-bg: #fbfcff;
  --active-bg: #eef1ff;
  --ink: #1f2430;
  --ink-soft: #5b6172;
  --border: #e1e5ef;

  --green: #2ecc71;
  --green-dark: #26ae60;
  --accent: #4c6ef5;

  --pin: #ffb703;
  --pin-bg: #fff8e6;

  --neg: #d90429;
  --neg-bg: #fff0f3;
  --neg-border: #ff8fa3;
  --pos: #1a8a3c;
  --pos-bg: #edfbf0;
  --pos-border: #74d68f;

  --danger: #c1121f;
  --danger-bg: #fff0f0;
  --danger-border: #f5b5b5;

  --backdrop-tint: rgba(20, 24, 34, 0.35);
  --shadow: 0 6px 18px rgba(31, 36, 48, 0.08);
  --shadow-strong: 0 10px 30px rgba(0, 0, 0, 0.25);

  --radius: 16px;
  --radius-sm: 10px;

  color-scheme: light; /* hints native controls (select, range, scrollbar) */
  font-size: 15px;
}

/* System preference — only takes effect when the toggle is on "System"
   (i.e. no explicit data-theme is set). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161f;
    --panel: #1d2029;
    --input-bg: #262a36;
    --active-bg: #2a2f42;
    --ink: #eef1f8;
    --ink-soft: #a4abbd;
    --border: #333a4d;

    --green: #34d17e;
    --green-dark: #1f9c5c;
    --accent: #7c95ff;

    --pin: #ffcf4d;
    --pin-bg: #3a3220;

    --neg: #ff6b81;
    --neg-bg: #3a1e25;
    --neg-border: #a94456;
    --pos: #4ade80;
    --pos-bg: #1a3324;
    --pos-border: #2f6b46;

    --danger: #ff6b6b;
    --danger-bg: #3a1f1f;
    --danger-border: #7a3a3a;

    --backdrop-tint: rgba(0, 0, 0, 0.55);
    --shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
    --shadow-strong: 0 10px 30px rgba(0, 0, 0, 0.6);

    color-scheme: dark;
  }
}

/* Explicit "Dark" choice — wins regardless of system preference. Same
   token values as the media-query block above, kept in sync by hand. */
:root[data-theme="dark"] {
  --bg: #14161f;
  --panel: #1d2029;
  --input-bg: #262a36;
  --active-bg: #2a2f42;
  --ink: #eef1f8;
  --ink-soft: #a4abbd;
  --border: #333a4d;

  --green: #34d17e;
  --green-dark: #1f9c5c;
  --accent: #7c95ff;

  --pin: #ffcf4d;
  --pin-bg: #3a3220;

  --neg: #ff6b81;
  --neg-bg: #3a1e25;
  --neg-border: #a94456;
  --pos: #4ade80;
  --pos-bg: #1a3324;
  --pos-border: #2f6b46;

  --danger: #ff6b6b;
  --danger-bg: #3a1f1f;
  --danger-border: #7a3a3a;

  --backdrop-tint: rgba(0, 0, 0, 0.55);
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
  --shadow-strong: 0 10px 30px rgba(0, 0, 0, 0.6);

  color-scheme: dark;
}

* { box-sizing: border-box; }

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

button {
  font-family: inherit;
  cursor: pointer;
}

#app {
  max-width: 900px;
  margin: 0 auto;
  min-height: 100%;
  padding: 8px 10px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

main {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- Header (kept deliberately small — the cog is rarely used) ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
}

.brand-emoji { font-size: 1.05rem; }
.brand-emoji svg { height: 1.05em; width: auto; vertical-align: middle; }
.brand-emoji img { height: 1.05em; width: auto; vertical-align: middle; }

.brand h1 {
  font-size: 0.95rem;
  margin: 0;
  letter-spacing: 0.02em;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  border: none;
  background: var(--panel);
  box-shadow: var(--shadow);
  border-radius: 50%;
  width: 38px;
  height: 38px;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  flex: none;
}

.icon-btn--sm {
  width: 26px;
  height: 26px;
  font-size: 0.85rem;
}

.icon-btn:active { transform: scale(0.94); }

/* ---------- Sentence bar: always visible while the rest of the page scrolls ---------- */
.sentence-area {
  position: sticky;
  top: 6px;
  z-index: 10;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sentence-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.sentence-bar {
  flex: 1;
  min-width: 0;
  font-size: 1.25rem;
  font-weight: 700;
  padding: 8px 10px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  background: var(--input-bg);
  /* Best-effort: reduces (but on some mobile browsers can't fully
     suppress) the OS's own selection/callout UI competing with our
     press-and-hold "Add to Words/Sentences/Numbers" menu. */
  -webkit-touch-callout: none;
}

.sentence-bar:focus {
  outline: none;
  border-color: var(--accent);
}

.clear-btn { flex: none; align-self: center; }

.speak-btn {
  border: none;
  background: var(--green);
  color: white;
  font-size: 1.1rem;
  font-weight: 800;
  padding: 10px;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 0 var(--green-dark);
}

.speak-btn:active {
  transform: translateY(4px);
  box-shadow: 0 0 0 var(--green-dark);
}

/* ---------- Suggestions ----------
   Single-line, swipeable rows: content past the container's edge is
   simply clipped rather than wrapping to a second line or spilling out. */
.chip-row {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  gap: 6px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.chip-row::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  border: none;
  background: var(--panel);
  box-shadow: var(--shadow);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  /* Press-and-hold (pin/unpin) shouldn't trigger the OS's own text-select
     callout on top of our own menu. */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.chip.pinned {
  background: var(--pin-bg);
  border: 2px solid var(--pin);
}

.chip:active { transform: scale(0.95); }

/* ---------- Moods ---------- */
.mood-row {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  gap: 6px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.mood-row::-webkit-scrollbar { display: none; }

.mood-toggle {
  flex: 0 0 auto;
  border: 2px solid var(--border);
  background: var(--panel);
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--ink-soft);
}

.mood-toggle[data-state="1"] {
  border-color: var(--neg-border);
  background: var(--neg-bg);
  color: var(--neg);
}

.mood-toggle[data-state="2"] {
  border-color: var(--pos-border);
  background: var(--pos-bg);
  color: var(--pos);
}

.mood-toggle:active { transform: scale(0.96); }

/* ---------- Fast nav ---------- */
.fastnav {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.nav-block {
  border: none;
  border-radius: var(--radius);
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: white;
  font-weight: 800;
  font-size: 0.8rem;
  box-shadow: var(--shadow);
}

.nav-block .nav-emoji { font-size: 1.4rem; }

.nav-block:active { transform: scale(0.96); }

/* ---------- Popovers ---------- */
.backdrop {
  position: fixed;
  /* top is set from JS so this box always starts just below the sentence
     area — it never covers or blocks it, whatever the header/sentence
     bar's actual rendered height is. */
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: var(--backdrop-tint);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 0;
}

.backdrop[hidden] { display: none; }

.popout {
  background: var(--panel);
  width: 100%;
  /* Matches #app's own content width so the popover uses exactly as much
     room as the rest of the UI already does — no more, no less — instead
     of an arbitrary narrower cap that wasted space on tablets/desktop. */
  max-width: 900px;
  max-height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow-strong);
  padding: 12px 14px 18px;
}

.popout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.popout-header h2 { margin: 0; font-size: 1rem; }

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.field select,
.field input[type="range"] {
  width: 100%;
}

.field select {
  padding: 8px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  font-size: 0.9rem;
  background: var(--input-bg);
  color: var(--ink);
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-weight: 800;
  font-size: 0.9rem;
}

.btn-secondary {
  background: var(--accent);
  color: white;
}

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border: 2px solid var(--danger-border);
  width: 100%;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  flex: none;
  width: 38px;
  height: 21px;
}

.toggle-switch input {
  position: absolute;
  inset: 0;
  opacity: 0;
  margin: 0;
  cursor: pointer;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  transition: background 0.15s;
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 17px;
  height: 17px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}

.toggle-switch input:checked + .toggle-track { background: var(--green); }
.toggle-switch input:checked + .toggle-track .toggle-thumb { transform: translateX(17px); }

.settings-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0 14px;
}

.popout-subtitle {
  margin: 0 0 4px;
  font-size: 0.95rem;
}

.popout-hint {
  margin: 0 0 10px;
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-weight: 500;
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.settings-actions .btn { flex: 1 1 150px; }

/* ---------- Nav / browse popover ---------- */
.nav-search {
  width: 100%;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 8px 10px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  background: var(--input-bg);
  color: var(--ink);
}

.nav-search:focus {
  outline: none;
  border-color: var(--accent);
}

.nav-section { margin-bottom: 12px; }

.nav-section-title {
  margin: 0 0 6px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
}

.category-tab-row {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  gap: 6px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.category-tab-row::-webkit-scrollbar { display: none; }

.category-tab {
  flex: 0 0 auto;
  border: 2px solid transparent;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 0.75rem;
  font-weight: 700;
  color: white;
  display: flex;
  align-items: center;
  gap: 4px;
  opacity: 0.55;
}

.category-tab.active {
  opacity: 1;
  border-color: var(--ink);
}

.category-tab:active { transform: scale(0.95); }

.nav-empty-hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-style: italic;
  white-space: nowrap;
}

/* Column count is set from JS per render (see applyAdaptiveColumns), based
   on how wide this category's own labels actually are — short items pack
   three-plus per row, long ones fall back to one or two. This is just the
   fallback before JS runs. */
.item-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
}

.item-chip {
  border: 2px solid var(--border);
  background: var(--input-bg);
  border-radius: var(--radius-sm);
  padding: 9px 8px;
  font-size: 0.85rem;
  font-weight: 700;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  color: var(--ink);
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.item-chip.is-pinned { border-color: var(--pin); background: var(--pin-bg); }

.item-chip .item-emoji { font-size: 1.3rem; }

.item-chip:active { transform: scale(0.95); background: var(--active-bg); }

/* ---------- Responsive ---------- */
@media (min-width: 560px) {
  .fastnav { grid-template-columns: repeat(6, 1fr); }
}

@media (min-width: 720px) {
  .popout { border-radius: 20px; margin-bottom: 40px; }
  .backdrop { align-items: center; }
}

/* ---------- Press-and-hold context menu ---------- */
.context-backdrop {
  align-items: center;
  z-index: 60; /* above the other popovers, since it can open on top of one */
}

.context-menu {
  background: var(--panel);
  width: calc(100% - 40px);
  max-width: 320px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-strong);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.context-menu button {
  border: none;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 0.95rem;
  font-weight: 700;
  text-align: left;
  color: var(--ink);
}

.context-menu button:active { transform: scale(0.98); }
.context-menu button.danger { background: var(--danger-bg); color: var(--danger); }
.context-menu button.cancel { background: none; color: var(--ink-soft); text-align: center; }

/* ---------- Toast ----------
   Intentionally theme-invariant (always a dark pill with white text,
   regardless of light/dark mode) rather than themed via custom
   properties — a transient overlay, not part of the page surface. */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 0);
  background: #1f2430;
  color: white;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
  z-index: 70;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  max-width: calc(100% - 40px);
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, -6px);
}

.toast[hidden] { display: none; }

/* ---------- Help ---------- */
.help-list {
  margin: 0 0 16px;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.9rem;
}

/* ---------- Accessibility ---------- */

/* A clear, consistent ring for keyboard focus specifically (mouse/touch
   activation doesn't trigger :focus-visible in modern browsers), on top
   of whatever a plain :focus rule already does for text inputs. */
:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Print: Emoji-Speak cards ----------
   Only ever populated right before window.print() (see printEmojiBoard()
   in app.js). Explicit white/black regardless of the active on-screen
   theme — a printed page shouldn't inherit dark mode. */
.print-sheet { display: none; }

@media print {
  /* .print-sheet is only as tall as its content, so on a partially-full
     last page the remaining page area is html/body showing through —
     force that to plain white too, or dark mode's near-black --bg prints
     as a big grey/black block wasting ink for no reason. */
  html, body { background: #fff !important; }
  body * { visibility: hidden; }
  .print-sheet, .print-sheet * { visibility: visible; }
  .print-sheet {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background: #fff;
    color: #000;
  }
  @page { margin: 12mm; }
}

.print-title {
  font-size: 16pt;
  margin: 0 0 6mm;
  text-align: center;
}

.print-grid {
  /* Rows are real block-level elements (.print-row, built in
     printEmojiBoard()) rather than CSS Grid: browsers don't reliably
     honor break-inside/page-break-inside on grid items, so a row sitting
     on a page boundary gets sliced in half — clipping the tops of the
     icons in the row below — instead of moving whole to the next page.
     Plain block boxes fragment correctly, so the dot grid still lines up
     with each card the same way it did as a CSS Grid. */
  display: block;
  /* A dot at every card corner as a cutting guide. Fixed row/column sizes
     (rather than auto-fill) keep this background tiling in exact
     lockstep with the cards. */
  background-image: radial-gradient(circle at 0 0, rgba(0, 0, 0, 0.35) 1px, transparent 1.2px);
  background-repeat: repeat;
}

.print-row {
  display: flex;
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  padding: 4mm;
  break-inside: avoid;
  page-break-inside: avoid;
}

.print-card-emoji { line-height: 1; }

.print-card-word {
  margin-top: 2mm;
  font-weight: 700;
  text-align: center;
  word-break: break-word;
}

.print-size-small .print-grid { background-size: 1.4in 1.4in; }
.print-size-small .print-card { width: 1.4in; height: 1.4in; }
.print-size-small .print-card-emoji { font-size: 0.6in; }
.print-size-small .print-card-word { font-size: 11pt; }

.print-size-medium .print-grid { background-size: 2in 2in; }
.print-size-medium .print-card { width: 2in; height: 2in; }
.print-size-medium .print-card-emoji { font-size: 0.9in; }
.print-size-medium .print-card-word { font-size: 14pt; }

.print-size-large .print-grid { background-size: 2.8in 2.8in; }
.print-size-large .print-card { width: 2.8in; height: 2.8in; }
.print-size-large .print-card-emoji { font-size: 1.3in; }
.print-size-large .print-card-word { font-size: 18pt; }
