/**
 * Nihongo Hanashi — Design System v0.1
 *
 * Inherits the "Mahjong Hall" visual language from Nihongo Dominos
 * (dark ink canvas, bone tiles with engraved kanji, vermilion + brass + indigo
 * accents) but tuned for a *reading* app: wider text columns, generous line
 * height, tappable inline tiles instead of grid-of-tiles.
 *
 * Target form factor: iPad primary, iPhone secondary.
 *
 * Tokens are duplicated from nihongo_dominos/css/design.css. When core token
 * values change in one project, they should be propagated through
 * `_shared/SHARED_DATA_CHANGELOG.md` (see that file for the process).
 */

/* ============================================================
   1. DESIGN TOKENS (duplicated from nihongo_dominos)
   ============================================================ */
:root {
  /* Canvas + surface */
  --canvas:      #1a1d2c;
  --canvas-elev: #232739;
  --canvas-deep: rgba(20, 23, 33, 0.5);

  /* Flip-card surfaces — subtle inset panel + the floating-card outline. These
     flip in light mode so the cards adopt day mode too (was: dark-only). */
  --card-inset:   rgba(255, 255, 255, 0.06);
  --card-outline: rgba(255, 255, 255, 0.85);

  /* Flip-card reading accents (color-coded readings on the cards). Dark values
     match the dominos cards exactly; light values deepen so they stay legible on
     the paper card. */
  --read-furi:    #c8e0ff;   /* furigana reading (Goi/Bunpou) */
  --read-pattern: #9ec5ff;   /* bunpou pattern reading */
  --read-kun:     #e87560;   /* kanji kun-yomi */
  --read-on:      #8aa0d8;   /* kanji on-yomi */
  --read-label:   #d8c08a;   /* kanji card labels */

  /* Tile material (bone / ivory) */
  --bone:        #e8dcc4;
  --bone-bright: #f0e5d0;
  --bone-shadow: #b8a888;
  --tile-edge:   #4a4533;

  /* Text on dark canvas */
  --text:       #f0e8d3;
  --text-soft:  #a89f8a;
  --text-faint: #6e6a5c;

  /* Accents — used sparingly */
  --vermilion: #d44a3a;
  --brass:     #c9a464;
  --indigo:    #2a3458;
  --hanada:    #5e85bb;

  /* Lines / rules */
  --rule: rgba(232, 220, 196, 0.12);

  /* Radii */
  --radius-tile: 12px;
  --radius-card: 14px;
  --radius-pill: 100px;

  /* Type stacks */
  --font-jp:   'Noto Serif JP', Georgia, serif;
  --font-en:   'Source Serif 4', Georgia, serif;
  --font-ui:   'Inter', -apple-system, system-ui, sans-serif;

  /* Reader-specific */
  --reading-column-w: min(680px, 92vw);
  --reading-column-w-ipad: min(820px, 78vw);
  --story-text-size: 1.35rem;
  --story-line-height: 2.1;
}

/* ============================================================
   2. BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  background: var(--canvas);
  color: var(--text);
  font-family: var(--font-jp);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: auto;
  text-size-adjust: auto;
  overscroll-behavior: none;
}

body::before {
  content: '';
  position: fixed; inset: 0;
  background-image:
    radial-gradient(ellipse at 20% 0%, rgba(212, 74, 58, 0.06) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(201, 164, 100, 0.05) 0%, transparent 50%),
    repeating-linear-gradient(45deg, rgba(255,255,255,0.008) 0px, transparent 1px, transparent 4px);
  pointer-events: none;
  z-index: 0;
}

a { color: var(--brass); text-decoration-color: rgba(201, 164, 100, 0.35); }

button {
  font-family: var(--font-ui);
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--text);
  padding: 0.6rem 1.1rem;
  border-radius: var(--radius-pill);
  cursor: pointer;
  font-size: 0.95rem;
  transition: border-color 120ms, color 120ms;
}
button:hover { border-color: var(--brass); color: var(--brass); }
button:disabled { opacity: 0.4; cursor: default; }
button:disabled:hover { border-color: var(--rule); color: var(--text); }

/* ============================================================
   3. APP CONTAINER — layout lives in layout.css (grid)
   ============================================================ */
