/* ================================================================
   HOLD STILL — clinical monitor styles
   1990s hospital monitoring equipment + paper intake form
   ================================================================ */

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

img { display: block; max-width: 100%; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

/* ---------- Tokens ---------- */
:root {
  --green:  #C9E5C7;
  --cyan:   #7ED4D6;
  --red:    #E63946;
  --amber:  #F4B942;
  --paper:  #F8F5EE;
  --ink:    #1A1A1A;

  --font-mono:  'VT323', 'IBM Plex Mono', 'Courier New', monospace;
  --font-label: 'Helvetica Neue', 'Arial', sans-serif;

  --grid-major: rgba(26, 26, 26, 0.07);
  --grid-minor: rgba(26, 26, 26, 0.03);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Base ---------- */
html, body {
  height: 100%;
}

body {
  min-height: 100dvh;
  font-family: var(--font-label);
  font-size: 16px;
  color: var(--ink);
  background-color: var(--paper);
  /* EKG chart paper grid */
  background-image:
    repeating-linear-gradient(0deg,  transparent 0, transparent 19px, var(--grid-major) 19px, var(--grid-major) 20px),
    repeating-linear-gradient(90deg, transparent 0, transparent 19px, var(--grid-major) 19px, var(--grid-major) 20px),
    repeating-linear-gradient(0deg,  transparent 0, transparent  4px, var(--grid-minor)  4px, var(--grid-minor)  5px),
    repeating-linear-gradient(90deg, transparent 0, transparent  4px, var(--grid-minor)  4px, var(--grid-minor)  5px);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}

/* CRT scanlines — sit above content but below modals/landscape warning */
body::after {
  content: '';
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 9997;
  background-image: repeating-linear-gradient(
    0deg,
    rgba(0, 0, 0, 0.04) 0px, rgba(0, 0, 0, 0.04) 1px,
    transparent 1px, transparent 3px
  );
}

/* Lock body scroll only while testing */
body[data-state="testing"] {
  overflow: hidden;
}

.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.display-large {
  font-family: var(--font-mono);
  font-size: clamp(96px, 28vw, 160px);
  line-height: 1;
}

/* ================================================================
   STATE-DRIVEN VISIBILITY
   ================================================================ */
#screen-idle,
#screen-unsupported,
#screen-permission-denied,
#screen-calibrating,
#screen-testing,
#overlay-pass,
#overlay-fail,
#screen-complete {
  display: none;
}

body[data-state="idle"]              #screen-idle              { display: flex; }
body[data-state="unsupported"]       #screen-unsupported       { display: flex; }
body[data-state="permission-denied"] #screen-permission-denied { display: flex; }
body[data-state="calibrating"]       #screen-calibrating       { display: flex; }
body[data-state="testing"]           #screen-testing           { display: flex; }
body[data-state="pass"]              #screen-testing           { display: flex; }
body[data-state="pass"]              #overlay-pass             { display: flex; }
body[data-state="fail"]              #screen-testing           { display: flex; }
body[data-state="fail"]              #overlay-fail             { display: flex; }
body[data-state="complete"]          #screen-complete          { display: flex; }

/* Hide disclaimer when space is tight */
body[data-state="calibrating"] #disclaimer,
body[data-state="testing"]     #disclaimer,
body[data-state="pass"]        #disclaimer,
body[data-state="fail"]        #disclaimer { display: none; }

/* ================================================================
   PERSISTENT CANVASES
   ================================================================ */
#ekg-canvas {
  position: fixed;
  top: 0; left: 0; right: 0;
  width: 100%;
  height: 80px;
  z-index: 10;
  display: block;
}
body:not([data-state="testing"]):not([data-state="pass"]):not([data-state="fail"]) #ekg-canvas {
  opacity: 0;
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 50;
  pointer-events: none;
  display: block;
  opacity: 0;
}
#confetti-canvas.confetti-active { opacity: 1; }

/* ================================================================
   IDLE / INTAKE FORM
   The whole screen is meant to read as a clinical paper intake form.
   ================================================================ */
#screen-idle {
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  min-height: 100dvh;
  padding: clamp(20px, 6vw, 40px) clamp(20px, 6vw, 36px) calc(clamp(20px, 6vw, 40px) + var(--safe-bottom));
  padding-top: calc(clamp(20px, 6vw, 40px) + var(--safe-top));
  gap: clamp(18px, 4vw, 28px);
  position: relative;
  z-index: 1;
}

