/* ============================================================================================
   charts.css — motion layer, module "charts" (prefix ch-)
   Owns: #card-radar (#radar) · #card-week (#week) · #card-exam (#countdown)

   The page's own <style> is appended to <head> AFTER this file (dc-lite moves <helmet> there),
   so every selector here carries an id to out-rank the single-class base rules.

   State classes, set by charts.js on the card (never present without the script):
     .ch-prep  hidden start pose, waiting for boot + visibility
     .ch-grid  radar only: the grid is drawing / drawn while the data still waits (.ch-prep stays on)
     .ch-run   intro playing (week, exam: replaces .ch-prep · radar: added to it)
     .ch-done  resting — equals the static base, plus hover affordances and the slow loops
     .ch-live  card on screen and tab visible (loops only run while this is set)
   ============================================================================================ */

#card-radar,
#card-week,
#card-exam {
  --ch-out: cubic-bezier(.16, 1, .3, 1);       /* long, soft settle */
  --ch-back: cubic-bezier(.3, 1.36, .5, 1);    /* settle with a slight overshoot */
  --ch-io: cubic-bezier(.65, 0, .35, 1);       /* symmetric, for breathing loops */
}

@keyframes ch-fade { from { opacity: 0 } to { opacity: 1 } }

/* ============================================================================================
   1. SKILL SCAN — #radar
   ============================================================================================ */
#radar {
  overflow: visible;
  --ch-acc: var(--ch-accent, var(--brand, #FF52A2));   /* --ch-accent = the data polygon's own colour (set by JS) */
  --ch-sweep-t: 7s;
}
#radar .radar-dot,
#radar .ch-ping,
#radar .ch-halo,
#radar .ch-lock { transform-box: fill-box; transform-origin: center; }

/* ---- intro, phase 1 (.ch-grid, as soon as the card is on screen): grid, spokes, labels ---- */
#card-radar.ch-prep .radar-grid,
#card-radar.ch-prep .radar-spoke {
  stroke-dasharray: var(--ch-len) var(--ch-len);
  stroke-dashoffset: var(--ch-len);
}
#card-radar.ch-prep .radar-data,
#card-radar.ch-prep .radar-dot,
#card-radar.ch-prep .radar-label { opacity: 0; }

#card-radar.ch-grid .radar-spoke {
  animation: ch-draw .6s var(--ch-out) both;
  animation-delay: calc(var(--ch-i) * 55ms);
}
#card-radar.ch-grid .radar-grid {
  animation: ch-draw .95s var(--ch-io) both;
  animation-delay: calc(140ms + var(--ch-i) * 130ms);
}
@keyframes ch-draw { from { stroke-dashoffset: var(--ch-len) } to { stroke-dashoffset: 0 } }
#card-radar.ch-grid .radar-label {
  animation: ch-label .7s var(--ch-out) both;
  animation-delay: calc(320ms + var(--ch-i) * 85ms);
}
@keyframes ch-label {
  from { opacity: 0; transform: translate(var(--ch-dx, 0px), var(--ch-dy, 0px)); }
  to   { opacity: 1; transform: translate(0, 0); }
}

/* ---- intro, phase 2 (.ch-run, after the page boot): data polygon (points tweened by JS), dots ----
   --ch-t0 is the head start the grid still needs (0 when it has been up for a while). */
#card-radar.ch-run .radar-data {
  animation: ch-fade .25s linear both;
  animation-delay: var(--ch-t0, 0ms);
}
#card-radar.ch-run .radar-dot {
  animation: ch-pop .55s var(--ch-back) both;
  animation-delay: calc(var(--ch-t0, 0ms) + 10ms + var(--ch-i) * 75ms);
}
@keyframes ch-pop {
  from { opacity: 0; transform: scale(0); }
  35%  { opacity: 1; }
  to   { opacity: 1; transform: scale(1); }
}

/* ---- ping ring at each vertex: once on landing, then whenever the sweep passes ---- */
#radar .ch-ping {
  fill: none;
  stroke: var(--ch-acc);
  stroke-width: 1.2;
  vector-effect: non-scaling-stroke;
  opacity: 0;
  pointer-events: none;
}
#card-radar.ch-run .ch-ping {
  animation: ch-ping-once .85s var(--ch-out) forwards;
  animation-delay: calc(var(--ch-t0, 0ms) + 600ms + var(--ch-i) * 75ms);
}
@keyframes ch-ping-once {
  from { opacity: .85; transform: scale(1); }
  to   { opacity: 0;   transform: scale(4); }
}
#card-radar.ch-done .ch-ping {
  animation: ch-ping-loop var(--ch-sweep-t) linear infinite;
  animation-delay: calc(var(--ch-sweep-t) * var(--ch-frac, 0));
  animation-play-state: paused;
}
@keyframes ch-ping-loop {
  0%   { opacity: 0;   transform: scale(1);   animation-timing-function: linear; }
  .6%  { opacity: .75; transform: scale(1.1); animation-timing-function: cubic-bezier(.16, 1, .3, 1); }
  13%  { opacity: 0;   transform: scale(3.6); }
  100% { opacity: 0;   transform: scale(3.6); }
}

