/* ---------------------------------------------------------------------------
   layout.css — the app shell.

   The one rule that matters: nothing on the path to the digits may carry a
   max-width, max-height or a clamp() upper bound. The digits are an SVG that
   scales to whatever box it is given, so the box is simply "the viewport".
--------------------------------------------------------------------------- */

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

/* Components below set display, which would otherwise beat the UA rule for
   [hidden] and leave empty pills and panels on screen. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  /* min-height, not height: the article below the timer has to be able to
     extend the page. */
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  touch-action: manipulation;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

.app {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto;
  /* minmax(0, 1fr), not the implicit "auto": a long badge would otherwise widen
     the column past the viewport and push the controls off the right edge. */
  grid-template-columns: minmax(0, 1fr);
  width: 100%;
  height: 100vh;
  height: 100dvh;
  padding:
    calc(0.75rem + env(safe-area-inset-top))
    calc(1rem + env(safe-area-inset-right))
    calc(0.75rem + env(safe-area-inset-bottom))
    calc(1rem + env(safe-area-inset-left));
  background: var(--bg);
  transition: background var(--fade) ease;
}

/* Stays invisible (but still laid out and measurable, so display.js can fit
   the digits via getBBox() while hidden) until main.js has applied every
   saved setting — duration, mode, hide/sound/theme labels, everything.
   Without this, the static markup's defaults (5:00, "Countdown", "Auto"...)
   paint first and only then get corrected once the deferred module script
   runs, which is exactly the reload glitch this prevents. The noscript rule
   keeps JS-disabled visitors seeing the plain static markup instead of a
   permanently blank page, since nothing will ever flip data-ready for them. */
html:not([data-ready="true"]) .app {
  visibility: hidden;
}

/* visibility (unlike display) keeps the element in the render tree, which
   means every CSS transition below is still "live" while .app is hidden —
   so the moment main.js corrects e.g. the mode toggle from its static
   default (Countdown) to a saved Stopwatch, that change queues a real
   transition, which then visibly plays out right as the page reveals: the
   exact "toggle switches to Stopwatch" glitch this rule exists to kill.
   Forcing transition: none while not yet ready makes every correction
   instant, so there is nothing left to animate by the time data-ready
   flips — normal transitions resume immediately after for real interactions. */
html:not([data-ready="true"]) .app,
html:not([data-ready="true"]) .app * {
  transition: none !important;
}

/* --- Progress ring ------------------------------------------------------ */

.ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
  opacity: 1;
  transition: opacity var(--fade) ease;
}

.ring__track {
  fill: none;
  stroke: var(--ring-track);
}

.ring__progress {
  fill: none;
  stroke: var(--ring-progress);
  stroke-linecap: round;
  transition: stroke var(--fade) ease;
}

.app[data-ring="off"] .ring,
.app[data-blank="true"] .ring {
  opacity: 0;
}

/* --- Top bar ------------------------------------------------------------ */

/* Three-column grid, not flex space-between: the mode toggle needs to sit
   exactly on the page's centre line regardless of what either side holds,
   and 1fr/auto/1fr is what guarantees that — the two 1fr side columns are
   always forced equal, so the centre column can never drift toward
   whichever side happens to hold more. */
.topbar {
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--gap);
  min-width: 0;
  min-height: 2rem;
  transition: opacity var(--fade) ease;
}

.topbar__side {
  display: flex;
  align-items: center;
  gap: var(--gap);
  min-width: 0;
}

.topbar__side--end {
  justify-content: flex-end;
}

/* --- Stage -------------------------------------------------------------- */

.stage {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  min-height: 0; /* lets the 1fr row actually shrink, instead of overflowing */
  min-width: 0;
}

/* Only does anything while the screen is blank (hidden mode): a tap there
   reveals the time. The rest of the time it must not catch clicks meant for
   the digit groups underneath, so it steps out of the way entirely. */