#intake-header {
  border-bottom: 2px solid var(--ink);
  padding-bottom: clamp(12px, 3vw, 18px);
}

#intake-logo {
  font-family: var(--font-label);
  font-weight: 700;
  font-size: clamp(32px, 9vw, 56px);
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--ink);
  /* compensate for letter-spacing pushing right edge past container */
  margin-right: -0.3em;
  line-height: 1;
}

#intake-subtitle {
  margin-top: clamp(6px, 2vw, 10px);
  font-family: var(--font-mono);
  font-size: clamp(13px, 3.6vw, 18px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.75;
}

#intake-form {
  display: flex;
  flex-direction: column;
  gap: 0;
  border-top: 1px solid var(--ink);
  border-bottom: 1px solid var(--ink);
}

.intake-field {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: clamp(10px, 2.6vw, 14px) 0;
  border-bottom: 1px dashed rgba(26, 26, 26, 0.35);
}
.intake-field:last-child { border-bottom: none; }

.intake-label {
  font-family: var(--font-label);
  font-size: clamp(10px, 2.6vw, 12px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.65;
  flex: 0 0 auto;
}

.intake-value {
  font-family: var(--font-mono);
  font-size: clamp(16px, 4.6vw, 22px);
  color: var(--ink);
  text-align: right;
  letter-spacing: 0.04em;
  word-break: break-all;
}

#intake-instructions {
  font-family: var(--font-label);
  font-size: clamp(11px, 3vw, 13px);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.75;
  line-height: 1.7;
  text-align: center;
  margin-top: clamp(6px, 2vw, 10px);
}

#screen-idle #btn-begin {
  align-self: center;
  margin-top: clamp(8px, 2vw, 14px);
  min-width: clamp(220px, 70vw, 320px);
}

#interruption-banner {
  visibility: hidden;
  margin-top: 8px;
  align-self: center;
  background: var(--ink);
  color: var(--amber);
  font-family: var(--font-mono);
  font-size: 14px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 8px 14px;
  border: 2px solid var(--amber);
}
#interruption-banner.visible {
  visibility: visible;
}

/* ================================================================
   UNSUPPORTED SCREEN
   ================================================================ */
#screen-unsupported {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 40px 24px;
  gap: 24px;
  text-align: center;
  position: relative;
  z-index: 1;
}

#unsupported-heading {
  font-family: var(--font-label);
  font-weight: 700;
  font-size: clamp(22px, 5vw, 32px);
  letter-spacing: 0.2em;
  text-transform: uppercase;
  max-width: 640px;
}

#unsupported-sub {
  font-family: var(--font-mono);
  font-size: clamp(14px, 3vw, 18px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.75;
  max-width: 560px;
}

#qr-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px;
  background: var(--paper);
  border: 2px solid var(--ink);
}

#qr-image {
  width: 160px;
  height: 160px;
  background: var(--paper);
}

#qr-label {
  font-family: var(--font-label);
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  opacity: 0.7;
}

/* ================================================================
   PERMISSION DENIED
   ================================================================ */
#screen-permission-denied {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 40px 24px;
  gap: 24px;
  text-align: center;
  position: relative;
  z-index: 1;
}

#denied-message {
  font-family: var(--font-label);
  font-weight: 700;
  font-size: clamp(18px, 4.4vw, 24px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  line-height: 1.6;
  max-width: 560px;
}

#denied-hint {
  font-family: var(--font-mono);
  font-size: clamp(12px, 3vw, 15px);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.65;
  max-width: 480px;
}

/* ================================================================
   CALIBRATING — sparse, anticipatory
   ================================================================ */
#screen-calibrating {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 40px 24px;
  gap: clamp(12px, 3vw, 24px);
  text-align: center;
  position: relative;
  z-index: 1;
}

#calib-level-name {
  font-family: var(--font-mono);
  font-size: clamp(18px, 5vw, 26px);
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.85;
}

#calib-instruction {
  font-family: var(--font-label);
  font-size: clamp(12px, 3vw, 14px);
  letter-spacing: 0.2em;
  text-transform: uppercase;
  opacity: 0.7;
  line-height: 1.7;
}

#calib-countdown {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 1px rgba(26, 26, 26, 0.2);
}

#calib-status {
  font-family: var(--font-mono);
  font-size: clamp(11px, 2.8vw, 14px);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.6;
}

/* ================================================================
   TESTING — single dashboard view
   ================================================================ */
