*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f5f0e8;
  --bg-warm: #ede7db;
  --ink: #2c2826;
  --ink-light: #8a8279;
  --ink-muted: #b8b0a4;
  --accent: #c45d3e;
  --accent-hover: #a84a2f;
  --filled: #5b7ab8;
  --mark: #d4ccc0;
  --grid-line: #938f8b;
  --grid-thick: #2c2826;
  --cell-bg: #fff;
  --cell-hover: rgba(196, 93, 62, 0.08);
  --done: #c9c1b3;
  --radius: 6px;
  --cell-size: 36px;
}

html.dark {
  --bg: #1a1714;
  --bg-warm: #222019;
  --ink: #e8e2d8;
  --ink-light: #8a8279;
  --ink-muted: #5a544e;
  --accent: #d4694a;
  --accent-hover: #b8533a;
  --filled: #3a5a9e;
  --mark: #3a3530;
  --grid-line: #6b6560;
  --grid-thick: #9e9890;
  --cell-bg: #252220;
  --cell-hover: rgba(212, 105, 74, 0.12);
  --done: #4a4540;
}

html { background: var(--bg); overscroll-behavior-y: none; }

body {
  font-family: 'DM Mono', monospace;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  overscroll-behavior-y: none;
}

/* ── Top bar: title + timer ── */
.top-bar {
  width: 100%;
  max-width: 600px;
  padding: 0.75rem 1rem 0.25rem;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

h1 {
  font-family: 'Fraunces', serif;
  font-weight: 700;
  font-size: 1.4rem;
  letter-spacing: -0.02em;
  color: var(--ink);
}

h1 span { color: var(--accent); }

.timer-info {
  font-size: 0.8rem;
  color: var(--ink-light);
  display: flex;
  gap: 0.75rem;
}

/* ── Spacer pushes content toward bottom on mobile ── */
.mobile-spacer { flex: 1 1 auto; }

/* ── Puzzle area ── */
.puzzle-wrap {
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  padding: 0.5rem 0.75rem 0.5rem;
}

.puzzle-scaler {
  display: inline-block;
  transform-origin: top center;
}

.puzzle-table {
  border-collapse: collapse;
  user-select: none;
  touch-action: manipulation;
}

.col-clue-cell {
  vertical-align: bottom;
  text-align: center;
  padding: 0 1px 3px;
  min-width: var(--cell-size);
  width: var(--cell-size);
}

.col-clue-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  justify-content: flex-end;
}

.clue-num {
  font-size: 0.72rem;
  font-weight: 500;
  line-height: 1.2;
  color: var(--ink);
  transition: color 0.25s, opacity 0.25s;
}

.clue-num.done {
  color: var(--done);
  text-decoration: line-through;
  text-decoration-color: var(--done);
}

.row-clue-cell {
  text-align: right;
  padding: 0 5px 0 0;
  white-space: nowrap;
  height: var(--cell-size);
}

.row-clue-inner {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
  align-items: center;
}

td.cell {
  touch-action: none;
  width: var(--cell-size);
  height: var(--cell-size);
  min-width: var(--cell-size);
  border: 1px solid var(--grid-line);
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  transition: background 0.08s;
  position: relative;
  background: var(--cell-bg);
}

td.cell:hover { background: var(--cell-hover); }
td.cell.thick-left { border-left: 2px solid var(--grid-thick); }
td.cell.thick-top { border-top: 2px solid var(--grid-thick); }
td.cell.thick-right { border-right: 2px solid var(--grid-thick); }
td.cell.thick-bottom { border-bottom: 2px solid var(--grid-thick); }
td.cell.filled { background: var(--filled); }
td.cell.filled:hover { background: var(--filled); }

