:root {
  --bg: #d7f0ff;
  --panel: rgba(246, 252, 255, 0.82);
  --panel-border: rgba(26, 89, 147, 0.16);
  --panel-soft: rgba(255, 255, 255, 0.72);
  --text: #17365a;
  --muted: rgba(33, 78, 122, 0.72);
  --accent: #ffb94a;
  --accent-strong: #ff9a2f;
  --danger: #db6b57;
  --success: #1c7c7c;
}

* {
  box-sizing: border-box;
}

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

body {
  background:
    radial-gradient(circle at top, rgba(255, 252, 216, 0.78), rgba(255, 252, 216, 0) 36%),
    radial-gradient(circle at 18% 30%, rgba(115, 240, 255, 0.22), rgba(115, 240, 255, 0) 32%),
    linear-gradient(180deg, #e6f8ff 0%, #d3efff 44%, #c7e5ff 100%);
  color: var(--text);
  font-family: "Space Grotesk", "Noto Serif SC", sans-serif;
}

button,
canvas {
  font: inherit;
}

.game-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 14px;
  width: min(1500px, 100%);
  margin: 0 auto;
  padding:
    calc(env(safe-area-inset-top, 0px) + 18px)
    calc(env(safe-area-inset-right, 0px) + 18px)
    calc(env(safe-area-inset-bottom, 0px) + 18px)
    calc(env(safe-area-inset-left, 0px) + 18px);
}

.game-hud {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.hud-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.hud-chip {
  min-width: 96px;
  display: grid;
  gap: 4px;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 28px rgba(18, 88, 144, 0.12);
}

.hud-word-chip {
  min-width: 220px;
}

.hud-label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
}

.hud-chip strong {
  font-size: 20px;
  line-height: 1;
}

.hud-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.hud-control-stack {
  position: relative;
  display: flex;
  justify-content: flex-end;
}