#screen-testing {
  flex-direction: column;
  justify-content: space-between;
  align-items: stretch;
  min-height: 100dvh;
  padding-top: 80px; /* under EKG strip */
  padding-bottom: 0;
  position: relative;
  z-index: 1;
}

#reticle-area {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(12px, 4vw, 24px);
  min-height: 0;
}

#reticle-canvas {
  display: block;
  aspect-ratio: 1;
  width: min(80vw, 80vh, 480px);
  height: auto;
  max-width: 80vw;
  max-height: calc(100dvh - 80px - 110px);
}

#vitals-panel {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  border-top: 2px solid var(--ink);
  background: var(--ink);
  padding: 0;
  gap: 1px;
  padding-bottom: var(--safe-bottom);
}

.vital-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: var(--paper);
  padding: 8px 2px 6px;
  min-height: 56px;
  text-align: center;
}

.vital-label {
  font-family: var(--font-label);
  font-size: 8px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.6;
  line-height: 1.2;
}

.vital-value {
  font-family: var(--font-mono);
  font-size: clamp(16px, 5vw, 28px);
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* ================================================================
   OVERLAY: PASS — green flash → semi-transparent green panel
   ================================================================ */
#overlay-pass {
  position: fixed;
  inset: 0;
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 3vw, 20px);
  padding: 32px 24px calc(32px + var(--safe-bottom));
  text-align: center;
  background: rgba(201, 229, 199, 0.92);
}

#overlay-pass.flash-green {
  animation: flash-green-anim 0.6s ease-out;
}

@keyframes flash-green-anim {
  0%   { background: rgba(201, 229, 199, 1); }
  30%  { background: rgba(201, 229, 199, 1); }
  100% { background: rgba(201, 229, 199, 0.92); }
}

#pass-heading {
  font-family: var(--font-label);
  font-weight: 700;
  font-size: clamp(24px, 7vw, 42px);
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink);
  margin-right: -0.2em;
}

#pass-level-name {
  color: var(--ink);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-align: center;
  word-break: break-word;
  font-size: clamp(40px, 12vw, 80px);
  line-height: 1.05;
  max-width: 92vw;
}

#pass-sub {
  font-family: var(--font-label);
  font-size: clamp(11px, 3vw, 14px);
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.75;
}

#pass-stabilization-note {
  font-family: var(--font-mono);
  font-size: clamp(11px, 2.8vw, 14px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.7;
  max-width: 440px;
  line-height: 1.5;
  min-height: 0;
}
#pass-stabilization-note:empty { display: none; }

/* ================================================================
   OVERLAY: FAIL — full-bleed red, meme, rotated stamp
   ================================================================ */
#overlay-fail {
  position: fixed;
  inset: 0;
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3.5vw, 24px);
  padding: 32px 24px calc(32px + var(--safe-bottom));
  text-align: center;
  background: var(--red);
}

#overlay-fail.flash-red {
  animation: flash-red-anim 0.4s steps(2, end);
}

@keyframes flash-red-anim {
  0%   { background: #ffffff; }
  50%  { background: var(--red); }
  100% { background: var(--red); }
}

#fail-meme-container {
  width: 240px;
  max-width: 70vw;
  max-height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.15);
  border: 2px solid var(--paper);
  padding: 8px;
  opacity: 0;
}

#overlay-fail.flash-red #fail-meme-container {
  animation: fail-meme-fade 0.4s ease-out 0.3s forwards;
}

@keyframes fail-meme-fade {
  from { opacity: 0; transform: scale(0.96); }
  to   { opacity: 1; transform: scale(1); }
}

#fail-meme-img {
  max-width: 100%;
  max-height: 50vh;
  width: auto;
  height: auto;
  object-fit: contain;
}

/* When meme src is empty, hide the container so the layout doesn't show an empty box */
#fail-meme-img[src=""],
#fail-meme-img:not([src]) { display: none; }
#fail-meme-container:has(#fail-meme-img:not([src])),
#fail-meme-container:has(#fail-meme-img[src=""]) {
  display: none;
}

#fail-stamp {
  font-family: var(--font-label);
  font-weight: 700;
  font-size: clamp(18px, 4.8vw, 40px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--paper);
  transform: rotate(-8deg);
  border: 4px solid var(--paper);
  padding: 8px 18px;
  margin-right: -0.12em;
  white-space: nowrap;
  max-width: 86vw;
  display: inline-block;
}

#fail-reason {
  font-family: var(--font-mono);
  font-size: clamp(13px, 3.4vw, 18px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--paper);
  opacity: 0.95;
  max-width: 90vw;
}

