* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #FDF6F0;
    --bg-secondary: #141417;
    --accent: #e8ff47;
    --accent-dim: #b8cc39;
    --text-primary: #3D2314;
    --text-secondary: #787880;
    --border: #2a2a30;
}

body {
    font-family: 'Outfit', sans-serif;
    background: linear-gradient(135deg, #FFB5C8 0%, #F0EDE8 40%, #FFE899 100%);
    /* Two-line viewport-height fallback. Older browsers ignore the
     * second declaration (unknown `dvh` unit) and keep `100vh`;
     * modern browsers (iOS Safari 15.4+, Chrome 108+, Firefox 101+)
     * apply `100dvh` and resize the layout dynamically as the
     * iOS Safari URL bar shows/hides. Without this, the bottom of
     * the page can land behind the URL bar — the flip-camera button
     * is the typical casualty. */
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-primary);
    overflow: hidden;
    /* Mobile-quality-of-life chrome — none of these change the visual
     * design, they only smooth out platform quirks:
     *   - `tap-highlight-color: transparent`: kills the iOS gray flash
     *     when tapping a button, so our soft-pink press states show
     *     through cleanly.
     *   - `touch-action: manipulation`: removes the legacy 300ms
     *     double-tap-to-zoom delay on every interactive element.
     *     `width=device-width` already gives most browsers fast taps,
     *     but this guarantees it everywhere.
     *   - `overscroll-behavior: none`: prevents iOS rubber-band
     *     bouncing and Android pull-to-refresh from triggering on the
     *     fixed-layout app (we have `overflow: hidden` so there's
     *     nothing to scroll, but the gesture can still misfire). */
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    overscroll-behavior: none;
}

/* Atmospheric background layer disabled — the new linear gradient on `body`
 * is the page background. Keeping the empty pseudo-element preserves the
 * stacking context exactly as before; only its fill is removed so the
 * gradient underneath shows through. */
body::before {
    content: '';
    position: fixed;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: none;
    z-index: -1;
}

.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;
    /* Padding floor of 24px on every side, lifted to whatever the
     * device's safe-area inset reports if it's larger. On a notched
     * iPhone with `viewport-fit=cover` (set in index.php), the top
     * inset is ~47-59px and the bottom inset is ~34px (home
     * indicator). Without the `max(...)` lift, the footer would sit
     * under the home indicator and the header could be partially
     * hidden behind the dynamic island. On non-notched devices and
     * desktop, every `env(...)` returns 0 and the values collapse
     * back to a clean 24px on every side. */
    padding:
        max(24px, env(safe-area-inset-top))
        max(24px, env(safe-area-inset-right))
        max(24px, env(safe-area-inset-bottom))
        max(24px, env(safe-area-inset-left));
    max-height: 100vh;
    max-height: 100dvh;
}

.header {
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.header h1 {
    font-size: 1.8rem;
    font-weight: 300;
    letter-spacing: 0.3em;
    text-transform: lowercase;
    color: #1a1a1a;
}

.tagline {
    font-size: 0.75rem;
    font-weight: 400;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--text-secondary);
}