.app a { color: var(--brass); }

/* ============================================================
   4. HEADER
   ============================================================ */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  /* Tighter horizontal padding so the reader's full control row (theme · size ·
     read · notebook · info · settings) fits one line at 375px (Joshua 2026-06-11). */
  padding: 0.55rem clamp(0.55rem, 2.5vw, 1.6rem);
  /* A brass-edged panel: the elevated surface + a faint golden sheen gathering
     toward the bottom hairline, and a soft drop shadow so it reads as a raised
     bar over the reading canvas (more depth = less flat). */
  background-color: var(--canvas-elev);
  background-image: linear-gradient(0deg, rgba(201, 164, 100, 0.07), rgba(201, 164, 100, 0) 60%);
  border-bottom: 1px solid var(--brass);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
  position: relative;
  z-index: 2;
}
.app-header .brand {
  font-family: var(--font-jp);
  font-weight: 900;            /* heavier kanji wordmark */
  font-size: 1.3rem;           /* prominent, but trimmed so the 6-icon reader row fits one line */
  color: var(--text);
  letter-spacing: 0.04em;
}

/* Header action buttons (right): the larger-text toggle + the read-this-scene
   control, grouped in .app-header-actions (header is space-between). Both are
   brass circles matching the per-sentence / card audio motif. Read-this-scene
   plays the whole scene via the shared NihonAudio [data-audio] delegate, which
   toggles .is-playing; its icon swaps from a speaker (read) to a stop square
   (tape stop) while reading. */
.app-header-actions { display: flex; align-items: center; gap: 0.28rem; }
/* Keep the back button + wordmark grouped at the left; actions stay at the right
   (margin-auto absorbs the free space, so this works with or without a back btn).
   nowrap is load-bearing: 日本語話 must never stack vertically (it would make the
   header tall and eat reading space — Joshua 2026-06-11). */
.app-header .brand { margin-right: auto; white-space: nowrap; }
.app-header .hdr-back,
.app-header .scene-read,
.app-header .text-size,
.app-header .theme-toggle,
.app-header .info-btn,
.app-header .settings-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0.35rem;
  border: 1px solid rgba(201, 164, 100, 0.4);
  border-radius: 999px;
  background: rgba(201, 164, 100, 0.10);
  color: var(--brass);
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  transition: color 140ms ease, background 140ms ease, border-color 140ms ease;
}
.app-header .hdr-back:hover,
.app-header .scene-read:hover,
.app-header .text-size:hover,
.app-header .theme-toggle:hover,
.app-header .info-btn:hover,
.app-header .settings-btn:hover { background: rgba(201, 164, 100, 0.2); border-color: var(--brass); }
.app-header .scene-read svg,
.app-header .theme-toggle svg,
.app-header .info-btn svg,
.app-header .settings-btn svg { width: 20px; height: 20px; display: block; flex: 0 0 auto; }
/* Back button — deliberately the most prominent control: brass-filled, a touch
   larger, with a bigger double-chevron. It's the primary "return" gesture. */
.app-header .hdr-back {
  margin-right: 0.2rem;
  background: var(--brass);
  border-color: var(--brass);
  color: var(--canvas);
}
.app-header .hdr-back:hover { background: var(--brass); border-color: var(--brass); filter: brightness(1.08); }
.app-header .hdr-back svg { width: 25px; height: 25px; display: block; flex: 0 0 auto; }
/* Settings is reserved for later — render it dormant. */
.app-header .settings-btn:disabled {
  opacity: 0.4;
  cursor: default;
  border-color: var(--rule);
  background: transparent;
  color: var(--text-soft);
}
.app-header .settings-btn:disabled:hover { background: transparent; border-color: var(--rule); }

/* Narrow phones — shrink the header action circles so the wordmark + up to four
   controls fit one row. */
