/* ================================================================
   "Karoke 123" — the karaoke Easter egg (US-002)
   ================================================================
   Markup: templates/fragments/karaoke.html. Behaviour: static/js/karaoke.js.

   The rules tanz67.css keeps, kept here for the same reasons:

   - EVERY animation sits inside @media (prefers-reduced-motion:
     no-preference). Without it the singer holds a still pose, mic up,
     notes visible. KaraokeWiringTest brace-matches the guard.
   - Only transform and opacity are animated, so the chat never janks.
   - Nothing flashes: the lights sweep once every 7 s and no colour
     changes faster than that (WCAG 2.3.1).
   - The song's progress bar is driven by the audio's own clock
     (--karaoke-progress, set on timeupdate), not by an animation, so it
     is right even when the song stalls or is muted.
   ================================================================ */

.karaoke {
    --karaoke-progress: 0;

    --karaoke-shirt:       #fbfcfe;
    --karaoke-shirt-edge:  #b9c6d6;
    --karaoke-trousers:    #262a31;
    --karaoke-skin:        #c58278;     /* sampled from the portrait's cheek */
    --karaoke-skin-shade:  #ac7067;     /* and from the neck */
    --karaoke-mic:         #2e333b;
    --karaoke-mic-grille:  #8a95a3;

    position: fixed;
    inset: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-width: none;
    max-height: none;
    margin: 0;
    border: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right)
             env(safe-area-inset-bottom) env(safe-area-inset-left);
    overflow: hidden;
    color: var(--text-primary);
    background:
        radial-gradient(ellipse at 50% 36%, rgba(255, 255, 255, 0.95) 0 22%, rgba(255, 255, 255, 0) 60%),
        linear-gradient(180deg, var(--bg-secondary), var(--bg-hover));
}

.karaoke[open] {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}

.karaoke::backdrop { background: transparent; }

/* ---- Stage lights: two soft beams, not a strobe ---- */
.karaoke-lights {
    position: absolute;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
}
.karaoke-lights i {
    position: absolute;
    top: -10%;
    width: 36vmax;
    height: 90vh;
    background: linear-gradient(180deg, rgba(31, 111, 191, 0.16), rgba(31, 111, 191, 0));
    clip-path: polygon(45% 0, 55% 0, 100% 100%, 0 100%);
    transform-origin: 50% 0;
}
.karaoke-lights i:nth-child(1) { left: 8%;  transform: rotate(-16deg); }
.karaoke-lights i:nth-child(2) { right: 8%; transform: rotate(16deg); }

/* ---- Close control ---- */
.karaoke-close {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 0.75rem);
    right: calc(env(safe-area-inset-right) + 0.75rem);
    width: 44px;
    height: 44px;
    font-size: var(--text-xl);
    background: var(--bg-primary);
    color: var(--text-primary);
    box-shadow: var(--shadow-md);
    z-index: 2;
}

/* ---- Stage ---- */
.karaoke-stage {
    position: relative;
    z-index: 1;
    display: grid;
    place-items: center;
    pointer-events: none;               /* a tap on the singer dismisses (karaoke.js) */
}

.karaoke-singer {
    display: block;
    height: min(46vh, 420px);
    height: min(46dvh, 420px);
    width: auto;
    aspect-ratio: 240 / 360;
    max-width: 80vw;
    overflow: visible;
}
.karaoke-singer[hidden],
.karaoke-video[hidden] { display: none; }

.karaoke-video {
    display: block;
    width: min(88vw, 560px);
    max-height: 46vh;
    max-height: 46dvh;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    background: #000000;
}

