/* =========================================================================
   LAST CALL — base theme, layout, HUD, components, accessibility modes
   ========================================================================= */

:root {
  --bg: #120a1f;
  --bg-2: #1a1029;
  --panel: #21153a;
  --panel-2: #2b1d4a;
  --line: #4a3576;
  --line-2: #6a4fa0;
  --text: #f3e9d2;
  --text-dim: #b9a9d6;
  --pink: #ff4fa3;
  --blue: #3ee0ff;
  --amber: #ffb347;
  --green: #6dff8a;
  --red: #ff5a6a;
  --wood: #6b3a1f;
  --wood-2: #8a4f2a;
  --focus: #ffe66d;
  --shadow: #07040d;
  --font-pixel: "Press Start 2P", ui-monospace, "Courier New", monospace;
  --font-body: "VT323", ui-monospace, "Courier New", monospace;
  --text-scale: 1;
  --fs: calc(22px * var(--text-scale));
  --radius: 0px;
  --gap: clamp(8px, 2vw, 16px);
  --tap: 48px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(ellipse at 50% -10%, #3a1b52 0%, transparent 60%),
    repeating-linear-gradient(0deg, transparent 0 15px, rgba(0,0,0,.18) 15px 16px),
    repeating-linear-gradient(90deg, transparent 0 31px, rgba(0,0,0,.12) 31px 32px),
    var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--fs);
  line-height: 1.25;
  overflow-x: hidden;
}
.readable body, html.readable body {
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-pixel: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --fs: calc(17px * var(--text-scale));
  line-height: 1.45;
}
h1, h2, h3, h4 { font-family: var(--font-pixel); font-weight: 400; line-height: 1.4; margin: 0 0 .6em; letter-spacing: .02em; }
h1 { font-size: clamp(16px, 3.6vw, 24px); }
h2 { font-size: clamp(13px, 2.6vw, 18px); }
h3 { font-size: clamp(11px, 2.1vw, 14px); }
h4 { font-size: 11px; }
p { margin: 0 0 .6em; }
a { color: var(--blue); }
img, canvas { image-rendering: pixelated; image-rendering: crisp-edges; }
.pixel { image-rendering: pixelated; }