/* ---- radar sweep (clipped to the outer pentagon) ---- */
#radar .ch-sweep-wrap { opacity: 0; pointer-events: none; transition: opacity .9s ease; }
#card-radar.ch-done .ch-sweep-wrap { opacity: 1; }
#card-radar.ch-done #radar.ch-hot .ch-sweep-wrap { opacity: .35; transition-duration: .3s; }
#radar .ch-sweep-fan { fill: var(--ch-acc); fill-opacity: .02; }
#radar .ch-sweep-line { stroke: var(--ch-acc); stroke-width: 1.2; stroke-linecap: round; opacity: .6; }
#card-radar.ch-done .ch-sweep {
  animation: ch-sweep var(--ch-sweep-t) linear infinite;   /* continuous rotation: linear on purpose */
  animation-play-state: paused;
}
@keyframes ch-sweep { to { transform: rotate(360deg); } }
#card-radar.ch-done.ch-live .ch-sweep,
#card-radar.ch-done.ch-live .ch-ping { animation-play-state: running; }

/* ---- hover / focus: lock on to one axis ---- */
#radar .ch-hit { fill: transparent; stroke: none; pointer-events: all; }
#radar .radar-label { outline: none; }

#card-radar.ch-done .radar-dot {
  transition: opacity .25s ease, transform .35s var(--ch-back), filter .25s ease;
}
#card-radar.ch-done .radar-label { transition: opacity .25s ease; }
#card-radar.ch-done .radar-val { transition: fill .2s ease; }
#card-radar.ch-done #radar.ch-hot .radar-dot:not(.ch-on) { opacity: .4; }
#card-radar.ch-done #radar.ch-hot .radar-label:not(.ch-on) { opacity: .45; }
#card-radar.ch-done .radar-dot.ch-on {
  transform: scale(1.6);
  filter: drop-shadow(0 0 2.5px var(--ch-acc));
}
#card-radar.ch-done .radar-label.ch-on .radar-val { fill: var(--ch-acc); }

#radar .ch-halo {
  fill: var(--ch-acc);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease, transform .45s var(--ch-out);
}
#radar .ch-halo.ch-on { opacity: .2; transform: scale(3.2); }

#radar .ch-axis {
  stroke: var(--ch-acc);
  stroke-width: 1.4;
  stroke-dasharray: var(--ch-len) var(--ch-len);
  stroke-dashoffset: var(--ch-len);
  opacity: 0;
  pointer-events: none;
  transition: stroke-dashoffset .3s ease, opacity .2s ease;
}
#radar .ch-axis.ch-on {
  stroke-dashoffset: 0;
  opacity: .85;
  transition: stroke-dashoffset .45s var(--ch-out), opacity .15s ease;
}

#radar .ch-level {
  fill: none;
  stroke: var(--ch-acc);
  stroke-width: 1;
  stroke-dasharray: 2 3;
  vector-effect: non-scaling-stroke;
  opacity: 0;
  pointer-events: none;
  transform: scale(var(--ch-lv, .5));
  transition: opacity .25s ease, transform .5s var(--ch-out);
}
#radar.ch-hot .ch-level { opacity: .6; }

#radar .ch-lock {
  fill: var(--ch-acc);
  fill-opacity: .1;
  stroke: var(--ch-acc);
  stroke-width: 1;
  opacity: 0;
  pointer-events: none;
  transform: scale(1.22, 1.3);
  transition: opacity .18s ease, transform .2s ease;
}
#radar .ch-lock.ch-on {
  opacity: 1;
  transform: scale(1);
  transition: opacity .18s ease, transform .4s var(--ch-back);
}
#radar .ch-lock.ch-kb { stroke-width: 1.8; fill-opacity: .16; }   /* keyboard focus */

/* ============================================================================================
   2. 7 DAYS — #week
   ============================================================================================ */