@media (max-width: 390px) {
  .app-header .scene-read,
  .app-header .text-size,
  .app-header .theme-toggle,
  .app-header .info-btn,
  .app-header .settings-btn { width: 34px; height: 34px; padding: 0.3rem; }
  .app-header .scene-read svg,
  .app-header .theme-toggle svg,
  .app-header .info-btn svg,
  .app-header .settings-btn svg { width: 19px; height: 19px; }
  .app-header-actions { gap: 0.26rem; }
}

/* ===== Empty state — reader with no story loaded ===================== */
.empty-state {
  position: fixed;
  inset: 0;
  z-index: 95;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: var(--canvas);
}
.empty-state[hidden] { display: none; }
.empty-state-card {
  width: min(380px, 100%);
  text-align: center;
  background: var(--canvas-elev);
  border: 1px solid var(--rule);
  border-radius: 16px;
  padding: 2.4rem 1.8rem 2rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}
.empty-state-glyph {
  font-family: var(--font-jp);
  font-weight: 900;
  font-size: 2.6rem;
  color: var(--brass);
  line-height: 1;
  margin-bottom: 1rem;
}
.empty-state-title {
  font-family: var(--font-jp);
  font-weight: 700;
  font-size: 1.5rem;
  color: var(--text);
  letter-spacing: 0.06em;
  margin: 0 0 0.6rem;
}
.empty-state-text {
  font-family: var(--font-en);
  font-size: 1rem;
  line-height: 1.55;
  color: var(--text-soft);
  margin: 0 0 1.6rem;
}
.empty-state-btn {
  display: inline-block;
  font-family: var(--font-ui);
  font-size: 0.92rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-decoration: none;
  color: var(--canvas);            /* dark text on brass (adapts per theme) */
  background: var(--brass);
  border-radius: 100px;
  padding: 0.6rem 1.4rem;
  transition: filter 140ms;
}
.empty-state-btn:hover { filter: brightness(1.08); }

/* Larger-text toggle — two A glyphs (small + large) read as a text-size
   control. Lights up solid brass when large mode is on (.is-active). */
.app-header .text-size { font-family: var(--font-jp); line-height: 1; gap: 0.06rem; }
.app-header .text-size .ts-sm { font-size: 0.82rem; }
.app-header .text-size .ts-lg { font-size: 1.18rem; font-weight: 700; }
.app-header .text-size.is-active { color: var(--canvas); background: var(--brass); border-color: var(--brass); }
/* 3-step cycle: default (plain) → large (.is-active, brass) → xl (.is-active +
   .is-xl, underlined large A). The xl underline lives HERE in design.css — which
   every page loads — so the notebook/study page distinguishes large vs xl too,
   not just the reader (it used to be in reader.css, absent on study.html → the
   button looked identical mid vs large; Joshua-flagged 2026-06-17). */
.app-header .text-size.is-xl .ts-lg { text-decoration: underline; text-underline-offset: 3px; }

/* Theme toggle — shows the CURRENT theme's icon (moon in dark, sun in light);
   tapping flips it. Icon swap driven by body[data-theme]. */
.app-header .theme-toggle .icon-sun,
.app-header .theme-toggle .icon-moon { display: inline-flex; }
.app-header .theme-toggle .icon-sun { display: none; }
body[data-theme="light"] .app-header .theme-toggle .icon-moon { display: none; }
body[data-theme="light"] .app-header .theme-toggle .icon-sun { display: inline-flex; }

.app-header .scene-read .icon-play,
.app-header .scene-read .icon-stop { display: inline-flex; }
.app-header .scene-read .icon-stop { display: none; }
.app-header .scene-read.is-playing {
  color: var(--vermilion);
  border-color: var(--vermilion);
  background: rgba(212, 74, 58, 0.14);
}
.app-header .scene-read.is-playing .icon-play { display: none; }
.app-header .scene-read.is-playing .icon-stop { display: inline-flex; }

/* ============================================================
   5. AUDIO BUTTON — tap-to-hear speaker icon on every back face.
   Copied from nihongo_dominos design.css. The `svg { width/height:100% }`
   rule is essential: without it the inline speaker SVG collapses and only
   the round button outline shows.
   ============================================================ */
