/* ==========================================================================
   THE APP — the interactive "daru os / today" card on /daru-os
   ==========================================================================
   Ported from the #os section of darustrong.com (the nav's "THE APP"), which
   canonicalises to this page. The original is a React component
   (src/marketing/home/OsCard.tsx, main @ d5177a4) built on the app's own
   HeroRing / StatusBadge / ReadinessSparkline; this is the same card rebuilt
   in plain HTML/CSS/JS on `ds.css` tokens, with the same copy, the same
   illustrative session data and the same behaviour.

   Two deliberate differences from the original:
     - the title bar reads "daru os / today", because this repository calls
       the app Daru OS everywhere it names it (see docs/daru-os.md);
     - the beam behind the card is CSS, not the original's WebGL LaserFlow.

   Loaded only by daru-os.html. Everything here is `os-` prefixed so it
   cannot collide with the shared `ds-` system.
   ========================================================================== */

.os-stage {
    position: relative;
    --os-beam-drop: clamp(150px, 26vw, 240px);
    padding-top: var(--os-beam-drop);
}

/* The box the beam is drawn in: LaserFlow's canvas fills this, and the
   element's own geometry is the live site's — 40px above the card, 240px
   past it on each side, 260px below it, with the top 200px masked away so
   the beam fades in rather than cutting off under the heading.

   `max(-240px, …)` is the one departure: it stops the bleed at the viewport
   edge, because this site has no page-level overflow clip and -240px on a
   1440 screen would have given the page a horizontal scrollbar. On a wide
   screen the bleed is the full 240px, as it is there. */
.os-beam {
    position: absolute;
    top: -40px;
    bottom: -260px;
    left: max(-240px, calc(50% - 50vw + 8px));
    right: max(-240px, calc(50% - 50vw + 8px));
    z-index: 0;
    pointer-events: none;
    -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 200px);
    mask-image: linear-gradient(180deg, transparent 0, #000 200px);
}

/* The fallback, shown only where WebGL2 is unavailable or JavaScript is off:
   the same beam in gradients, landing on the same edge. js/laser-flow.js
   sets data-webgl="on" as soon as it has a context, which takes this off. */
.os-beam-fallback {
    position: absolute;
    top: 0;
    left: 50%;
    width: min(900px, 100%);
    height: calc(var(--os-beam-drop) + 40px);
    translate: -50% 0;
}

.os-beam[data-webgl="on"] .os-beam-fallback { display: none; }

.os-beam-fallback::before,
.os-beam-fallback::after {
    content: "";
    position: absolute;
    inset: 0;
}

/* The shaft: a narrow column of light widening a little as it falls. */
.os-beam-fallback::before {
    background:
        radial-gradient(11% 96% at 50% 100%, rgba(var(--gold-rgb), 0.3), transparent 72%),
        radial-gradient(3.2% 100% at 50% 100%, rgba(var(--gold-rgb), 0.85), transparent 68%),
        radial-gradient(1.1% 100% at 50% 100%, rgba(255, 246, 221, 0.95), transparent 64%);
}

/* The flare, where the beam lands on the top of the card. */
.os-beam-fallback::after {
    background:
        radial-gradient(42% 13% at 50% 100%, rgba(var(--gold-rgb), 0.34), transparent 72%),
        radial-gradient(16% 7% at 50% 100%, rgba(255, 243, 212, 0.55), transparent 70%),
        radial-gradient(5% 4% at 50% 100%, rgba(255, 252, 240, 0.95), transparent 68%);
}

@media (prefers-reduced-motion: no-preference) {
    .os-beam-fallback { animation: os-beam-breathe 7s ease-in-out infinite; }
}

@keyframes os-beam-breathe {
    0%, 100% { opacity: 0.85; }
    50%      { opacity: 1; }
}

/* ---------- the card ---------- */

.os-card {
    position: relative;
    z-index: 1;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    background: linear-gradient(180deg, var(--ink-1), #060606);
    box-shadow: 0 40px 120px -40px rgba(var(--gold-rgb), 0.18);
    overflow: hidden;

    /* Set per status by js/daru-os-card.js. */
    --os-color: var(--green);
    --os-soft: transparent;
    --os-border: transparent;
}

.os-card[data-status="yellow"] {
    --os-color: #facc15;
    --os-soft: rgba(250, 204, 21, 0.1);
    --os-border: rgba(250, 204, 21, 0.3);
}

.os-card[data-status="red"] {
    --os-color: #f87171;
    --os-soft: rgba(248, 113, 113, 0.1);
    --os-border: rgba(248, 113, 113, 0.3);
}

/* ---------- title bar ---------- */

.os-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 16px;
    padding: 12px clamp(12px, 4vw, 16px);
    border-bottom: 1px solid var(--line);
}

.os-bar-id { display: flex; align-items: center; gap: 8px; }

.os-bar-id i {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12);
}

.os-path {
    margin-left: 10px;
    font-family: var(--mono);
    font-size: 12px;
    color: var(--fg-4);
}

