:root {
  --bg: #f1eee8;
  --surface: #faf8f4;
  --line: #e4dfd5;
  --line-bold: #b3ac9d;
  --ink: #2b2a27;
  --ink-soft: #918b7f;
  --accent: #4e6e81;
  --sel: rgba(78, 110, 129, 0.18);
  --peer: rgba(78, 110, 129, 0.07);
  --same: rgba(78, 110, 129, 0.26);
  --error: #b5544c;
  --error-bg: rgba(181, 84, 76, 0.10);
  --shadow: 0 1px 2px rgba(35, 30, 18, 0.05), 0 14px 36px rgba(35, 30, 18, 0.07);
  --shadow-sm: 0 1px 2px rgba(35, 30, 18, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151619;
    --surface: #1d1f23;
    --line: #2c2f34;
    --line-bold: #52565e;
    --ink: #e7e4de;
    --ink-soft: #85888f;
    --accent: #93b8cf;
    --sel: rgba(147, 184, 207, 0.22);
    --peer: rgba(147, 184, 207, 0.08);
    --same: rgba(147, 184, 207, 0.30);
    --error: #d07b74;
    --error-bg: rgba(208, 123, 116, 0.13);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 14px 36px rgba(0, 0, 0, 0.35);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  background: var(--bg);
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button {
  font-family: inherit;
  cursor: pointer;
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#app {
  --size: min(100vw - 28px, 500px, 100dvh - 250px);
  width: var(--size);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- header ---------- */

header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0 2px;
}

.hlabel {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

/* ---------- board ---------- */

#board {
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--surface);
  border: 2px solid var(--line-bold);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: var(--shadow);
  font-size: calc(var(--size) / 16);
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  transition: background-color 0.12s ease;
  cursor: pointer;
}

.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(n+73) { border-bottom: none; }
.cell.bv { border-right: 2px solid var(--line-bold); }
.cell.bh { border-bottom: 2px solid var(--line-bold); }

.cell.error { background: var(--error-bg); }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }

.cell .v {
  position: relative;
  z-index: 1;
  font-weight: 500;
  color: var(--ink);
}

.cell:not(.given) .v { color: var(--accent); }
.cell.error .v { color: var(--error); }

.cell .n {
  position: absolute;
  inset: 2px;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
}

.cell .n i {
  font-style: normal;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--size) / 45);
  font-weight: 500;
  color: var(--ink-soft);
}

/* flash / wave overlay */
.cell::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--accent);
  opacity: 0;
  pointer-events: none;
}

.cell.flash::after {
  animation: cellflash 0.55s ease var(--fd, 0ms) both;
}

.cell.wave::after {
  animation: cellflash 0.6s ease var(--d, 0ms) both;
}

.cell.wave .v {
  animation: popbig 0.6s cubic-bezier(0.2, 1.2, 0.3, 1) var(--d, 0ms) both;
}

.cell.pop .v {
  animation: pop 0.28s cubic-bezier(0.3, 1.6, 0.4, 1);
}

.cell.shake {
  animation: shake 0.3s ease;
}

#board.deal .cell .v,
#board.deal .cell .n {
  animation: dealin 0.4s ease var(--d, 0ms) both;
}

@keyframes cellflash {
  0% { opacity: 0; }
  30% { opacity: 0.32; }
  100% { opacity: 0; }
}

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

@keyframes popbig {
  0% { transform: scale(1); }
  40% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  50% { transform: translateX(3px); }
  75% { transform: translateX(-2px); }
}

@keyframes dealin {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* ---------- controls ---------- */

#controls {
  display: flex;
  justify-content: space-between;
  padding: 0 2px;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: none;
  border: none;
  color: var(--ink-soft);
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.03em;
  padding: 6px 10px;
  border-radius: 10px;
  transition: color 0.15s ease, transform 0.08s ease;
}

.tool svg {
  width: 22px;
  height: 22px;
}

.tool:active { transform: scale(0.88); }
.tool.on { color: var(--accent); }

/* ---------- number pad ---------- */

#pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}

.key {
  height: clamp(46px, 13vw, 58px);
  border: none;
  border-radius: 10px;
  background: var(--surface);
  color: var(--accent);
  font-size: clamp(20px, 6vw, 26px);
  font-weight: 500;
  box-shadow: var(--shadow-sm);
  transition: transform 0.08s ease, opacity 0.3s ease, background-color 0.15s ease;
}

.key:active {
  transform: scale(0.88);
  background: var(--sel);
}

.key.done {
  opacity: 0;
  pointer-events: none;
}

/* ---------- overlay + sheet ---------- */

#overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(20, 19, 16, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}

#overlay.open {
  opacity: 1;
  pointer-events: auto;
}

#sheet {
  width: min(100%, 440px);
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 24px 20px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.16);
  transform: translateY(24px);
  transition: transform 0.26s cubic-bezier(0.2, 0.9, 0.3, 1);
}

#overlay.open #sheet {
  transform: none;
}

@media (min-width: 560px) {
  #overlay { align-items: center; }
  #sheet { border-radius: 22px; }
}

#sheet h2 {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  text-align: center;
  color: var(--ink-soft);
  margin-bottom: 18px;
}

.diff {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 14px 16px;
  margin-bottom: 8px;
  border: none;
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  font-weight: 500;
  transition: transform 0.08s ease, background-color 0.15s ease;
}

.diff:active {
  transform: scale(0.97);
  background: var(--sel);
}

.diff .best {
  font-size: 12px;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.ghost {
  display: block;
  width: 100%;
  padding: 12px;
  margin-top: 4px;
  background: none;
  border: none;
  color: var(--ink-soft);
  font-size: 14px;
  font-weight: 500;
}

#sheet-new.fresh #btn-cancel { display: none; }

#win-time {
  font-size: 44px;
  font-weight: 200;
  text-align: center;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
  margin: 2px 0 6px;
}

#win-sub {
  text-align: center;
  color: var(--ink-soft);
  font-size: 13px;
}

#win-best {
  text-align: center;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  min-height: 1.2em;
  margin: 4px 0 16px;
}

/* ---------- misc ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
