:root {
  --bg-main: #130f0c;
  --bg-accent: #2a1e14;
  --panel: rgba(45, 32, 22, 0.74);
  --panel-soft: rgba(92, 66, 40, 0.28);
  --line: rgba(221, 175, 116, 0.36);
  --text: #f4ead9;
  --muted: rgba(244, 233, 211, 0.78);
  --glow: rgba(241, 171, 87, 0.36);
  --ok: #58e09c;
  --warn: #ffcc66;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  color: var(--text);
  font-family: "Trebuchet MS", "Segoe UI", Tahoma, Verdana, sans-serif;
  background:
    radial-gradient(1200px 700px at 10% -10%, rgba(230, 155, 74, 0.22), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(196, 118, 51, 0.14), transparent 58%),
    linear-gradient(165deg, #17110d 0%, #120d0a 34%, #0f0b09 100%);
  overflow-x: hidden;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px);
  background-size: 28px 28px;
  mask-image: radial-gradient(circle at 50% 40%, black 40%, transparent 88%);
  opacity: 0.45;
}

.app {
  width: min(1100px, 100% - 28px);
  margin: 10px auto 0;
  padding: 10px 14px 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(45, 31, 20, 0.66), rgba(28, 20, 14, 0.76));
  box-shadow:
    0 16px 44px rgba(2, 6, 18, 0.6),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(8px);
}

.hud {
  text-align: center;
}

.hud h1 {
  margin: 0;
  font-size: clamp(28px, 4.2vw, 42px);
  font-weight: 900;
  letter-spacing: 0.02em;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.42), 0 0 24px rgba(99, 176, 255, 0.5);
}

.stats {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
}

.stats span {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  min-height: 36px;
  padding: 8px 10px;
  border-radius: 11px;
  border: 1px solid rgba(216, 176, 120, 0.4);
  background: linear-gradient(180deg, rgba(108, 79, 50, 0.78), rgba(66, 47, 30, 0.86));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.18);
  font-weight: 800;
  font-size: clamp(17px, 2vw, 24px);
}

.actions-line {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

button {
  border: 1px solid rgba(228, 184, 123, 0.44);
  background: linear-gradient(180deg, rgba(142, 101, 54, 0.86), rgba(90, 62, 34, 0.94));
  color: var(--text);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 16px;
  font-weight: 700;
  transition: transform 0.08s ease, filter 0.12s ease;
  box-shadow: 0 8px 22px rgba(6, 14, 30, 0.48), inset 0 1px 0 rgba(255,255,255,0.2);
}

button:active {
  transform: translateY(1px) scale(0.99);
}

button:hover {
  filter: brightness(1.1);
}

.game-shell {
  margin: 10px auto 0;
  width: min(950px, 100%);
}

.game-frame {
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid rgba(224, 179, 121, 0.4);
  box-shadow: 0 0 0 1px rgba(255,255,255,0.08), 0 18px 42px rgba(5, 13, 30, 0.7);
  background: #000;
}

canvas {
  width: 100%;
  height: auto;
  display: block;
  border: 0;
}

.status {
  margin: 10px auto 0;
  max-width: 950px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.35;
}

.controls {
  margin: 10px auto 0;
  max-width: 950px;
  display: grid;
  grid-template-columns: auto minmax(300px, 1fr);
  gap: 12px;
  align-items: end;
}

@media (min-width: 861px) {
  .app {
    width: min(980px, 100vw);
    margin: 0 auto;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
  }

  .hud {
    position: fixed;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: min(950px, calc(100vw - 16px));
    z-index: 80;
    pointer-events: none;
  }

  .hud h1,
  .stats {
    display: none;
  }

  .actions-line {
    margin: 0;
    justify-content: flex-end;
    gap: 8px;
    pointer-events: auto;
  }

  .actions-line button {
    min-height: 34px;
    padding: 6px 12px;
    font-size: 15px;
    border-radius: 10px;
    background: linear-gradient(180deg, rgba(144, 104, 58, 0.58), rgba(85, 60, 35, 0.68));
  }

  .game-shell {
    margin: 0 auto;
    width: min(950px, 100vw);
  }

  .controls {
    display: none;
  }
}

.move,
.combat {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.move {
  align-items: center;
}

.row {
  display: flex;
  gap: 8px;
}

.move-pad button {
  min-width: 54px;
  min-height: 52px;
  display: grid;
  place-items: center;
  gap: 2px;
  font-size: 22px;
  padding: 6px;
}

.move-key,
.combat-key {
  font-size: 11px;
  opacity: 0.8;
}

.combat {
  align-items: stretch;
  gap: 8px;
}

.combat-btn {
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
}

.combat-icon {
  font-size: 19px;
  opacity: 0.9;
}

.combat-label {
  font-size: 17px;
  font-weight: 800;
}

.combat-key {
  margin-left: auto;
}

.quiz-modal,
.battle-modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  background: rgba(1, 4, 12, 0.44);
  z-index: 90;
}

.quiz-modal.hidden,
.battle-modal.hidden {
  display: none;
}

.quiz-card,
.battle-card {
  width: min(450px, 69.8vw);
  max-height: 82vh;
  overflow: auto;
  border-radius: 16px;
  border: 1px solid rgba(208, 162, 102, 0.46);
  background: linear-gradient(180deg, rgba(244, 246, 252, 0.92), rgba(236, 241, 250, 0.88));
  color: #13161f;
  box-shadow: 0 28px 60px rgba(4, 8, 18, 0.66);
}

.quiz-card {
  background: linear-gradient(180deg, rgba(244, 246, 252, 0.52), rgba(236, 241, 250, 0.5));
}

.battle-card {
  background: linear-gradient(180deg, rgba(244, 246, 252, 0.52), rgba(236, 241, 250, 0.5));
}

.quiz-card {
  padding: 14px;
}

.battle-card {
  padding: 16px 18px;
}

.quiz-header {
  font-size: clamp(24px, 3.6vw, 32px);
  font-weight: 900;
  margin-bottom: 8px;
  color: #111423;
}

.quiz-prompt {
  font-size: clamp(20px, 3vw, 26px);
  line-height: 1.3;
  color: #151a2b;
  margin-bottom: 10px;
}

.quiz-choices {
  display: grid;
  gap: 10px;
}

.quiz-choice {
  width: 100%;
  border-radius: 12px;
  border: 1px solid rgba(58, 76, 118, 0.34);
  background: rgba(255, 255, 255, 0.235);
  color: #121727;
  min-height: 58px;
  display: flex;
  align-items: center;
  text-align: left;
  gap: 10px;
  padding: 10px 14px;
  font-size: 20px;
}

.move-pad button,
.combat-btn,
#battleCloseBtn {
  border-radius: 12px;
  border: 1px solid rgba(58, 76, 118, 0.34);
  background: rgba(255, 255, 255, 0.235);
  color: #121727;
  box-shadow: none;
}