/* ---- Figure paint ---- */
.karaoke-shadow      { fill: rgba(24, 20, 14, 0.14); }
.karaoke-trousers    { fill: var(--karaoke-trousers); }
.karaoke-shoe        { fill: #0b0b0d; }
.karaoke-belt        { fill: #1a1c21; }
.karaoke-skin        { fill: var(--karaoke-skin); }
.karaoke-skin-shade  { fill: var(--karaoke-skin-shade); }
.karaoke-shirt {
    fill: var(--karaoke-shirt);
    stroke: var(--karaoke-shirt-edge);
    stroke-width: 1.5;
    stroke-linejoin: round;
}
.karaoke-placket     { fill: none; stroke: var(--karaoke-shirt-edge); stroke-width: 1.5; }
.karaoke-button      { fill: var(--karaoke-shirt-edge); }

/* A white sleeve on a light ground needs an edge: a wider grey stroke
   under a white one. */
.karaoke-sleeve-edge,
.karaoke-sleeve,
.karaoke-forearm,
.karaoke-roll,
.karaoke-mic-handle,
.karaoke-mic-grille {
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.karaoke-sleeve-edge { stroke: var(--karaoke-shirt-edge); stroke-width: 19; }
.karaoke-sleeve      { stroke: var(--karaoke-shirt); stroke-width: 16; }
.karaoke-roll        { stroke: var(--karaoke-shirt-edge); stroke-width: 4; }
.karaoke-forearm     { stroke: var(--karaoke-skin); stroke-width: 12; }
.karaoke-mic-handle  { stroke: var(--karaoke-mic); stroke-width: 7; }
.karaoke-mic-head    { fill: var(--karaoke-mic); }
.karaoke-mic-grille  { stroke: var(--karaoke-mic-grille); stroke-width: 1.2; }

.karaoke-head-rim {
    fill: none;
    stroke: #ffffff;
    stroke-width: 3;
}

.karaoke-note {
    font-size: 26px;
    font-weight: 700;
    fill: var(--biru);
    stroke: #ffffff;
    stroke-width: 4px;
    stroke-linejoin: round;
    paint-order: stroke;
}

/* ---- Pivots, in viewBox units ---- */
.karaoke-upper      { transform-origin: 120px 250px; }
.karaoke-head       { transform-origin: 120px 130px; }
.karaoke-arm--open  { transform-origin: 80px 154px; }
.karaoke-arm--mic   { transform-origin: 160px 154px; }
.karaoke-shadow     { transform-origin: 120px 344px; }

/* ---- Card: karaoke screen, status, label, actions ---- */
.karaoke-card {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.6rem;
    width: min(92vw, 30rem);
    padding: 0.85rem 1.2rem 1rem;
    text-align: center;
    background: var(--bg-primary);
    border: 1px solid var(--surface-border);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
}

.karaoke-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    width: 100%;
    padding: 0.7rem 0.9rem 0.8rem;
    border-radius: var(--radius-lg);
    background: var(--biru-deep);
    color: #ffffff;                      /* 13.0:1 on --biru-deep */
}

.karaoke-kicker {
    font-size: var(--text-sm);
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--langit-soft);           /* 8.9:1 on --biru-deep */
}

.karaoke-title {
    font-family: var(--font-display);
    font-size: var(--display-md);
    font-weight: 700;
    line-height: 1.15;
    color: #ffffff;
}

/* The "lyrics": eight abstract bars and a ball that hops along them. */
.karaoke-bars {
    position: relative;
    display: flex;
    gap: 6px;
    width: 100%;
    max-width: 18rem;
    padding-top: 16px;
}
.karaoke-bars i {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.35);
}
.karaoke-bars i:nth-child(odd) { flex: 1.6; }
.karaoke-ball-track {                  /* as wide as the bars: its own 100% is the run */
    position: absolute;
    inset: 0 0 auto 0;
    height: 12px;
}
.karaoke-ball {
    position: absolute;
    top: 0;
    left: 0;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ffffff;
}

.karaoke-meter {
    width: 100%;
    max-width: 18rem;
    height: 3px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.2);
    overflow: hidden;
}
.karaoke-meter span {
    display: block;
    height: 100%;
    background: var(--langit-soft);
    transform-origin: left center;
    transform: scaleX(var(--karaoke-progress));
}

.karaoke-credit {
    font-size: var(--text-xs);
    line-height: 1.4;
    color: var(--langit-soft);
}

.karaoke-status p {
    font-size: var(--text-sm);
    color: var(--text-secondary);
}
.karaoke-retry {
    margin-top: 0.35rem;
    min-height: 44px;
    padding: 0.5rem 1rem;
    font-size: var(--text-base);
}
.karaoke-retry[hidden] { display: none; }