.viewport-wrapper {
    position: relative;
    width: min(320px, 80vw);
    aspect-ratio: 9 / 16;
    /* `200px` is the chrome budget — header + footer + container
     * vertical padding + flex gaps — so the camera fits cleanly
     * inside `100vh` on desktop. On notched mobile devices the
     * container padding grows by `env(safe-area-inset-top + bottom)`
     * via the `.container` rule above; subtracting the same amount
     * here keeps the camera shrinking in lockstep so the footer
     * never gets pushed off-screen. On devices/browsers without
     * safe-area insets, both `env()` calls return 0 and the calc
     * matches the fallback exactly. */
    max-height: calc(100vh - 200px);
    max-height: calc(100dvh - 200px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}

.viewport {
    position: relative;
    width: 100%;
    height: 100%;
    background: var(--bg-secondary);
    border-radius: 24px;
    overflow: hidden;
    box-shadow: 
        0 0 0 1px var(--border),
        0 25px 50px -12px rgba(0, 0, 0, 0.6),
        0 0 100px -20px rgba(232, 255, 71, 0.1);
}

.viewport-frame {
    position: absolute;
    inset: -3px;
    border-radius: 27px;
    border: 1px solid transparent;
    background: linear-gradient(135deg, rgba(232, 255, 71, 0.3), transparent 50%, rgba(232, 255, 71, 0.1)) border-box;
    -webkit-mask: linear-gradient(#fff 0 0) padding-box, linear-gradient(#fff 0 0);
    mask: linear-gradient(#fff 0 0) padding-box, linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
    z-index: 10;
}

#webcam {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1); /* Mirror effect */
    display: block;
}

.overlay {
    position: absolute;
    inset: 0;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.5s ease, visibility 0.5s ease;
}

.overlay.hidden {
    opacity: 0;
    visibility: hidden;
}

.loading-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    color: var(--text-secondary);
    font-size: 0.85rem;
    letter-spacing: 0.05em;
}

.pulse-ring {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: 2px solid var(--accent);
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(0.9);
        opacity: 0.5;
    }
    50% {
        transform: scale(1.1);
        opacity: 1;
    }
}

.error-message {
    position: absolute;
    inset: 0;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    padding: 24px;
    text-align: center;
}

.error-message.visible {
    display: flex;
}

.error-message svg {
    opacity: 0.6;
}

.error-message p {
    font-size: 1rem;
    color: var(--text-primary);
}

.error-message small {
    font-size: 0.8rem;
    max-width: 200px;
    line-height: 1.5;
}

.footer {
    display: flex;
    gap: 12px;
}

.btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    transform: scale(1.05);
}

.btn:active {
    transform: scale(0.95);
}

.btn svg {
    transition: transform 0.3s ease;
}

.btn:hover svg {
    transform: rotate(180deg);
}

/* Responsive adjustments */
@media (max-height: 700px) {
    .container {
        gap: 16px;
        /* Same `max(...)` lift as the base rule, but with a 16px floor
         * because short screens need every pixel of vertical room.
         * Safe-area insets still take precedence on notched phones. */
        padding:
            max(16px, env(safe-area-inset-top))
            max(16px, env(safe-area-inset-right))
            max(16px, env(safe-area-inset-bottom))
            max(16px, env(safe-area-inset-left));
    }
    
    .header h1 {
        font-size: 1.4rem;
    }
    
    .viewport-wrapper {
        /* Tighter chrome budget (150px) reflects the smaller padding
         * + smaller header above. Safe-area insets are subtracted on
         * notched devices for the same reason as the base rule. */
        max-height: calc(100vh - 150px);
        max-height: calc(100dvh - 150px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    }
}

@media (max-width: 400px) {
    .viewport-wrapper {
        width: 90vw;
    }
}

.camera-wrap { position: relative; }

#landmarks-canvas, .guides-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  transform: scaleX(-1); /* Mirror to match video */
}

.guides-svg {
  z-index: 5;
  overflow: visible;
}

/* Glow layers styling */
#forehead-line-glow {
  /* Gradient stroke is set via JS */
}

/* Row that holds the primary START/STOP button and the secondary SKIP button.
 * Floats inside the camera viewport, anchored 24px above the bottom edge,
 * horizontally centered via `left: 50% + translateX(-50%)`.
 * `align-items: stretch` forces SKIP to take the full row height (= START's
 * natural 56px) without hand-tuning padding math against font-size.
 * `z-index: 16` keeps the row above the SVG guide overlay (5), the viewport
 * frame stroke (10), and the countdown / instruction pills injected by
 * app.js (15). */
.massage-controls {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: stretch;
    justify-content: center;
    gap: 12px;
    z-index: 16;
}