.audio-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 6px;
  border: none;
  background: transparent;
  color: var(--brass);
  cursor: pointer;
  flex-shrink: 0;
  border-radius: 50%;
  transition: color 0.15s ease, background 0.15s ease, transform 0.18s ease;
  -webkit-tap-highlight-color: transparent;
  vertical-align: middle;
}
.audio-btn svg {
  width: 100%;
  height: 100%;
  display: block;
}
.audio-btn:hover {
  color: var(--text);
  background: rgba(232, 220, 196, 0.06);
}
.audio-btn:active {
  transform: scale(0.94);
}
.audio-btn:focus-visible {
  outline: 2px solid var(--brass);
  outline-offset: 2px;
}
.audio-btn.is-playing {
  color: var(--vermilion);
  animation: audio-btn-pulse 1.1s ease-in-out infinite;
}
@keyframes audio-btn-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.55; }
}
html.audio-playback-off .audio-btn {
  display: none !important;
}

/* ============================================================
   LIGHT THEME — in-app day/night toggle (header sun/moon button →
   body[data-theme="light"]). A washi-paper palette: warm paper canvas, sumi-ink
   text, and brass/vermilion/hanada deepened so the accents read on a light
   ground. Tokens cascade, so most components flip for free.
   ============================================================ */
body[data-theme="light"] {
  --canvas:      #efe6d2;
  --canvas-elev: #e6dabf;
  --canvas-deep: rgba(230, 218, 191, 0.6);
  --text:        #2b2620;
  --text-soft:   #6c6354;
  --text-faint:  #978d7a;
  --vermilion:   #c2402f;
  --brass:       #97701f;
  --hanada:      #3f6aa0;
  --rule:        rgba(60, 50, 30, 0.22);
  /* Flip cards adopt day mode too — inset panels + outline go dark-on-light. */
  --card-inset:   rgba(43, 38, 32, 0.06);
  --card-outline: rgba(43, 38, 32, 0.28);
  /* Deepen the card reading accents so they're legible on the paper card. */
  --read-furi:    #41699e;
  --read-pattern: #3a5f93;
  --read-kun:     #bf4030;
  --read-on:      #41699e;
  --read-label:   #8a6a1e;
}

/* Reading-surface spots that use literal brass/gold (not --brass) and must
   deepen for the light ground: the tappable underline, the gold sentence
   numbers + English, and the recessed per-sentence icons. */
body[data-theme="light"] .reader .tile-span { border-bottom-color: rgba(151, 112, 31, 0.85); }
body[data-theme="light"] .reader .sentence-num,
body[data-theme="light"] .reader .block-en,
body[data-theme="light"] .grammar-rack-head { color: #8a6a1e; }
body[data-theme="light"] .reader .sentence-voice .audio-btn,
body[data-theme="light"] .reader .bunpou-btn { color: rgba(151, 112, 31, 0.7); }
/* Dialogue text uses --bone-bright (a light ink for the dark canvas) — it would
   vanish on paper, so darken it to the body ink in light mode. */
body[data-theme="light"] .reader .dialog { color: var(--text); }

/* === Light-theme fixes for the kanji secondary popups (2026-06-04, Joshua) ===
 * These body-level overlays (#radical-overlay / #kanji-examples-overlay /
 * #goi-from-kanji-overlay / #goi-jump-overlay) are OUTSIDE .reader, so the
 * reader-scoped overrides above never reach them, and they were authored only
 * for the dark theme. */
/* E2 — the radical glyph falls back to --bone (pale cream, never re-pinned for
   light) → white-on-paper. Use the body ink as the light-mode fallback. */
body[data-theme="light"] .radical-overlay .radical-glyph,
body[data-theme="light"] .radical-chip .chip-glyph { color: var(--chip-color, var(--text)); }
/* E3 — the examples / goi popups have a hardcoded near-opaque DARK backdrop with
   no light override → a dark frame + fade-in flicker around the paper card. Swap
   to a light sumi-ink scrim. */
body[data-theme="light"] :is(.kanji-examples-overlay, .goi-from-kanji-overlay, .goi-jump-overlay) {
  background: rgba(43, 38, 32, 0.45);
}