.stage__hit {
  position: absolute;
  inset: 0;
  z-index: 2;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.app[data-blank="false"] .stage__hit {
  pointer-events: none;
}

.stage__hit:focus-visible {
  outline: 0.15rem solid var(--accent);
  outline-offset: -0.4rem;
  border-radius: var(--radius-lg);
}

/* Absolute, not a percentage-sized grid item: an <svg> with a viewBox is a
   replaced element, so a percentage height against an indefinite grid area
   silently falls back to its intrinsic aspect ratio and overflows. Positioning
   it against the stage makes both percentages definite. */
.digits {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  transition: opacity var(--fade) ease;
  /* Clicking a unit still works (that is a click handler, not a selection);
     this only stops drag/double-click from highlighting the digits like text. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.digits__text {
  fill: var(--digit);
  /* Inconsolata (self-hosted, see tokens.css) first, so every browser draws
     the exact same slashed zero — the system-font stack after it is only a
     fallback for the instant before that file loads. */
  font-family: "Inconsolata", ui-monospace, "SF Mono", "Cascadia Mono", "Segoe UI Mono",
    "Roboto Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-variant-numeric: tabular-nums slashed-zero;
  font-feature-settings: "tnum" 1, "zero" 1;
  font-size: 100px; /* user units only — the viewBox does the real scaling */
  font-weight: 700;
  letter-spacing: -0.02em;
  transition: fill var(--fade) ease;
}

/* Each unit is its own tspan so a click can land on just the hours, minutes,
   or seconds — see js/display.js. Only reachable when the digits are visible
   (layout.css turns off pointer-events on .stage__hit in that state). */
.digits__seg {
  cursor: pointer;
}

/* Only paused is editable — a running countdown holds still, so the digits
   stop inviting a click that would not do anything. */
.app[data-running="true"] .digits__seg {
  cursor: default;
}

.digits__seg--selected {
  fill: var(--accent);
}

.app[data-running="false"] .digits__text {
  opacity: 0.86;
}

/* Hidden mode: the screen goes blank between reveals. */
.app[data-blank="true"] .digits {
  opacity: 0;
}

/* Typed entry: a real (but invisible) input holds focus so phones raise the
   numeric keyboard; the digits themselves are the display (the selected group
   is highlighted, as for the wheel and arrow keys). 16px keeps iOS from
   zooming the page on focus. */
.digits__input {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  font-size: 16px;
  pointer-events: none;
}

.app[data-editing="true"] .digits {
  touch-action: pan-x pinch-zoom;
}

/* "Time's up": a countdown that just finished has already reset to 0:00, which
   alone looks the same as never started — so for a few seconds the digits go
   red behind a lightly blurred overlay carrying a large centred label (see
   main.js finishCountdown). */
.app[data-finished="true"] {
  --digit: var(--danger);
  --ring-progress: var(--danger);
}

.app[data-finished="true"] .digits__text {
  opacity: 1;
  animation: finish-flash 0.9s ease-in-out infinite;
}

.finish {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  -webkit-backdrop-filter: blur(0.5rem);
  backdrop-filter: blur(0.5rem);
  pointer-events: none;
}

.finish[hidden] {
  display: none;
}

.finish__text {
  padding: 0.25em 0.9em;
  border-radius: 0.3em;
  background: var(--danger);
  color: #fff;
  font-size: clamp(2rem, 12vmin, 12rem);
  font-weight: 800;
  line-height: 1.1;
  text-align: center;
  box-shadow: 0 0.5rem 2.5rem rgba(0, 0, 0, 0.45);
}

@keyframes finish-flash {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.25; }
}

@media (prefers-reduced-motion: reduce) {
  .app[data-finished="true"] .digits__text { animation: none; }
}

/* A dim breathing dot, so a blank screen still looks alive. */
.pulse {
  position: absolute;
  right: 0.25rem;
  bottom: 0.25rem;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--text-muted);
  opacity: 0.35;
  animation: breathe 2.6s ease-in-out infinite;
}

@keyframes breathe {
  0%, 100% { opacity: 0.12; }
  50%      { opacity: 0.45; }
}

@media (prefers-reduced-motion: reduce) {
  .pulse { animation: none; }
}

/* Visually hidden, but read by screen readers and crawlers. */
.sr-only,
.live {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --- Controls ----------------------------------------------------------- */

/* Same reasoning as .topbar: a 1fr/auto/1fr grid, not flex, so Start/Reset
   in the centre column sit exactly on the page's centre line no matter what
   either side tray holds — Help/Settings on the left, Fullscreen on the
   right can never push the primary controls off-centre. */
.controls {
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--gap);
  min-width: 0;
  padding-top: 0.6rem;
  /* Matches .segmented--mode's margin-top, so the clearance above the mode
     toggle and below Start/Reset reads as the same deliberate gap, not one
     row sitting snug against the edge and the other not. */
  margin-bottom: 0.5rem;
  transition: opacity var(--fade) ease;
}

.controls__center {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--gap);
  min-width: 0;
}

/* Start and Reset read as a matched pair, not "the important one plus a
   smaller afterthought" — same width regardless of which has the longer
   label ("Pause" vs "Reset"). js/main.js then measures this row and sizes
   the mode toggle above to match, so the two rows line up as one block. */
.controls__center .btn {
  min-width: 8rem;
}

.controls__side {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}

.controls__side--end {
  justify-content: flex-end;
}

/* Idle in fullscreen: chrome fades away, layout stays put so nothing jumps. */
.app[data-idle="true"] .controls,
.app[data-idle="true"] .topbar {
  opacity: 0;
  pointer-events: none;
}

/* --- Fullscreen --------------------------------------------------------- */

html.pseudo-fs,
html.pseudo-fs body {
  overflow: hidden;
}

:root:fullscreen,
:root:-webkit-full-screen {
  background: var(--bg);
}

/* iOS Safari has no Fullscreen API for ordinary elements — fake it. */
html.pseudo-fs,
html.pseudo-fs body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
