/* ── DESIGN TOKENS ────────────────────────────────────────── */
:root {
    --green:        #00ff88;
    --green-dim:    #00b85c;
    --green-faint:  #003d1f;
    --green-glow:   rgba(0,255,136,0.18);
    --amber:        #ffb300;
    --red-alert:    #ff3355;
    --bg:           #040a06;
    --bg-panel:     #060d08;
    --bg-graph:     #020704;
    --border:       #0d2e16;
    --border-bright:#1a5c2e;
    --font-mono:    "Lucida Console", "Monaco", "Consolas", monospace;
    --font-display: 'VT323', monospace;
    --scanline-gap: 3px;
}

/* ── RESET ────────────────────────────────────────────────── */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

/* ── CRT EFFECTS ──────────────────────────────────────────── */
body {
    background: #000;
    color: var(--green);
    font-family: var(--font-mono);
    font-size: 12px;
    min-height: 100vh;
    overflow-x: hidden;
}

/* Scanlines overlay */
.scanlines {
    pointer-events: none;
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: repeating-linear-gradient(
        to bottom,
        transparent 0px,
        transparent calc(var(--scanline-gap) - 1px),
        rgba(0,0,0,0.25) calc(var(--scanline-gap) - 1px),
        rgba(0,0,0,0.25) var(--scanline-gap)
    );
}

/* Subtle vignette + flicker */
.crt-flicker {
    pointer-events: none;
    position: fixed;
    inset: 0;
    z-index: 9998;
    background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.55) 100%);
    animation: flicker 8s infinite;
}
@keyframes flicker {
    0%,100% { opacity: 1; }
    92%      { opacity: 1; }
    93%      { opacity: 0.85; }
    94%      { opacity: 1; }
    97%      { opacity: 0.92; }
    98%      { opacity: 1; }
}

/* ── OUTER FRAME ──────────────────────────────────────────── */
.frame {
    max-width: 1100px;
    margin: 0 auto;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--border-bright);
    border-right: 1px solid var(--border-bright);
    background: var(--bg);
    box-shadow:
        0 0 40px rgba(0,255,136,0.07),
        inset 0 0 80px rgba(0,0,0,0.4);
}

/* ── TOP BAR ──────────────────────────────────────────────── */
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    border-bottom: 1px solid var(--border-bright);
    background: var(--bg-panel);
    letter-spacing: 0.06em;
}
.brand {
    font-family: var(--font-display);
    font-size: 26px;
    color: var(--green);
    text-shadow: 0 0 12px var(--green), 0 0 24px rgba(0,255,136,0.4);
    letter-spacing: 0.04em;
}
.brand .accent { color: var(--amber); text-shadow: 0 0 10px var(--amber); }
.model {
    margin-left: 14px;
    font-size: 10px;
    color: var(--green-dim);
    letter-spacing: 0.12em;
    vertical-align: middle;
}
.topbar-right {
    display: flex;
    align-items: center;
    gap: 14px;
    font-size: 11px;
    color: var(--green-dim);
    letter-spacing: 0.1em;
}
.status-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: var(--green-dim);
    box-shadow: 0 0 6px var(--green-dim);
    transition: background 0.3s, box-shadow 0.3s;
}
.status-dot.active {
    background: var(--green);
    box-shadow: 0 0 10px var(--green), 0 0 20px rgba(0,255,136,0.5);
    animation: pulse-dot 1.2s ease-in-out infinite;
}
@keyframes pulse-dot {
    0%,100% { opacity: 1; }
    50%      { opacity: 0.4; }
}
.status-label { color: var(--green); letter-spacing: 0.14em; }
.clock {
    font-family: var(--font-display);
    font-size: 20px;
    color: var(--amber);
    text-shadow: 0 0 8px var(--amber);
    letter-spacing: 0.08em;
}

/* ── MAIN LAYOUT ──────────────────────────────────────────── */
.main {
    display: flex;
    flex: 1;
    gap: 0;
}

/* ── PANELS ───────────────────────────────────────────────── */
.panel {
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.panel-left  {
    width: 320px;
    min-width: 280px;
    border-right: 1px solid var(--border-bright);
    background: var(--bg-panel);
}
.panel-right {
    flex: 1;
    background: var(--bg);
}
.panel-label {
    font-size: 10px;
    letter-spacing: 0.15em;
    color: var(--green-dim);
    border-bottom: 1px solid var(--border);
    padding-bottom: 6px;
}

/* ── CAMERA ───────────────────────────────────────────────── */

/* On desktop this wrapper is invisible — children lay out as normal column items */
.mobile-top-row {
    display: contents;
}
.camera-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    background: #000;
    border: 1px solid var(--border-bright);
    overflow: hidden;
}
#video {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.0; /* hidden — we render via pixelCanvas */
}
#pixelCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}
.camera-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
}
.crosshair-h, .crosshair-v {
    position: absolute;
    background: rgba(0,255,136,0.15);
}
.crosshair-h { top: 50%; left: 0; right: 0; height: 1px; transform: translateY(-50%); }
.crosshair-v { left: 50%; top: 0; bottom: 0; width: 1px; transform: translateX(-50%); }
.corner {
    position: absolute;
    width: 14px; height: 14px;
    border-color: var(--green);
    border-style: solid;
    opacity: 0.7;
}
.corner.tl { top: 6px;    left: 6px;   border-width: 1px 0 0 1px; }
.corner.tr { top: 6px;    right: 6px;  border-width: 1px 1px 0 0; }
.corner.bl { bottom: 6px; left: 6px;   border-width: 0 0 1px 1px; }
.corner.br { bottom: 6px; right: 6px;  border-width: 0 1px 1px 0; }
.camera-label {
    position: absolute;
    bottom: 6px; left: 8px;
    font-size: 9px;
    letter-spacing: 0.14em;
    color: rgba(0,255,136,0.5);
}

