:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --border: #dfe3e8;
  --border-strong: #cbd2da;
  --text: #17202a;
  --muted: #66717f;
  --muted-2: #8a94a1;
  --primary: #3578e5;
  --primary-hover: #2c68ca;
  --primary-soft: #eaf1ff;
  --danger: #c94b4b;
  --warning: #d88a23;
  --success: #2f8f62;
  --shadow: 0 8px 30px rgba(31, 41, 55, 0.06);
  --shadow-small: 0 3px 12px rgba(31, 41, 55, 0.06);
  --radius: 14px;
  --transition: 180ms ease;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #11161c;
  --surface: #1a2129;
  --surface-soft: #202831;
  --border: #2c3743;
  --border-strong: #3b4856;
  --text: #eef2f6;
  --muted: #a1adba;
  --muted-2: #7e8a97;
  --primary: #5b91eb;
  --primary-hover: #76a6f0;
  --primary-soft: #202f47;
  --danger: #e57676;
  --warning: #e0a45a;
  --success: #55b686;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  --shadow-small: 0 3px 12px rgba(0, 0, 0, 0.18);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(53, 120, 229, 0.36);
  outline-offset: 2px;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.site-header {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  color: var(--text);
  text-decoration: none;
  font-weight: 800;
  letter-spacing: -0.02em;
  font-size: 18px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-chip {
  min-width: 74px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow-small);
  display: grid;
  gap: 1px;
  text-align: center;
}

.stat-chip strong {
  font-size: 14px;
  line-height: 1.05;
}

.stat-label {
  color: var(--muted);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  background: var(--surface);
  transition: background var(--transition), border-color var(--transition), transform var(--transition);
}

.icon-button:hover {
  background: var(--surface-soft);
  border-color: var(--border-strong);
}

.icon-button:active,
.tool-button:active,
.secondary-button:active,
.primary-button:active {
  transform: translateY(1px);
}

.theme-icon {
  font-size: 17px;
  line-height: 1;
}

