:root {
  --black: #0b0d11;
  --charcoal: #171a20;
  --panel: rgba(18, 20, 26, 0.94);
  --silver: #d8dde2;
  --silver-dark: #737b84;
  --red: #c4121c;
  --red-bright: #f22531;
  --gold: #d7ad55;
  --cream: #fff8e9;
  --success: #43d17d;
  --danger: #ff4d5a;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.48);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at 15% 5%, rgba(196, 18, 28, 0.20), transparent 34%),
    radial-gradient(circle at 85% 15%, rgba(215, 173, 85, 0.11), transparent 30%),
    linear-gradient(145deg, #090a0e 0%, #151820 55%, #090a0e 100%);
  color: var(--cream);
  font-family: Inter, ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
}

body { padding: 14px; }
button, select { font: inherit; }
button { color: inherit; }
button:focus-visible, select:focus-visible {
  outline: 4px solid rgba(242, 37, 49, 0.55);
  outline-offset: 3px;
}

.app-shell {
  width: min(1240px, 100%);
  margin: 0 auto;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
  padding: 11px 14px;
  background: rgba(10, 11, 15, 0.86);
  border: 1px solid rgba(255, 255, 255, 0.10);
  border-radius: 18px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.brand-block { display: flex; align-items: center; gap: 12px; }
.crest {
  width: 48px; height: 54px;
  display: grid; place-items: center;
  clip-path: polygon(50% 0, 92% 18%, 84% 76%, 50% 100%, 16% 76%, 8% 18%);
  background: linear-gradient(145deg, var(--silver), #666d75 44%, #f5f8fa 49%, #828991 72%);
  color: var(--red);
  font-weight: 1000;
  font-size: 24px;
  text-shadow: 0 1px 0 #fff;
}
.eyebrow, .panel-kicker {
  margin: 0 0 3px;
  color: var(--red-bright);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.16em;
}
h1 { margin: 0; font-size: clamp(1.1rem, 2vw, 1.65rem); line-height: 1.05; }
.header-actions { display: flex; align-items: center; gap: 8px; }
.lab-link {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 42px; padding: 0 14px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  background: #232730; color: var(--cream);
  text-decoration: none; font-size: .72rem; font-weight: 900; letter-spacing: .08em;
}
.lab-link:hover { background: #303640; }
.icon-button {
  width: 42px; height: 42px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  background: #232730;
  font-weight: 900;
  cursor: pointer;
}
.icon-button:hover { background: #303640; }

.game-card {
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 22px;
  background: rgba(14, 16, 21, 0.92);
  box-shadow: var(--shadow);
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border-bottom: 1px solid rgba(255,255,255,0.1);
  background: linear-gradient(180deg, #232730, #171a20);
}
.hud-item { padding: 9px 14px; text-align: center; border-right: 1px solid rgba(255,255,255,0.08); }
.hud-item:last-child { border-right: 0; }
.hud-item span { display: block; color: #aeb5bd; font-size: 0.72rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.11em; }
.hud-item strong { display: block; margin-top: 1px; font-size: clamp(1.15rem, 2vw, 1.55rem); }

.problem-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  min-height: 70px;
  padding: 10px 14px;
  background:
    linear-gradient(90deg, transparent, rgba(196,18,28,.18), transparent),
    #111319;
  border-bottom: 1px solid rgba(255,255,255,.10);
}
.problem-label { color: var(--gold); font-size: .75rem; letter-spacing: .14em; font-weight: 900; }
#problemText { font-size: clamp(1.9rem, 5vw, 3.4rem); letter-spacing: .02em; }

.stage-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 12 / 7;
  min-height: 430px;
  background: #111319;
}
canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.status-message {
  pointer-events: none;
  position: absolute;
  top: 14%; left: 50%;
  transform: translate(-50%, -50%) scale(.9);
  z-index: 6;
  padding: 7px 16px;
  border-radius: 999px;
  opacity: 0;
  font-size: clamp(1.1rem, 3vw, 1.7rem);
  font-weight: 1000;
  text-shadow: 0 2px 10px #000;
  transition: opacity .14s ease, transform .14s ease;
}
.status-message.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.status-message.correct { background: rgba(25, 127, 70, .88); }
.status-message.wrong { background: rgba(170, 22, 35, .91); }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(5, 6, 8, 0.79);
  backdrop-filter: blur(7px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
}
.overlay.visible { opacity: 1; pointer-events: auto; }
.overlay-panel {
  width: min(790px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: clamp(20px, 4vw, 34px);
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 24px;
  background:
    linear-gradient(145deg, rgba(196,18,28,.10), transparent 36%),
    var(--panel);
  box-shadow: 0 24px 80px rgba(0,0,0,.7);
  text-align: center;
}
.overlay-panel h2 { margin: 0 0 8px; font-size: clamp(1.7rem, 4vw, 2.7rem); }
.intro-copy { margin: 0 auto 20px; max-width: 630px; color: #c5cbd1; }
fieldset { border: 0; padding: 0; margin: 0 0 16px; }
legend, label { color: #d4d8dd; font-weight: 800; }
legend { margin-bottom: 8px; }
.choice-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.choice {
  min-height: 70px;
  padding: 8px;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 14px;
  background: #252932;
  cursor: pointer;
  font-weight: 800;
}
.choice span { display: block; margin-bottom: 2px; color: var(--silver); font-size: 1.55rem; }
.choice:hover { background: #303640; }
.choice.active { border-color: var(--red-bright); background: linear-gradient(180deg, #4f151b, #2f171b); box-shadow: inset 0 0 0 2px rgba(242,37,49,.22); }
.setup-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 0 0 18px; }
.setup-row label { display: grid; gap: 6px; text-align: left; font-size: .84rem; }
select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 11px;
  background: #272b34;
  color: #fff;
}
.primary-button, .secondary-button {
  min-width: 210px;
  padding: 13px 18px;
  border-radius: 12px;
  font-weight: 1000;
  letter-spacing: .04em;
  cursor: pointer;
}
.primary-button { border: 0; background: linear-gradient(180deg, #ef2a36, #a90e18); box-shadow: 0 8px 24px rgba(196,18,28,.30); }
.primary-button:hover { filter: brightness(1.08); }
.secondary-button { margin-top: 9px; border: 1px solid rgba(255,255,255,.15); background: #282c35; }
.controls-copy { margin: 13px 0 0; color: #9ea6ae; font-size: .82rem; }
.compact-panel { width: min(430px, 100%); }
.results-panel { width: min(600px, 100%); }
.results-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 20px 0; }
.results-grid div { padding: 14px; border: 1px solid rgba(255,255,255,.11); border-radius: 14px; background: #232730; }
.results-grid span { display: block; color: #aeb5bd; font-size: .74rem; text-transform: uppercase; letter-spacing: .09em; font-weight: 800; }
.results-grid strong { display: block; margin-top: 4px; font-size: 1.8rem; }

.touch-controls {
  display: grid;
  grid-template-columns: 1fr 1.4fr 1fr;
  gap: 9px;
  padding: 10px;
  background: #151820;
  border-top: 1px solid rgba(255,255,255,.10);
}
.touch-controls button {
  min-height: 52px;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 12px;
  background: #2a2f38;
  font-weight: 1000;
  font-size: 1.05rem;
  cursor: pointer;
  touch-action: manipulation;
}
.touch-controls button:active { transform: translateY(2px); }
.touch-controls .attack-control { background: linear-gradient(180deg, #5f151d, #350f14); border-color: rgba(242,37,49,.55); }
.game-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 13px;
  color: #8e969f;
  font-size: .75rem;
  background: #0f1116;
  border-top: 1px solid rgba(255,255,255,.08);
}

@media (max-width: 760px) {
  body { padding: 6px; }
  .game-header { padding: 9px 10px; }
  .crest { width: 40px; height: 46px; }
  .header-actions { gap: 5px; }
  .icon-button { width: 38px; height: 38px; }
  .stage-wrap { min-height: 500px; aspect-ratio: 4 / 5; }
  .choice-grid { grid-template-columns: repeat(2, 1fr); }
  .choice:last-child { grid-column: 1 / -1; }
  .setup-row { grid-template-columns: 1fr; }
  .problem-bar { min-height: 61px; flex-direction: column; gap: 0; }
  .hud-item { padding: 8px 4px; }
  .hud-item span { font-size: .61rem; }
  #heartsValue { font-size: 1rem; }
  .game-footer { flex-direction: column; text-align: center; gap: 2px; }
}

@media (max-height: 760px) and (min-width: 761px) {
  .stage-wrap { min-height: 360px; }
  .overlay-panel { padding: 16px 24px; }
  .choice { min-height: 58px; }
  .controls-copy { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}

@media (max-width: 640px) {
  .game-header { align-items: flex-start; }
  .header-actions { flex-wrap: wrap; justify-content: flex-end; }
  .lab-link { width: 100%; order: 2; }
}