/* Instruction pill overrides — its base styles are written inline by
 * `createInstructionElement` in app.js (position, padding, background,
 * etc.), so any property we want to change from CSS needs `!important`
 * to beat inline-style specificity. Centering itself (`left: 50%` +
 * `transform: translateX(-50%)`) is already done inline by app.js, so
 * we don't need to redeclare it here.
 *
 *   - `bottom: 90px`       — floats just above the STOP/SKIP controls row
 *                            (controls footprint ≈ y 24..80; ~10px gap above)
 *   - `max-width: 90%`     — never wider than 90% of the camera viewport
 *   - `width: auto`        — shrink-to-fit when copy is short
 *   - `white-space: normal`— allow wrapping (overrides the inline `nowrap`
 *                            so long instruction copy doesn't escape the
 *                            viewport horizontally)
 *   - `font-size: 13px`    — slightly tighter than the previous 0.85rem so
 *                            more copy fits per line on narrow screens */
#massage-instruction {
    bottom: 90px !important;
    max-width: 90% !important;
    width: auto !important;
    white-space: normal !important;
    font-size: 13px !important;
}

/* Start Massage button — unified chip style shared with STOP + SKIP.
 * `padding: 12px 28px` + `font-size: 14px` give a button height of ~41px;
 * the controls row sits at `bottom: 24px` so the button's top lands at
 * ~bottom: 65px in the camera. With `.massage-hint` floating at
 * `bottom: 90px`, that leaves ~25px of clear space between the hint's
 * baseline and the button's top — they never overlap.
 * `white-space: nowrap` keeps "START MASSAGE" on a single line even when
 * the camera viewport is narrow. */
.start-massage-btn {
    align-self: center;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 28px;
    border: none;
    border-radius: 24px;
    background: #E8B8C2;
    color: #ffffff;
    font-family: 'Inter', -apple-system, sans-serif;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    white-space: nowrap;
    cursor: pointer;
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.35) inset,
        0 8px 20px -10px rgba(61, 35, 20, 0.28),
        0 0 0 1px rgba(61, 35, 20, 0.04);
    transition: transform 0.2s ease, box-shadow 0.25s ease, background-color 0.2s ease;
}

.start-massage-btn:hover {
    /* Softer dusty-rose shade of the resting `#E8B8C2` so the white text
     * (set on the base `.start-massage-btn` rule) stays legible on hover.
     * Previous value `#f5f5f5` predated the resting → pink switch and
     * left the white label invisible on a near-white hover background. */
    background: #DAA9B5;
    transform: translateY(-1px);
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.4) inset,
        0 12px 24px -10px rgba(61, 35, 20, 0.32),
        0 0 0 1px rgba(61, 35, 20, 0.05);
}

.start-massage-btn:active {
    transform: translateY(0);
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.3) inset,
        0 5px 12px -8px rgba(61, 35, 20, 0.26),
        0 0 0 1px rgba(61, 35, 20, 0.06);
}

.start-massage-btn:focus-visible {
    outline: 2px solid #3D2314;
    outline-offset: 3px;
}

/* STOP state — toggled via JS while a massage session is running.
 * Shares the unified chip geometry/typography with START + SKIP
 * (same `border-radius`, `padding`, `font-size`, `font-weight`,
 * `font-family` ⇒ same height in the flex row); only the colors flip.
 * `width: 45%` + matching value on `.skip-zone-btn` pair STOP and SKIP
 * as equal-width siblings inside `.massage-controls` for every zone.
 * `text-align: center` keeps the text centered if `display: inline-flex`
 * is ever overridden to a block-like layout. */
.start-massage-btn.is-active {
    background: #E8B8C2;
    color: #ffffff;
    border-radius: 24px;
    padding: 12px 28px;
    font-family: 'Inter', -apple-system, sans-serif;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0;
    width: 45%;
    text-align: center;
    /* Soft primary halo shared with the resting `.start-massage-btn`
     * so START → STOP feels like the same button at a different verb,
     * not two different design eras. Press feedback (scale + opacity)
     * still owns the interaction states; resting is where the system
     * coherence lives. */
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.35) inset,
        0 8px 20px -10px rgba(61, 35, 20, 0.28),
        0 0 0 1px rgba(61, 35, 20, 0.04);
    transition: transform 0.1s, opacity 0.1s, box-shadow 0.1s;
}