td.cell.marked::after {
  content: '\00d7';
  font-size: 2.2rem;
  font-weight: 500;
  color: var(--ink-light);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Win banner ── */
.win-banner {
  display: none;
  text-align: center;
  padding: 0.5rem;
  font-family: 'Fraunces', serif;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  animation: pop 0.4s ease;
}
.win-banner.show { display: block; }

@keyframes pop {
  0% { transform: scale(0.8); opacity: 0; }
  60% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

/* ── Top controls (seed/size) ── */
.top-controls {
  width: 100%;
  max-width: 600px;
  padding: 0.25rem 1rem 0.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* ── Bottom controls ── */
.bottom-controls {
  width: 100%;
  max-width: 600px;
  padding: 0.35rem 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.mode-slider {
  position: relative;
  cursor: pointer;
  user-select: none;
}

.mode-slider-track {
  position: relative;
  width: 115px;
  height: 43px;
  background: var(--cell-bg);
  border: 1.5px solid var(--grid-line);
  border-radius: 22px;
}

.mode-slider-thumb {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 53px;
  height: 34px;
  background: var(--ink);
  border-radius: 17px;
  transition: left 0.2s ease;
  z-index: 0;
}

.mode-slider[data-mode="fill"] .mode-slider-thumb {
  left: calc(100% - 57px);
}

.mode-side {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  z-index: 1;
  color: var(--ink-light);
  transition: color 0.2s;
}

.mode-side[data-mode="mark"] { left: 0; }
.mode-side[data-mode="fill"] { right: 0; }

.mode-slider[data-mode="mark"] .mode-side[data-mode="mark"],
.mode-slider[data-mode="fill"] .mode-side[data-mode="fill"] {
  color: var(--bg);
}

.settings-wrap {
  position: relative;
}

.settings-btn {
  border: none;
  background: none;
  padding: 0.35rem;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--ink-light);
  line-height: 1;
  transition: color 0.15s;
}
.settings-btn:hover { color: var(--ink); border: none; }
.settings-btn:active { transform: scale(0.93); }

.settings-panel {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  background: var(--bg);
  border: 1.5px solid var(--grid-thick);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-width: 210px;
  z-index: 50;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  animation: pop 0.2s ease;
}

.settings-item {
  padding: 0.65rem 1rem;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--grid-line);
  cursor: pointer;
  font-family: 'DM Mono', monospace;
  font-size: 0.85rem;
  color: var(--ink-light);
  transition: background 0.12s, color 0.12s;
  border-radius: 0;
}
.settings-item:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.settings-item:last-child { border-bottom: none; border-radius: 0 0 var(--radius) var(--radius); }
.settings-item:hover { background: var(--cell-bg); color: var(--ink); }
.settings-item.active { color: var(--ink); }

.controls-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.controls-row input[type="text"] {
  font-family: 'DM Mono', monospace;
  font-size: 0.85rem;
  padding: 0.45rem 0.65rem;
  border: 1.5px solid var(--grid-line);
  border-radius: var(--radius);
  background: var(--cell-bg);
  color: var(--ink);
  flex: 1;
  min-width: 0;
  outline: none;
  transition: border-color 0.15s;
}

.controls-row input[type="text"]:focus,
.controls-row input[type="number"]:focus { border-color: var(--accent); }
.controls-row input[type="text"]::placeholder { color: var(--ink-muted); }

select, button {
  font-family: 'DM Mono', monospace;
  font-size: 0.85rem;
  padding: 0.45rem 0.65rem;
  border: 1.5px solid var(--grid-line);
  border-radius: var(--radius);
  background: var(--cell-bg);
  color: var(--ink);
  cursor: pointer;
  transition: all 0.15s;
}

button:hover { border-color: var(--accent); color: var(--accent); }
button:active { transform: scale(0.97); }
.btn-accent { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-accent:hover { background: var(--accent-hover); color: #fff; border-color: var(--accent-hover); }

.seed-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  font-size: 0.7rem;
  color: var(--ink-muted);
}

.seed-display code {
  background: var(--bg-warm);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  user-select: all;
}

/* Safe-area padding for phones with gesture bars */
.bottom-pad {
  height: env(safe-area-inset-bottom, 0.5rem);
  min-height: 0.5rem;
}

/* ── Mobile ── */
@media (max-width: 420px) {
  :root { --cell-size: 30px; }
  h1 { font-size: 1.2rem; }
  .clue-num { font-size: 0.65rem; }
  .top-bar { padding: 0.5rem 0.75rem 0.15rem; }
  .bottom-controls { padding: 0.25rem 0.75rem 0.5rem; }
}

/* ── Desktop: no spacer, content starts at top ── */
@media (min-width: 700px) {
  :root { --cell-size: 38px; }
  .mobile-spacer { flex: 0 0 0; }
  .top-bar { padding: 1.5rem 1rem 0.5rem; }
  h1 { font-size: 1.6rem; }
}
#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 999;
}

@keyframes cell-flash-red {
  0%   { background: var(--cell-bg); }
  20%  { background: #d94040; }
  65%  { background: #d94040; }
  100% { background: var(--cell-bg); }
}
td.cell.cell-error { animation: cell-flash-red 0.55s ease forwards !important; }
td.cell.filled.cell-error { animation: cell-flash-red 0.55s ease forwards !important; }

@keyframes puzzle-shake {
  0%   { transform: translateX(0); }
  15%  { transform: translateX(-7px); }
  30%  { transform: translateX(7px); }
  50%  { transform: translateX(-5px); }
  70%  { transform: translateX(5px); }
  85%  { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}
.puzzle-wrap.puzzle-shake { animation: puzzle-shake 0.5s ease; }

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal-box {
  background: var(--bg);
  border: 1.5px solid var(--grid-thick);
  border-radius: var(--radius);
  padding: 1.75rem 2.25rem;
  text-align: center;
  display: flex; flex-direction: column; gap: 0.75rem;
  min-width: 230px;
  animation: pop 0.35s ease;
}
.modal-title {
  font-family: 'Fraunces', serif;
  font-size: 1.5rem; font-weight: 700;
  color: var(--accent);
}
.modal-sub { font-size: 0.75rem; color: var(--ink-muted); }
.modal-actions { display: flex; gap: 0.5rem; justify-content: center; margin-top: 0.25rem; }

#heartsDisplay, #heartsDisplayMobile { font-size: 1.35rem; letter-spacing: 0.15em; color: var(--accent); text-align: center; }
#heartsDisplayMobile { margin-bottom: 0.4rem; }
@media (max-width: 699px) { #heartsDisplay { display: none !important; } }
@media (min-width: 700px) { #heartsDisplayMobile { display: none !important; } }
.heart-lost { color: var(--ink-muted); }
