:root {
  color-scheme: light;
  --bg: #f7f2e8;
  --board: #fffaf0;
  --line: #5c6470;
  --line-cross: #f05d4f;
  --pin: #ffca3a;
  --pin-edge: #704a23;
  --pin-hot: #4ecdc4;
  --text: #2f2f35;
  --muted: #6d7280;
  --panel: rgba(255, 250, 240, .94);
  --shadow: rgba(65, 46, 21, .18);
  --accent: #4ecdc4;
}

body.dark {
  color-scheme: dark;
  --bg: #141820;
  --board: #202632;
  --line: #d5d9e2;
  --line-cross: #ff796e;
  --pin: #ffd166;
  --pin-edge: #33261b;
  --pin-hot: #64dfdf;
  --text: #f2f4f8;
  --muted: #a9b2c2;
  --panel: rgba(31, 38, 50, .95);
  --shadow: rgba(0, 0, 0, .32);
  --accent: #64dfdf;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 15% 10%, rgba(255, 202, 58, .24), transparent 30%),
    radial-gradient(circle at 88% 84%, rgba(78, 205, 196, .24), transparent 28%),
    var(--bg);
  color: var(--text);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

body.dark {
  background: var(--bg);
}

button,
input {
  font: inherit;
}

.game-shell {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: clamp(12px, 2.5vw, 28px);
  min-height: 100vh;
  min-height: 100dvh;
}

#gameCanvas {
  width: min(100%, 1320px);
  height: min(100%, 860px);
  max-height: calc(100vh - clamp(24px, 5vw, 56px));
  border-radius: 8px;
  background:
    linear-gradient(120deg, rgba(255, 255, 255, .32), transparent 38%),
    repeating-linear-gradient(0deg, transparent 0 38px, rgba(90, 68, 42, .045) 39px 40px),
    repeating-linear-gradient(90deg, transparent 0 38px, rgba(90, 68, 42, .045) 39px 40px),
    var(--board);
  box-shadow: 0 22px 60px var(--shadow), inset 0 0 0 4px rgba(255, 255, 255, .22);
  display: block;
  cursor: grab;
}

body.dark #gameCanvas {
  background:
    repeating-linear-gradient(0deg, transparent 0 38px, rgba(127, 137, 154, .045) 39px 40px),
    repeating-linear-gradient(90deg, transparent 0 38px, rgba(127, 137, 154, .045) 39px 40px),
    var(--board);
}

#gameCanvas.dragging {
  cursor: grabbing;
}

.top-button {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  z-index: 5;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 10px 26px var(--shadow);
  cursor: pointer;
  transition: transform .16s ease, background .16s ease;
  -webkit-tap-highlight-color: transparent;
}

.top-button:hover {
  transform: translateY(-1px);
}

.top-button:active {
  transform: translateY(1px) scale(.98);
}

.top-button svg {
  width: 24px;
  height: 24px;
  stroke-width: 2.6;
}

#restartBtn {
  left: max(14px, env(safe-area-inset-left));
}

#settingsBtn {
  right: max(14px, env(safe-area-inset-right));
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: none;
  place-items: center;
  padding: 18px;
  background: rgba(12, 14, 18, .26);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

.overlay.open {
  display: grid;
}

.runtime-warning {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(247, 242, 232, .88);
  color: var(--text);
  text-align: center;
}

body.dark .runtime-warning {
  background: rgba(20, 24, 32, .9);
}

body.js-ready .runtime-warning {
  display: none;
}

.runtime-warning-inner {
  width: min(86vw, 420px);
  border-radius: 8px;
  padding: 22px;
  background: var(--panel);
  box-shadow: 0 18px 48px var(--shadow);
  line-height: 1.7;
  font-weight: 700;
}

.runtime-warning-inner small {
  display: block;
  margin-top: 10px;
  color: var(--muted);
  font-weight: 600;
}

.settings-panel,
.win-panel {
  width: min(92vw, 420px);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 24px 70px var(--shadow);
  border: 1px solid rgba(255, 255, 255, .24);
}

.settings-panel {
  padding: 22px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
}

h1 {
  font-size: 22px;
  line-height: 1.2;
  margin: 0;
  letter-spacing: 0;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: rgba(127, 137, 154, .14);
  color: var(--text);
  cursor: pointer;
}

.setting-row {
  display: grid;
  gap: 10px;
  margin: 18px 0;
}

.setting-row label,
.setting-title {
  font-size: 14px;
  color: var(--muted);
  font-weight: 700;
}

.mode-toggle,
.number-pair {
  display: grid;
  gap: 10px;
}

.mode-toggle {
  grid-template-columns: repeat(3, 1fr);
}

.number-pair {
  grid-template-columns: 1fr 1fr;
}

.mode-toggle button {
  min-height: 44px;
  border: 1px solid rgba(127, 137, 154, .28);
  border-radius: 8px;
  background: rgba(127, 137, 154, .10);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}

.mode-toggle button.active {
  background: var(--accent);
  color: #102023;
  border-color: transparent;
}

.field {
  display: grid;
  gap: 7px;
}

input[type="number"],
input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

input[type="number"] {
  min-height: 44px;
  border: 1px solid rgba(127, 137, 154, .32);
  border-radius: 8px;
  padding: 0 12px;
  background: rgba(255, 255, 255, .12);
  color: var(--text);
}

.apply-btn {
  width: 100%;
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  background: #1f9d89;
  color: white;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  margin-top: 8px;
}

.win-panel {
  display: grid;
  justify-items: center;
  gap: 12px;
  padding: 30px 26px 28px;
  text-align: center;
}

.checkmark {
  width: 94px;
  height: 94px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #21bf73;
  color: white;
  box-shadow: 0 16px 34px rgba(33, 191, 115, .34);
}

.win-panel strong {
  font-size: 24px;
  line-height: 1.2;
}

.win-panel span {
  color: var(--muted);
  font-size: 15px;
}

@media (max-width: 620px) {
  .game-shell {
    height: var(--app-height, 100dvh);
    min-height: var(--app-height, 100dvh);
    padding: 8px;
  }

  #gameCanvas {
    width: calc(100vw - 16px);
    height: calc(var(--app-height, 100dvh) - 16px);
    max-height: none;
    border-radius: 0;
  }

  .top-button {
    width: 44px;
    height: 44px;
  }

  .settings-panel {
    padding: 18px;
  }

  .number-pair {
    grid-template-columns: 1fr;
  }
}
