/* ============================================================
   SCREEN: STATS & GOALS  (#screen-stats)
   The dashboard — a sellable, world-class surface for the feature-rich
   reading data. Everything here ELEVATES the reparented #statsBlock markup
   (today total, goal hero, pace chart, breakdown, history) into the enrapt
   design system rather than re-inventing it: stats.js tags the existing nodes
   with .ds-card / .ds-seg / .ds-track / .ds-figure primitives (defined in
   components.css); this stylesheet composes + tunes those for the larger,
   calmer dashboard context.

   Brand = "the joy of reading": warm, candle-lit library. Espresso surfaces,
   parchment ink, ONE candlelight-amber accent, forest green for on-track/ahead.
   ENCOURAGEMENT RULE: never red for "behind pace" — muted gray. Red = errors.

   Mobile-first (390px). Tokens only — no new palette, no new fonts.
   Every rule is scoped under #screen-stats so nothing leaks to sibling screens
   that share the foundation/component surfaces.
   ============================================================ */

/* ---- Screen rhythm: a touch more breathing room than the shell default. ---- */
#screen-stats > .screen-inner { gap: var(--s5); }
#screen-stats .screen-head { padding: var(--s2) 2px var(--s1); }

/* The "Stats & Goals" title reads as a dashboard heading — a quiet parchment
   two-tone, matching the brand's gradient ink, no loud color. */
#screen-stats .screen-head h2 {
  font-size: var(--fs-h2);
  letter-spacing: -0.02em;
  background: linear-gradient(180deg, var(--text), color-mix(in srgb, var(--text) 64%, var(--faint)));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* ============================================================
   THE STATS CARD  (#statsBlock → .ds-card .ds-card-hero)
   The screen's primary surface. .ds-card gives the gradient, border, radius
   and the 1px glass top-line; .ds-card-hero deepens the shadow. We add a soft
   candlelight glow at the top and generous, structured internal rhythm so the
   long-form data inside reads as distinct stacked sections, not one dense slab.
   ============================================================ */
#screen-stats #statsBlock {
  display: flex;
  flex-direction: column;
  padding: var(--s5) var(--s5) calc(var(--s5) + 2px);
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% -10%, var(--accent-2-soft), transparent 58%),
    var(--grad-card);
}

/* ============================================================
   TODAY HERO  (.stats-today → .stats-hero)
   The at-a-glance opener: an eyebrow-style "TODAY" label, the word count as a
   big amber .ds-figure (tagged by stats.js), the ≈pages in calm caps, and the
   History toggle pushed to the right as a quiet chip. A hairline seam closes
   the section so the goal panel below reads as its own surface.
   ============================================================ */
#screen-stats .stats-hero {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--s1) var(--s2);
  /* Tight, no divider: the old big padding + margin + border-bottom left a dead
     empty band between "Today" and "Monthly goal" that read as a recessed,
     do-nothing rectangle. Today now flows straight into the goal section. */
  padding-bottom: 0;
  margin-bottom: var(--s2);
  font-size: var(--fs-body);
  color: var(--muted);
}
/* "Today:" → a quiet uppercase eyebrow. */
#screen-stats .stats-hero > span:first-child {
  text-transform: uppercase;
  letter-spacing: 0.09em;
  font-size: var(--fs-cap);
  font-weight: 700;
  color: var(--faint);
}
/* The word-count figure — the day's headline number, big + amber + tabular.
   (.ds-figure .ds-figure-accent come from stats.js; we just size it up for the
   dashboard hero.) */
#screen-stats .stats-hero #statsTodayWords {
  font-size: var(--fs-figure);
  letter-spacing: -0.02em;
}
#screen-stats .stats-hero > span:nth-of-type(2) { /* "words" */
  font-size: var(--fs-cap);
  font-weight: 600;
  color: var(--muted);
}
#screen-stats .stats-hero .rd-pages {
  font-size: var(--fs-cap);
  color: var(--faint);
}
/* History toggle → a quiet ghost chip, pushed to the far right of the row. */
#screen-stats .stats-hero .small-btn {
  margin-left: auto;
  align-self: center;
  padding: 6px 14px;
  border-radius: var(--radius-pill);
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  color: var(--muted);
  font-size: var(--fs-cap);
  font-weight: 600;
  box-shadow: none;
  transition: background var(--tap), color var(--tap), border-color var(--tap), transform var(--tap);
}
#screen-stats .stats-hero .small-btn:hover {
  background: var(--accent-2-soft);
  border-color: transparent;
  color: var(--accent-2-strong);
  filter: none;
}
#screen-stats .stats-hero .small-btn:active { transform: translateY(1px); }