.move-pad button,
.combat-btn {
  background: rgba(255, 255, 255, 0.15);
}

.quiz-choice-index {
  font-weight: 900;
}

.battle-title {
  margin: 0;
  font-size: 30px;
  color: #10162a;
}

.battle-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.battle-summary {
  color: #151a2b;
}

.battle-summary-line {
  font-size: 20px;
  margin-bottom: 8px;
}

.battle-summary-prompt {
  font-size: 19px;
  border: 1px solid rgba(28, 42, 74, 0.16);
  background: rgba(255, 255, 255, 0.235);
  border-radius: 11px;
  padding: 10px;
}

.battle-summary-answer {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed rgba(34,34,34,0.16);
  font-size: 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 8px;
  row-gap: 4px;
}

.battle-summary-answer-label {
  display: inline;
  font-weight: 800;
  white-space: nowrap;
}

.battle-summary-answer-value {
  display: inline;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
  flex: 0 1 auto;
}

.quiz-card .katex,
.quiz-card .katex *,
.battle-card .katex,
.battle-card .katex * {
  color: #121727 !important;
}

.question-preview {
  margin-top: 16px;
}

.question-preview.hidden {
  display: none;
}

.question-preview-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}

.question-preview-list {
  margin-top: 8px;
  display: grid;
  gap: 10px;
}

.question-preview-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(12, 20, 40, 0.82), rgba(8, 14, 30, 0.9));
  padding: 10px;
}

@media (max-width: 860px) {
  :root {
    --mobile-controls-h: 116px;
  }

  html, body {
    height: 100dvh;
    overflow: hidden;
  }

  body {
    background: #04060e;
  }

  .app {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    margin: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
    padding: 0;
    display: block;
    background: linear-gradient(180deg, #17120d 0%, #14100d 100%);
  }

  .hud {
    position: fixed;
    top: calc(env(safe-area-inset-top) + 4px);
    left: 6px;
    right: 6px;
    z-index: 75;
    background: transparent;
    border: 0;
    border-radius: 0;
    padding: 0;
    backdrop-filter: none;
  }

  .hud h1 {
    display: none;
  }

  .stats {
    display: none;
  }

  .actions-line {
    margin-top: 0;
    gap: 6px;
    justify-content: space-between;
  }

  .actions-line button {
    min-height: 30px;
    font-size: 13px;
    padding: 4px 9px;
    border-radius: 9px;
    background: linear-gradient(180deg, rgba(144, 104, 58, 0.58), rgba(85, 60, 35, 0.68));
  }

  .game-shell {
    width: 100%;
    margin: 0;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    height: 100dvh;
  }

  .game-frame {
    width: 100vw;
    height: 100%;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    border-top: 0;
  }

  canvas {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: left top;
  }

  .controls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: none;
    width: 100%;
    min-height: var(--mobile-controls-h);
    grid-template-columns: 0.88fr 1.12fr;
    z-index: 70;
    gap: 7px;
    padding: 8px 7px calc(env(safe-area-inset-bottom) + 6px);
    margin: 0;
    border-radius: 0;
    border: 0;
    background: linear-gradient(180deg, rgba(20, 16, 12, 0.12), rgba(20, 16, 12, 0.38));
    box-shadow: none;
  }

  .move-pad button {
    min-width: 40px;
    min-height: 36px;
    font-size: 16px;
    border-radius: 10px;
    padding: 4px;
    border-color: rgba(58, 76, 118, 0.34);
    background: rgba(255, 255, 255, 0.235);
  }

  .combat-btn {
    min-height: 35px;
    padding: 6px 9px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.235);
    border-color: rgba(58, 76, 118, 0.34);
  }

  .combat-label {
    font-size: 13px;
  }

  .combat-icon {
    font-size: 14px;
  }

  .combat-key,
  .move-key {
    font-size: 10px;
  }

  .status {
    display: none;
  }

  .quiz-card,
  .battle-card {
    width: min(80.9vw, 323px);
    max-height: 76vh;
  }

  .quiz-header {
    font-size: 34px;
  }

  .quiz-prompt {
    font-size: 22px;
  }

  .quiz-choice {
    min-height: 52px;
    font-size: 18px;
  }
}

@media (pointer: coarse) and (orientation: landscape) and (min-width: 861px) {
  .app {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    display: grid;
    grid-template-rows: auto auto 1fr auto;
  }

  .game-shell {
    width: min(640px, 62vw);
    margin: 8px auto 0;
  }

  .controls {
    width: min(640px, 62vw);
    margin: 8px auto 0;
  }

  .status {
    display: none;
  }
}