.os-toggle {
    display: flex;
    gap: 4px;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.04);
}

.os-tab {
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: 32px;
    padding: 6px 12px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    font-family: var(--mono);
    font-size: 12px;
    color: var(--fg-4);
    cursor: pointer;
    transition: background var(--t), color var(--t);
}

.os-tab:hover { color: var(--fg-2); }

.os-tab[aria-pressed="true"] {
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
}

.os-tab i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--green);
}

.os-tab[data-os-status="yellow"] i { background: #facc15; }
.os-tab[data-os-status="red"] i    { background: #f87171; }

/* ---------- layout ---------- */

.os-body { display: flex; flex-wrap: wrap; }

.os-readiness {
    flex: 1 1 300px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    padding: clamp(20px, 5vw, 32px);
    border-right: 1px solid var(--line);
}

.os-session {
    flex: 2 1 520px;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 18px;
    padding: clamp(12px, 4vw, 24px);
}

.os-label {
    font-family: var(--mono);
    font-size: 12px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--fg-4);
}

.os-row-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

/* ---------- status badge ---------- */

.os-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 4px 10px;
    border: 1px solid var(--os-border, rgba(74, 222, 128, 0.3));
    border-radius: 999px;
    background: var(--os-soft, rgba(74, 222, 128, 0.1));
    font-family: var(--mono);
    font-size: 10px;
    font-weight: 500;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--os-color);
    white-space: nowrap;
}

.os-card[data-status="green"] .os-badge {
    border-color: rgba(74, 222, 128, 0.3);
    background: rgba(74, 222, 128, 0.1);
}

.os-badge i {
    position: relative;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--os-color);
}

.os-badge i::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background: inherit;
    opacity: 0.75;
}

@media (prefers-reduced-motion: no-preference) {
    .os-badge i::after { animation: os-ping 1.4s cubic-bezier(0, 0, .2, 1) infinite; }
}

@keyframes os-ping {
    75%, 100% { transform: scale(2.2); opacity: 0; }
}

/* ---------- readiness ring ---------- */

.os-ring-wrap {
    position: relative;
    display: grid;
    place-items: center;
    width: 200px;
    max-width: 100%;
    margin-inline: auto;
    padding: 8px 0;
    aspect-ratio: 1;
}

.os-ring { position: absolute; inset: 0; width: 100%; height: 100%; }

.os-ring-track { fill: none; stroke: var(--ink-4); stroke-width: 12; }

.os-ring-arc {
    fill: none;
    stroke: var(--os-color);
    stroke-width: 12;
    stroke-linecap: round;
    stroke-dasharray: 565.49;
    stroke-dashoffset: 565.49;
    transform: rotate(-90deg);
    transform-origin: 100px 100px;
    transition: stroke-dashoffset 1.2s cubic-bezier(.16, 1, .3, 1), stroke 420ms var(--ease);
}

.os-ring-tick { stroke: var(--fg); stroke-linecap: round; }

.os-ring-centre {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 0;
}

.os-score {
    font-size: 48px;
    font-weight: 700;
    line-height: 1;
    letter-spacing: -0.03em;
    font-variant-numeric: tabular-nums;
    color: var(--fg);
}

.os-verdict {
    margin-top: 9px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--fg-4);
}

@media (prefers-reduced-motion: reduce) {
    .os-ring-arc { transition: none; }
}

/* ---------- 7-day sparkline ---------- */

.os-spark {
    position: relative;
    overflow: hidden;
    padding: 20px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--ink-2);
}

.os-spark-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 4px;
}

.os-spark-title {
    font-family: var(--font);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--fg-4);
}

.os-spark-avg {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 24px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.04);
    font-size: 10.5px;
    color: var(--fg-4);
    font-variant-numeric: tabular-nums;
}

.os-spark-avg b { color: var(--fg-2); font-weight: 600; }

.os-spark-now {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin: 0 0 12px;
}

.os-spark-latest {
    font-size: 30px;
    font-weight: 600;
    line-height: 1;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
    color: var(--fg);
}

.os-spark-delta { font-size: 11.5px; font-variant-numeric: tabular-nums; color: var(--green); }
.os-spark-delta[data-dir="down"] { color: #facc15; }

/* `currentColor` is what the area gradient under the line is mixed from. */
.os-spark svg { display: block; width: 100%; height: auto; color: var(--os-color); }

.os-spark-line {
    fill: none;
    stroke: var(--os-color);
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
}

.os-spark-dot {
    fill: var(--ink-2);
    stroke: var(--os-color);
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

.os-spark-last { fill: var(--os-color); }

.os-spark-days {
    display: flex;
    justify-content: space-between;
    margin: 8px 0 0;
    font-size: 10px;
    font-weight: 500;
    letter-spacing: 0.04em;
    color: var(--fg-5);
}

.os-spark-days span:last-child { color: var(--gold); }

/* ---------- today's session ---------- */

.os-action {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-family: var(--mono);
    font-size: 12px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--os-color);
    transition: color 300ms ease;
}

.os-action i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0;
    transition: opacity 200ms ease;
}

