/* ─── CSS Variables ─────────────────────────── */
:root {
  --bg: #F5F8FC;
  --surface: #FFFFFF;
  --text: #344861;
  --text-secondary: #6B7B8D;
  --text-muted: #94A3B8;
  --primary: #325AAF;
  --primary-hover: #2A4E99;
  --border: #E2E8F0;
  --shadow: 0 2px 8px rgba(0,0,0,.08);
  --shadow-hover: 0 4px 16px rgba(0,0,0,.12);
  --radius: 8px;
  --radius-sm: 4px;
  --nav-height: 48px;
  --status-height: 44px;
  --control-height: 64px;
}

[data-theme="dark"] {
  --bg: #0B1422;
  --surface: #16213E;
  --text: #E0E0E0;
  --text-secondary: #A0AEC0;
  --text-muted: #718096;
  --primary: #5B8DEF;
  --primary-hover: #4A7BD9;
  --border: #2D3748;
  --shadow: 0 2px 8px rgba(0,0,0,.3);
  --shadow-hover: 0 4px 16px rgba(0,0,0,.4);
}

/* ─── Base ──────────────────────────────────── */
body {
  background: var(--bg);
  color: var(--text);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ─── Navbar ───────────────────────────────── */
.nav-bar {
  background: var(--surface);
  box-shadow: var(--shadow);
  position: relative;
  z-index: 100;
  height: var(--nav-height);
}

.nav-inner {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 100%;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.nav-hamburger {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
}

.nav-logo-text {
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--primary);
}

.btn-new-game {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 6px 16px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-new-game:hover { background: var(--primary-hover); }

/* ─── Side Menu ────────────────────────────── */
.side-menu-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 200;
}
.side-menu-overlay.open { display: block; }

.side-menu {
  position: fixed;
  top: 0;
  left: -280px;
  width: 280px;
  height: 100%;
  background: var(--surface);
  z-index: 201;
  transition: left 0.25s ease;
  overflow-y: auto;
}
.side-menu.open { left: 0; }

.side-menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.side-menu-close {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
}

.side-menu-items { padding: 8px 0; }

.side-menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  background: none;
  border: none;
  width: 100%;
  font-size: 0.95rem;
  transition: background 0.15s;
}
.side-menu-item:hover { background: var(--bg); }

/* ─── Game Status Bar ──────────────────────── */
.game-status-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  height: var(--status-height);
}

.game-status-inner {
  max-width: 800px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 16px;
  height: 100%;
}
.game-status-inner > :first-child { justify-self: start; }
.game-status-inner > :nth-child(2) { justify-self: center; }
.game-status-inner > :last-child { justify-self: end; }

/* Difficulty selector (combobox style) */
.difficulty-selector {
  position: relative;
}

.difficulty-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--text);
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.difficulty-btn:hover { background: var(--bg); }
.difficulty-btn svg { opacity: 0.5; }

.difficulty-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-hover);
  z-index: 50;
  min-width: 140px;
  padding: 4px 0;
}
.difficulty-dropdown.open { display: block; }

.difficulty-dropdown button {
  display: block;
  width: 100%;
  padding: 10px 16px;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.1s;
}
.difficulty-dropdown button:hover { background: var(--bg); }
.difficulty-dropdown button.active {
  color: var(--primary);
  font-weight: 700;
}

.game-stat {
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.game-stat strong {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.timer { display: flex; align-items: center; gap: 4px; }

/* ─── Main Layout ──────────────────────────── */
.main-layout {
  display: flex;
  justify-content: center;
  padding: 8px 8px 0;
  height: calc(100dvh - var(--nav-height) - var(--status-height));
}

.game-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 600px;
  height: 100%;
}

.board-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 0;
}

#gameCanvas {
  display: block;
}

/* ─── Control Bar ──────────────────────────── */
.control-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 6px 0;
  width: 100%;
  height: var(--control-height);
  flex-shrink: 0;
}


.ctrl-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 0.7rem;
  transition: all 0.15s;
}
.ctrl-btn:hover { color: var(--text); border-color: var(--text-muted); }
.ctrl-btn:active { background: var(--border); }

/* Undo/Redo buttons */
.ctrl-btn.small {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
}
.ctrl-btn.small svg { width: 20px; height: 20px; }

