html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  padding: 20px;
  font-family: Arial, sans-serif;
  background: #1a1a1a;
  color: #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  min-height: 100dvh;
}

.game-container {
  text-align: center;
  width: min(1000px, 100%);
}

h1 {
  margin: 0 0 20px 0;
  font-size: 2.5em;
  color: #4A90E2;
}

.canvas-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  width: min(1000px, 100%);
  aspect-ratio: 1000 / 700;
  max-height: min(700px, calc(100dvh - 9rem));
  margin: 0 auto 20px auto;
}

canvas {
  border: 2px solid #4A90E2;
  background: #2a2a2a;
  display: block;
  cursor: crosshair;
  /* Keep the internal 1000x700 bitmap; CSS only scales the element. */
  image-rendering: auto;
}

.controls-hint {
  margin: 10px 0 0;
  padding: 0 8px;
  color: #8b93a0;
  font-size: 0.72rem;
  line-height: 1.7;
  letter-spacing: 0.01em;
  text-align: center;
  user-select: none;
}

.controls-hint kbd {
  display: inline-block;
  margin: 0 1px;
  padding: 1px 5px;
  font: 600 0.68rem/1.3 ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  color: #a8b0bc;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-bottom-color: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.controls-hint .dot {
  margin: 0 0.45em;
  color: #6e7682;
}

@media (pointer: coarse) {
  .controls-hint {
    display: none;
  }
}

.rotate-hint {
  display: none;
}