#week {
  --ch-tc: var(--ch-today, var(--brand, #FF52A2));   /* --ch-today = today's bar colour (set by JS) */
}
#week .wk-bar i { position: relative; }

/* ---- intro: bars rise from the floor, staggered, slight overshoot ---- */
#card-week.ch-prep .wk-bar i { transform: translateY(104%); }
#card-week.ch-run .wk-bar i {
  animation: ch-rise 1s var(--ch-back) both;
  animation-delay: calc(var(--ch-i) * 80ms);
  will-change: transform;
}
@keyframes ch-rise { from { transform: translateY(104%); } to { transform: translateY(0); } }
/* skirt: keeps the bar attached to the floor while it overshoots upward */
#card-week.ch-prep .wk-bar i::after,
#card-week.ch-run .wk-bar i::after {
  content: '';
  position: absolute;
  left: 0; right: 0; top: 100%;
  height: 18px;
  background: inherit;
}
/* bright leading edge that fades as the bar lands */
#card-week.ch-run .wk-bar i::before {
  content: '';
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 10px;
  max-height: 100%;
  border-radius: inherit;
  background: linear-gradient(to bottom, var(--surface, #FFFFFF), transparent);
  animation: ch-edge 1s ease-out both;
  animation-delay: calc(var(--ch-i) * 80ms);
}
@keyframes ch-edge { 0% { opacity: .7; } 55% { opacity: .55; } 100% { opacity: 0; } }

/* ---- standby: while the data waits for the page boot, every empty track shows a floor tick that
   breathes (so the card never looks dead); the tick fades as its bar starts to rise ---- */
#card-week.ch-prep .wk-bar,
#card-week.ch-run .wk-bar { position: relative; }
#card-week.ch-prep .wk-bar::before,
#card-week.ch-run .wk-bar::before {
  content: '';
  position: absolute;
  left: 26%; right: 26%; bottom: 6px;
  height: 2px;
  border-radius: 1px;
  background: currentColor;
  opacity: .22;
  pointer-events: none;
}
#card-week.ch-prep .wk-bar::before {
  animation: ch-standby 1s var(--ch-io) infinite alternate;
  animation-delay: calc(var(--ch-i, 0) * -230ms);      /* negative: each tick starts at its own phase */
  animation-play-state: paused;
}
#card-week.ch-prep.ch-live .wk-bar::before { animation-play-state: running; }
#card-week.ch-run .wk-bar::before {
  animation: ch-standby-out .3s ease-out both;
  animation-delay: calc(var(--ch-i, 0) * 80ms);
}
@keyframes ch-standby { from { opacity: .12; } to { opacity: .45; } }
@keyframes ch-standby-out { to { opacity: 0; } }

/* ---- today's bar: soft glow that breathes ---- */
#week .wk-col.ch-today { position: relative; }
#card-week.ch-done .wk-col.ch-today::before {
  content: '';
  position: absolute;
  left: 0; right: 0; top: 0;
  height: var(--ch-bar-h, 0px);
  border-radius: 8px;
  box-shadow: 0 0 0 1.5px var(--ch-tc), 0 0 13px 1px var(--ch-tc);
  opacity: 0;
  pointer-events: none;
  animation: ch-glow 2.6s var(--ch-io) infinite alternate;
  animation-play-state: paused;
}
@keyframes ch-glow { from { opacity: .1; } to { opacity: .58; } }
#card-week.ch-done .wk-bar.is-today i::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(to bottom, var(--surface, #FFFFFF), transparent 75%);
  opacity: 0;
  animation: ch-sheen 2.6s var(--ch-io) infinite alternate;
  animation-play-state: paused;
}
@keyframes ch-sheen { from { opacity: 0; } to { opacity: .3; } }
#card-week.ch-done.ch-live .wk-col.ch-today::before,
#card-week.ch-done.ch-live .wk-bar.is-today i::after { animation-play-state: running; }
/* another day is being read: today's glow steps back with its (dimmed) bar */
#card-week.ch-done .wk-col.ch-today::before { transition: filter .22s ease; }
#card-week.ch-done #week.ch-hov .wk-col.ch-today:not(.ch-on)::before { filter: opacity(.3); }

/* ---- hover: focus one day, show its minutes ---- */
#card-week.ch-done .wk-bar,
#card-week.ch-done .wk-day { transition: opacity .22s ease; }
#card-week.ch-done #week.ch-hov .wk-col:not(.ch-on) .wk-bar { opacity: .45; }
#card-week.ch-done #week.ch-hov .wk-col:not(.ch-on) .wk-day { opacity: .55; }
/* each day is a keyboard stop (tabindex set by charts.js); the ring goes on the bar, not the column */
#week .wk-col { outline: none; }
#week .wk-col:focus-visible .wk-bar { outline: 2px solid var(--brand, #FF52A2); outline-offset: 2px; }

