/* ==========================================================================
   Blackbox Courier — interface styles
   Mobile-first. The canvas owns the play area; everything here is chrome.
   ========================================================================== */

:root {
  --void: #04060e;
  --deep: #070c1c;
  --navy: #0d1730;
  --line: #1b3358;
  --cyan: #3ff2ff;
  --cyan-dim: #1b8fa8;
  --violet: #a878ff;
  --violet-hot: #d4b4ff;
  --orange: #ff9a3c;
  --red: #ff3d5e;
  --green: #4dffb0;
  --white: #eaf6ff;
  --muted: #7f97b8;

  --app-height: 100dvh;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);

  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;

  --radius: 12px;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--void);
  color: var(--white);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: none;
}

body { user-select: none; -webkit-user-select: none; }

#app {
  position: fixed;
  inset: 0;
  height: var(--app-height);
  width: 100%;
  overflow: hidden;
}

/* ------------------------------- Stage ---------------------------------- */

#stage {
  position: absolute;
  inset: 0;
  touch-action: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* -------------------------------- HUD ----------------------------------- */

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.28s var(--ease);
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) calc(10px + var(--safe-b)) calc(12px + var(--safe-l));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#hud.is-visible { opacity: 1; }

.hud-top {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.hud-block { display: flex; flex-direction: column; line-height: 1.05; min-width: 0; }
.hud-score-block { flex: 1 1 auto; }
.hud-mult-block, .hud-dist-block { text-align: right; }

.hud-label {
  font-size: 9px;
  letter-spacing: 0.16em;
  color: var(--muted);
  font-weight: 700;
}

.hud-value {
  font-family: var(--mono);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--white);
  text-shadow: 0 0 12px rgba(63, 242, 255, 0.35);
  font-variant-numeric: tabular-nums;
}
.hud-mult-block .hud-value { font-size: 17px; color: var(--cyan); }
.hud-mult-block .hud-value[data-hot="true"] { color: var(--orange); text-shadow: 0 0 14px rgba(255, 154, 60, 0.6); }
.hud-dist-block .hud-value { font-size: 15px; color: var(--muted); text-shadow: none; }

.icon-btn {
  pointer-events: auto;
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(7, 12, 28, 0.72);
  color: var(--cyan);
  display: grid;
  place-items: center;
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.icon-btn svg { width: 18px; height: 18px; fill: currentColor; }
.icon-btn:active { transform: scale(0.94); }

/* Meters ------------------------------------------------------------------ */

.hud-meters { display: flex; flex-direction: column; gap: 6px; max-width: 460px; }

.meter { display: flex; flex-direction: column; gap: 3px; }

.meter-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 9px;
  letter-spacing: 0.14em;
  font-weight: 700;
}
.meter-name { color: var(--muted); }
.meter-state { color: var(--cyan); flex: 1 1 auto; }
.meter-num { font-family: var(--mono); color: var(--white); font-variant-numeric: tabular-nums; }