.home-link {
  position: fixed;
  top: max(16px, env(safe-area-inset-top, 0px));
  left: max(16px, env(safe-area-inset-left, 0px));
  z-index: 30;
  color: #e8f2ff;
  text-decoration: none;
  font-size: 0.95rem;
  font-weight: 700;
  padding: 8px 12px;
  min-height: 48px;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  border-radius: 8px;
  background: rgba(26, 26, 26, 0.85);
  border: 1px solid #4A90E2;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.home-link:hover,
.home-link:focus-visible {
  color: #ffffff;
  border-color: #7eb6f0;
}

.home-link:focus-visible {
  outline: 2px solid #7eb6f0;
  outline-offset: 2px;
}

/* --- Touch overlay (added only on touch / coarse-pointer devices) ------ */

.touch-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.dpad,
.touch-actions,
.touch-btn {
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.dpad {
  position: absolute;
  left: max(12px, env(safe-area-inset-left, 0px));
  bottom: max(16px, env(safe-area-inset-bottom, 0px));
  display: grid;
  grid-template-columns: 56px 56px 56px;
  grid-template-rows: 56px 56px 56px;
  grid-template-areas:
    ". up ."
    "left mid right"
    ". down .";
  gap: 4px;
}

.dpad-up { grid-area: up; }
.dpad-left { grid-area: left; }
.dpad-right { grid-area: right; }
.dpad-down { grid-area: down; }

.touch-actions {
  position: absolute;
  right: max(12px, env(safe-area-inset-right, 0px));
  bottom: max(16px, env(safe-area-inset-bottom, 0px));
  display: grid;
  grid-template-columns: 64px 64px;
  gap: 8px;
  justify-items: stretch;
}

.touch-btn {
  margin: 0;
  padding: 0;
  min-width: 48px;
  min-height: 48px;
  border: 1px solid rgba(126, 182, 240, 0.7);
  border-radius: 14px;
  background: rgba(18, 28, 44, 0.45);
  color: #e8f2ff;
  font: 700 0.72rem/1.1 Arial, system-ui, sans-serif;
  letter-spacing: 0.02em;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.dpad-btn {
  font-size: 1.15rem;
  border-radius: 16px;
}

.action-hint {
  font-size: 0.65rem;
  font-weight: 700;
  opacity: 0.8;
}

.action-label {
  font-size: 0.72rem;
  font-weight: 700;
}

.touch-btn.is-down {
  background: rgba(74, 144, 226, 0.55);
  border-color: #9ec7f0;
  color: #fff;
  transform: scale(0.96);
}

.restart-btn {
  grid-column: 1 / -1;
  min-height: 48px;
}

/* --- Phone layout ------------------------------------------------------ */

html.touch-controls,
html.touch-controls body {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  height: 100%;
  min-height: 100%;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

html.touch-controls body {
  display: block;
  padding: 0;
  width: 100%;
  min-height: 100dvh;
  min-height: 100vh;
}

html.touch-controls .game-container {
  width: 100%;
  height: 100dvh;
  height: 100vh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: calc(env(safe-area-inset-top, 0px) + 56px);
  padding-right: env(safe-area-inset-right, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
}

html.touch-controls h1 {
  font-size: 1.2rem;
  margin: 0 0 6px 0;
}

html.touch-controls .controls-hint {
  display: none;
}

html.touch-controls .canvas-wrap {
  flex: 1 1 auto;
  width: 100%;
  max-height: none;
  aspect-ratio: auto;
  min-height: 0;
  margin: 0;
}

html.touch-controls canvas {
  cursor: default;
}

@media (pointer: coarse) and (orientation: portrait) {
  .rotate-hint {
    display: block;
    margin: 0 16px 8px;
    padding: 6px 12px;
    font-size: 0.85rem;
    color: #c5d8f0;
    background: rgba(26, 26, 26, 0.65);
    border: 1px solid rgba(74, 144, 226, 0.45);
    border-radius: 8px;
  }

  html.touch-controls .game-container {
    padding-top: calc(env(safe-area-inset-top, 0px) + 64px);
  }

  html.touch-controls .canvas-wrap {
    align-items: flex-end;
    padding-bottom: 300px;
  }
}

@media (pointer: coarse) and (orientation: landscape) {
  html.touch-controls h1 {
    position: absolute;
    left: 50%;
    top: max(6px, env(safe-area-inset-top, 0px));
    transform: translateX(-50%);
    font-size: 1rem;
    margin: 0;
    z-index: 15;
  }

  html.touch-controls .game-container {
    padding-top: calc(env(safe-area-inset-top, 0px) + 28px);
  }

  html.touch-controls .canvas-wrap {
    padding-left: 176px;
    padding-right: 144px;
  }

  .dpad {
    grid-template-columns: 48px 48px 48px;
    grid-template-rows: 48px 48px 48px;
  }

  .touch-actions {
    grid-template-columns: 56px 56px;
    gap: 6px;
  }
}

/* --- Math quiz modal --------------------------------------------------- */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.quiz-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  background: rgba(8, 12, 20, 0.78);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.quiz-overlay[hidden] {
  display: none;
}

.quiz-card {
  width: min(28rem, 100%);
  padding: 28px 24px 22px;
  background: #16202c;
  border: 2px solid #4A90E2;
  border-radius: 18px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  text-align: center;
  color: #e8f2ff;
}

.quiz-card h2 {
  margin: 0 0 12px;
  font-size: 1.35rem;
  font-weight: 700;
  color: #7eb6f0;
}

.quiz-question {
  margin: 0 0 18px;
  font-size: clamp(2rem, 6vw, 2.8rem);
  font-weight: 800;
  letter-spacing: 0.02em;
  color: #ffffff;
  line-height: 1.2;
}

.quiz-answer {
  display: block;
  width: min(8rem, 100%);
  margin: 0 auto 16px;
  padding: 10px 12px;
  font-size: 2rem;
  font-weight: 700;
  text-align: center;
  color: #ffffff;
  background: #0f1620;
  border: 2px solid #4A90E2;
  border-radius: 12px;
  outline: none;
  caret-color: #7eb6f0;
}

.quiz-answer:focus {
  border-color: #9ec7f0;
  box-shadow: 0 0 0 3px rgba(126, 182, 240, 0.35);
}

.quiz-pad {
  display: none;
  grid-template-columns: repeat(3, minmax(56px, 1fr));
  gap: 8px;
  width: min(17rem, 100%);
  margin: 0 auto 12px;
}

.quiz-overlay.is-touch .quiz-pad {
  display: grid;
}

.quiz-overlay.is-touch .quiz-enter-hint {
  display: none;
}

.quiz-pad-btn {
  margin: 0;
  min-width: 48px;
  min-height: 52px;
  padding: 0;
  border: 1px solid rgba(126, 182, 240, 0.7);
  border-radius: 12px;
  background: rgba(18, 28, 44, 0.9);
  color: #e8f2ff;
  font: 700 1.2rem/1 Arial, system-ui, sans-serif;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.quiz-pad-btn:active,
.quiz-pad-btn:focus-visible {
  background: rgba(74, 144, 226, 0.45);
  border-color: #9ec7f0;
}

.quiz-pad-ok {
  background: rgba(74, 144, 226, 0.35);
  font-size: 1rem;
}

.quiz-pad-del {
  font-size: 1.1rem;
}

.quiz-feedback {
  min-height: 1.4em;
  margin: 4px 0 0;
  font-size: 1.05rem;
  font-weight: 700;
  color: #c5d8f0;
}

.quiz-feedback.is-wrong {
  color: #ffc48a;
}

.quiz-feedback.is-ok {
  color: #9be7a8;
}

.quiz-enter-hint {
  margin: 10px 0 0;
  font-size: 0.85rem;
  color: #a8b7c8;
}

.quiz-enter-hint kbd {
  display: inline-block;
  padding: 1px 5px;
  font: 600 0.8rem/1.3 ui-monospace, Menlo, Consolas, monospace;
  color: #d5dde8;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 3px;
}

html.quiz-open,
html.quiz-open body {
  overflow: hidden;
}

html.quiz-open .touch-overlay {
  visibility: hidden;
  pointer-events: none;
}