#fail-level-line {
  font-family: var(--font-mono);
  font-size: clamp(14px, 3.8vw, 20px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--paper);
  max-width: 90vw;
}
#fail-level-line #fail-level-name {
  color: var(--amber);
}

#overlay-fail #btn-restart,
#overlay-fail #btn-share-fail {
  background: var(--red);
  color: var(--paper);
  border-color: var(--paper);
}
#overlay-fail #btn-restart:hover,
#overlay-fail #btn-restart:active,
#overlay-fail #btn-share-fail:hover,
#overlay-fail #btn-share-fail:active {
  background: var(--paper);
  color: var(--red);
}

/* ================================================================
   COMPLETE SCREEN
   ================================================================ */
#screen-complete {
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  min-height: 100dvh;
  padding: clamp(28px, 7vw, 56px) clamp(20px, 6vw, 36px) calc(clamp(28px, 7vw, 40px) + var(--safe-bottom));
  padding-top: calc(clamp(28px, 7vw, 56px) + var(--safe-top));
  gap: clamp(14px, 3.5vw, 22px);
  text-align: center;
}

#complete-heading {
  font-family: var(--font-mono);
  font-size: clamp(36px, 10vw, 64px);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink);
  line-height: 1;
}

#complete-sub {
  font-family: var(--font-label);
  font-size: clamp(12px, 3vw, 14px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0.75;
  line-height: 1.7;
}

#complete-patient-id {
  font-family: var(--font-mono);
  font-size: clamp(14px, 3.6vw, 18px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  border-top: 1px solid var(--ink);
  border-bottom: 1px solid var(--ink);
  padding: 10px 14px;
  margin-top: clamp(6px, 2vw, 10px);
}

#complete-level-summary {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 440px;
  font-family: var(--font-mono);
  font-size: clamp(13px, 3.4vw, 16px);
  letter-spacing: 0.05em;
  text-align: left;
  margin: clamp(4px, 1.5vw, 8px) 0;
}
#complete-level-summary:empty { display: none; }

/* ================================================================
   OVERLAY ACTIONS (button rows)
   ================================================================ */
.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  width: 100%;
  max-width: 360px;
  margin-top: clamp(6px, 2vw, 12px);
}
.overlay-actions button {
  width: 100%;
}
@media (min-width: 480px) {
  .overlay-actions {
    flex-direction: row;
    max-width: none;
    justify-content: center;
  }
  .overlay-actions button {
    width: auto;
  }
}

/* ================================================================
   BUTTONS — squared, 2px ink border, uppercase tracked sans
   ================================================================ */
button {
  display: inline-block;
  font-family: var(--font-label);
  font-size: clamp(13px, 3.4vw, 15px);
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 0;
  padding: 12px 24px;
  min-height: 44px;
  min-width: 44px;
  cursor: pointer;
  transition: background 80ms linear, color 80ms linear;
  -webkit-tap-highlight-color: transparent;
}

button:hover,
button:active,
button:focus-visible {
  background: var(--ink);
  color: var(--paper);
  outline: none;
}

button:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 2px;
}

/* ================================================================
   DISCLAIMER FOOTER
   ================================================================ */
#disclaimer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 8px 16px calc(8px + var(--safe-bottom));
  font-family: var(--font-label);
  font-size: 9px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.5;
  text-align: center;
  pointer-events: none;
  z-index: 5;
  background: linear-gradient(to bottom, transparent, var(--paper) 35%);
  /* gradient is purely for legibility over chart paper, not decorative styling.
     If preferred, swap for a solid block — but it doesn't introduce color. */
}

/* ================================================================
   LANDSCAPE WARNING
   ================================================================ */
#landscape-warning {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-label);
  font-size: 18px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  text-align: center;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  line-height: 1.5;
}

@media (orientation: landscape) and (max-width: 900px) {
  #landscape-warning { display: flex; }
  body > *:not(#landscape-warning):not(script) { visibility: hidden; }
}

/* ================================================================
   REDUCED MOTION
   ================================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  /* Keep meme visible even with reduced motion */
  #fail-meme-container { opacity: 1; }
}

/* ================================================================
   SMALL VIEWPORT TWEAKS (320px)
   ================================================================ */
@media (max-width: 360px) {
  .vital-cell { padding: 6px 1px 4px; min-height: 50px; }
  .vital-value { font-size: 14px; }
  .vital-label { font-size: 7px; letter-spacing: 0.1em; }
}