.main-content {
  flex: 1;
  width: 100%;
  padding: 16px 16px 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.screen {
  display: none;
  width: min(980px, 100%);
}

.screen.is-active {
  display: block;
  animation: screen-in 220ms ease both;
}

.card {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.home-card {
  max-width: 660px;
  margin: 0 auto;
  padding: clamp(32px, 7vw, 68px) 28px;
  text-align: center;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.home-card h1,
.game-heading h2,
.results-heading h2 {
  margin: 0;
  letter-spacing: -0.035em;
}

.home-card h1 {
  font-size: clamp(40px, 8vw, 64px);
}

.home-subtitle {
  margin: 14px auto 0;
  max-width: 500px;
  font-size: clamp(18px, 3vw, 22px);
  font-weight: 600;
}

.home-description {
  margin: 9px auto 28px;
  max-width: 510px;
  color: var(--muted);
  line-height: 1.55;
}

.primary-button,
.secondary-button,
.tool-button {
  border: 1px solid transparent;
  border-radius: 10px;
  transition: background var(--transition), border-color var(--transition), color var(--transition), transform var(--transition), opacity var(--transition);
}

.primary-button {
  min-height: 42px;
  padding: 0 18px;
  color: #fff;
  background: var(--primary);
  font-weight: 700;
}

.primary-button:hover {
  background: var(--primary-hover);
}

.primary-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.large-button {
  min-width: 190px;
  min-height: 48px;
  font-size: 16px;
}

.availability {
  margin: 18px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.storage-note {
  margin: 9px 0 0;
  color: var(--muted-2);
  font-size: 11px;
}

.game-card {
  padding: clamp(18px, 3vw, 28px);
}

.game-heading {
  text-align: center;
  margin-bottom: 18px;
}

.game-heading h2 {
  font-size: clamp(28px, 5vw, 42px);
}

.game-hint {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.drawing-stage {
  width: 100%;
  display: flex;
  justify-content: center;
}

.canvas-stack {
  width: min(840px, 100%);
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: #fff;
  box-shadow: var(--shadow-small);
  touch-action: none;
}

.canvas-stack canvas {
  display: block;
  width: 100%;
  height: auto;
}

#drawingCanvas {
  position: relative;
  z-index: 1;
  background: #fff;
  cursor: crosshair;
}

#gridCanvas {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

.toolbar {
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-soft);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.tool-group + .tool-group {
  padding-left: 10px;
  border-left: 1px solid var(--border);
}

.tool-group-main,
.tool-group-actions {
  flex-wrap: wrap;
}

.tool-button {
  min-height: 36px;
  padding: 0 10px;
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
}

.tool-button:hover {
  background: var(--surface-soft);
  border-color: var(--border-strong);
}

.tool-button.is-active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary);
}

.tool-button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.tool-symbol {
  font-size: 15px;
  line-height: 1;
}

.group-label,
.slider-group label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.swatches {
  display: flex;
  align-items: center;
  gap: 5px;
}

.swatch {
  width: 25px;
  height: 25px;
  padding: 0;
  border: 2px solid var(--surface);
  outline: 1px solid var(--border);
  border-radius: 50%;
  cursor: pointer;
  transition: transform var(--transition), outline-color var(--transition);
}

.swatch:hover {
  transform: scale(1.08);
}

.swatch.is-selected {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.custom-color {
  width: 27px;
  height: 27px;
  overflow: hidden;
  border-radius: 50%;
  border: 2px solid var(--surface);
  outline: 1px solid var(--border);
  display: block;
  flex: 0 0 auto;
}

.custom-color input {
  width: 42px;
  height: 42px;
  margin: -8px;
  padding: 0;
  border: 0;
  cursor: pointer;
}

.slider-group {
  min-width: 150px;
  display: grid;
  gap: 5px;
}

.slider-group label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.slider-group input[type="range"] {
  width: 150px;
  accent-color: var(--primary);
}

.game-footer {
  margin-top: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.shortcut-hint {
  color: var(--muted-2);
  font-size: 11px;
  line-height: 1.4;
}

.finish-button {
  min-height: 46px;
  min-width: 190px;
}

.results-card {
  padding: clamp(22px, 4vw, 36px);
}

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

.results-heading h2 {
  font-size: clamp(26px, 4vw, 38px);
}

.score-wrap {
  margin-top: 16px;
}

.score-number {
  font-size: clamp(66px, 12vw, 104px);
  line-height: 0.95;
  letter-spacing: -0.06em;
  font-weight: 800;
}

.score-message {
  min-height: 25px;
  margin-top: 10px;
  color: var(--muted);
  font-weight: 700;
  font-size: 17px;
}

.score-bar {
  width: min(600px, 100%);
  height: 12px;
  margin: 18px auto 0;
  border-radius: 999px;
  overflow: hidden;
  background: var(--border);
}

.score-bar-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width 180ms ease;
}

.score-number.score-low {
  color: var(--danger);
}

.score-number.score-mid-low {
  color: var(--warning);
}

.score-number.score-good {
  color: var(--primary);
}

.score-number.score-high {
  color: var(--success);
}

.flag-comparison {
  margin-top: 30px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

.flag-panel {
  margin: 0;
}

.flag-panel figcaption {
  margin-bottom: 7px;
  color: var(--muted);
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.flag-frame {
  width: 100%;
  aspect-ratio: 3 / 2;
  border: 1px solid var(--border-strong);
  border-radius: 11px;
  overflow: hidden;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.flag-frame img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.personal-best {
  min-height: 23px;
  margin-top: 16px;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
}

.personal-best.record {
  color: var(--success);
  font-weight: 800;
  animation: record-pop 420ms ease both;
}

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

.secondary-button {
  min-height: 44px;
  padding: 0 18px;
  border-color: var(--border-strong);
  background: var(--surface-soft);
  color: var(--text);
  font-weight: 700;
}

.secondary-button:hover {
  background: var(--surface);
}

.site-footer {
  min-height: 42px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--muted-2);
  font-size: 11px;
}

.processing-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 23, 42, 0.28);
}

.processing-overlay.is-visible {
  display: flex;
}

.processing-card {
  padding: 16px 18px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 700;
}

.processing-spinner {
  width: 17px;
  height: 17px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 760ms linear infinite;
}

.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;
}

@keyframes screen-in {
  from {
    opacity: 0;
    transform: translateY(7px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes record-pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

@media (max-width: 800px) {
  .toolbar {
    align-items: stretch;
  }

  .tool-group {
    min-width: 100%;
    justify-content: flex-start;
  }

  .tool-group + .tool-group {
    padding-left: 0;
    padding-top: 9px;
    border-left: 0;
    border-top: 1px solid var(--border);
  }

  .slider-group {
    min-width: 100%;
  }

  .slider-group input[type="range"] {
    width: min(320px, 100%);
  }
}

@media (max-width: 620px) {
  .site-header {
    width: min(100% - 20px, 1180px);
    min-height: 60px;
  }

  .header-actions {
    gap: 5px;
  }

  .stat-chip {
    min-width: 63px;
    padding: 6px 7px;
  }

  .stat-label {
    font-size: 9px;
  }

  .main-content {
    padding: 10px 10px 22px;
    align-items: flex-start;
  }

  .home-card,
  .game-card,
  .results-card {
    border-radius: 12px;
  }

  .game-footer {
    align-items: stretch;
    flex-direction: column;
  }

  .shortcut-hint {
    text-align: center;
    order: 2;
  }

  .finish-button {
    width: 100%;
  }

  .flag-comparison {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .flag-frame {
    aspect-ratio: 3 / 2;
  }

  .results-actions {
    flex-direction: column;
  }

  .results-actions button {
    width: 100%;
  }
}

@media (max-width: 430px) {
  .brand {
    font-size: 16px;
  }

  .icon-button {
    width: 36px;
    height: 36px;
  }

  .game-card {
    padding: 12px;
  }

  .tool-button {
    flex: 1 1 auto;
  }

  .swatches {
    flex-wrap: wrap;
  }

  .finish-button,
  .secondary-button,
  .primary-button {
    min-height: 46px;
  }
}

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