/* ============================================================
   GOAL PANEL  (#goalBlock → .stats-goal)
   The dashboard's centrepiece. The summary stacks: an editable monthly target,
   a Day/Week/Month segmented control, the big "to go" headline figure, an amber
   progress track, the pace chart, optional override row, and the 3-up stat tiles.
   ============================================================ */
#screen-stats .stats-goal { display: flex; flex-direction: column; }
#screen-stats .goal-summary {
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}

/* "Monthly goal" label + editable target — a tidy header row. */
#screen-stats .goal-summary-head {
  display: flex;
  align-items: center;
  gap: var(--s3);
}
#screen-stats .goal-summary-label {
  font-size: var(--fs-cap);
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--faint);
}
#screen-stats .goal-set-input {
  width: auto;
  flex: 0 1 auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 11px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  transition: border-color var(--tap), box-shadow var(--tap);
}
#screen-stats .goal-set-input:focus {
  outline: none;
  border-color: var(--accent-2);
  box-shadow: var(--focus-ring);
}

/* Day / Week / Month — the .ds-seg segmented control (tagged by stats.js).
   components.css gives the track + pills + the neutral raised active segment;
   we only tune the rhythm and let it sit left-aligned. Active stays the soft
   raised fill (cream ink) — amber is reserved for primary actions. */
#screen-stats .goal-view-tabs.ds-seg { align-self: flex-start; }
#screen-stats .goal-view-tabs .seg-item { padding: 7px 16px; }
#screen-stats .goal-view-tabs .seg-item:hover { color: var(--text); }

/* The headline figure — the single biggest number on the screen. The amount is
   parchment ink; the suffix ("to go today") is a calm muted run-on. */
#screen-stats .goal-headline {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--s2);
}
#screen-stats .goal-headline #goalRemaining {
  letter-spacing: -0.03em;
  line-height: 0.95;
}
#screen-stats .goal-headline-suffix {
  font-size: var(--fs-body);
  color: var(--muted);
}

/* Progress track → .ds-track > .fill (amber). components.css owns the look;
   we give the dashboard bar a hair more height + lay the label beside it. */
#screen-stats .goal-progress {
  display: flex;
  align-items: center;
  gap: var(--s3);
  font-size: var(--fs-cap);
}
/* The track: transparent + borderless, so at 0% there is NOTHING to see (no
   embossed/hollow rectangle), exactly like the reader's progress bar — only the
   silver fill appears once you've actually read something. */
#screen-stats .goal-progress-bar.ds-track {
  flex: 1;
  height: 4px;
  border: none;
  background: transparent;
}
#screen-stats .goal-progress-bar.ds-track > #goalProgressFill {
  background: linear-gradient(90deg, var(--progress-dim), var(--progress-light));
  border-radius: 999px;
}
#screen-stats .goal-progress-label {
  flex: 0 0 auto;
  color: var(--muted);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ============================================================
   PACE CHART  (#goalChart)
   Given real room on its own recessed panel so the cumulative lines read as a
   proper figure. The "you" line is candlelight amber; the target line is a
   quiet dashed espresso guide. (render/goal-chart.js builds the SVG + legend;
   we only color its classes here.)
   ============================================================ */
#screen-stats .goal-chart {
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: var(--s4) var(--s4) var(--s3);
}
#screen-stats .goal-chart-svg { max-height: 260px; width: 100%; }
#screen-stats .goal-chart-label { fill: var(--faint); font-size: 11px; }
#screen-stats .goal-chart-grid { stroke: var(--border-soft); opacity: 0.7; }
/* "You" — the achievement line, confident amber. */
#screen-stats .goal-chart-actual {
  stroke: var(--accent-2);
  stroke-width: 2.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* Target — a calm dashed espresso guide that the amber line is measured against. */
