:root {
  --paper: #fff7db;
  --sand: #efd28d;
  --sun: #f5b642;
  --canyon: #b24a24;
  --brick: #6f2f1d;
  --ink: #16120d;
  --smoke: #4c4941;
  --leaf: #2f7a47;
  --sky: #88c5d8;
  --water: #2377a6;
  --danger: #d93636;
  --gold: #f6c737;
  --orange: #f0782f;
  --panel: rgba(255, 250, 228, 0.93);
  --shadow: 0 18px 48px rgba(67, 34, 10, 0.22);
  --line: 3px solid var(--ink);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: 'Courier New', Courier, monospace;
  background:
    linear-gradient(
      180deg,
      rgba(136, 197, 216, 0.7),
      rgba(255, 247, 219, 0.85) 46%,
      rgba(239, 210, 141, 0.95)
    ),
    repeating-linear-gradient(
      90deg,
      rgba(22, 18, 13, 0.04) 0 2px,
      transparent 2px 16px
    );
}

button {
  font: inherit;
  touch-action: manipulation;
}

input {
  font: inherit;
}

.game-shell {
  width: min(1280px, calc(100vw - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 20px 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 18px;
  align-items: start;
}

.game-panel,
.side-panel {
  border: var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.game-panel {
  min-width: 0;
}

.top-bar {
  min-height: 84px;
  padding: 14px;
  display: grid;
  grid-template-columns: minmax(170px, 1fr) auto auto 48px;
  gap: 14px;
  align-items: center;
  border-bottom: var(--line);
}

.brand-block h1,
.overlay-card h2,
.quiz-card h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand-block h1 {
  font-size: clamp(1.35rem, 3vw, 2.25rem);
  line-height: 1;
  text-transform: uppercase;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--canyon);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
}

.scoreboard {
  display: grid;
  grid-template-columns: repeat(3, minmax(72px, 1fr));
  gap: 8px;
}

.scoreboard div,
.stat-pill,
.panel-section {
  border: 2px solid var(--ink);
  background: #fffdf2;
}

.scoreboard div {
  min-height: 50px;
  padding: 6px 10px;
}

.scoreboard span,
.stat-pill span,
.section-title span,
.quest-card span,
.shop-card span {
  display: block;
  color: var(--smoke);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

.scoreboard strong,
.stat-pill strong {
  display: block;
  margin-top: 3px;
  font-size: 1.25rem;
  line-height: 1;
}

.icon-button,
.primary-button,
.shop-card button,
.answer-grid button {
  border: 2px solid var(--ink);
  color: var(--ink);
  background: var(--gold);
  box-shadow: 4px 4px 0 var(--ink);
  cursor: pointer;
  transition:
    transform 120ms ease,
    box-shadow 120ms ease,
    background 120ms ease;
}

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  font-weight: 900;
}

.primary-button {
  min-height: 48px;
  padding: 0 24px;
  font-weight: 900;
  text-transform: uppercase;
}

.icon-button:hover,
.primary-button:hover,
.shop-card button:hover,
.answer-grid button:hover {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 var(--ink);
}

.icon-button:disabled,
.primary-button:disabled,
.shop-card button:disabled,
.answer-grid button:disabled {
  color: #7f7866;
  background: #d9d1b4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 7;
  min-height: 280px;
  overflow: hidden;
  background: var(--sky);
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  padding: 18px;
  display: grid;
  place-items: center;
  background: rgba(22, 18, 13, 0.28);
  overflow-y: auto;
}

.overlay.hidden,
.hidden {
  display: none !important;
}

.overlay-card,
.quiz-card {
  width: min(520px, 100%);
  border: var(--line);
  background: #fff7db;
  box-shadow: 8px 8px 0 var(--ink);
  padding: 22px;
}

.overlay-card h2,
.quiz-card h2 {
  font-size: clamp(1.55rem, 4vw, 2.6rem);
  line-height: 1.05;
}

.overlay-copy {
  color: var(--smoke);
  font-weight: 700;
  line-height: 1.5;
}

/* ---------- Hearts HUD (top bar) ---------- */

.hearts-hud {
  min-width: 112px;
  min-height: 44px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 2px solid var(--ink);
  background: #fffdf2;
}

.heart-icon {
  color: var(--danger);
  font-size: 1.5rem;
  line-height: 1;
  text-shadow: 1px 1px 0 var(--ink);
}

.heart-icon.empty {
  color: #c9c3b2;
  text-shadow: none;
  opacity: 0.6;
}

/* ---------- Mode selection ---------- */

.mode-card {
  width: min(540px, 100%);
}

.mode-grid {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}

.mode-tile {
  padding: 18px 14px 16px;
  border: 3px solid var(--ink);
  background: #fffdf2;
  box-shadow: 5px 5px 0 var(--ink);
  cursor: pointer;
  text-align: center;
  transition:
    transform 120ms ease,
    box-shadow 120ms ease,
    background 120ms ease;
}

.mode-tile:hover {
  transform: translate(2px, 2px);
  box-shadow: 3px 3px 0 var(--ink);
  background: var(--sand);
}

.mode-tile:active {
  transform: translate(4px, 4px);
  box-shadow: 1px 1px 0 var(--ink);
}

.mode-icon {
  display: block;
  margin: 0 auto 8px;
  width: 54px;
  height: 54px;
  border: 3px solid var(--ink);
  background: var(--gold);
  box-shadow: 3px 3px 0 var(--ink);
  font-size: 1.4rem;
  font-weight: 900;
  line-height: 48px;
  text-align: center;
}

.mode-tile--multi .mode-icon {
  background: var(--orange);
}

.mode-label {
  display: block;
  margin-bottom: 4px;
  font-size: 1.15rem;
  font-weight: 900;
  text-transform: uppercase;
  line-height: 1.15;
}

.mode-desc {
  display: block;
  color: var(--smoke);
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.3;
}

/* ---------- Lobby ---------- */

.lobby-card {
  position: relative;
  width: min(480px, 100%);
}

.lobby-field {
  margin: 10px 0 18px;
}

.field-label {
  display: block;
  margin: 0 0 6px;
  color: var(--smoke);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.text-input {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 3px solid var(--ink);
  color: var(--ink);
  background: #fffdf2;
  font-weight: 900;
  box-shadow: inset 2px 2px 0 rgba(22, 18, 13, 0.08);
}

.text-input:focus {
  outline: none;
  border-color: var(--canyon);
  box-shadow: 0 0 0 3px rgba(178, 74, 36, 0.2);
}

.text-input::placeholder {
  color: #b0a88e;
  font-weight: 700;
}

.code-input {
  text-transform: uppercase;
  letter-spacing: 0.15em;
  text-align: center;
  font-size: 1.1rem;
}

.lobby-actions {
  display: grid;
  gap: 14px;
}

.lobby-action-section {
  display: grid;
  gap: 8px;
}

.lobby-action-label {
  display: block;
  color: var(--smoke);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.lobby-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #b0a88e;
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
}

.lobby-divider::before,
.lobby-divider::after {
  content: '';
  flex: 1;
  height: 2px;
  background: #d9d1b4;
}

.join-row {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 10px;
}

.lobby-error {
  min-height: 1.3em;
  margin: 14px 0 0;
  color: var(--danger);
  font-weight: 900;
  font-size: 0.85rem;
}

.lobby-back-link {
  margin-top: 6px;
  padding: 8px;
  border: none;
  background: none;
  color: var(--smoke);
  font-weight: 900;
  font-size: 0.82rem;
  text-transform: uppercase;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.lobby-back-link:hover {
  color: var(--canyon);
}

/* ---------- Party room ---------- */

.party-code {
  margin: 6px 0 16px;
  padding: 12px 16px;
  border: 3px solid var(--ink);
  background:
    repeating-linear-gradient(
      90deg,
      #fffdf2 0,
      #fffdf2 18%,
      #f0e8c8 18%,
      #f0e8c8 20%,
      #fffdf2 20%
    );
  box-shadow: 4px 4px 0 var(--ink);
  font-size: clamp(2.2rem, 8vw, 3.6rem);
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0.2em;
  text-align: center;
}

.lobby-meta {
  margin-bottom: 14px;
  padding: 10px 12px;
  border: 2px solid var(--ink);
  background: #fffdf2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.lobby-meta-count {
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 900;
  text-transform: uppercase;
}

.lobby-meta-count strong {
  font-size: 1.1rem;
}

.lobby-meta-hint {
  color: var(--smoke);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}

.player-list {
  margin-bottom: 14px;
  display: grid;
  gap: 6px;
}

.player-row {
  min-height: 42px;
  padding: 8px 10px;
  border: 2px solid var(--ink);
  background: #fffdf2;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.player-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-tags {
  flex: 0 0 auto;
  color: var(--smoke);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.party-room-buttons {
  display: grid;
  gap: 4px;
  text-align: center;
}

.start-party-button {
  min-height: 52px;
  font-size: 1.05rem;
}

/* ---------- Countdown ---------- */

.countdown-splash {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  color: var(--gold);
  background: rgba(22, 18, 13, 0.78);
  font-size: clamp(4rem, 18vw, 9rem);
  font-weight: 900;
  line-height: 1;
  text-shadow:
    5px 5px 0 var(--ink),
    -2px -2px 0 var(--ink),
    2px -2px 0 var(--ink),
    -2px 2px 0 var(--ink);
}

/* ---------- Opponent sidebar ---------- */

.opponent-sidebar {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 2;
  width: min(190px, calc(100% - 28px));
  max-height: calc(100% - 28px);
  padding: 6px;
  display: grid;
  gap: 5px;
  overflow-y: auto;
  border: 2px solid var(--ink);
  background: rgba(255, 247, 219, 0.9);
  box-shadow: 3px 3px 0 rgba(22, 18, 13, 0.3);
}

.opponent-row {
  padding: 6px 7px;
  border: 2px solid var(--ink);
  background: #fffdf2;
  display: grid;
  gap: 3px;
}

.opponent-name {
  min-width: 0;
  overflow: hidden;
  color: var(--ink);
  font-size: 0.72rem;
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mini-hearts {
  display: flex;
  gap: 4px;
}

.mini-hearts .heart-icon {
  font-size: 0.78rem;
  text-shadow: none;
}

/* ---------- Results ---------- */

.results-card {
  text-align: center;
}

.results-card h2 {
  margin-bottom: 14px;
}

.placements-list {
  margin-bottom: 16px;
  display: grid;
  gap: 8px;
  text-align: left;
}

.placement-row {
  min-height: 46px;
  padding: 10px 12px;
  border: 2px solid var(--ink);
  background: #fffdf2;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.placement-row.first {
  border-width: 3px;
  background:
    linear-gradient(135deg, #fff4c2 0%, #ffe38d 50%, #ffd54f 100%);
  box-shadow: 3px 3px 0 var(--ink);
}

.placement-row.second {
  background:
    linear-gradient(135deg, #f0ede6 0%, #dedad0 100%);
}

.placement-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.placement-reward {
  flex: 0 0 auto;
  padding: 3px 8px;
  border: 2px solid var(--ink);
  background: var(--gold);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.placement-row.first .placement-reward {
  background: var(--orange);
  color: #fff7db;
}

.reward-badge {
  margin: 0 auto 16px;
  padding: 8px 20px;
  border: 3px solid var(--ink);
  background: var(--gold);
  box-shadow: 4px 4px 0 var(--ink);
  font-size: 1.2rem;
  font-weight: 900;
  text-transform: uppercase;
  display: inline-block;
}

/* ---------- Quiz ---------- */

.quiz-card {
  width: min(720px, 100%);
}

.quiz-card h2 {
  margin-bottom: 18px;
  font-size: clamp(1.05rem, 3vw, 1.55rem);
  line-height: 1.35;
}

.answer-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.answer-grid button {
  min-height: 56px;
  padding: 10px 12px;
  background: #ffffff;
  text-align: left;
  font-weight: 800;
  line-height: 1.25;
}

.answer-grid button.correct {
  background: #8ee18e;
}

.answer-grid button.wrong {
  background: #ff8a76;
}

/* ---------- Banner ---------- */

.banner {
  position: absolute;
  top: 22px;
  left: 50%;
  z-index: 3;
  translate: -50% 0;
  padding: 8px 16px;
  border: 2px solid var(--ink);
  color: var(--ink);
  background: var(--gold);
  box-shadow: 4px 4px 0 var(--ink);
  font-size: clamp(1rem, 3vw, 2rem);
  font-weight: 900;
  text-transform: uppercase;
}

.banner.level-three {
  background: var(--orange);
}

/* ---------- Bottom strip ---------- */

.bottom-strip {
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  border-top: var(--line);
}

.stat-pill {
  min-height: 58px;
  padding: 9px 11px;
}

/* ---------- Side panel ---------- */

.side-panel {
  padding: 12px;
  display: grid;
  gap: 14px;
}

.panel-section {
  padding: 12px;
}

.section-title {
  margin-bottom: 10px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  list-style: none;
}

.section-title::-webkit-details-marker {
  display: none;
}

summary.section-title {
  cursor: pointer;
}

summary.section-title::after {
  content: '-';
  color: var(--ink);
  font-weight: 900;
}

.panel-section:not([open]) summary.section-title {
  margin-bottom: 0;
}

.panel-section:not([open]) summary.section-title::after {
  content: '+';
}

.quest-list,
.shop-list {
  display: grid;
  gap: 10px;
}

.quest-card,
.shop-card {
  border: 2px solid var(--ink);
  background: #fff7db;
  padding: 10px;
}

.quest-card.complete {
  background: #e0f4c7;
}

.quest-card p,
.shop-card p {
  margin: 4px 0 8px;
  font-size: 0.9rem;
  font-weight: 800;
  line-height: 1.25;
}

.progress-track {
  height: 12px;
  border: 2px solid var(--ink);
  background: #ffffff;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0;
  background: var(--leaf);
}

.shop-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
}

.shop-card button {
  min-width: 78px;
  min-height: 38px;
  padding: 0 10px;
  background: var(--sun);
  font-weight: 900;
}

/* ---------- Responsive: tablet ---------- */

@media (max-width: 980px) {
  .game-shell {
    grid-template-columns: 1fr;
  }

  .side-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* ---------- Responsive: mobile ---------- */

@media (max-width: 720px) {
  .game-shell {
    width: 100%;
    min-height: 100svh;
    padding: 6px;
    gap: 8px;
  }

  .game-panel,
  .side-panel {
    border-width: 2px;
    box-shadow: 5px 5px 0 rgba(22, 18, 13, 0.22);
  }

  .top-bar {
    min-height: 0;
    padding: 8px;
    grid-template-columns: minmax(0, 1fr) auto 44px;
    gap: 8px;
  }

  .brand-block h1 {
    font-size: 1.15rem;
    line-height: 1.05;
  }

  .scoreboard {
    grid-column: 1 / -1;
    grid-row: 2;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .hearts-hud {
    min-width: 96px;
    min-height: 40px;
    padding: 6px 8px;
  }

  .heart-icon {
    font-size: 1.15rem;
  }

  .scoreboard div {
    min-height: 44px;
    padding: 6px;
  }

  .scoreboard strong,
  .stat-pill strong {
    font-size: 1rem;
  }

  .canvas-wrap {
    aspect-ratio: 4 / 3;
    min-height: min(340px, calc(100svh - 230px));
  }

  .side-panel,
  .answer-grid {
    grid-template-columns: 1fr;
  }

  .bottom-strip {
    padding: 8px;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .stat-pill {
    min-height: 46px;
    padding: 7px 6px;
  }

  .scoreboard span,
  .stat-pill span {
    font-size: 0.62rem;
  }

  .side-panel {
    padding: 8px;
    gap: 8px;
  }

  .panel-section {
    padding: 10px;
  }

  .overlay {
    padding: 10px;
    place-items: center;
  }

  .overlay-card,
  .quiz-card {
    max-height: calc(100% - 20px);
    overflow-y: auto;
    padding: 14px;
    box-shadow: 5px 5px 0 var(--ink);
  }

  .overlay-copy {
    margin: 8px 0 12px;
    font-size: 0.85rem;
    line-height: 1.35;
  }

  .primary-button {
    width: 100%;
    min-height: 52px;
  }

  .mode-grid {
    gap: 10px;
  }

  .mode-tile {
    padding: 14px 10px 12px;
  }

  .mode-icon {
    width: 44px;
    height: 44px;
    font-size: 1.15rem;
    line-height: 38px;
  }

  .mode-label {
    font-size: 0.95rem;
  }

  .join-row {
    grid-template-columns: 1fr;
  }

  .lobby-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .party-code {
    font-size: 2.4rem;
    letter-spacing: 0.15em;
  }

  .opponent-sidebar {
    top: auto;
    right: 8px;
    bottom: 8px;
    width: min(170px, calc(100% - 16px));
    max-height: 38%;
    padding: 5px;
  }

  .opponent-row {
    padding: 5px 6px;
  }

  .quiz-card h2 {
    margin-bottom: 12px;
    font-size: 0.95rem;
    line-height: 1.25;
  }

  .answer-grid {
    gap: 8px;
  }

  .answer-grid button {
    min-height: 48px;
    padding: 9px 10px;
    font-size: 0.84rem;
  }

  .banner {
    top: 10px;
    width: max-content;
    max-width: calc(100% - 28px);
    padding: 6px 10px;
    text-align: center;
  }

  .shop-card {
    grid-template-columns: minmax(0, 1fr) 68px;
  }

  .shop-card button {
    min-width: 68px;
    min-height: 48px;
  }
}

@media (max-width: 380px) {
  .canvas-wrap {
    min-height: 280px;
  }

  .brand-block h1 {
    font-size: 1rem;
  }

  .mode-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .stat-pill span {
    font-size: 0.56rem;
  }

  .answer-grid button {
    font-size: 0.78rem;
  }
}
