/* =========================================================================
   Oconee Together: "Take a breath" (see assets/js/breathe.js)
   A bloom of translucent leaves that opens as you breathe in, rests while
   you hold, and folds as you breathe out. Tokens come from styles.css;
   the fallbacks match DESIGN.md.
   ========================================================================= */

.breathe {
  --br-size: clamp(260px, 76vw, 340px);
  --br-word: clamp(1.875rem, 1.35rem + 1.5vw, 2.5rem);
  --br-gap: clamp(18px, 2.6vw, 28px);

  /* Leaves (light: watercolour on paper, overlaps deepen). Each leaf is
     pale where it meets the centre and richer toward the tip. */
  --br-blend: multiply;
  --br-petal-op: .9;
  --br-a0: var(--lichen, #DCE6D7);  --br-a0-o: .3;
  --br-a1: var(--sage, #9DB8A3);    --br-a1-o: .92;
  --br-b0: var(--lichen, #DCE6D7);  --br-b0-o: .3;
  --br-b1: var(--moss, #3F6B50);    --br-b1-o: .52;
  --br-c0: var(--lichen, #DCE6D7);  --br-c0-o: .3;
  --br-c1: var(--sun, #E9B872);     --br-c1-o: .68;
  --br-vein: var(--surface, #FFFFFF); --br-vein-o: .55;
  /* Light behind and inside the bloom */
  --br-h0: var(--sun, #E9B872);     --br-h0-o: .30;
  --br-h1: var(--lichen, #DCE6D7);  --br-h1-o: .62;
  --br-h2: var(--mist, #E9EFE8);    --br-h2-o: 0;
  --br-heart: var(--sun, #E9B872);  --br-heart-o: .9;
  --br-heart-blend: normal;
  /* Ring */
  --br-track: var(--line, rgba(27, 30, 26, .10));
  --br-ring: var(--moss, #3F6B50);
  --br-ring-hold: var(--br-ring);
  --br-ring-w: 1.5;
  --br-track-w: 1;

  position: relative;
  color: var(--text, #2A2E29);
  text-align: center;
}
@supports (color: color-mix(in oklab, red, blue)) {
  .breathe { --br-ring-hold: color-mix(in oklab, var(--moss, #3F6B50) 62%, var(--sun, #E9B872)); }
}

/* Dark: "Night forest". Leaves glow where they overlap. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .breathe {
    --br-blend: screen;
    --br-petal-op: .92;
    --br-a0: var(--moss, #9FD0AE);    --br-a0-o: .1;
    --br-a1: var(--sage, #9DB8A3);    --br-a1-o: .62;
    --br-b0: var(--moss, #9FD0AE);    --br-b0-o: .1;
    --br-b1: var(--moss, #9FD0AE);    --br-b1-o: .66;
    --br-c0: var(--sun, #F2CF7E);     --br-c0-o: .08;
    --br-c1: var(--sun, #F2CF7E);     --br-c1-o: .7;
    --br-vein: var(--paper, #0D110F); --br-vein-o: .35;
    --br-h0: var(--sun, #F2CF7E);     --br-h0-o: .2;
    --br-h1: var(--moss, #9FD0AE);    --br-h1-o: .1;
    --br-h2: var(--moss, #9FD0AE);    --br-h2-o: 0;
    --br-heart: var(--sun, #F2CF7E);  --br-heart-o: .6;
    --br-heart-blend: screen;
    --br-track: var(--line, rgba(238, 242, 236, .10));
    --br-ring: var(--moss, #9FD0AE);
  }
}
:root[data-theme="dark"] .breathe {
  --br-blend: screen;
  --br-petal-op: .92;
  --br-a0: var(--moss, #9FD0AE);    --br-a0-o: .1;
  --br-a1: var(--sage, #9DB8A3);    --br-a1-o: .62;
  --br-b0: var(--moss, #9FD0AE);    --br-b0-o: .1;
  --br-b1: var(--moss, #9FD0AE);    --br-b1-o: .66;
  --br-c0: var(--sun, #F2CF7E);     --br-c0-o: .08;
  --br-c1: var(--sun, #F2CF7E);     --br-c1-o: .7;
  --br-vein: var(--paper, #0D110F); --br-vein-o: .35;
  --br-h0: var(--sun, #F2CF7E);     --br-h0-o: .2;
  --br-h1: var(--moss, #9FD0AE);    --br-h1-o: .1;
  --br-h2: var(--moss, #9FD0AE);    --br-h2-o: 0;
  --br-heart: var(--sun, #F2CF7E);  --br-heart-o: .6;
  --br-heart-blend: screen;
  --br-track: var(--line, rgba(238, 242, 236, .10));
  --br-ring: var(--moss, #9FD0AE);
}

/* ---------- Without JavaScript: a quiet line of guidance ---------- */
.breathe:not(.is-ready) {
  display: grid;
  justify-items: center;
  gap: 14px;
}
.breathe:not(.is-ready)::before {
  /* one small leaf, drawn in CSS */
  content: "";
  width: 22px;
  height: 22px;
  border-radius: 0 100% 0 100%;
  background: var(--sage, #9DB8A3);
  opacity: .9;
  transform: rotate(-8deg);
}
.breathe:not(.is-ready) > p {
  margin: 0;
  max-width: 30ch;
  font-family: var(--font-serif, "Newsreader", "Iowan Old Style", "Palatino Linotype", Georgia, serif);
  font-style: italic;
  font-size: clamp(1.25rem, 1.05rem + .6vw, 1.5rem);
  line-height: 1.4;
  letter-spacing: -0.005em;
  color: var(--ink, #1B1E1A);
  text-wrap: pretty;
}

/* ---------- Enhanced ---------- */
.breathe.is-ready > .breathe-fallback,
.breathe-live {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.breathe-inner {
  display: grid;
  justify-items: center;
  gap: var(--br-gap);
}

.breathe-visual {
  position: relative;
  width: var(--br-size);
  max-width: 100%;
  aspect-ratio: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
.breathe-svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  isolation: isolate;
}

/* Gradient stops take their colour from the tokens above */
.br-a0 { stop-color: var(--br-a0); stop-opacity: var(--br-a0-o); }
.br-am { stop-color: var(--br-a1); stop-opacity: calc(var(--br-a1-o) * .82); }
.br-a1 { stop-color: var(--br-a1); stop-opacity: var(--br-a1-o); }
.br-b0 { stop-color: var(--br-b0); stop-opacity: var(--br-b0-o); }
.br-bm { stop-color: var(--br-b1); stop-opacity: calc(var(--br-b1-o) * .82); }
.br-b1 { stop-color: var(--br-b1); stop-opacity: var(--br-b1-o); }
.br-c0 { stop-color: var(--br-c0); stop-opacity: var(--br-c0-o); }
.br-cm { stop-color: var(--br-c1); stop-opacity: calc(var(--br-c1-o) * .82); }
.br-c1 { stop-color: var(--br-c1); stop-opacity: var(--br-c1-o); }
.br-h0 { stop-color: var(--br-h0); stop-opacity: var(--br-h0-o); }
.br-h1 { stop-color: var(--br-h1); stop-opacity: var(--br-h1-o); }
.br-h2 { stop-color: var(--br-h2); stop-opacity: var(--br-h2-o); }
.br-c0h { stop-color: var(--br-heart); stop-opacity: var(--br-heart-o); }
.br-c1h { stop-color: var(--br-heart); stop-opacity: 0; }

/* The bloom. breathe.js sets --o (how open, 0 to 1), --rot and, per leaf, --oi. */
.br-bloom {
  transform: rotate(calc(var(--rot, 0) * 1deg)) scale(calc(.63 + .37 * var(--o, .4)));
}
.br-petal {
  mix-blend-mode: var(--br-blend);
  opacity: var(--br-petal-op);
  /* Folded leaves lift toward you (shorter), turn into a spiral and close in. */
  transform:
    rotate(calc((var(--a, 0) + (1 - var(--oi, .4)) * 40) * 1deg))
    translateY(calc(var(--oi, .4) * -3px))
    scale(calc(var(--k, 1) * (.72 + .28 * var(--oi, .4))), calc(var(--k, 1) * (.46 + .54 * var(--oi, .4))));
}
.br-vein {
  fill: none;
  stroke: var(--br-vein);
  stroke-opacity: var(--br-vein-o);
  stroke-width: .9;
  stroke-linecap: round;
}
.br-heart {
  mix-blend-mode: var(--br-heart-blend);
  opacity: calc(.2 + .8 * var(--o, .4) + .15 * var(--glow, 0));
  transform: scale(calc(.55 + .55 * var(--o, .4)));
}
.br-glow {
  transform: scale(calc(.8 + .2 * var(--o, .4) + .015 * var(--glow, 0)));
}
.br-halo {
  opacity: calc(.6 + .4 * var(--o, .4));
}
/* A slow shimmer of light while the bloom waits (opacity only) */
.breathe[data-state="idle"].is-inview .br-halo,
.breathe[data-state="done"].is-inview .br-halo {
  animation: br-shimmer 9s cubic-bezier(.65, 0, .35, 1) infinite;
}
@keyframes br-shimmer {
  0%, 100% { opacity: .62; }
  50% { opacity: .92; }
}

/* Ring: a hairline track, the current phase drawn over it, and a bead of light */
.br-ring circle { fill: none; }
.br-track { stroke: var(--br-track); stroke-width: var(--br-track-w); }
.br-prog,
.br-prev {
  stroke: var(--br-ring);
  stroke-width: var(--br-ring-w);
  stroke-linecap: round;
  stroke-dasharray: 0 100;
}
.br-prev { stroke-dasharray: 100 0; opacity: 0; }
.breathe[data-phase="hold"] .br-prog,
.breathe[data-phase="rest"] .br-prog,
.breathe[data-prev-phase="hold"] .br-prev,
.breathe[data-prev-phase="rest"] .br-prev { stroke: var(--br-ring-hold); }
.br-bead { opacity: 0; }
.br-bead-dot { fill: var(--br-ring); }
.br-bead-glow { fill: var(--sun, #E9B872); opacity: .38; }
.breathe[data-phase="hold"] .br-bead-dot,
.breathe[data-phase="rest"] .br-bead-dot { fill: var(--br-ring-hold); }
.breathe:is([data-state="idle"], [data-state="done"]) .br-prog { opacity: 0; }

/* Paused: everything rests a little dimmer */
.br-bloom,
.br-glow { transition: opacity var(--dur-3, 720ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)); }
.breathe[data-state="paused"] .br-bloom { opacity: .72; }
.breathe[data-state="paused"] .br-glow { opacity: .6; }

/* ---------- Words ---------- */
.breathe-panel {
  container: oc-breathe-panel / inline-size;
  display: grid;
  justify-items: center;
  gap: clamp(16px, 2.2vw, 22px);
  width: 100%;
}
.breathe-stage {
  display: grid;
  justify-items: center;
  align-content: start;
  gap: 4px;
  min-height: calc(var(--br-word) * 1.2 + 1.5rem);
}
.breathe .breathe-word {
  margin: 0;
  max-width: 24ch;
  font-family: var(--font-serif, "Newsreader", "Iowan Old Style", "Palatino Linotype", Georgia, serif);
  font-style: italic;
  font-weight: 400;
  font-optical-sizing: auto;
  font-size: var(--br-word);
  line-height: 1.2;
  letter-spacing: -0.012em;
  color: var(--ink, #1B1E1A);
  text-wrap: balance;
}
.breathe .breathe-word.is-end {
  font-size: calc(var(--br-word) * .62);
  line-height: 1.4;
  letter-spacing: -0.004em;
  max-width: 27ch;
}
.breathe .breathe-count {
  margin: 0;
  min-height: 1.45em;
  font-family: var(--font-body, "Figtree", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif);
  font-size: .9375rem;
  font-weight: 500;
  line-height: 1.45;
  letter-spacing: 0;
  font-variant-numeric: tabular-nums;
  color: var(--text-2, #5E635B);
  text-wrap: balance;
}
.breathe[data-state="paused"] .breathe-word { color: var(--text-2, #5E635B); }
.breathe[data-state="done"] .breathe-count { display: none; }

/* Easy-to-read font replaces the serif too */
html[data-font="legible"] .breathe-word,
html[data-font="legible"] .breathe:not(.is-ready) > p {
  font-family: "Atkinson Hyperlegible", var(--font-body, ui-sans-serif, system-ui, sans-serif);
  font-style: normal;
  letter-spacing: 0;
}

/* ---------- Controls ---------- */
.breathe-controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  width: 100%;
}
.breathe-toggle { grid-column: 2; }
.breathe-restart { grid-column: 3; justify-self: start; }
/* Not enough room beside the button (narrow column or large text): stack */
@container oc-breathe-panel (max-width: 17em) {
  .breathe-controls { display: flex; flex-direction: column; align-items: center; gap: 4px; }
}

.breathe-btn {
  -webkit-appearance: none;
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5em;
  min-width: 44px;
  min-height: 48px;
  margin: 0;
  border: 0;
  border-radius: var(--r-pill, 999px);
  font-family: var(--font-body, "Figtree", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif);
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.01em;
  white-space: nowrap;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition:
    background-color var(--dur-1, 160ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)),
    color var(--dur-1, 160ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)),
    opacity var(--dur-2, 320ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)),
    transform var(--dur-2, 320ms) var(--ease-spring, cubic-bezier(.34, 1.4, .64, 1));
}
.breathe-btn:focus { outline: none; }
.breathe-btn:focus-visible {
  outline: 3px solid var(--focus, #2C6E4F);
  outline-offset: 2px;
}
.breathe-btn:active { transform: scale(.96); }
.breathe-ico { width: 1.125em; height: 1.125em; flex: none; }
.breathe-ico-wrap { display: inline-flex; }

.breathe-toggle {
  min-width: 8.5em;
  padding: 0 1.5em 0 1.3em;
  background: var(--lichen, #DCE6D7);
  color: var(--pine, #234D3A);
}
@supports (color: color-mix(in oklab, red, blue)) {
  .breathe-toggle:hover { background: color-mix(in oklab, var(--lichen, #DCE6D7), var(--pine, #234D3A) 10%); }
}
.breathe-restart {
  min-height: 44px;
  padding: 0 1em 0 .85em;
  gap: .4em;
  background: transparent;
  color: var(--text-2, #5E635B);
  font-size: .9375rem;
  font-weight: 500;
  opacity: 0;
  visibility: hidden;
  transition:
    background-color var(--dur-1, 160ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)),
    color var(--dur-1, 160ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)),
    opacity var(--dur-2, 320ms) var(--ease-out, cubic-bezier(.22, 1, .36, 1)),
    visibility 0s linear var(--dur-2, 320ms),
    transform var(--dur-2, 320ms) var(--ease-spring, cubic-bezier(.34, 1.4, .64, 1));
}
.breathe-restart .breathe-ico { width: 1em; height: 1em; }
.breathe-restart.is-shown {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}
.breathe-restart:hover {
  background: var(--mist, #E9EFE8);
  color: var(--ink, #1B1E1A);
}

/* ---------- Compact (Get help now page) ---------- */
.breathe[data-breathe="compact"] {
  --br-size: clamp(160px, 46vw, 196px);
  --br-word: clamp(1.5rem, 1.25rem + .7vw, 1.75rem);
  --br-gap: 14px;
  --br-ring-w: 2;
  --br-track-w: 1.4;
  container: oc-breathe / inline-size;
  min-inline-size: min(100%, 16rem);
}
.breathe[data-breathe="compact"] .breathe-panel { gap: 14px; }
.breathe[data-breathe="compact"] .breathe-word.is-end { font-size: calc(var(--br-word) * .72); }
.breathe[data-breathe="compact"] .breathe-toggle { min-height: 44px; }

/* Side by side when the compact one has room */
@container oc-breathe (min-width: 30rem) {
  .breathe-inner {
    grid-template-columns: auto minmax(0, 1fr);
    justify-items: start;
    align-items: center;
    column-gap: clamp(20px, 4cqi, 36px);
    text-align: start;
  }
  .breathe-panel,
  .breathe-stage { justify-items: start; }
  .breathe-stage { min-height: calc(var(--br-word) * 1.2 * 2 + 1.5rem); align-content: end; }
  .breathe-controls { display: flex; flex-wrap: wrap; justify-content: flex-start; }
}

/* ---------- Reduced motion: no scaling or turning, light and words still guide ---------- */
.breathe.is-calm .br-bloom { transform: rotate(-4deg) scale(.86); transition: none; }
.breathe.is-calm .br-petal {
  transform: rotate(calc(var(--a, 0) * 1deg)) translateY(-2px) scale(calc(var(--k, 1) * .95), calc(var(--k, 1) * .9));
  opacity: calc(var(--br-petal-op) * (.42 + .58 * var(--oi, .4)));
}
.breathe.is-calm .br-glow { transform: none; }
.breathe.is-calm .br-heart { transform: scale(.85); }
.breathe.is-calm .br-halo { animation: none; }
.breathe.is-calm .breathe-btn,
.breathe.is-calm .breathe-btn:active { transform: none; }

/* ---------- High contrast: solid lines, no translucency or glow ---------- */
/* :root raises this above the dark-theme rules so they can't tint it */
:root[data-contrast="high"] .breathe {
  --br-ring: currentColor;
  --br-ring-hold: currentColor;
  --br-track: currentColor;
  --br-ring-w: 4;
  --br-track-w: 1.5;
  color: var(--text, #000000);
}
html[data-contrast="high"] .br-glow,
html[data-contrast="high"] .br-heart,
html[data-contrast="high"] .br-vein,
html[data-contrast="high"] .br-prev,
html[data-contrast="high"] .br-bead-glow { display: none; }
html[data-contrast="high"] .br-petal,
html[data-contrast="high"] .breathe.is-calm .br-petal {
  mix-blend-mode: normal;
  opacity: 1;
}
html[data-contrast="high"] .br-leaf {
  fill: none;
  stroke: currentColor;
  stroke-width: 2px;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}
html[data-contrast="high"] .br-bead-dot { fill: currentColor; }
html[data-contrast="high"] .breathe[data-state="paused"] .br-bloom { opacity: 1; }
html[data-contrast="high"] .breathe-word,
html[data-contrast="high"] .breathe-count,
html[data-contrast="high"] .breathe[data-state="paused"] .breathe-word,
html[data-contrast="high"] .breathe:not(.is-ready) > p { color: var(--text, #000000); }
html[data-contrast="high"] .breathe:not(.is-ready)::before { background: currentColor; }
html[data-contrast="high"] .breathe-btn {
  border: 2px solid currentColor;
  background: var(--surface, #FFFFFF);
  color: var(--text, #000000);
}
html[data-contrast="high"] .breathe-btn:hover { background: var(--surface, #FFFFFF); text-decoration: underline; }

@media (forced-colors: active) {
  .br-glow, .br-heart, .br-vein, .br-prev, .br-bead-glow { display: none; }
  .br-petal { mix-blend-mode: normal; opacity: 1; }
  .br-leaf { fill: none; stroke: CanvasText; stroke-width: 2px; vector-effect: non-scaling-stroke; }
  .br-prog, .br-bead-dot { stroke: Highlight; fill: none; }
  .br-bead-dot { fill: Highlight; }
  .br-track { stroke: CanvasText; }
  .breathe-btn { border: 2px solid ButtonText; }
}

/* ---------- Print: the plain guidance only ---------- */
@media print {
  .breathe-inner { display: none !important; }
  .breathe.is-ready > .breathe-fallback {
    position: static !important;
    width: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    clip: auto;
    clip-path: none;
    white-space: normal;
  }
}