/* ── BPM BLOCK ────────────────────────────────────────────── */
.bpm-block {
    border: 1px solid var(--border-bright);
    padding: 10px 12px 8px;
    background: var(--bg-graph);
}
.bpm-label {
    font-size: 9px;
    letter-spacing: 0.18em;
    color: var(--green-dim);
    margin-bottom: 4px;
}
.bpm-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
}
.bpm-value {
    font-family: var(--font-display);
    font-size: 72px;
    line-height: 1;
    color: var(--green);
    text-shadow: 0 0 16px var(--green), 0 0 40px rgba(0,255,136,0.35);
    min-width: 3ch;
    letter-spacing: -0.02em;
}
.bpm-value.alert { color: var(--red-alert); text-shadow: 0 0 16px var(--red-alert), 0 0 40px rgba(255,51,85,0.35); }
.bpm-unit {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    font-size: 11px;
    color: var(--green-dim);
    letter-spacing: 0.1em;
}
.heart-icon {
    font-size: 18px;
    color: var(--red-alert);
    text-shadow: 0 0 8px var(--red-alert);
    animation: heartbeat 1s ease-in-out infinite;
    animation-play-state: paused;
}
.heart-icon.beating { animation-play-state: running; }
@keyframes heartbeat {
    0%,100% { transform: scale(1);    opacity: 1; }
    15%      { transform: scale(1.35); opacity: 1; }
    30%      { transform: scale(1);    opacity: 0.7; }
    45%      { transform: scale(1.15); opacity: 1; }
    60%      { transform: scale(1); }
}

/* BPM bar */
.bpm-bar-wrap {
    margin-top: 8px;
    height: 5px;
    background: var(--green-faint);
    border: 1px solid var(--border);
    position: relative;
    overflow: hidden;
}
.bpm-bar {
    height: 100%;
    width: 0%;
    background: var(--green);
    box-shadow: 0 0 8px var(--green);
    transition: width 0.6s ease;
}
.bpm-scale {
    display: flex;
    justify-content: space-between;
    font-size: 8px;
    color: var(--green-faint);
    margin-top: 2px;
    letter-spacing: 0.08em;
}

/* ── CONTROLS ─────────────────────────────────────────────── */
.controls {
    display: flex;
    gap: 8px;
}
.btn {
    flex: 1;
    padding: 9px 6px;
    font-family: var(--font-mono);
    font-size: 11px;
    letter-spacing: 0.12em;
    border: 1px solid var(--border-bright);
    background: var(--bg-panel);
    color: var(--green-dim);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    transition: all 0.15s;
    text-transform: uppercase;
}
.btn:hover:not(:disabled) {
    background: var(--green-faint);
    color: var(--green);
    border-color: var(--green);
    box-shadow: 0 0 10px var(--green-glow), inset 0 0 8px rgba(0,255,136,0.06);
}
.btn:active:not(:disabled) { background: rgba(0,255,136,0.12); }
.btn:disabled { opacity: 0.3; cursor: not-allowed; }
.btn-start:hover:not(:disabled) { color: var(--green); border-color: var(--green); }
.btn-stop:hover:not(:disabled)  { color: var(--red-alert); border-color: var(--red-alert); box-shadow: 0 0 10px rgba(255,51,85,0.2); }
.btn-torch:hover:not(:disabled) { color: var(--amber); border-color: var(--amber); box-shadow: 0 0 10px rgba(255,179,0,0.2); }

/* ── INSTRUCTIONS ─────────────────────────────────────────── */
.instructions {
    border: 1px solid var(--border);
    padding: 10px 12px;
    background: var(--bg-graph);
}
.inst-title {
    font-size: 9px;
    letter-spacing: 0.18em;
    color: var(--green-dim);
    margin-bottom: 6px;
}
.inst-line {
    font-size: 10px;
    color: rgba(0,255,136,0.45);
    letter-spacing: 0.06em;
    line-height: 1.8;
}

