:root,
:root[data-theme="dark"] {
  --bg: #12141c;
  --panel: #1b1e2b;
  --cell: #2a2e42;
  --cell-border: #3a3f5c;
  --accent: #5b8cff;
  --accent2: #ffb454;
  --good: #4ade80;
  --bad: #ff6b6b;
  --text: #f0f2fa;
  --text-dim: #8a8fa8;
  --cleared-border: rgba(255, 255, 255, 0.14);
  --cleared-text: rgba(255, 255, 255, 0.16);
  --grid-line: rgba(240, 242, 250, 0.3);
  --grid-line-strong: rgba(240, 242, 250, 0.75);
  --cell-size: 38px;
  --gap: 0px;
}

:root[data-theme="light"] {
  --bg: #f0f2f8;
  --panel: #ffffff;
  --cell: #e8eaf3;
  --cell-border: #d3d7e6;
  --accent: #3d6bf0;
  --accent2: #e08a1e;
  --good: #22b361;
  --bad: #e0453f;
  --text: #1c1e2b;
  --text-dim: #6b7086;
  --cleared-border: rgba(0, 0, 0, 0.15);
  --cleared-text: rgba(0, 0, 0, 0.2);
  --grid-line: rgba(28, 30, 43, 0.3);
  --grid-line-strong: rgba(28, 30, 43, 0.85);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f0f2f8;
    --panel: #ffffff;
    --cell: #e8eaf3;
    --cell-border: #d3d7e6;
    --accent: #3d6bf0;
    --accent2: #e08a1e;
    --good: #22b361;
    --bad: #e0453f;
    --text: #1c1e2b;
    --text-dim: #6b7086;
    --cleared-border: rgba(0, 0, 0, 0.15);
    --cleared-text: rgba(0, 0, 0, 0.2);
    --grid-line: rgba(28, 30, 43, 0.3);
    --grid-line-strong: rgba(28, 30, 43, 0.85);
  }
}

html {
  background: var(--bg);
  transition: background 0.2s ease;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
  transition: background 0.2s ease, color 0.2s ease;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--cell-border);
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.stats {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--text-dim);
}

#remaining {
  color: var(--accent2);
  font-weight: 700;
}

.theme-btn {
  border: 1px solid var(--cell-border);
  background: var(--cell);
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, background 0.15s ease;
}

.theme-btn:active {
  transform: scale(0.9);
}

#board-wrap {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  display: flex;
  justify-content: center;
  padding: 12px 8px 24px;
}

#board {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: 100%;
  max-width: calc(9 * var(--cell-size));
  background: var(--panel);
  border: 2px solid var(--grid-line-strong);
  border-radius: 2px;
}

.row {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: var(--gap);
  transition: transform 0.28s ease, opacity 0.28s ease;
}

.row.row-clearing {
  opacity: 0;
  transform: scaleY(0.2);
  transform-origin: top;
}

.row:not(:last-child) .cell {
  border-bottom: 1px solid var(--grid-line);
}

.cell {
  position: relative;
  z-index: 1;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel);
  border-right: 1px solid var(--grid-line);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  user-select: none;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease, box-shadow 0.2s ease, color 0.2s ease;
}

.cell:nth-child(9n) {
  border-right: none;
}

.cell:active {
  transform: scale(0.9);
}

.cell.selected {
  z-index: 2;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 2px var(--accent);
  transform: scale(1.08);
}

.cell.cleared {
  background: transparent;
  color: var(--cleared-text);
  cursor: default;
  font-weight: 400;
}

.cell.hint {
  z-index: 2;
  animation: hint-pulse 0.9s ease-in-out infinite;
}

.cell.match-good {
  z-index: 2;
  animation: pop-good 0.35s ease forwards;
}

.cell.match-bad {
  z-index: 2;
  animation: shake-bad 0.35s ease;
}

@keyframes hint-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 180, 84, 0.6); }
  50% { box-shadow: 0 0 0 6px rgba(255, 180, 84, 0); }
}

@keyframes pop-good {
  0% { transform: scale(1); background: var(--good); box-shadow: 0 0 0 2px var(--good); }
  60% { transform: scale(1.25); background: var(--good); box-shadow: 0 0 0 2px var(--good); }
  100% { transform: scale(0.3); opacity: 0; }
}

@keyframes shake-bad {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); background: var(--bad); box-shadow: 0 0 0 2px var(--bad); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

#toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  background: var(--panel);
  border: 1px solid var(--cell-border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 10;
  white-space: nowrap;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.controls {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--cell-border);
}

.ctrl-btn {
  flex: 1;
  padding: 12px 0;
  font-size: 15px;
  font-weight: 600;
  border-radius: 10px;
  border: 1px solid var(--cell-border);
  background: var(--cell);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
}

.ctrl-btn:active {
  transform: scale(0.95);
  background: var(--accent);
}

#btn-add {
  position: relative;
  flex: 0 0 56px;
  font-size: 22px;
}

#btn-add:disabled {
  opacity: 0.4;
  cursor: default;
}

#btn-add:disabled:active {
  transform: none;
  background: var(--cell);
}

.add-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent2);
  color: #1c1e2b;
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

@media (min-width: 480px) {
  :root { --cell-size: 44px; }
}