#screen-stats .goal-chart-target {
  stroke: var(--faint);
  stroke-width: 1.5;
  stroke-dasharray: 4 5;
  opacity: 0.85;
}
/* "Now" marker — a faint vertical hairline. */
#screen-stats .goal-chart-now {
  stroke: var(--muted);
  stroke-width: 1;
  stroke-dasharray: 2 4;
  opacity: 0.55;
}
/* Legend — two quiet swatch chips under the plot. */
#screen-stats .goal-chart-legend {
  display: flex;
  gap: var(--s4);
  margin-top: var(--s2);
  padding-left: 2px;
  font-size: var(--fs-cap);
  color: var(--muted);
}
#screen-stats .goal-chart-legend .lg {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
#screen-stats .goal-chart-legend .lg-swatch {
  width: 16px;
  height: 3px;
  border-radius: var(--radius-pill);
  display: inline-block;
}
#screen-stats .goal-chart-legend .lg-actual .lg-swatch { background: var(--accent-2); }
#screen-stats .goal-chart-legend .lg-target .lg-swatch {
  background: var(--faint);
  height: 0;
  border-top: 2px dashed var(--faint);
  border-radius: 0;
}

/* Per-day target override — a subtle inset utility row. */
#screen-stats .goal-override-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s2) var(--s3);
  padding: var(--s3) var(--s3);
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  font-size: var(--fs-cap);
  color: var(--muted);
}
#screen-stats .goal-override-label { font-weight: 600; color: var(--muted); }
#screen-stats .goal-override-row input[type="number"] {
  width: auto;
  max-width: 130px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font: inherit;
  font-variant-numeric: tabular-nums;
  transition: border-color var(--tap), box-shadow var(--tap);
}
#screen-stats .goal-override-row input[type="number"]:focus {
  border-color: var(--accent-2);
  box-shadow: var(--focus-ring);
  outline: none;
}
#screen-stats .goal-override-row .small-btn {
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  color: var(--muted);
  font-size: var(--fs-cap);
  font-weight: 600;
  box-shadow: none;
}
#screen-stats .goal-override-row .small-btn:hover {
  background: var(--accent-2-soft);
  border-color: transparent;
  color: var(--accent-2-strong);
  filter: none;
}
#screen-stats .goal-override-row .goal-hint { color: var(--faint); }

/* ============================================================
   STAT TILES  (.goal-stats → Need / Pace / ETA)
   A clean 3-up row of figure cards: a small uppercase cap label over a big
   tabular figure, each on its own lifted tile. The calm, data-rich core.
   ============================================================ */
#screen-stats .goal-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--s2);
}
#screen-stats .goal-stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--s2);
  min-width: 0;
  padding: var(--s3) var(--s3) calc(var(--s3) + 1px);
  background: linear-gradient(180deg, var(--elevate), var(--panel-2));
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
}
/* Labels carry .ds-eyebrow (from stats.js); keep them tight for the small tile. */
#screen-stats .goal-stat .goal-stat-label {
  font-size: 10px;
  letter-spacing: 0.08em;
}
#screen-stats .goal-stat b {
  font-size: 22px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
#screen-stats .goal-stat b svg { width: 20px; height: 20px; }
#screen-stats .goal-stat-unit { font-size: 11px; color: var(--muted); }

/* Pace delta — ENCOURAGEMENT colours from the engine's semantic classes:
   ahead = forest green, level = amber, behind = MUTED GRAY (never red). */
#screen-stats .goal-delta.ahead { color: var(--ok); }
#screen-stats .goal-delta.level { color: var(--accent-2-strong); }
#screen-stats .goal-delta.behind { color: var(--muted); }

/* ============================================================
   GOAL EMPTY STATE  (.goal-empty)
   No goal set yet — a calm, inviting prompt, never a void. (JS toggles its
   display vs. the summary; we only style it.)
   ============================================================ */
#screen-stats .goal-empty {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--s4);
  padding: var(--s5);
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
#screen-stats .goal-empty-title {
  font-size: var(--fs-h3);
  font-weight: 700;
  color: var(--text);
}
#screen-stats .goal-empty-sub {
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--muted);
}
#screen-stats .goal-empty-input {
  display: flex;
  align-items: center;
  gap: var(--s3);
}
#screen-stats .goal-empty-input input {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  font: inherit;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  transition: border-color var(--tap), box-shadow var(--tap);
}
#screen-stats .goal-empty-input input:focus {
  outline: none;
  border-color: var(--accent-2);
  box-shadow: var(--focus-ring);
}
#screen-stats .goal-empty-input .goal-hint {
  flex: 0 0 auto;
  font-size: var(--fs-cap);
  color: var(--faint);
}

