:root {
  --ink: #14131d;
  --paper: #fffaf2;
  --panel: rgba(255, 250, 242, 0.94);
  --panel-strong: #ffffff;
  --line: rgba(20, 19, 29, 0.15);
  --muted: #665f70;
  --red: #e40303;
  --orange: #ff8c00;
  --yellow: #ffed00;
  --green: #008026;
  --blue: #2447ff;
  --violet: #732982;
  --pink: #ff6fb5;
  --cyan: #63d5ff;
  --shadow: 0 22px 60px rgba(20, 19, 29, 0.22);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--paper);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  font-family: "Trebuchet MS", "Gill Sans", "Segoe UI", sans-serif;
  background:
    linear-gradient(90deg, rgba(228, 3, 3, 0.14), rgba(255, 140, 0, 0.14), rgba(255, 237, 0, 0.16), rgba(0, 128, 38, 0.12), rgba(36, 71, 255, 0.12), rgba(115, 41, 130, 0.16)),
    repeating-linear-gradient(135deg, rgba(20, 19, 29, 0.04) 0 1px, transparent 1px 18px),
    var(--paper);
}

button,
canvas {
  font: inherit;
}

button {
  border: 0;
}

.skip-link {
  position: absolute;
  left: 16px;
  top: -48px;
  z-index: 30;
  padding: 10px 14px;
  color: #fff;
  background: var(--ink);
  border-radius: 4px;
  transition: top 0.15s ease;
}

.skip-link:focus {
  top: 12px;
}

.page-shell {
  width: min(1440px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto;
  gap: 18px;
  align-items: stretch;
  margin-bottom: 18px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: 0 12px 32px rgba(20, 19, 29, 0.1);
  position: relative;
  overflow: hidden;
}

.topbar::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 7px;
  background: linear-gradient(90deg, var(--red), var(--orange), var(--yellow), var(--green), var(--blue), var(--violet), var(--pink), var(--cyan));
}

.brand {
  display: flex;
  gap: 14px;
  align-items: center;
  min-width: 0;
  padding-top: 5px;
}

.brand-mark {
  width: 54px;
  aspect-ratio: 1;
  flex: 0 0 auto;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background:
    linear-gradient(90deg, transparent 0 20%, rgba(255,255,255,0.42) 20% 28%, transparent 28%),
    linear-gradient(180deg, var(--red) 0 16.66%, var(--orange) 16.66% 33.33%, var(--yellow) 33.33% 50%, var(--green) 50% 66.66%, var(--blue) 66.66% 83.33%, var(--violet) 83.33% 100%);
  box-shadow: 7px 7px 0 var(--ink);
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.7rem, 4vw, 3.6rem);
  line-height: 0.95;
}

h2 {
  margin-bottom: 0;
  font-size: clamp(1.45rem, 3vw, 2.4rem);
}

h3 {
  margin-bottom: 8px;
  font-size: 1.12rem;
}

.mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  gap: 8px;
  align-self: center;
  padding-top: 5px;
}

.mode-tab {
  min-height: 56px;
  padding: 10px 12px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid var(--line);
  border-radius: 6px;
  text-align: left;
  cursor: pointer;
  transition: transform 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

.mode-tab:hover,
.mode-tab:focus-visible,
.mode-card:hover,
.mode-card:focus-visible,
.primary-action:hover,
.primary-action:focus-visible,
.secondary-action:hover,
.secondary-action:focus-visible,
.touch-controls button:hover,
.touch-controls button:focus-visible,
.choice-button:hover,
.choice-button:focus-visible,
.memory-card:hover,
.memory-card:focus-visible {
  transform: translateY(-2px);
  outline: none;
}

.mode-tab.is-active {
  background: var(--ink);
  color: #fff;
  box-shadow: 0 10px 0 rgba(20, 19, 29, 0.16);
}

.tab-number {
  display: block;
  margin-bottom: 3px;
  color: inherit;
  opacity: 0.72;
  font-size: 0.72rem;
  font-weight: 900;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  gap: 18px;
  align-items: start;
}

.stage-panel,
.control-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.stage-panel {
  padding: 14px;
}

.stage-header {
  display: flex;
  gap: 18px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(70px, 1fr));
  gap: 8px;
  width: min(360px, 100%);
}

.stat {
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 900;
  text-transform: uppercase;
}

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

.play-area {
  position: relative;
  min-height: 360px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #111522;
  overflow: hidden;
}

#gameCanvas {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #111522;
}

.dom-game {
  min-height: min(68vh, 540px);
  padding: clamp(16px, 3vw, 28px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(255, 250, 242, 0.9)),
    repeating-linear-gradient(90deg, rgba(20, 19, 29, 0.04) 0 1px, transparent 1px 24px);
}

.is-hidden {
  display: none !important;
}

.touch-controls {
  display: none;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
}