.karaoke-label {
    font-size: var(--text-sm);
    color: var(--text-secondary);
}

.karaoke-actions {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.karaoke-mute {
    width: 44px;
    height: 44px;
    font-size: var(--text-lg);
    border: 1px solid var(--surface-border);
}
.karaoke-mute-off                          { display: none; }
.karaoke-mute[aria-pressed="true"] .karaoke-mute-on  { display: none; }
.karaoke-mute[aria-pressed="true"] .karaoke-mute-off { display: inline; }
/* Without a song there is nothing to mute. */
.karaoke:not(.has-music) .karaoke-mute { display: none; }

.karaoke-back {
    font-size: var(--text-md);
    padding: 0.65rem 1.2rem;
    min-height: 44px;
}

/* ---- Short landscape screens: side by side ---- */
@media (max-height: 520px) and (orientation: landscape) {
    .karaoke[open] { flex-direction: row; gap: 1.5rem; }
    .karaoke-singer { height: min(78vh, 360px); height: min(78dvh, 360px); }
    .karaoke-title  { font-size: var(--display-sm); }
    .karaoke-card   { width: min(56vw, 26rem); padding: 0.6rem 1rem 0.75rem; gap: 0.45rem; }
}

/* ================================================================
   Motion — all of it, and only here
   ================================================================
   One sway is 1.2 s. Every keyframe set starts and ends on the same
   frame, so the loops have no seam. */
@media (prefers-reduced-motion: no-preference) {
    .karaoke.is-playing .karaoke-upper {
        animation: karaoke-sway 1.2s ease-in-out infinite;
    }
    .karaoke.is-playing .karaoke-head {
        animation: karaoke-nod 1.2s ease-in-out infinite;
    }
    .karaoke.is-playing .karaoke-arm--open {
        animation: karaoke-wave 2.4s ease-in-out infinite;
    }
    .karaoke.is-playing .karaoke-shadow {
        animation: karaoke-shadow 1.2s ease-in-out infinite;
    }
    .karaoke.is-playing .karaoke-note {
        animation: karaoke-rise 2.4s ease-out infinite;
    }
    .karaoke.is-playing .karaoke-note--2 { animation-delay: -0.8s; }
    .karaoke.is-playing .karaoke-note--3 { animation-delay: -1.6s; }
    .karaoke.is-playing .karaoke-ball-track {
        animation: karaoke-hop-x 4.8s linear infinite;
    }
    .karaoke.is-playing .karaoke-ball {
        animation: karaoke-hop-y 0.6s ease-in-out infinite;
    }
    .karaoke.is-playing .karaoke-lights i:nth-child(1) {
        animation: karaoke-beam-left 7s ease-in-out infinite;
    }
    .karaoke.is-playing .karaoke-lights i:nth-child(2) {
        animation: karaoke-beam-right 7s ease-in-out infinite;
    }
}

@keyframes karaoke-sway {
    0%, 100% { transform: rotate(3deg); }
    50%      { transform: rotate(-3deg); }
}
@keyframes karaoke-nod {
    0%, 100% { transform: rotate(-4deg); }
    50%      { transform: rotate(4deg); }
}
@keyframes karaoke-wave {
    0%, 100% { transform: rotate(0deg); }
    50%      { transform: rotate(18deg); }
}
@keyframes karaoke-shadow {
    0%, 100% { transform: scaleX(1); }
    50%      { transform: scaleX(0.94); }
}
@keyframes karaoke-rise {
    0%   { transform: translate(0, 10px); opacity: 0; }
    20%  { opacity: 1; }
    100% { transform: translate(14px, -34px); opacity: 0; }
}
@keyframes karaoke-hop-x {
    0%   { transform: translateX(0); }
    100% { transform: translateX(calc(100% - 10px)); }
}
@keyframes karaoke-hop-y {
    0%, 100% { transform: translateY(8px); }
    50%      { transform: translateY(-2px); }
}
@keyframes karaoke-beam-left {
    0%, 100% { transform: rotate(-16deg); }
    50%      { transform: rotate(6deg); }
}
@keyframes karaoke-beam-right {
    0%, 100% { transform: rotate(16deg); }
    50%      { transform: rotate(-6deg); }
}