/* ── GRAPH BLOCKS ─────────────────────────────────────────── */
.graph-block {
    border: 1px solid var(--border-bright);
    background: var(--bg-graph);
    display: flex;
    flex-direction: column;
}
.graph-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 10px;
    border-bottom: 1px solid var(--border);
    font-size: 10px;
    letter-spacing: 0.12em;
}
.graph-title { color: var(--green-dim); }
.graph-tag   { color: rgba(0,255,136,0.3); font-size: 9px; }
.graph-wrap {
    position: relative;
    width: 100%;
    height: 180px;
    padding: 6px 4px;
}
.graph-wrap canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* ── STATS ROW ────────────────────────────────────────────── */
.stats-row {
    display: flex;
    gap: 0;
    border: 1px solid var(--border-bright);
    background: var(--bg-panel);
}
.stat-block {
    flex: 1;
    padding: 10px 0 8px;
    text-align: center;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.stat-block:last-child { border-right: none; }
.stat-label {
    font-size: 8px;
    letter-spacing: 0.16em;
    color: var(--green-dim);
}
.stat-value {
    font-family: var(--font-display);
    font-size: 30px;
    color: var(--green);
    text-shadow: 0 0 10px var(--green);
    line-height: 1;
}
.stat-unit {
    font-size: 8px;
    color: rgba(0,255,136,0.3);
    letter-spacing: 0.12em;
}

/* ── BOTTOM BAR ───────────────────────────────────────────── */
.bottombar {
    display: flex;
    justify-content: space-between;
    padding: 5px 16px;
    border-top: 1px solid var(--border-bright);
    background: var(--bg-panel);
    font-size: 9px;
    letter-spacing: 0.14em;
    color: rgba(0,255,136,0.3);
}

/* ── RESPONSIVE ───────────────────────────────────────────── */
@media (max-width: 700px) {

    /* Lock the whole app to the viewport — no page scroll */
    html, body { height: 100%; overflow: hidden; }
    .frame {
        height: 100dvh;
        min-height: unset;
        overflow: hidden;
        border-left: none;
        border-right: none;
    }

    /* Slim the top bar */
    .topbar { padding: 5px 10px; }
    .brand  { font-size: 20px; }
    .model  { display: none; }
    .topbar-right { gap: 8px; }
    .clock  { font-size: 16px; }

    /* Stack layout */
    .main {
        flex-direction: column;
        flex: 1;
        overflow: hidden;
        min-height: 0;
    }

    /* ── COMPACT TOP STRIP ──────────────────────────── */
    /* Camera + BPM side by side in one short row        */
    .panel-left {
        width: 100%;
        min-width: unset;
        border-right: none;
        border-bottom: 1px solid var(--border-bright);
        padding: 8px;
        gap: 8px;
        /* Prevent this panel from growing — fixed compact height */
        flex: 0 0 auto;
    }

    /* Hide the verbose label on mobile */
    .panel-left > .panel-label { display: none; }

    /* Camera + BPM side by side */
    .mobile-top-row {
        display: flex;
        gap: 8px;
        align-items: stretch;
    }
    .camera-wrap {
        width: 110px;
        min-width: 110px;
        aspect-ratio: 1;
        flex-shrink: 0;
    }

    .bpm-block {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 8px 10px;
    }

    .bpm-value { font-size: 58px; }

    /* Slim controls — single row, short buttons */
    .controls { gap: 6px; }
    .btn {
        padding: 7px 4px;
        font-size: 10px;
    }

    /* Hide instructions on mobile — they eat space and you know what to do */
    .instructions { display: none; }

    /* ── GRAPHS fill the remaining space ─────────────── */
    .panel-right {
        flex: 1;
        min-height: 0;
        padding: 6px 8px;
        display: flex;
        flex-direction: column;
        gap: 6px;
        overflow: hidden;
    }

    .graph-block {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }

    .graph-wrap {
        flex: 1;
        min-height: 0;
        height: unset; /* override the fixed 180px */
        padding: 4px;
    }

    /* Stats row — tighter */
    .stats-row { flex-shrink: 0; }
    .stat-block { padding: 5px 0 4px; }
    .stat-value { font-size: 22px; }
    .stat-label { font-size: 7px; }

    /* Bottom bar — hide on very small screens to save space */
    .bottombar { display: none; }
}

/* Styling the backdrop (the area behind the modal) */
.modal::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.modal {
  border: none;
  border-radius: 8px;
  padding: 2rem;
  max-width: 400px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
  align-items: center;
  text-align: center;
  background: var(--bg-panel);
  color: var(--green);

  /* 1. Reset any weird inherited styles */
  padding: 0;
  border: none;
  border-radius: 8px;

  /* 2. The 'Fixed Coordinate' centering */
  position: fixed;
  top: 20%;
  left: 50%;

  /* 3. Pull it back by exactly half its own size */
  transform: translate(-50%, -50%);

  /* 4. Ensure it doesn't touch the screen edges on mobile */
  width: 90%;
  max-width: 450px;
  margin: 0;
}

.modal-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  text-align: center;
  padding: 10px;
}

.btn:focus {
  outline: 3px solid var(--green-glow);
  outline-offset: 2px;
  box-shadow: 0 0 8px rgba(50, 205, 50, 0.5);
}

.source-link {
  color: var(--green-dim);
}

.source-link:focus {
  outline: 3px solid var(--green-glow);
  outline-offset: 2px;
  box-shadow: 0 0 8px rgba(50, 205, 50, 0.5);
}