.start-massage-btn.is-active:hover {
    /* Re-assert the resting background so the `.start-massage-btn:hover`
     * rule (1-class specificity) doesn't repaint STOP to the START-hover
     * shade. Shadow drops to zero on press so the chip "settles into"
     * the surface instead of lifting toward the user — the tactile
     * counterpart to the resting halo. */
    background: #E8B8C2;
    transform: scale(0.96);
    opacity: 0.9;
    box-shadow: none;
}

.start-massage-btn.is-active:active {
    transform: scale(0.93);
    box-shadow: none;
}

.start-massage-btn.is-active:focus-visible {
    outline: none;
}

/* SKIP → button — secondary control, only present during an active session.
 * Hidden by default; `.is-visible` is added by JS in lockstep with the
 * START → STOP toggle, and removed again on stop / natural end-of-session. */
.skip-zone-btn {
    display: none;
    align-items: center;
    justify-content: center;
    padding: 12px 28px;
    border: none;
    border-radius: 24px;
    /* Warm linen / off-cream — pairs with the dusty rose primary
     * (same warm hue family) instead of the cold pure-white that
     * fought the new palette. `#3D2314` is the same warm brown the
     * primary shadow stacks already use, so dark text reads softer
     * than the previous near-black `#1a1a1a` and reinforces the
     * single-palette look. */
    background: #F4ECE4;
    color: #3D2314;
    font-family: 'Inter', -apple-system, sans-serif;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0;
    text-transform: uppercase;
    white-space: nowrap;
    width: 45%;
    text-align: center;
    cursor: pointer;
    /* Quieter halo than the primary STOP sibling so the secondary
     * action visually steps back without going flat. Same stack
     * shape as primary, ~half the outer-shadow opacity. */
    box-shadow:
        0 1px 0 rgba(255, 255, 255, 0.5) inset,
        0 6px 16px -10px rgba(61, 35, 20, 0.16),
        0 0 0 1px rgba(61, 35, 20, 0.05);
    transition: transform 0.1s, opacity 0.1s, box-shadow 0.1s;
}

.skip-zone-btn.is-visible {
    display: inline-flex;
}

.skip-zone-btn:hover {
    transform: scale(0.96);
    opacity: 0.9;
    box-shadow: none;
}

.skip-zone-btn:active {
    transform: scale(0.93);
    box-shadow: none;
}

.skip-zone-btn:focus-visible {
    outline: none;
}

/* Hint text directly under the Start Massage button */
/* Hint sits as an overlay inside `.viewport.camera-wrap` (which is
 * `position: relative`), floating 90px above the camera's bottom edge —
 * comfortably above the STOP/SKIP control row that lives at `bottom: 24px`.
 * `pointer-events: none` keeps it from blocking clicks on the START
 * button beneath it; `z-index: 16` matches the controls row so it stays
 * above the SVG guides (z 5) and viewport-frame (z 10). */
.massage-hint {
    position: absolute;
    bottom: 90px;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
    padding: 8px 16px;
    color: #1a1a1a;
    font-size: 13px;
    font-style: normal;
    text-align: center;
    background: rgba(255, 255, 255, 0.85);
    border-radius: 12px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 16;
}

/* Countdown timer — top-right of the mirror view */
.countdown-timer {
    position: absolute;
    top: 16px;
    right: 16px;
    min-width: 76px;
    padding: 10px 18px;
    background: rgba(10, 10, 11, 0.55);
    border-radius: 18px;
    color: #ffffff;
    font-family: 'Outfit', sans-serif;
    font-size: 2.25rem;
    font-weight: 600;
    line-height: 1;
    letter-spacing: 0.02em;
    text-align: center;
    font-variant-numeric: tabular-nums;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    pointer-events: none;
    user-select: none;
    z-index: 15;
    transition: opacity 0.2s ease, visibility 0.2s ease;
}

.countdown-timer.hidden {
    opacity: 0;
    visibility: hidden;
}