#card-week .ch-tip {
  position: absolute;
  left: 0; top: 0;
  z-index: 3;
  pointer-events: none;
  white-space: nowrap;
  padding: 3px 8px 4px;
  border-radius: 8px;
  background: var(--fill, #006D40);
  color: var(--surface, #FFFFFF);
  box-shadow: 0 0 0 2px var(--surface, #FFFFFF);
  font-family: var(--font, 'KhaoNiaw', 'Zen Maru Gothic', sans-serif);
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: .02em;
  font-variant-numeric: tabular-nums;
  opacity: 0;
  transform: translate(var(--ch-x, 0px), var(--ch-y, 0px)) translate(-50%, 5px) scale(.88);
  transform-origin: 50% 100%;
  transition: opacity .14s ease, transform .14s ease;
}
#card-week .ch-tip.ch-show {
  opacity: 1;
  transform: translate(var(--ch-x, 0px), var(--ch-y, 0px)) translate(-50%, 0) scale(1);
  transition: opacity .16s ease, transform .34s var(--ch-out);
}
#card-week .ch-tip::after {
  content: '';
  position: absolute;
  left: 50%; top: 100%;
  margin-left: -4px;
  border: 4px solid transparent;
  border-top-color: var(--fill, #006D40);
}

/* ============================================================================================
   3. NEXT EXAM — #countdown (odometer digits)
   ============================================================================================ */
#countdown .ch-odo {
  position: relative;
  display: block;
  height: 1em;
  white-space: nowrap;
}
/* the base rule ".cd-cell span" (unit captions) must not reach the digit spans */
#countdown .ch-odo span { font-size: inherit; letter-spacing: inherit; }
/* one fixed-width window per digit; the window is .12em taller than the line on both sides
   (negative margins keep the layout height at exactly 1em) so glyph ink is never cropped */
#countdown .ch-dg {
  display: inline-block;
  box-sizing: content-box;
  position: relative;
  vertical-align: top;
  width: var(--ch-dw, auto);
  height: 1em;
  line-height: 1;
  padding: .12em 0;
  margin: -.12em 0;
  overflow: hidden;
  text-align: center;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #FFFFFF .12em, #FFFFFF calc(100% - .12em), transparent 100%);
          mask-image: linear-gradient(to bottom, transparent 0, #FFFFFF .12em, #FFFFFF calc(100% - .12em), transparent 100%);
}
#countdown .ch-col { display: block; }
#countdown .ch-g { display: block; height: 1em; line-height: 1; }
#countdown .ch-probe { display: inline-block; position: absolute; left: 0; top: 0; visibility: hidden; width: auto; }
#countdown .ch-sr {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- standby + intro: while the read-out waits for the page boot each digit window shows a
   breathing dash (the cells never sit empty); the dash fades as the digits spin in ---- */
#card-exam.ch-prep .ch-col { opacity: 0; }
#card-exam.ch-run .ch-col {
  animation: ch-fade .3s ease-out both;
  animation-delay: calc(var(--ch-i) * 90ms);
}
#card-exam.ch-prep .ch-dg::before,
#card-exam.ch-run .ch-dg::before {
  content: '';
  position: absolute;
  left: 22%; right: 22%; top: 50%;
  height: 2px;
  margin-top: -1px;
  border-radius: 1px;
  background: currentColor;
  opacity: .22;
}
#card-exam.ch-prep .ch-dg::before {
  animation: ch-standby 1s var(--ch-io) infinite alternate;
  animation-delay: calc(var(--ch-i, 0) * -260ms);      /* negative: each unit starts at its own phase */
  animation-play-state: paused;
}
#card-exam.ch-prep.ch-live .ch-dg::before { animation-play-state: running; }
#card-exam.ch-run .ch-dg::before {
  animation: ch-standby-out .28s ease-out both;
  animation-delay: calc(var(--ch-i) * 90ms);
}

/* ============================================================================================
   Reduced motion — resting state only (charts.js also skips every intro and loop)
   ============================================================================================ */
@media (prefers-reduced-motion: reduce) {
  #card-radar *, #card-week *, #card-exam *,
  #card-week .wk-col::before, #card-week .wk-bar::before, #card-week .wk-bar i::before, #card-week .wk-bar i::after,
  #card-exam .ch-dg::before {
    animation: none !important;
    transition: none !important;
  }
  #radar .ch-sweep-wrap, #radar .ch-ping,
  #card-week .wk-bar::before, #card-exam .ch-dg::before { display: none; }
  #card-week.ch-done .wk-col.ch-today::before { opacity: .35; }
}
