:root {
  --color-bg: #f4f6f9;
  --color-panel: #ffffff;
  --color-grid-line: #d7dce3;
  --color-grid-line-bold: #1c2b4a;
  --color-given: #1c2b4a;
  --color-entered: #12a3a4;
  --color-peer-bg: #eaeef3;
  --color-same-value-bg: #fdf3b6;
  --color-selected-bg: #7fd8d8;
  --color-conflict: #e0432b;
  --color-accent: #12a3a4;
  --color-accent-contrast: #ffffff;
  --color-muted: #9aa4b2;
  --color-text: #24313f;
  --color-note: #5b6c80;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0f1620;
    --color-panel: #17202b;
    --color-grid-line: #2b3947;
    --color-grid-line-bold: #dbe6f2;
    --color-given: #e7edf5;
    --color-entered: #38d1cf;
    --color-peer-bg: #202b38;
    --color-same-value-bg: #5a4f1f;
    --color-selected-bg: #1c7676;
    --color-conflict: #ff6b52;
    --color-accent: #2bc3c1;
    --color-accent-contrast: #06282a;
    --color-muted: #5c6b7c;
    --color-text: #e7edf5;
    --color-note: #9fb0c2;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: inherit;
  touch-action: manipulation;
}

#app {
  min-height: 100dvh;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(env(safe-area-inset-top) + 12px) calc(env(safe-area-inset-right) + 12px)
            calc(env(safe-area-inset-bottom) + 12px) calc(env(safe-area-inset-left) + 12px);
  max-width: 640px;
  margin: 0 auto;
  gap: 10px;
}

/* Header */

#header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#header-spacer { width: 32px; }

#header-title {
  text-align: center;
  flex: 1;
}

#difficulty-label {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--color-text);
}

#puzzle-meta {
  display: flex;
  justify-content: center;
  gap: 10px;
  font-size: 0.85rem;
  color: var(--color-muted);
}

#menu-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--color-text);
  font-size: 1.4rem;
  border-radius: 8px;
  touch-action: manipulation;
}
#menu-btn:active { background: var(--color-peer-bg); }

/* Grid */

#board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
}

#grid {
  width: min(100%, 560px);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  border: 2px solid var(--color-grid-line-bold);
  border-radius: 6px;
  overflow: hidden;
}

.cell {
  background: var(--color-panel);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: clamp(16px, 4.2vw, 30px);
  font-weight: 600;
  cursor: pointer;
  border-right: 1px solid var(--color-grid-line);
  border-bottom: 1px solid var(--color-grid-line);
}
.cell[data-col="8"] { border-right: none; }
.cell[data-row="8"] { border-bottom: none; }

/* Bold box borders every 3 columns/rows (each cell owns its own edge, so
   there's no overlap/paint-order ambiguity with the neighboring cell). */
.cell[data-col="2"], .cell[data-col="5"] { border-right: 2px solid var(--color-grid-line-bold); }
.cell[data-row="2"], .cell[data-row="5"] { border-bottom: 2px solid var(--color-grid-line-bold); }

.cell.given { color: var(--color-given); }
.cell.entered { color: var(--color-entered); }
.cell.conflict { color: var(--color-conflict) !important; }

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

.cell-notes {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 2px;
}
.cell-notes span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(7px, 1.5vw, 12px);
  font-weight: 500;
  color: var(--color-note);
  line-height: 1;
}

/* Message / win banner area (reserved space to avoid layout shift) */

#message-area {
  min-height: 4px;
  width: 100%;
}

/* Digit pads */

#pad {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.digit-row {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}

.digit-btn {
  position: relative;
  background: var(--color-panel);
  border: 1px solid var(--color-grid-line);
  border-radius: 10px;
  color: var(--color-given);
  font-size: clamp(20px, 5vw, 34px);
  font-weight: 600;
  padding: 8px 0;
  min-height: 48px;
  touch-action: manipulation;
}
.digit-btn:active { background: var(--color-peer-bg); }

#notes-row .digit-btn { font-style: italic; }

#numbers-row .digit-btn .badge-count {
  position: absolute;
  top: 3px;
  left: 6px;
  font-size: clamp(9px, 1.8vw, 12px);
  font-style: normal;
  font-weight: 600;
  color: var(--color-muted);
}

.digit-btn.depleted {
  opacity: 0.4;
}

/* Controls */

#controls {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  background: var(--color-peer-bg);
  border-radius: 14px;
  padding: 8px;
}

.ctrl-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: transparent;
  border: none;
  color: var(--color-text);
  font-size: 0.8rem;
  padding: 6px 0;
  border-radius: 10px;
}
.ctrl-btn:active { background: rgba(0,0,0,0.06); }
.ctrl-btn:disabled { opacity: 0.4; }

.ctrl-icon {
  position: relative;
  font-size: 1.3rem;
}

.badge {
  position: absolute;
  top: -8px;
  right: -12px;
  background: var(--color-conflict);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

.ctrl-btn-accent {
  color: var(--color-accent);
  font-weight: 700;
}

/* Modal */

#modal-overlay, #win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}
#modal-overlay { z-index: 11; }
#modal-overlay.hidden, #win-overlay.hidden { display: none; }

#modal, #win-panel {
  background: var(--color-panel);
  border-radius: 16px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  text-align: center;
}

#modal-title { margin: 0 0 14px; }

#difficulty-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.difficulty-btn {
  background: var(--color-peer-bg);
  border: none;
  border-radius: 10px;
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text);
}
.difficulty-btn:active { background: var(--color-selected-bg); }

#smart-notes-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  text-align: left;
  font-size: 0.85rem;
  margin-bottom: 16px;
}
#smart-notes-row small { color: var(--color-muted); display: block; font-size: 0.75rem; }

#modal-close {
  background: transparent;
  border: none;
  color: var(--color-muted);
  font-size: 0.95rem;
  padding: 8px;
}

#win-title { font-size: 1.6rem; font-weight: 700; color: var(--color-accent); margin-bottom: 10px; }
#win-stats { color: var(--color-text); margin-bottom: 18px; font-size: 1rem; display: flex; flex-direction: column; gap: 4px; }
#win-new-game-btn {
  background: var(--color-accent);
  color: var(--color-accent-contrast);
  border: none;
  border-radius: 10px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 700;
}

.hidden { display: none !important; }