/* Mode buttons (Pen/X) — larger, prominent */
.mode-btn {
  padding: 14px 32px;
  min-width: 84px;
  border-radius: 14px;
  background: var(--bg);
  border: 2px solid var(--border);
}
.mode-btn svg { width: 30px; height: 30px; }

.mode-btn.active {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}
.mode-btn:hover:not(.active) {
  border-color: var(--primary);
  color: var(--primary);
}

/* ─── Fullscreen mode ──────────────────────── */
body.fullscreen .nav-bar,
body.fullscreen .game-status-bar,
body.fullscreen .seo-content { display: none; }

body.fullscreen .main-layout {
  height: 100dvh;
  padding: 4px 4px 0;
}

body.fullscreen .ctrl-btn.fullscreen-btn {
  color: var(--primary);
  border-color: var(--primary);
}

/* ─── Modals ───────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 300;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open {
  display: flex;
}

.modal-box {
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 28px;
  max-width: 380px;
  width: 90%;
  text-align: center;
  box-shadow: var(--shadow-hover);
}

.modal-box h2 {
  margin-bottom: 8px;
  font-size: 1.3rem;
}

.modal-box p {
  color: var(--text-secondary);
  margin-bottom: 20px;
  font-size: 0.95rem;
}

.clear-time {
  font-size: 1.1rem;
  margin-bottom: 20px;
}
.clear-time strong {
  font-size: 1.5rem;
  color: var(--primary);
}

.modal-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.modal-btn {
  padding: 10px 24px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.15s;
}
.modal-btn:hover { background: var(--bg); }
.modal-btn.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.modal-btn.primary:hover { background: var(--primary-hover); }

/* Settings modal */
.modal-settings { text-align: left; }
.modal-settings h2 { text-align: center; }

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.setting-row:last-of-type { border-bottom: none; }

/* Toggle switch */
.toggle {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 24px;
  cursor: pointer;
  transition: 0.2s;
}
.slider::before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
}
.toggle input:checked + .slider { background: var(--primary); }
.toggle input:checked + .slider::before { transform: translateX(20px); }

/* ─── SEO Content ──────────────────────────── */
.seo-content {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.7;
}
.seo-content h1 {
  font-size: 1.5rem;
  color: var(--text);
  margin-bottom: 12px;
}
.seo-content h2 {
  font-size: 1.1rem;
  color: var(--text);
  margin: 20px 0 8px;
}

/* ─── Responsive ───────────────────────────── */
@media (max-width: 480px) {
  .nav-inner { padding: 0 12px; }
  .game-status-inner { padding: 0 12px; }
  .main-layout { padding: 4px 4px 0; }
  .mode-btn { padding: 10px 24px; min-width: 72px; }
  .btn-new-game { padding: 6px 12px; font-size: 0.8rem; }
}

@media (min-width: 769px) {
  body { overflow: auto; }
  .main-layout { padding: 16px; height: auto; }
  .board-container { max-width: 600px; max-height: 600px; }
  .game-area { height: auto; }
}

/* Collection fullscreen */
.collection-screen {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 250;
  flex-direction: column;
  overflow: hidden;
}
.collection-screen.open {
  display: flex;
}

.collection-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.collection-back {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
  display: flex;
}

.collection-title {
  font-weight: 700;
  font-size: 1.1rem;
}

.collection-tabs {
  display: flex;
  gap: 0;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
}

.col-tab {
  padding: 8px 12px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.col-tab:hover { color: var(--text); }
.col-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.collection-progress {
  padding: 12px 16px;
  flex-shrink: 0;
}

.collection-progress span {
  font-size: 0.85rem;
  color: var(--text-secondary);
  display: block;
  margin-bottom: 6px;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  transition: width 0.3s;
}

.collection-grid {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 8px;
  align-content: start;
}

.collection-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
  padding: 6px 4px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.collection-item:hover { background: var(--surface); }

.collection-item canvas {
  border: 1px solid var(--border);
  border-radius: 3px;
  display: block;
}

.collection-item .label {
  font-size: 0.65rem;
  color: var(--text-muted);
  text-align: center;
}

.collection-item.locked canvas {
  opacity: 0.3;
}
.collection-item.locked .label {
  color: var(--text-muted);
}

.empty-stats {
  text-align: center;
  color: var(--text-muted);
  padding: 20px;
}