.meter-track {
  height: 7px;
  border-radius: 4px;
  background: rgba(13, 23, 48, 0.85);
  border: 1px solid var(--line);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 100%;
  transform-origin: left center;
  transform: scaleX(1);
  background: linear-gradient(90deg, var(--cyan-dim), var(--cyan));
  transition: background-color 0.3s linear;
}
.meter-fill[data-tone="ok"]   { background: linear-gradient(90deg, #1b8fa8, var(--cyan)); }
.meter-fill[data-tone="warn"] { background: linear-gradient(90deg, #a87a1b, var(--orange)); }
.meter-fill[data-tone="bad"]  { background: linear-gradient(90deg, #a8341b, var(--red)); }
.meter-fill[data-tone="crit"] { background: linear-gradient(90deg, #7a0f22, var(--red)); animation: meterPulse 0.55s steps(2, end) infinite; }
.meter-fill[data-tone="phase"] { background: linear-gradient(90deg, #5b3aa8, var(--violet-hot)); }
.meter-fill[data-low="true"]  { background: linear-gradient(90deg, #4a2a6a, #7a5aa0); }

@keyframes meterPulse { 0% { opacity: 1; } 100% { opacity: 0.45; } }

#hud[data-stability="crit"] .meter-state { color: var(--red); }
#hud[data-stability="bad"] .meter-state { color: var(--red); }
#hud[data-stability="warn"] .meter-state { color: var(--orange); }

/* Status / toast / tutorial ----------------------------------------------- */

.hud-status {
  align-self: center;
  margin-top: 2px;
  font-size: 11px;
  letter-spacing: 0.18em;
  font-weight: 700;
  color: var(--cyan);
  opacity: 0;
  transition: opacity 0.2s var(--ease);
  text-shadow: 0 0 10px currentColor;
}
.hud-status.is-visible { opacity: 0.95; }
.hud-status[data-tone="bad"] { color: var(--red); }
.hud-status[data-tone="warn"] { color: var(--orange); }
.hud-status[data-tone="violet"] { color: var(--violet-hot); }

.toast {
  position: absolute;
  left: 50%;
  top: 34%;
  transform: translate(-50%, 0);
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.2em;
  color: var(--cyan);
  text-shadow: 0 0 16px currentColor;
  opacity: 0;
  white-space: nowrap;
  pointer-events: none;
}
.toast.is-visible { animation: toastIn 1.4s var(--ease) forwards; }
.toast[data-tone="green"] { color: var(--green); }
.toast[data-tone="red"] { color: var(--red); }
.toast[data-tone="violet"] { color: var(--violet-hot); }

@keyframes toastIn {
  0%   { opacity: 0; transform: translate(-50%, 12px) scale(0.94); }
  14%  { opacity: 1; transform: translate(-50%, 0) scale(1); }
  72%  { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -14px); }
}

.tutorial {
  position: absolute;
  left: 50%;
  bottom: calc(140px + var(--safe-b));
  transform: translateX(-50%);
  padding: 9px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(7, 12, 28, 0.86);
  color: var(--white);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.16em;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.3s var(--ease);
  backdrop-filter: blur(6px);
}
.tutorial.is-visible { opacity: 1; }

.countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--mono);
  font-size: 84px;
  font-weight: 800;
  color: var(--cyan);
  text-shadow: 0 0 40px rgba(63, 242, 255, 0.6);
  opacity: 0;
  pointer-events: none;
}
.countdown.is-visible { animation: countPop 0.7s var(--ease) forwards; }
@keyframes countPop {
  0%   { opacity: 0; transform: scale(1.5); }
  20%  { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.85); }
}

/* Phase button ------------------------------------------------------------ */

.phase-btn {
  pointer-events: auto;
  position: absolute;
  right: calc(16px + var(--safe-r));
  bottom: calc(22px + var(--safe-b));
  width: 92px;
  height: 92px;
  border-radius: 50%;
  border: 2px solid rgba(168, 120, 255, 0.55);
  background: radial-gradient(circle at 50% 45%, rgba(168, 120, 255, 0.24), rgba(7, 12, 28, 0.86) 70%);
  color: var(--violet-hot);
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.18em;
  display: grid;
  place-items: center;
  cursor: pointer;
  touch-action: none;
  transition: transform 0.09s var(--ease), border-color 0.15s linear, box-shadow 0.15s linear;
}
.phase-btn .phase-text { position: relative; z-index: 1; }
.phase-ring {
  position: absolute;
  inset: 8px;
  border-radius: 50%;
  border: 1px dashed rgba(168, 120, 255, 0.4);
}
.phase-btn.is-held, .phase-btn.is-active {
  transform: scale(0.95);
  border-color: var(--violet-hot);
  box-shadow: 0 0 26px rgba(168, 120, 255, 0.6), inset 0 0 22px rgba(168, 120, 255, 0.35);
}
.phase-btn.is-empty {
  border-color: rgba(120, 120, 150, 0.4);
  color: #7d7d95;
  box-shadow: none;
}

/* ------------------------------ Screens --------------------------------- */

.screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  background: radial-gradient(ellipse at 50% 30%, rgba(13, 23, 48, 0.86), rgba(4, 6, 14, 0.96));
  backdrop-filter: blur(9px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.28s var(--ease), visibility 0.28s;
  z-index: 10;
}
.screen.is-visible { opacity: 1; visibility: visible; pointer-events: auto; }
.screen-overlay { background: rgba(4, 6, 14, 0.82); z-index: 20; }
#screen-confirm { z-index: 30; }

.panel {
  width: min(440px, 100%);
  max-height: 100%;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(13, 23, 48, 0.92), rgba(7, 12, 28, 0.96));
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(63, 242, 255, 0.12);
  padding: 22px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: panelIn 0.32s var(--ease);
}
.panel-scroll { overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; }
.panel-small { width: min(340px, 100%); }
.panel-loading { align-items: center; text-align: center; }

@keyframes panelIn {
  from { opacity: 0; transform: translateY(14px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}

/* Typography -------------------------------------------------------------- */

.logo {
  margin: 0;
  text-align: center;
  line-height: 0.95;
  font-weight: 800;
}
.logo span {
  display: block;
  font-size: 13px;
  letter-spacing: 0.44em;
  color: var(--cyan);
  text-shadow: 0 0 18px rgba(63, 242, 255, 0.5);
}
.logo b {
  display: block;
  font-size: clamp(30px, 10vw, 42px);
  letter-spacing: 0.08em;
  color: var(--white);
  margin-top: 4px;
}

.tagline {
  margin: -2px 0 6px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

h2 {
  margin: 0;
  font-size: 15px;
  letter-spacing: 0.24em;
  color: var(--cyan);
  text-align: center;
}
h3 {
  margin: 10px 0 0;
  font-size: 11px;
  letter-spacing: 0.22em;
  color: var(--muted);
}
.muted { color: var(--muted); font-size: 12px; margin: 0; }
.muted b { color: var(--white); font-family: var(--mono); }

/* Buttons ----------------------------------------------------------------- */

.btn {
  appearance: none;
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(13, 23, 48, 0.9);
  color: var(--white);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.16em;
  cursor: pointer;
  transition: transform 0.09s var(--ease), border-color 0.15s, background 0.15s, box-shadow 0.15s;
  min-height: 48px;
}
.btn:hover { border-color: var(--cyan-dim); }
.btn:active { transform: scale(0.98); }
.btn:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.btn-primary {
  border-color: rgba(63, 242, 255, 0.55);
  background: linear-gradient(180deg, rgba(63, 242, 255, 0.2), rgba(13, 23, 48, 0.9));
  color: var(--cyan);
  box-shadow: 0 0 24px rgba(63, 242, 255, 0.18);
}
.btn-ghost { background: transparent; color: var(--muted); }
.btn-danger { border-color: rgba(255, 61, 94, 0.5); color: var(--red); }

.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1 1 0; }

kbd {
  font-family: var(--mono);
  font-size: 11px;
  padding: 1px 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(4, 6, 14, 0.8);
  color: var(--cyan);
}

/* Lists and rows ---------------------------------------------------------- */

.menu-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 4px 0 6px;
}
.menu-stats > div {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 6px;
  text-align: center;
  background: rgba(4, 6, 14, 0.55);
  min-width: 0;
}
.menu-stats span {
  display: block;
  font-size: 8px;
  letter-spacing: 0.14em;
  color: var(--muted);
  font-weight: 700;
}
.menu-stats b {
  display: block;
  margin-top: 3px;
  font-family: var(--mono);
  font-size: 14px;
  color: var(--cyan);
  overflow: hidden;
  text-overflow: ellipsis;
}

.help-list, .legend { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 9px; }
.help-list li, .legend li { font-size: 13px; line-height: 1.5; color: #c3d5ea; }
.help-list b { color: var(--white); }

.legend { list-style: none; padding-left: 0; }
.legend li { display: flex; align-items: center; gap: 10px; }
.sw { width: 18px; height: 18px; border-radius: 4px; flex: 0 0 auto; border: 1px solid rgba(255,255,255,0.16); }
.sw-solid   { background: #131e33; border-color: #7fa6d8; }
.sw-energy  { background: linear-gradient(180deg, rgba(212,180,255,0.25), rgba(168,120,255,0.65)); border-color: var(--violet-hot); }
.sw-mine    { background: radial-gradient(circle, var(--red) 40%, #2a1608 42%); border-color: var(--orange); border-radius: 50%; }
.sw-corrupt { background: rgba(255, 61, 94, 0.28); border-color: var(--red); }
.sw-calib   { background: rgba(77, 255, 176, 0.14); border-color: var(--green); }
.sw-frag    { background: rgba(63, 242, 255, 0.3); border-color: var(--cyan); transform: rotate(45deg); }
.sw-repair  { background: rgba(77, 255, 176, 0.24); border-color: var(--green); border-radius: 50%; }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(4, 6, 14, 0.5);
  font-size: 13px;
  cursor: pointer;
  min-height: 48px;
}
.row input[type="checkbox"] {
  appearance: none;
  width: 46px;
  height: 26px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(13, 23, 48, 0.9);
  position: relative;
  cursor: pointer;
  flex: 0 0 auto;
  transition: background 0.18s, border-color 0.18s;
}
.row input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.18s var(--ease), background 0.18s;
}
.row input[type="checkbox"]:checked { border-color: var(--cyan-dim); background: rgba(63, 242, 255, 0.18); }
.row input[type="checkbox"]:checked::after { transform: translateX(20px); background: var(--cyan); }
.row input:focus-visible, .row select:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.row select {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(13, 23, 48, 0.9);
  color: var(--white);
  font-family: inherit;
  font-size: 12px;
  padding: 8px 10px;
  cursor: pointer;
}

.stats-list { display: flex; flex-direction: column; gap: 1px; }
.stat-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  font-size: 12.5px;
  background: rgba(4, 6, 14, 0.42);
  border-radius: 6px;
}
.stat-row span { color: var(--muted); }
.stat-row b { font-family: var(--mono); color: var(--white); font-variant-numeric: tabular-nums; }

/* Results ------------------------------------------------------------------ */

.grade {
  text-align: center;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.22em;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
}
.grade[data-tone="grade-s"] { color: var(--violet-hot); border-color: rgba(212,180,255,0.5); box-shadow: 0 0 26px rgba(168,120,255,0.3); }
.grade[data-tone="grade-a"] { color: var(--cyan); border-color: rgba(63,242,255,0.45); }
.grade[data-tone="grade-b"] { color: var(--green); border-color: rgba(77,255,176,0.4); }
.grade[data-tone="grade-c"] { color: var(--orange); border-color: rgba(255,154,60,0.4); }
.grade[data-tone="grade-d"] { color: var(--red); border-color: rgba(255,61,94,0.4); }

.res-score-block { text-align: center; display: flex; flex-direction: column; gap: 2px; align-items: center; }
.res-score {
  font-family: var(--mono);
  font-size: 40px;
  font-weight: 800;
  color: var(--white);
  text-shadow: 0 0 26px rgba(63, 242, 255, 0.4);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.newbest {
  display: none;
  font-size: 10px;
  letter-spacing: 0.24em;
  font-weight: 800;
  color: var(--orange);
  text-shadow: 0 0 14px currentColor;
}
.newbest.is-visible { display: block; }

/* Loading ------------------------------------------------------------------ */

.pulse-bar {
  width: 180px;
  height: 3px;
  border-radius: 2px;
  background: rgba(27, 51, 88, 0.9);
  overflow: hidden;
}
.pulse-bar i {
  display: block;
  width: 40%;
  height: 100%;
  background: linear-gradient(90deg, transparent, var(--cyan), transparent);
  animation: scan 1.1s linear infinite;
}
@keyframes scan { from { transform: translateX(-100%); } to { transform: translateX(250%); } }

body.is-loading #screen-loading { opacity: 1; visibility: visible; }

.noscript {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--void);
  color: var(--white);
  font-family: var(--font);
  z-index: 100;
}

/* ---------------------------- Responsive -------------------------------- */

@media (max-width: 360px) {
  .hud-value { font-size: 17px; }
  .phase-btn { width: 80px; height: 80px; font-size: 11px; }
  .panel { padding: 18px 15px; }
  .menu-stats b { font-size: 12px; }
}

@media (min-height: 780px) {
  .phase-btn { width: 104px; height: 104px; }
}

/* Landscape / desktop: keep the play field portrait-ish and centred. */
@media (min-aspect-ratio: 1/1) and (min-width: 700px) {
  #stage {
    left: 50%;
    transform: translateX(-50%);
    width: min(100%, calc(var(--app-height) * 0.62));
  }
  #stage::before,
  #stage::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100vw;
    background: var(--void);
    pointer-events: none;
  }
  #stage::before { right: 100%; }
  #stage::after { left: 100%; }
}

/* --------------------------- Accessibility ------------------------------ */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.05s !important;
  }
  .toast.is-visible { opacity: 1; animation: none; }
  .countdown.is-visible { opacity: 1; animation: none; }
}

body.reduced-effects .meter-fill[data-tone="crit"] { animation: none; }

@media (forced-colors: active) {
  .btn, .panel, .row { border: 1px solid CanvasText; }
}
