:root {
  --ink: #21262f;
  --paper: #f6f2e9;
  --panel: #fffdf8;
  --cell-bg: #ffffff;
  --accent: #1f7a6c;
  --accent-dark: #145a4f;
  --accent-soft: #e3efe9;
  --accent-soft-strong: #cfe6de;
  --grid-line: #ddd6c6;
  --box-line: #2c3138;
  --cage-line: #c96a2e;
  --cage-sum: #8a3f12;
  --danger: #bf483f;
  --danger-bg: #fbe9e5;
  --given: #33383f;
  --user-num: #175d52;
  --hint-num: #4f7a34;
  --peer-bg: #edf0e6;
  --selected-bg: #cbe4dc;
  --same-value-bg: #f4e2b3;
  --note-color: #8c8571;
  --muted-text: #726c5a;
  --radius: 14px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0 16px 28px;
}

.hidden { display: none !important; }

/* ============ START SCREEN ============ */
.brand {
  text-align: center;
  padding: 32px 0 8px;
}
.brand h1 {
  margin: 0;
  font-size: 2rem;
  letter-spacing: -0.02em;
}
.tagline {
  margin: 6px 0 0;
  color: var(--muted-text);
  font-size: 0.95rem;
}

.continue-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--grid-line);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 18px 0;
}
.continue-card strong { display: block; }
.continue-card span[id="continue-meta"] { color: var(--muted-text); font-size: 0.85rem; }

.difficulty-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 20px;
}
.diff-card {
  background: var(--panel);
  border: 1px solid var(--grid-line);
  border-radius: var(--radius);
  padding: 16px 14px;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
  font-family: inherit;
}
.diff-card:hover {
  border-color: var(--accent);
  box-shadow: 0 4px 14px rgba(31, 122, 108, 0.16);
  transform: translateY(-1px);
}
.diff-name { font-weight: 700; font-size: 1.15rem; }
.diff-desc { color: var(--muted-text); font-size: 0.82rem; line-height: 1.3; }
.diff-best { color: var(--accent-dark); font-size: 0.8rem; font-weight: 600; min-height: 1.1em; }

#how-to-play-btn { display: block; margin: 26px auto 0; }

/* ============ BUTTONS ============ */
.btn {
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: 999px;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-secondary { background: var(--accent-soft); color: var(--accent-dark); }
.btn-secondary:hover { background: var(--accent-soft-strong); }
.btn-link { background: none; color: var(--accent-dark); text-decoration: underline; padding: 6px 10px; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--grid-line);
  background: var(--panel);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--ink);
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent-dark); }

/* ============ GAME TOPBAR ============ */
.game-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0 10px;
  gap: 10px;
}
.topbar-info { display: flex; align-items: center; gap: 10px; }
.pill {
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 5px 12px;
  border-radius: 999px;
}
.timer {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: 1.05rem;
  min-width: 3.5em;
  text-align: center;
}

.cage-info {
  background: var(--panel);
  border: 1.5px solid var(--cage-line);
  color: var(--cage-sum);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 8px;
  text-align: center;
  transition: color 0.15s, border-color 0.15s;
}
.cage-info.bad {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-bg);
}

.downgrade-note {
  background: #fceee0;
  color: #8a4a12;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.82rem;
  margin-bottom: 8px;
  text-align: center;
}

/* ============ BOARD ============ */
.board-wrap { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  width: 100%;
  aspect-ratio: 1;
  background: var(--cell-bg);
  border: 3px solid var(--box-line);
  border-radius: 8px;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--grid-line);
  font-size: clamp(14px, 3.6vw, 22px);
  cursor: pointer;
  background: var(--cell-bg);
}
/* thick 3x3 box separators */
.cell.box-right { border-right: 2px solid var(--box-line); }
.cell.box-bottom { border-bottom: 2px solid var(--box-line); }

.cell.peer { background: var(--peer-bg); }
.cell.same-value { background: var(--same-value-bg); }
.cell.selected { background: var(--selected-bg); }
.cell.conflict .cell-value { color: var(--danger); }
.cell.conflict { background: var(--danger-bg); }

.cell-value {
  font-weight: 600;
  color: var(--user-num);
  line-height: 1;
}
.cell-value.given { color: var(--given); font-weight: 700; }
.cell-value.hinted { color: var(--hint-num); }

.notes-grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 4px 3px 2px;
}
.notes-grid span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(7px, 1.7vw, 10px);
  color: var(--note-color);
  font-weight: 600;
  line-height: 1;
}

/* inset dashed cage outline, drawn independent of grid/box borders */
.cage-outline {
  position: absolute;
  top: 3px; right: 3px; bottom: 3px; left: 3px;
  pointer-events: none;
  border-style: dashed;
  border-color: var(--cage-line);
  border-width: 0;
}
.cell.cage-error .cage-outline { border-color: var(--danger); }

.cage-sum {
  position: absolute;
  top: 1px; left: 3px;
  font-size: clamp(8.5px, 2.3vw, 12.5px);
  font-weight: 800;
  color: var(--cage-sum);
  line-height: 1;
  letter-spacing: -0.02em;
  pointer-events: none;
}
.cell.cage-error .cage-sum { color: var(--danger); }

.pause-overlay {
  position: absolute;
  inset: 0;
  background: rgba(246, 242, 233, 0.94);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

/* ============ TOOLBAR & NUMPAD ============ */
.toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 14px;
}
.tool-btn {
  background: var(--panel);
  border: 1px solid var(--grid-line);
  border-radius: 12px;
  padding: 9px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-family: inherit;
  font-size: 0.7rem;
  color: var(--ink);
  cursor: pointer;
}
.tool-btn .tool-icon { font-size: 1.15rem; line-height: 1; }
.tool-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tool-btn:disabled { opacity: 0.45; cursor: default; }
#hint-count { font-weight: 700; }

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
  margin-top: 10px;
}
.num-btn {
  background: var(--panel);
  border: 1px solid var(--grid-line);
  border-radius: 10px;
  padding: 12px 0;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent-dark);
  cursor: pointer;
  font-family: inherit;
}
.num-btn:hover { border-color: var(--accent); }

.status-line {
  text-align: center;
  min-height: 1.3em;
  color: var(--muted-text);
  font-size: 0.85rem;
  margin: 10px 0 0;
}

/* ============ MODALS ============ */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(33, 38, 47, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.modal {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 26px 24px;
  max-width: 420px;
  width: 100%;
  max-height: 86vh;
  overflow-y: auto;
  text-align: center;
}
.modal h2 { margin-top: 0; }
.rules-list {
  text-align: left;
  padding-left: 18px;
  line-height: 1.5;
  font-size: 0.92rem;
  color: #3a372d;
}
.rules-list li { margin-bottom: 10px; }

.win-stats {
  display: flex;
  justify-content: space-around;
  margin: 18px 0;
  gap: 8px;
}
.win-stats > div { display: flex; flex-direction: column; gap: 2px; }
.stat-label { font-size: 0.72rem; color: var(--muted-text); text-transform: uppercase; letter-spacing: 0.03em; }
.stat-value { font-size: 1.2rem; font-weight: 800; color: var(--accent-dark); }

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

.spinner {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 4px solid var(--peer-bg);
  border-top-color: var(--accent);
  margin: 0 auto 14px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.generating-sub { color: var(--muted-text); font-size: 0.85rem; margin-top: -6px; }

@media (min-width: 520px) {
  .toolbar { grid-template-columns: repeat(4, 1fr); }
  .tool-btn { font-size: 0.78rem; padding: 11px 6px; }
}