.hud-btn {
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  padding: 11px 18px;
  cursor: pointer;
  transition: transform 0.18s ease-out, opacity 0.18s ease-out;
  box-shadow: 0 8px 20px rgba(18, 88, 144, 0.12);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.hud-btn:hover:not(:disabled) {
  transform: translateY(-1px);
}

.hud-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.hud-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hud-btn-primary {
  background: var(--accent);
  color: #3f2900;
  border-color: rgba(232, 163, 29, 0.45);
  font-weight: 700;
}

.hud-icon-btn {
  width: 48px;
  min-width: 48px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
}

.settings-popover {
  position: absolute;
  top: calc(100% + 12px);
  right: 0;
  z-index: 10;
  width: min(360px, calc(100vw - 36px));
  display: grid;
  gap: 14px;
  padding: 18px;
  border-radius: 22px;
  background: rgba(255, 249, 238, 0.96);
  border: 1px solid rgba(116, 88, 37, 0.16);
  box-shadow: 0 24px 48px rgba(137, 102, 37, 0.22);
  backdrop-filter: blur(12px);
}

.settings-popover.hidden {
  display: none;
}

.settings-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.settings-title {
  font-size: 18px;
  font-weight: 700;
}

.settings-caption {
  margin: 6px 0 0;
  color: var(--muted);
  line-height: 1.5;
  font-size: 13px;
}

.settings-badge {
  min-width: 66px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(244, 183, 63, 0.18);
  color: #8a5900;
  text-align: center;
  font-weight: 700;
  font-size: 18px;
}

.settings-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.settings-range {
  width: 100%;
  accent-color: var(--accent-strong);
}

.settings-scale {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 12px;
}

.settings-row {
  display: flex;
  align-items: end;
  gap: 12px;
}

.settings-number-wrap {
  flex: 1 1 auto;
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

.settings-number {
  width: 100%;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.75);
  color: var(--text);
  padding: 12px 14px;
  font: inherit;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

.settings-number:focus,
.settings-range:focus,
.hud-icon-btn:focus-visible {
  outline: 2px solid rgba(232, 163, 29, 0.46);
  outline-offset: 2px;
}

.settings-done {
  flex: 0 0 auto;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(40, 25, 4, 0.42);
  backdrop-filter: blur(10px);
}

.modal-backdrop.hidden {
  display: none;
}

.modal-card {
  width: min(760px, 100%);
  max-height: min(90dvh, 920px);
  overflow: auto;
  display: grid;
  gap: 16px;
  padding: 24px;
  border-radius: 28px;
  background: rgba(255, 250, 241, 0.98);
  border: 1px solid rgba(116, 88, 37, 0.14);
  box-shadow: 0 30px 68px rgba(79, 56, 17, 0.24);
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.modal-tag {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(244, 183, 63, 0.16);
  color: #8a5900;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.modal-head h2 {
  margin: 10px 0 0;
  font-size: 28px;
  line-height: 1.1;
}

.modal-close {
  width: 42px;
  height: 42px;
  border: 1px solid var(--panel-border);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  color: var(--text);
  font: inherit;
  font-size: 20px;
  cursor: pointer;
}

.modal-copy {
  margin: 0;
  color: var(--muted);
  line-height: 1.7;
}

.modal-mode-toggle {
  display: inline-flex;
  gap: 8px;
  padding: 6px;
  border-radius: 999px;
  background: rgba(47, 37, 22, 0.06);
  justify-self: start;
}

.modal-mode-btn {
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 700;
  padding: 10px 16px;
  cursor: pointer;
}

.modal-mode-btn.active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 8px 18px rgba(79, 56, 17, 0.12);
}

.modal-panel {
  display: none;
  gap: 8px;
}

.modal-panel.active {
  display: grid;
}

.modal-label {
  font-size: 14px;
  font-weight: 700;
  color: var(--muted);
}

.modal-textarea {
  width: 100%;
  min-height: 220px;
  resize: vertical;
  padding: 16px 18px;
  border-radius: 18px;
  border: 1px solid rgba(116, 88, 37, 0.14);
  background: #fffdf8;
  color: var(--text);
  font: inherit;
  line-height: 1.7;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.68);
}

.modal-textarea:focus {
  outline: 2px solid rgba(232, 163, 29, 0.36);
  outline-offset: 2px;
}

.modal-hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.modal-preview {
  padding: 16px 18px;
  border-radius: 18px;
  background: rgba(255, 245, 225, 0.8);
  border: 1px solid rgba(244, 183, 63, 0.18);
  color: #7c5d28;
  line-height: 1.7;
}

.modal-preview strong {
  color: var(--text);
}

.modal-error {
  min-height: 24px;
  color: var(--danger);
  font-size: 14px;
  line-height: 1.5;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.game-stage {
  position: relative;
  min-height: 0;
  border-radius: 28px;
  overflow: hidden;
  border: 1px solid rgba(26, 89, 147, 0.14);
  background: linear-gradient(180deg, #eaf8ff 0%, #d8efff 100%);
  box-shadow: 0 28px 54px rgba(18, 88, 144, 0.16);
}

#gameCanvas {
  width: 100%;
  height: min(74vh, 900px);
  min-height: 480px;
  display: block;
}

.screen-card {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 4;
  transform: translate(-50%, -50%);
  width: min(560px, calc(100% - 40px));
  display: grid;
  gap: 14px;
  padding: 28px;
  border-radius: 24px;
  background: rgba(246, 252, 255, 0.9);
  border: 1px solid rgba(26, 89, 147, 0.14);
  box-shadow: 0 20px 40px rgba(18, 88, 144, 0.18);
}

.screen-card.hidden {
  display: none;
}

.screen-tag {
  justify-self: start;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(115, 240, 255, 0.18);
  color: #14567b;
  font-size: 12px;
  font-weight: 700;
}

.screen-card h1 {
  margin: 0;
  font-size: clamp(32px, 4vw, 54px);
  line-height: 1.05;
}

.screen-card p {
  margin: 0;
  color: var(--muted);
  line-height: 1.7;
}

.screen-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.game-feedback-banner {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.game-feedback-banner[data-state="ready"] {
  background: rgba(255, 249, 236, 0.96);
  color: #7a6130;
}

.game-feedback-banner[data-state="warn"] {
  background: rgba(255, 242, 210, 0.97);
  border-color: rgba(232, 163, 29, 0.28);
  color: #8c5d00;
}

.game-feedback-banner[data-state="pass"] {
  background: rgba(234, 251, 242, 0.96);
  border-color: rgba(37, 155, 119, 0.24);
  color: #1f765b;
}

.game-feedback-banner[data-state="fail"] {
  background: rgba(255, 240, 234, 0.97);
  border-color: rgba(219, 107, 87, 0.26);
  color: #a14837;
}

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

@media (max-width: 900px) {
  .game-shell {
    padding:
      calc(env(safe-area-inset-top, 0px) + 14px)
      calc(env(safe-area-inset-right, 0px) + 14px)
      calc(env(safe-area-inset-bottom, 0px) + 14px)
      calc(env(safe-area-inset-left, 0px) + 14px);
  }

  .hud-chip strong {
    font-size: 18px;
  }

  #gameCanvas {
    min-height: 420px;
    height: min(68vh, 700px);
  }

  .screen-card {
    padding: 22px;
  }

  .game-feedback-banner {
    top: 18px;
    width: calc(100% - 32px);
    min-height: 68px;
    padding: 16px 20px;
    font-size: clamp(19px, 3vw, 28px);
  }

  .modal-backdrop {
    padding: 16px;
  }

  .modal-card {
    padding: 20px;
    border-radius: 24px;
  }

  .modal-mode-toggle {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .modal-mode-btn {
    text-align: center;
  }
}

@media (max-width: 640px) {
  .hud-stats,
  .hud-actions,
  .hud-control-stack,
  .screen-actions {
    width: 100%;
  }

  .hud-actions .hud-btn,
  .screen-actions .hud-btn {
    flex: 1 1 0;
    justify-content: center;
  }

  .hud-actions .hud-icon-btn {
    flex: 0 0 48px;
  }

  .hud-chip,
  .hud-word-chip {
    min-width: calc(50% - 6px);
  }

  #gameCanvas {
    min-height: 360px;
    height: 58vh;
  }

  .game-feedback-banner {
    top: 14px;
    width: calc(100% - 20px);
    min-height: 60px;
    padding: 14px 16px;
    border-radius: 18px;
    font-size: clamp(17px, 4.8vw, 22px);
    line-height: 1.3;
  }

  .settings-popover {
    left: 0;
    right: 0;
    width: auto;
  }

  .modal-head {
    flex-direction: column;
  }

  .modal-close {
    align-self: flex-end;
  }

  .modal-actions {
    flex-direction: column-reverse;
  }

  .modal-actions .hud-btn {
    width: 100%;
    justify-content: center;
  }
}