/* ----- Accessibility helpers ----- */
.sr-only {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip-link {
  position: absolute; left: 8px; top: -60px; z-index: 1000;
  background: var(--focus); color: #000; padding: 10px 14px; font-family: var(--font-pixel); font-size: 12px;
}
.skip-link:focus { top: 8px; }
:focus { outline: none; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

/* ----- HUD ----- */
.hud {
  position: sticky; top: 0; z-index: 50;
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: var(--gap);
  padding: 8px clamp(8px, 2vw, 20px);
  padding-top: max(8px, env(safe-area-inset-top));
  background: linear-gradient(180deg, #0c0616, #160d27);
  border-bottom: 4px solid var(--line);
  box-shadow: 0 4px 0 var(--shadow);
}
.neon-logo { text-decoration: none; font-family: var(--font-pixel); font-size: clamp(12px, 2.8vw, 20px); white-space: nowrap; }
.neon { display: inline-block; padding: 0 2px; }
.neon-pink { color: #ffd6ec; text-shadow: 0 0 4px var(--pink), 0 0 10px var(--pink), 0 0 20px var(--pink); animation: flicker 6s infinite steps(1); }
.neon-blue { color: #d6fbff; text-shadow: 0 0 4px var(--blue), 0 0 10px var(--blue), 0 0 20px var(--blue); animation: flicker 9s 1.3s infinite steps(1); }
@keyframes flicker {
  0%, 18%, 22%, 25%, 53%, 57%, 100% { opacity: 1; }
  20%, 24%, 55% { opacity: .35; }
}
.hud-player { min-width: 0; display: flex; flex-direction: column; gap: 4px; max-width: 420px; justify-self: center; width: 100%; }
.hud-level { font-family: var(--font-pixel); font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lvl-badge { background: var(--amber); color: #2a1300; padding: 3px 5px; box-shadow: 2px 2px 0 var(--shadow); }
.hud-title { color: var(--text-dim); margin-left: 6px; }
.xpbar { position: relative; height: 16px; background: #0a0612; border: 2px solid var(--line-2); }
.xpbar-fill { height: 100%; width: 0; background: repeating-linear-gradient(90deg, var(--green) 0 6px, #42c95f 6px 8px); transition: width .5s steps(12); }
.xpbar-text { position: absolute; inset: 0; display: grid; place-items: center; font-size: 15px; line-height: 1; color: #fff; text-shadow: 1px 1px 0 #000; }
.hud-actions { display: flex; gap: 6px; align-items: center; }
@media (max-width: 560px) {
  .hud { grid-template-columns: 1fr auto; grid-template-areas: "logo actions" "player player"; row-gap: 6px; }
  .neon-logo { grid-area: logo; }
  .hud-actions { grid-area: actions; }
  .hud-player { grid-area: player; max-width: none; }
}

/* ----- Layout ----- */
main { flex: 1; width: 100%; max-width: 1200px; margin: 0 auto; padding: var(--gap); padding-bottom: 32px; }
main:focus { outline: none; }
.footer {
  display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom)); font-size: .8em; color: var(--text-dim);
  border-top: 4px solid var(--line); background: #0c0616;
}
.row { display: flex; gap: var(--gap); flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
.grid { display: grid; gap: var(--gap); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); }
.muted { color: var(--text-dim); }
.center { text-align: center; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ----- Panels (pixel-border) ----- */
.panel {
  position: relative;
  background: var(--panel);
  border: 4px solid var(--line);
  box-shadow: inset 0 0 0 2px #120a22, 6px 6px 0 var(--shadow);
  padding: clamp(10px, 2vw, 18px);
}
.panel.inset { background: #160e28; box-shadow: inset 0 0 0 2px #0b0614; }
.panel-title { display: flex; align-items: center; gap: 10px; justify-content: space-between; flex-wrap: wrap; margin-bottom: 10px; }
.panel-title h2, .panel-title h3 { margin: 0; }

/* ----- Buttons ----- */
.btn {
  --b: var(--panel-2); --bt: var(--text); --bs: var(--line-2);
  appearance: none; border: 3px solid var(--bs); background: var(--b); color: var(--bt);
  font-family: var(--font-pixel); font-size: clamp(10px, 1.6vw, 12px); line-height: 1.3;
  min-height: var(--tap); min-width: var(--tap); padding: 10px 14px;
  box-shadow: 0 4px 0 var(--shadow); cursor: pointer; position: relative; top: 0;
  text-transform: uppercase; letter-spacing: .03em; touch-action: manipulation;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
  transition: filter .1s;
}
.btn:hover { filter: brightness(1.18); }
.btn:active, .btn.is-held { top: 3px; box-shadow: 0 1px 0 var(--shadow); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .45; cursor: not-allowed; filter: grayscale(.6); }
.btn-primary { --b: var(--pink); --bt: #fff; --bs: #ff9fcf; text-shadow: 1px 1px 0 #7a1050; }
.btn-blue { --b: #1d7f96; --bt: #fff; --bs: var(--blue); }
.btn-good { --b: #1f7a3a; --bt: #fff; --bs: var(--green); }
.btn-warn { --b: #7a3f12; --bt: #fff; --bs: var(--amber); }
.btn-bad { --b: #7a1f2a; --bt: #fff; --bs: var(--red); }
.btn-ghost { --b: transparent; box-shadow: none; }
.btn-small { min-height: 40px; padding: 6px 10px; font-size: 10px; }
.btn-icon { padding: 6px; font-family: var(--font-body); font-size: 22px; }
.btn-big { font-size: clamp(12px, 2.4vw, 16px); padding: 18px 24px; min-height: 72px; }
.btn[aria-pressed="true"].toggle, .chip[aria-pressed="true"] { --b: var(--blue); --bt: #001b22; --bs: #b4f5ff; }
.btn-block { width: 100%; }

.chip {
  appearance: none; border: 2px solid var(--line-2); background: #1a1030; color: var(--text);
  font-family: var(--font-body); font-size: .9em; padding: 6px 10px; min-height: 40px; cursor: pointer;
  box-shadow: 0 3px 0 var(--shadow);
}
.chip[aria-pressed="true"] { background: var(--blue); color: #001b22; border-color: #b4f5ff; }

/* ----- Forms ----- */
label { display: block; }
input[type="text"], input[type="search"], input[type="number"], select, textarea {
  font: inherit; color: var(--text); background: #0d0818; border: 3px solid var(--line-2);
  padding: 8px 10px; min-height: var(--tap); width: 100%; border-radius: 0;
}
input[type="range"] { width: 100%; accent-color: var(--pink); min-height: 32px; }
input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--pink); vertical-align: middle; }
.field { display: grid; gap: 4px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 2px dashed #34245a; }

/* ----- Tags, badges, meters ----- */
.tag { display: inline-block; font-family: var(--font-pixel); font-size: 9px; padding: 4px 6px; background: #2c1d4c; border: 2px solid var(--line-2); text-transform: uppercase; }
.tag.good { background: #15391f; border-color: var(--green); color: var(--green); }
.tag.bad { background: #3c121a; border-color: var(--red); color: #ffc2c8; }
.tag.amber { background: #3b2508; border-color: var(--amber); color: var(--amber); }
.statline { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dotted #3a2a60; padding: 3px 0; }
.statline b { color: var(--amber); font-weight: 400; }
.meter { height: 12px; background: #0b0614; border: 2px solid var(--line-2); }
.meter > i { display: block; height: 100%; background: var(--amber); }

/* ----- Speech bubble ----- */
.bubble {
  position: relative; background: #fff8e7; color: #1c1230; padding: 10px 14px; border: 3px solid #1c1230;
  box-shadow: 4px 4px 0 var(--shadow); max-width: 520px; font-size: 1.05em;
}
.bubble::after {
  content: ""; position: absolute; left: -12px; top: 18px; width: 12px; height: 12px;
  background: #fff8e7; border-left: 3px solid #1c1230; border-bottom: 3px solid #1c1230; transform: skewY(30deg);
}

/* ----- Score pop-ups & toasts ----- */
.score-pop {
  position: fixed; z-index: 300; pointer-events: none; font-family: var(--font-pixel); font-size: 14px;
  color: var(--green); text-shadow: 2px 2px 0 #000; animation: pop-up 1.1s steps(10) forwards;
}
.score-pop.bad { color: var(--red); }
.score-pop.amber { color: var(--amber); }
@keyframes pop-up { 0% { transform: translate(-50%, 0) scale(.6); opacity: 0; } 15% { transform: translate(-50%, -8px) scale(1.2); opacity: 1; } 100% { transform: translate(-50%, -60px) scale(1); opacity: 0; } }
.toasts { position: fixed; z-index: 400; right: 12px; bottom: 56px; display: flex; flex-direction: column; gap: 8px; max-width: min(92vw, 360px); }
.toast {
  background: #1b0f2f; border: 3px solid var(--amber); color: var(--text); padding: 10px 12px; box-shadow: 4px 4px 0 var(--shadow);
  animation: toast-in .25s steps(5); display: flex; gap: 10px; align-items: center;
}
.toast.achv { border-color: var(--green); }
.toast b { font-family: var(--font-pixel); font-size: 10px; display: block; color: var(--amber); margin-bottom: 4px; }
@keyframes toast-in { from { transform: translateX(110%); } to { transform: none; } }

/* ----- Dialog ----- */
dialog.modal {
  border: 4px solid var(--line-2); background: var(--panel); color: var(--text); padding: 0;
  width: min(640px, 94vw); max-height: 90vh; box-shadow: 8px 8px 0 var(--shadow);
}
dialog.modal::backdrop { background: rgba(5, 2, 12, .78); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 12px 14px; background: #160d29; border-bottom: 4px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 13px; }
.modal-body { padding: 14px; overflow: auto; max-height: calc(90vh - 140px); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 12px 14px; border-top: 4px solid var(--line); background: #160d29; }

/* ----- CRT overlay ----- */
.crt-overlay {
  pointer-events: none; position: fixed; inset: 0; z-index: 500;
  background:
    repeating-linear-gradient(0deg, rgba(0,0,0,.22) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,.45) 100%);
  mix-blend-mode: multiply;
}
html.no-crt .crt-overlay { display: none; }

/* ----- Hub ----- */
.scene-wrap { position: relative; border: 4px solid var(--line); box-shadow: 6px 6px 0 var(--shadow); background: #000; margin-bottom: var(--gap); }
.scene-canvas { display: block; width: 100%; height: auto; aspect-ratio: 320 / 110; }
.scene-sign {
  position: absolute; left: 50%; top: 9%; transform: translateX(-50%);
  font-family: var(--font-pixel); font-size: clamp(10px, 2.6vw, 22px); color: #ffe0f0; white-space: nowrap;
  text-shadow: 0 0 4px var(--pink), 0 0 12px var(--pink), 0 0 24px var(--pink); animation: flicker 7s infinite steps(1);
}
.scene-sign small { display: block; text-align: center; font-size: .5em; color: #dffbff; text-shadow: 0 0 4px var(--blue), 0 0 12px var(--blue); margin-top: 4px; }
.module-grid { display: grid; gap: var(--gap); grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); }
.module-card {
  display: grid; grid-template-columns: 64px 1fr; gap: 12px; align-items: center; text-align: left;
  width: 100%; min-height: 110px; font-family: var(--font-body); font-size: 1em; text-transform: none; letter-spacing: 0;
}
.module-card .mc-icon { width: 64px; height: 64px; display: grid; place-items: center; background: #0e0819; border: 2px solid var(--line-2); }
.module-card .mc-icon canvas { width: 56px; height: 56px; }
.module-card h3 { margin: 0 0 6px; font-size: 11px; }
.module-card p { margin: 0; color: var(--text-dim); font-size: .85em; }
.module-card .mc-meta { margin-top: 6px; font-size: .75em; color: var(--amber); }
.achv-grid { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 220px), 1fr)); }
.achv { display: flex; gap: 10px; align-items: center; padding: 8px; border: 2px solid #3a2a60; background: #160e28; }
.achv .ai { font-size: 26px; width: 36px; text-align: center; filter: grayscale(1) opacity(.4); }
.achv.on { border-color: var(--green); }
.achv.on .ai { filter: none; }
.achv b { display: block; font-family: var(--font-pixel); font-size: 9px; margin-bottom: 3px; }
.achv span { font-size: .8em; color: var(--text-dim); }

/* ----- Module frame ----- */
.module-head { display: flex; align-items: center; gap: 10px; margin-bottom: var(--gap); flex-wrap: wrap; }
.module-head h1 { margin: 0; flex: 1; min-width: 200px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: var(--gap); }
.tabs .btn[aria-selected="true"] { --b: var(--amber); --bt: #2a1300; --bs: #ffe0b0; }
.feedback { border-left: 6px solid var(--line-2); padding: 8px 12px; background: #150d27; margin-top: 10px; }
.feedback.good { border-color: var(--green); }
.feedback.bad { border-color: var(--red); }
.feedback.amber { border-color: var(--amber); }
.big-num { font-family: var(--font-pixel); font-size: clamp(18px, 4vw, 30px); color: var(--amber); }
.kbd { font-family: var(--font-pixel); font-size: 9px; border: 2px solid var(--line-2); padding: 2px 5px; background: #0d0818; }

/* ----- Animations (disabled by reduced motion) ----- */
.slide-in { animation: slide-in .45s steps(6) both; }
@keyframes slide-in { from { transform: translateX(-60px); opacity: 0; } to { transform: none; opacity: 1; } }
.bounce { animation: bounce .4s steps(4); }
@keyframes bounce { 0%,100% { transform: none; } 50% { transform: translateY(-6px); } }
.shake-x { animation: shake-x .35s steps(6); }
@keyframes shake-x { 0%,100% { transform: none; } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

html.reduced-motion *, html.reduced-motion *::before, html.reduced-motion *::after {
  animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; scroll-behavior: auto !important;
}
html.reduced-motion .neon-pink, html.reduced-motion .neon-blue, html.reduced-motion .scene-sign { animation: none !important; }

/* ----- High contrast mode ----- */
html.high-contrast {
  --bg: #000; --bg-2: #000; --panel: #000; --panel-2: #111; --line: #fff; --line-2: #fff; --text: #fff; --text-dim: #e6e6e6;
  --pink: #ff7cc0; --blue: #7ff0ff; --amber: #ffd23f; --green: #7dff9a; --red: #ff8a8a; --shadow: #444;
}
html.high-contrast body { background: #000; }
html.high-contrast .crt-overlay { display: none; }
html.high-contrast .bubble { background: #fff; color: #000; border-color: #fff; }
html.high-contrast .btn { --bs: #fff; }
html.high-contrast .muted, html.high-contrast .module-card p { color: #fff; }

@media (prefers-reduced-motion: reduce) {
  .neon-pink, .neon-blue, .scene-sign { animation: none; }
}
@media (forced-colors: active) {
  .btn, .panel, .chip { border: 2px solid ButtonText; }
  .crt-overlay { display: none; }
}
@media print {
  .hud, .footer, .crt-overlay, .toasts { display: none !important; }
  body { background: #fff; color: #000; }
}