.touch-controls button,
.primary-action,
.secondary-action {
  min-height: 46px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 900;
  transition: transform 0.16s ease, box-shadow 0.16s ease;
}

.touch-controls button {
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
}

.control-panel {
  display: grid;
  gap: 12px;
  padding: 14px;
  position: sticky;
  top: 18px;
}

.mission,
.status-card {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
}

.mission p:last-child,
.status-card p:last-child {
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.5;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 0.72fr;
  gap: 8px;
}

.primary-action {
  color: #fff;
  background: linear-gradient(90deg, var(--red), var(--orange), var(--green), var(--blue), var(--violet));
  box-shadow: 0 9px 0 rgba(20, 19, 29, 0.18);
}

.secondary-action {
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
}

.mode-list {
  display: grid;
  gap: 8px;
}

.mode-card {
  display: grid;
  grid-template-columns: 12px 1fr;
  gap: 10px;
  align-items: center;
  min-height: 62px;
  padding: 10px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  transition: transform 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

.mode-card.is-active {
  background: var(--ink);
  color: #fff;
}

.mode-card small {
  display: block;
  margin-top: 3px;
  color: currentColor;
  opacity: 0.68;
}

.card-stripe {
  width: 10px;
  height: 42px;
  border-radius: 20px;
}

.card-stripe.parade {
  background: linear-gradient(180deg, var(--red), var(--orange), var(--yellow), var(--green), var(--blue), var(--violet));
}

.card-stripe.builder {
  background: linear-gradient(180deg, var(--cyan), var(--pink), #fff, var(--pink), var(--cyan));
}

.card-stripe.match {
  background: linear-gradient(180deg, #000, #784f17, var(--red), var(--orange), var(--yellow), var(--green), var(--blue), var(--violet));
}

.builder-board,
.match-board {
  max-width: 900px;
  margin: 0 auto;
}

.builder-title,
.match-title {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: end;
  justify-content: space-between;
  margin-bottom: 18px;
}

.builder-title h3,
.match-title h3 {
  margin-bottom: 0;
  font-size: clamp(1.4rem, 3vw, 2.3rem);
}

.round-pill {
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-weight: 900;
}

.flag-slots {
  display: grid;
  gap: 6px;
  margin-bottom: 18px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.flag-slot {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  min-height: 42px;
  padding: 0 12px;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.62)),
    var(--slot-color, #f2efe7);
  border-bottom: 1px solid rgba(20, 19, 29, 0.12);
  font-weight: 900;
}

.flag-slot:last-child {
  border-bottom: 0;
}

.slot-empty {
  color: rgba(20, 19, 29, 0.48);
}

.choices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  gap: 10px;
}

.choice-button {
  min-height: 54px;
  padding: 10px;
  border: 2px solid rgba(20, 19, 29, 0.18);
  border-radius: 6px;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(255,255,255,0.86), rgba(255,255,255,0.5)),
    var(--choice-color);
  cursor: pointer;
  font-weight: 900;
  text-align: left;
  box-shadow: 0 7px 0 rgba(20, 19, 29, 0.08);
  transition: transform 0.16s ease, box-shadow 0.16s ease, opacity 0.16s ease;
}

.choice-button:disabled {
  cursor: not-allowed;
  opacity: 0.38;
  transform: none;
  box-shadow: none;
}

.builder-message,
.match-message {
  min-height: 26px;
  margin: 14px 0 0;
  color: var(--muted);
  font-weight: 900;
}

.memory-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(92px, 1fr));
  gap: 10px;
}

.memory-card {
  min-height: 112px;
  padding: 10px;
  color: var(--ink);
  background: #fff;
  border: 2px solid rgba(20, 19, 29, 0.18);
  border-radius: 8px;
  cursor: pointer;
  font-weight: 900;
  box-shadow: 0 8px 0 rgba(20, 19, 29, 0.1);
  transition: transform 0.16s ease, box-shadow 0.16s ease;
}

.memory-card.is-open,
.memory-card.is-matched {
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.82), rgba(255,255,255,0.46)),
    var(--card-color);
}

.memory-card.is-matched {
  border-color: var(--green);
}

.memory-card span {
  display: block;
  font-size: 2rem;
  line-height: 1;
}

.memory-card small {
  display: block;
  margin-top: 8px;
}

@media (max-width: 1120px) {
  .topbar,
  .game-layout {
    grid-template-columns: 1fr;
  }

  .control-panel {
    position: static;
  }
}

@media (max-width: 760px) {
  .page-shell {
    padding: 10px;
  }

  .topbar,
  .stage-panel,
  .control-panel {
    border-radius: 6px;
  }

  .mode-tabs,
  .stats,
  .actions {
    grid-template-columns: 1fr;
  }

  .stage-header {
    display: grid;
  }

  .touch-controls {
    display: grid;
  }

  .memory-grid {
    grid-template-columns: repeat(3, minmax(70px, 1fr));
  }

  .memory-card {
    min-height: 92px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