/* Mobile layout — screens narrower than 768px.
 *
 * `max-width: 767px` mirrors the `window.innerWidth < 768` check used
 * by `startCamera()` in app.js (which appends `zoom: 1` to the camera
 * constraints below the same threshold), so both the CSS layout and
 * the JS camera-side fixes flip in lockstep on the same breakpoint.
 *
 * Goals on phones:
 *   - cap the camera at 70vh so there's guaranteed space below for
 *     the START / STOP / SKIP row even on tall+narrow displays where
 *     the desktop's `aspect-ratio: 9/16` wrapper would otherwise eat
 *     nearly the whole viewport
 *   - pull all in-camera overlays (timer, controls row, instruction
 *     pill, hint pill) tighter against the camera edges to claw back
 *     vertical space that's scarce on mobile
 *   - shrink the chrome above the camera (logo + "MIRROR VIEW"
 *     tagline) so the camera itself dominates the visible layout
 *
 * Placed at the end of the file so the rules cascade over the
 * equivalent desktop declarations above (same selector specificity,
 * later-rule-wins). */
@media (max-width: 767px) {
    /* Camera container capped at 70vh. The pre-existing
     * `width: min(320px, 80vw)` (and the `width: 90vw` from the
     * `max-width: 400px` rule on very narrow phones) still drive
     * width; setting both width and height collapses the wrapper's
     * `aspect-ratio: 9/16` rule, but that's harmless because
     * `#webcam { object-fit: cover }` crops the live video to
     * whatever box we hand it.
     *
     * Two layered fixes:
     *   1. `70dvh` so the camera resizes with the iOS Safari URL
     *      bar (was `70vh`, which is the largest viewport — content
     *      below got clipped when the URL bar was visible).
     *   2. Subtract safe-area insets so notched phones don't push
     *      the footer behind the home indicator. Without this, on a
     *      notched iPhone the camera + chrome could exceed the
     *      visible area by 30-40px. */
    .viewport-wrapper {
        height: 70vh;
        height: calc(70dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    }

    /* Controls row (START in idle state, or STOP+SKIP during an
     * active session) hugs the bottom edge of the camera. Same
     * `.massage-controls` row holds all three buttons, so a single
     * `bottom` override covers START / STOP / SKIP at once. */
    .massage-controls {
        bottom: 16px;
    }

    /* Instruction pill — `!important` needed to beat the desktop
     * value (also `!important`) declared on `#massage-instruction`
     * above. Lands ~54px above the controls row so the pill and
     * the START / STOP button never overlap. */
    #massage-instruction {
        bottom: 70px !important;
    }

    /* Premium guidance card (zone 0 trial) — same mobile lift as
     * the regular instruction pill so both elements sit at the
     * same vertical anchor whether the trial card or the regular
     * pill is active. The card's larger height (~82px vs the
     * pill's ~32px) is fine here: card top lands at ~bottom: 152px
     * on mobile, well above the controls row at bottom: 16px and
     * below the typical jaw guide line range. `!important` beats
     * the inline `bottom: 90px` declared by `createPremiumInstructionElement`
     * in app.js, mirroring the pattern used for `#massage-instruction`. */
    #massage-premium-instruction {
        bottom: 70px !important;
    }

    /* Countdown moves slightly closer to the camera corner on
     * mobile, shaving 4px off both inset axes. */
    .countdown-timer {
        top: 12px;
        right: 12px;
    }

    /* Hint pill aligns with the new instruction pill bottom — the
     * two never share the screen (hint is shown only when idle,
     * instruction only during an active session), so matching
     * bottoms keep the "info pill" anchor visually consistent
     * across both states. */
    .massage-hint {
        bottom: 70px;
    }

    /* Header chrome — smaller logo + tagline free up vertical room
     * for the 70vh camera below. 20px is ~70% of the desktop 1.8rem
     * (≈28.8px); 10px is ~83% of the desktop 0.75rem (≈12px). */
    .header h1 {
        font-size: 20px;
    }

    .tagline {
        font-size: 10px;
    }
}