/* ============================================================
   TODAY BREAKDOWN  (#statsTodayBreakdown)
   The day's individual readings, below the goal hero. A clear section seam +
   comfortable row rhythm. Its calm empty line ("Nothing recorded today yet.")
   stays muted — never red.
   ============================================================ */
#screen-stats .stats-breakdown {
  margin-top: var(--s5);
  padding-top: var(--s4);
  border-top: 1px solid var(--border-soft);
}
#screen-stats .stats-breakdown:empty { display: none; }

/* Reading rows (shared .rd-row markup from render/readings.js). */
#screen-stats .rd-row { padding: 9px 0; }
#screen-stats .rd-row + .rd-row { border-top: 1px solid var(--border-soft); }
#screen-stats .rd-row-head {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 8px;
}
/* Book title for a reading. The shared panels.css rule makes this a shrinking
   flex child (flex:1; min-width:0; nowrap; ellipsis) — fine on a single-line
   desktop row, but on a wrapping mobile row it collapses to a few characters
   beside the range/words/pages/delete items. Here the row head WRAPS, so give
   the title its own full-width line and let it read in full (wrap, no ellipsis,
   no collapse). The meta items flow onto the line below. */
#screen-stats .rd-title {
  flex: 1 1 100%;
  min-width: 0;
  overflow: visible;
  white-space: normal;
  overflow-wrap: anywhere;
  text-overflow: clip;
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--text);
}
#screen-stats .rd-title.rd-resume { cursor: pointer; }
#screen-stats .rd-title.rd-resume:hover { color: var(--accent-2-strong); }
#screen-stats .rd-range,
#screen-stats .rd-words { font-size: var(--fs-cap); color: var(--muted); font-variant-numeric: tabular-nums; }
#screen-stats .rd-words { cursor: text; }
#screen-stats .rd-range [data-edit] { cursor: text; }
#screen-stats .rd-pages { font-size: var(--fs-cap); color: var(--faint); }
#screen-stats .rd-del {
  margin-left: auto;
  padding: 4px;
  background: transparent;
  border: none;
  border-radius: var(--radius-pill);
  color: var(--faint);
  box-shadow: none;
  opacity: 0.7;
  transition: color var(--tap), background var(--tap), opacity var(--tap);
}
#screen-stats .rd-del:hover {
  background: var(--danger-soft);
  color: var(--accent);
  opacity: 1;
  filter: none;
}
#screen-stats .rd-del svg { width: 15px; height: 15px; }

/* Calm empty lines (breakdown + history). Muted, never an alarm. */
#screen-stats .stats-empty {
  padding: var(--s3) 0;
  color: var(--faint);
  font-size: var(--fs-body);
}

/* Inline number editor (words / from / til). */
#screen-stats .stats-row-edit {
  width: 84px;
  background: var(--panel);
  border: 1px solid var(--accent-2);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 3px 7px;
  font: inherit;
  font-size: var(--fs-cap);
  font-variant-numeric: tabular-nums;
}
#screen-stats .stats-row-edit:focus { outline: none; box-shadow: var(--focus-ring); }

/* ============================================================
   HISTORY  (#statsHistory)
   Its own recessed surface beneath the goal hero: a By day / By week segmented
   control + a tidy list of day/week totals (days expand to their readings).
   ============================================================ */
#screen-stats #statsHistory {
  margin-top: var(--s4) !important;
  padding-top: var(--s4);
  border-top: 1px solid var(--border-soft);
}

/* By day / By week — the .ds-seg segmented control (tagged by stats.js).
   Active is the neutral raised segment (cream ink) — identical to .ds-seg. */
#screen-stats .stats-tabs.ds-seg {
  align-self: flex-start;
  width: fit-content;
  margin-bottom: var(--s3);
}
#screen-stats .stats-tabs .seg-item { padding: 7px 16px; }
#screen-stats .stats-tabs .seg-item:hover { color: var(--text); }

