/* ============================================================
   Drop of Life — hand motion control
   The camera preview panel, the dwell cursor and the calibration
   overlay. Loaded only by pages that offer camera control.
   ============================================================ */

@layer components {
  /* ---------- preview panel ------------------------------- */

  .handpanel {
    position: fixed;
    inset-inline-end: 16px;
    inset-block-end: 16px;
    z-index: 70;
    width: 200px;
    display: grid;
    gap: 8px;
    padding: 10px;
    border-radius: var(--r-md);
    border: 1px solid var(--stroke-2);
    /* Opaque on purpose: a backdrop-filter here sits over a canvas that
       repaints every frame, so the blur would be recomputed 60×/second. */
    background: var(--ink-900);
    box-shadow: var(--sh-md);
    contain: layout paint;
    animation: hand-in var(--t-med) var(--e-out) both;
  }

  @keyframes hand-in {
    from { opacity: 0; transform: translateY(12px) scale(0.96); }
  }

  .handpanel[hidden] { display: none; }

  .handpanel__view {
    position: relative;
    border-radius: var(--r-sm);
    overflow: hidden;
    background: #000;
    aspect-ratio: 4 / 3;
  }

  .handpanel__cam {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
    filter: saturate(0.85) contrast(1.05);
  }

  .handpanel__badge {
    position: absolute;
    inset-block-start: 6px;
    inset-inline-start: 6px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 7px;
    border-radius: var(--r-pill);
    background: rgba(0, 0, 0, 0.55);
    color: var(--mint-400);
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .handpanel.is-lost .handpanel__view { outline: 1.5px solid var(--blood-400); outline-offset: -1.5px; }
  .handpanel.is-lost .handpanel__badge { color: var(--blood-300); }

  .handpanel__chips {
    display: flex;
    gap: 5px;
    justify-content: center;
  }

  .handchip {
    flex: 1;
    display: grid;
    place-items: center;
    height: 26px;
    border-radius: var(--r-xs);
    border: 1px solid var(--stroke-1);
    background: var(--surface-0);
    color: var(--ink-300);
    font-size: 12px;
    transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast);
  }

  .handchip.is-hot {
    background: color-mix(in oklab, var(--mint-400) 22%, transparent);
    border-color: var(--mint-400);
    color: var(--mint-400);
  }

  .handchip--hold { opacity: 0.4; }
  .handchip--hold.is-armed { opacity: 1; border-color: var(--stroke-2); color: var(--plasma); }

  .handpanel__status {
    text-align: center;
    font-size: 11px;
    line-height: 1.35;
    color: var(--ink-300);
    min-height: 2.4em;
  }

  .handpanel.is-lost .handpanel__status { color: var(--blood-300); }

  .handpanel__actions {
    display: flex;
    gap: 6px;
  }

  .handpanel__actions .btn {
    flex: 1;
    padding-inline: 6px;
    font-size: 11px;
    justify-content: center;
  }

  /* Out of the way on small screens — the game itself needs the room. */
  @media (max-width: 720px) {
    .handpanel { width: 140px; inset-inline-end: 8px; inset-block-end: 8px; }
    .handpanel__actions .btn span { display: none; }
    .handpanel__status { font-size: 10px; }
  }

  /* ---------- dwell cursor -------------------------------- */

  .handcursor {
    position: fixed;
    inset-block-start: 0;
    inset-inline-start: 0;
    z-index: 90;
    width: 48px;
    height: 48px;
    margin: -24px 0 0 -24px;
    pointer-events: none;
    will-change: transform;
    transition: opacity var(--t-fast);
  }

  .handcursor[hidden] { display: none; }

  .handcursor svg {
    position: absolute;
    inset: 0;
    transform: rotate(-90deg);
    overflow: visible;
  }

  .handcursor__bg {
    fill: rgba(12, 18, 32, 0.45);
    stroke: rgba(255, 255, 255, 0.35);
    stroke-width: 2;
  }

  .handcursor__fg {
    fill: none;
    stroke: var(--mint-400);
    stroke-width: 4;
    stroke-linecap: round;
    opacity: 0;
    filter: drop-shadow(0 0 6px var(--mint-glow));
    transition: opacity var(--t-fast);
  }

  .handcursor.is-dwelling .handcursor__fg { opacity: 1; }

  /* Armed: a target is under the cursor and waiting for a fist. Deliberately
     static — a countdown would imply it is about to fire on its own. */
  .handcursor.is-armed .handcursor__bg {
    stroke: var(--mint-400);
    stroke-width: 3;
    fill: rgba(52, 233, 176, 0.14);
  }

  .handcursor.is-armed .handcursor__dot { background: var(--mint-400); }

  .handcursor__dot {
    position: absolute;
    inset-block-start: 50%;
    inset-inline-start: 50%;
    width: 10px;
    height: 10px;
    margin: -5px 0 0 -5px;
    border-radius: 50%;
    background: var(--cream);
    box-shadow: 0 0 12px rgba(255, 255, 255, 0.6);
  }

  .handcursor.is-firing { animation: hand-fire 260ms var(--e-out); }

  @keyframes hand-fire {
    0% { transform-origin: center; }
    40% { filter: brightness(1.8); }
  }

  /* Anything the hand is resting on lifts, so the target is never in doubt. */
  .is-handhover {
    outline: 2px solid var(--mint-400) !important;
    outline-offset: 2px;
    box-shadow: var(--glow-mint) !important;
  }

  /* ---------- calibration overlay ------------------------- */

  .handcal {
    position: fixed;
    inset: 0;
    z-index: 120;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(7, 11, 18, 0.82);
    backdrop-filter: blur(10px);
    animation: hand-fade var(--t-med) var(--e-out) both;
  }

  @keyframes hand-fade { from { opacity: 0; } }

  .handcal__box {
    width: min(420px, 100%);
    display: grid;
    gap: 14px;
    padding: 22px;
    border-radius: var(--r-lg);
    border: 1px solid var(--stroke-2);
    background: var(--ink-900);
    box-shadow: var(--sh-lg);
    text-align: center;
  }

  .handcal__title {
    margin: 0;
    font-family: var(--font-display);
    font-size: 22px;
  }

  .handcal__view {
    position: relative;
    border-radius: var(--r-md);
    overflow: hidden;
    background: #000;
    aspect-ratio: 4 / 3;
  }

  .handcal__cam {
    width: 100%;
    height: 100%;
    display: block;
  }

  .handcal__target {
    position: absolute;
    display: grid;
    place-items: center;
    border: 2px dashed var(--mint-400);
    border-radius: var(--r-sm);
    background: color-mix(in oklab, var(--mint-400) 10%, transparent);
    font-size: 30px;
    opacity: 0.9;
  }

  .handcal.is-sampling .handcal__target {
    border-style: solid;
    animation: hand-pulse 900ms var(--e-in-out) infinite;
  }

  .handcal.is-failed .handcal__target {
    border-color: var(--blood-400);
    background: color-mix(in oklab, var(--blood-400) 12%, transparent);
  }

  @keyframes hand-pulse {
    50% { box-shadow: 0 0 0 8px color-mix(in oklab, var(--mint-400) 18%, transparent); }
  }

  .handcal__hint {
    margin: 0;
    font-size: 13px;
    line-height: 1.5;
    min-height: 3.6em;
  }

  .handcal.is-failed .handcal__hint { color: var(--blood-300); }

  .handcal__bar {
    height: 6px;
    border-radius: var(--r-pill);
    background: var(--surface-1);
    overflow: hidden;
  }

  .handcal__barfill {
    display: block;
    height: 100%;
    width: 0;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--mint-500), var(--mint-400));
  }

  /* Indeterminate: the model load has no meaningful progress to report. */
  .handcal__barfill.is-indeterminate {
    width: 40%;
    animation: hand-sweep 1.1s var(--e-in-out) infinite;
  }

  @keyframes hand-sweep {
    0% { transform: translateX(-110%); }
    100% { transform: translateX(260%); }
  }

  .handcal__actions {
    display: flex;
    gap: 8px;
    justify-content: center;
  }

  .handcal__retry { display: none; }
  .handcal.is-failed .handcal__retry { display: inline-flex; }

  /* ---------- station hold indicator ---------------------- */

  /* The station prompt doubles as the check-in progress meter: it fills as the
     player holds their hand still, so the wait is never a mystery. */
  .prompt.is-holding {
    --hold: 0;
    position: absolute;
    overflow: hidden;
    isolation: isolate;
    animation: none;
  }

  .prompt.is-holding::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: -1;
    transform-origin: left center;
    scale: var(--hold) 1;
    background: color-mix(in oklab, var(--mint-400) 42%, transparent);
  }

  :root[dir="rtl"] .prompt.is-holding::before { transform-origin: right center; }

  /* ---------- start-screen toggle ------------------------- */

  .handtoggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
  }

  .handtoggle.is-on {
    border-color: var(--mint-400);
    color: var(--mint-400);
  }
}

@layer motion {
  @media (prefers-reduced-motion: reduce) {
    .handpanel,
    .handcal { animation: none; }
    .handcal.is-sampling .handcal__target { animation: none; }
  }
}