.os-card[data-calculating="true"] .os-action { color: var(--gold); }
.os-card[data-calculating="true"] .os-action i { opacity: 1; }

.os-rows {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* The scan line that sweeps the session while the day is recalculated. */
.os-scan {
    position: absolute;
    left: -8px;
    right: -8px;
    top: 0;
    height: 64px;
    z-index: 2;
    translate: 0 -50%;
    opacity: 0;
    pointer-events: none;
    background: linear-gradient(180deg, transparent, color-mix(in srgb, var(--os-color) 10%, transparent) 50%, transparent);
}

.os-scan::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: 50%;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--os-color) 20%, var(--os-color) 80%, transparent);
    box-shadow: 0 0 12px var(--os-color);
}

.os-scan[data-scan="running"] {
    top: 100%;
    opacity: 1;
    transition: top 900ms cubic-bezier(.65, 0, .35, 1), opacity 200ms ease;
}

.os-scan[data-scan="done"] { top: 100%; opacity: 0; transition: opacity 350ms ease; }

@media (prefers-reduced-motion: reduce) {
    .os-scan { display: none; }
}

/* ---------- one exercise ---------- */

.os-ex {
    all: unset;
    box-sizing: border-box;
    display: grid;
    grid-template-columns: 112px minmax(0, 1fr) auto;
    align-items: center;
    column-gap: 16px;
    row-gap: 8px;
    padding: 8px 16px 8px 8px;
    border: 1px solid rgba(255, 255, 255, 0.04);
    border-radius: 12px;
    cursor: pointer;
    transition: background 300ms ease, border-color 300ms ease, opacity 600ms ease, transform 700ms var(--ease);
}

.os-ex[aria-pressed="true"] {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.14);
}

.os-ex[data-changed="true"] { border-color: color-mix(in srgb, var(--os-color) 14%, transparent); }

.os-ex:focus-visible { outline: 2px solid rgba(255, 255, 255, 0.7); outline-offset: 2px; }

/* Rows slide in when the card is first seen and after each recalculation. */
.os-rows[data-rows="out"] .os-ex { opacity: 0; transform: translateX(18px); }

.os-ex-shot {
    position: relative;
    width: 100%;
    aspect-ratio: 4 / 3;
    border-radius: 8px;
    overflow: hidden;
    background: #000;
}

.os-ex-shot img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.75;
}

.os-ex[aria-pressed="true"] .os-ex-shot img { opacity: 1; }

.os-ex[data-off="true"] .os-ex-shot img { opacity: 0.3; filter: grayscale(1); }

.os-ex-n {
    position: absolute;
    top: 5px;
    left: 5px;
    padding: 2px 5px;
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.6);
    font-family: var(--mono);
    font-size: 10px;
    color: var(--fg-2);
}

.os-ex-text { display: flex; flex-direction: column; gap: 5px; min-width: 0; }

.os-ex-was {
    font-size: 12px;
    color: var(--fg-4);
    text-decoration: line-through;
    text-decoration-color: var(--os-color);
    text-decoration-thickness: 2px;
}

.os-ex-name {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--fg);
}

.os-ex[data-off="true"] .os-ex-name { color: var(--fg-4); }

.os-ex-dose {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    font-family: var(--mono);
    font-size: 12px;
    color: var(--fg-3);
}

.os-ex-dose s { color: var(--fg-4); text-decoration-color: var(--os-color); text-decoration-thickness: 2px; }
.os-ex-dose em { font-style: normal; color: var(--fg-5); }

.os-ex[data-changed="true"] .os-ex-dose b { color: var(--os-color); font-weight: 600; }
.os-ex[data-off="true"] .os-ex-dose b { color: var(--fg-4); font-weight: 400; }
.os-ex-dose b { font-weight: 400; color: var(--fg-3); }

.os-ex-chip {
    justify-self: end;
    padding: 4px 8px;
    border: 1px solid var(--os-border);
    border-radius: 6px;
    background: var(--os-soft);
    font-family: var(--mono);
    font-size: 11px;
    letter-spacing: 0.06em;
    white-space: nowrap;
    color: var(--os-color);
}

.os-note {
    margin: 0;
    font-size: 13px;
    line-height: 1.55;
    color: var(--fg-3);
    transition: opacity 500ms ease;
}

.os-card[data-calculating="true"] .os-note { opacity: 0; }

/* ---------- narrow ---------- */

@media (max-width: 759px) {
    .os-readiness { border-right: 0; border-bottom: 1px solid var(--line); }

    .os-ex {
        grid-template-columns: 76px minmax(0, 1fr);
        column-gap: 12px;
        padding-right: 10px;
    }

    .os-ex-shot { grid-row: span 2; }
    .os-ex-chip { grid-column: 2; justify-self: start; }
    .os-rows[data-rows="out"] .os-ex { transform: translateY(10px); }
}