/* History list — comfortable rows with a hairline between days. */
#screen-stats .stats-list {
  display: flex;
  flex-direction: column;
  max-height: 420px;
  overflow-y: auto;
}
#screen-stats .stats-row,
#screen-stats .stats-row-day {
  display: flex;
  align-items: center;
  gap: var(--s2);
  padding: 10px 2px;
}
#screen-stats .stats-day + .stats-day,
#screen-stats .stats-row + .stats-row { border-top: 1px solid var(--border-soft); }
#screen-stats .stats-row-day { cursor: pointer; }
#screen-stats .stats-row-caret {
  display: inline-flex;
  align-items: center;
  color: var(--faint);
  transition: color var(--tap);
}
#screen-stats .stats-row-day:hover .stats-row-caret { color: var(--muted); }
#screen-stats .stats-row-caret svg { width: 16px; height: 16px; }
#screen-stats .stats-row-label {
  flex: 1;
  min-width: 0;
  font-size: var(--fs-body);
  color: var(--text);
}
#screen-stats .stats-row-words {
  flex: 0 0 auto;
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
/* Expanded day → its readings, slightly inset on a recessed shelf. */
#screen-stats .stats-day-sources {
  margin: 2px 0 8px;
  padding: 4px 12px;
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
}

/* ============================================================
   WIDE SCREENS — the dashboard breathes; bigger figures + more padding.
   ============================================================ */
@media (min-width: 560px) {
  #screen-stats #statsBlock { padding: var(--s5) var(--s6) calc(var(--s6) - 6px); }
  #screen-stats .goal-stats { gap: var(--s3); }
  #screen-stats .goal-stat b { font-size: 24px; }
  #screen-stats .stats-hero #statsTodayWords { font-size: 34px; }
}

/* ============================================================
   MEASURED STRIP  (#stats-measured)
   The StoryGraph-style summary built by js/screens/stats.js BELOW the goal hero:
   finished count, lifetime words read, measured reading speed (with trend), and
   a genre breakdown over finished books. ENCOURAGEMENT RULE holds — a slower
   recent pace is MUTED GRAY, never red.
   ============================================================ */
#screen-stats .stats-measured {
  display: flex; flex-direction: column; gap: var(--s4);
  padding: var(--s5);
}
#screen-stats .stats-measured .sm-head { margin-bottom: -2px; }

/* The 3-up figure row — finished / words read / reading speed. */
#screen-stats .stats-measured .sm-tiles {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--s2);
}
#screen-stats .stats-measured .sm-tile {
  display: flex; flex-direction: column; gap: var(--s1);
  min-width: 0;
  padding: var(--s3);
  background: linear-gradient(180deg, var(--elevate), var(--panel-2));
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
}
#screen-stats .stats-measured .sm-cap {
  font-size: 10px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--faint);
}
#screen-stats .stats-measured .sm-fig {
  font-size: 22px; font-weight: 700; line-height: 1; letter-spacing: -0.02em;
  color: var(--text); font-variant-numeric: tabular-nums; overflow-wrap: anywhere;
}
/* Speed trend: faster = amber lift, slower = MUTED GRAY (never red). */
#screen-stats .stats-measured .sm-fig.is-up { color: var(--accent-2-strong); }
#screen-stats .stats-measured .sm-fig.is-down { color: var(--muted); }
#screen-stats .stats-measured .sm-unit { font-size: 11px; color: var(--muted); line-height: 1.3; }

/* Genre breakdown — proportional silver bars, one per finished genre. */
#screen-stats .stats-measured .sm-genres {
  display: flex; flex-direction: column; gap: var(--s2);
  padding-top: var(--s4);
  border-top: 1px solid var(--border-soft);
}
#screen-stats .stats-measured .sm-genres-cap { margin-bottom: 2px; }
#screen-stats .stats-measured .sm-genre {
  display: grid; grid-template-columns: 96px 1fr auto; align-items: center; gap: var(--s3);
}
#screen-stats .stats-measured .sm-genre-name {
  font-size: var(--fs-cap); color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#screen-stats .stats-measured .sm-genre-track {
  height: 8px; border-radius: var(--radius-pill);
  background: var(--panel-2); overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.45);
}
#screen-stats .stats-measured .sm-genre-fill {
  display: block; height: 100%; border-radius: var(--radius-pill);
  background: linear-gradient(90deg, var(--progress-dim), var(--progress-light));
}
#screen-stats .stats-measured .sm-genre-count {
  font-size: var(--fs-cap); font-weight: 600; color: var(--muted);
  font-variant-numeric: tabular-nums;
}

@media (min-width: 560px) {
  #screen-stats .stats-measured { padding: var(--s5) var(--s6); }
  #screen-stats .stats-measured .sm-tiles { gap: var(--s3); }
  #screen-stats .stats-measured .sm-fig { font-size: 24px; }
}
