:root {
  --bg: #eef2f6;
  --panel: #ffffff;
  --board-bg: #ffffff;
  --grid-line: #b9c4d3;
  --grid-line-thick: #33415c;
  --cell-hover: #e0eaf5;
  --cell-same: #d3e3f6;
  --cell-selected: #bcd7f7;
  --cell-error: #fde2e2;
  --cell-same-error: #f8cfcf;

  --text: #1f2937;
  --muted: #6b7688;
  --given: #1f2937;
  --user: #2b6cb0;
  --note: #7a8899;

  --accent: #3b82f6;
  --accent-dark: #2b6cb0;
  --accent-soft: #e0ebff;

  --error: #e53e3e;
  --success: #38a169;

  --shadow: 0 10px 30px rgba(20, 40, 80, 0.12);
  --radius: 16px;
}

body.dark {
  --bg: #10141c;
  --panel: #192030;
  --board-bg: #1c2536;
  --grid-line: #33405a;
  --grid-line-thick: #7f8db0;
  --cell-hover: #243049;
  --cell-same: #29416a;
  --cell-selected: #34578f;
  --cell-error: #4a2323;
  --cell-same-error: #5a2a2a;

  --text: #e6ecf5;
  --muted: #8b97ab;
  --given: #cbd6e6;
  --user: #74b3ff;
  --note: #8b97ab;

  --accent: #5a9bff;
  --accent-dark: #74b3ff;
  --accent-soft: #25406b;

  --error: #fc8181;
  --success: #68d391;

  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: "Poppins", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 80% -10%, var(--accent-soft), transparent 60%),
    var(--bg);
  display: flex;
  justify-content: center;
  padding: 24px 16px 48px;
  transition: background 0.3s ease, color 0.3s ease;
}

.app {
  width: 100%;
  max-width: 520px;
}

.app__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.logo {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-dark));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.theme-toggle {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--panel);
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: 1.1rem;
  display: grid;
  place-items: center;
  transition: transform 0.2s ease, background 0.3s ease;
}
.theme-toggle:hover { transform: scale(1.08); }
.theme-toggle:active { transform: scale(0.95); }

/* Controls */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.difficulty__seg {
  display: inline-grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  background: var(--panel);
  padding: 5px;
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.difficulty__btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.18s ease;
}
.difficulty__btn:hover { color: var(--text); }
.difficulty__btn.is-active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

.stats {
  display: flex;
  gap: 14px;
}
.stat {
  background: var(--panel);
  border-radius: 12px;
  padding: 8px 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 72px;
}
.stat__label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
}
.stat__value {
  font-family: "JetBrains Mono", monospace;
  font-weight: 600;
  font-size: 0.95rem;
}

/* Board */
.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  margin-bottom: 20px;
}
.board {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--grid-line);
  gap: 1px;
  border: 3px solid var(--grid-line-thick);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.cell {
  background: var(--board-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.1rem, 5.4vw, 1.7rem);
  font-weight: 500;
  cursor: pointer;
  position: relative;
  user-select: none;
  transition: background 0.12s ease;
  font-family: "JetBrains Mono", monospace;
}
.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(9n - 3) { border-right: 2px solid var(--grid-line-thick); }
.cell:nth-child(9n - 6) { border-right: 2px solid var(--grid-line-thick); }

/* thick horizontal dividers via rows 3 and 6 */
.cell-row-thick { border-bottom: 2px solid var(--grid-line-thick); }

.cell--given { color: var(--given); font-weight: 600; }
.cell--user { color: var(--user); }
.cell--selected { background: var(--cell-selected) !important; }
.cell--same-value { background: var(--cell-same); }
.cell--hover { background: var(--cell-hover); }
.cell--error { background: var(--cell-error) !important; color: var(--error); }
.cell--same-error { background: var(--cell-same-error) !important; color: var(--error); }

.cell__notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
  padding: 2px;
}
.cell__notes span {
  font-size: clamp(0.4rem, 2.2vw, 0.7rem);
  color: var(--note);
  font-family: "JetBrains Mono", monospace;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
/* A `hidden` element relies on the user-agent `display: none` rule, but our
   `.cell__notes { display: grid }` rule has equal specificity and would win. */
.cell__notes[hidden] {
  display: none;
}

.cell__status {
  position: absolute;
  bottom: 3px;
  right: 4px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--error);
  display: none;
}
.cell.has-note .cell__status { display: block; }

/* Win overlay */
.win-overlay {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  animation: fade 0.3s ease;
}
/* The hidden attribute uses display:none (user-agent rule); match or beat
   its specificity so a true `hidden` actually hides the overlay. */
.win-overlay[hidden] {
  display: none;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.win-card {
  text-align: center;
  background: var(--panel);
  padding: 28px 32px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  animation: pop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes pop { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.win-card__emoji { font-size: 2.6rem; }
.win-card__title { margin: 8px 0 4px; font-size: 1.6rem; }
.win-card__sub { margin: 0 0 18px; color: var(--muted); font-size: 0.9rem; }

/* Actions */
.actions {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 16px;
}
.action {
  flex: 1;
  border: none;
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, background 0.2s ease;
}
.action:hover { transform: translateY(-2px); }
.action:active { transform: translateY(0); }
.action__icon { font-size: 1.2rem; }
.action.is-on { background: var(--accent-soft); color: var(--accent-dark); }

/* Numpad */
.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.num-btn {
  border: none;
  background: var(--panel);
  color: var(--accent-dark);
  font-family: "JetBrains Mono", monospace;
  font-size: 1.5rem;
  font-weight: 600;
  aspect-ratio: 1 / 1.15;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
  position: relative;
  transition: transform 0.12s ease, background 0.2s ease;
}
.num-btn:hover { transform: translateY(-2px); background: var(--accent-soft); }
.num-btn:active { transform: translateY(0); }
.num-btn--done {
  color: var(--muted);
  opacity: 0.4;
  pointer-events: none;
}
.num-btn__count {
  position: absolute;
  bottom: 3px;
  right: 5px;
  font-size: 0.6rem;
  font-family: "Poppins", sans-serif;
  color: var(--muted);
}

/* Footer */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.btn {
  border: none;
  font-family: inherit;
  font-weight: 600;
  padding: 12px 22px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 0.95rem;
  transition: transform 0.12s ease, box-shadow 0.2s ease, background 0.2s ease;
}
.btn--primary {
  background: linear-gradient(90deg, var(--accent), var(--accent-dark));
  color: #fff;
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
}
.btn--primary:hover { transform: translateY(-2px); }
.btn--ghost {
  background: transparent;
  color: var(--muted);
  box-shadow: none;
}
.btn--ghost:hover { color: var(--text); }
.footer__hint {
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
  flex: 1;
}

/* Laya log — append-only trace of every action Laya takes */
.laya-log {
  margin-top: 18px;
  background: var(--panel);
  border: 1px solid color-mix(in srgb, var(--muted) 25%, transparent);
  border-radius: 12px;
  padding: 10px 14px 12px;
}
.laya-log__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.laya-log__title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.laya-log__clear {
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 8px;
}
.laya-log__clear:hover { color: var(--text); background: color-mix(in srgb, var(--muted) 15%, transparent); }
.laya-log__entries {
  max-height: 180px;
  overflow-y: auto;
  font-family: "JetBrains Mono", monospace;
  font-size: 0.72rem;
  line-height: 1.55;
}
.laya-log__entry {
  display: flex;
  gap: 10px;
  padding: 1px 0;
}
.laya-log__time {
  color: var(--muted);
  flex: none;
  opacity: 0.75;
}
.laya-log__msg { color: var(--text); word-break: break-word; }
.laya-log__entry--backtrack .laya-log__msg { color: #d97706; }
.laya-log__entry--wait .laya-log__msg { color: var(--muted); font-style: italic; }
.laya-log__entry--done .laya-log__msg { color: var(--accent); font-weight: 600; }
.dark .laya-log__entry--backtrack .laya-log__msg { color: #fbbf24; }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  background: var(--error);
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 50;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 90vw;
  text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 420px) {
  .logo { font-size: 1.6rem; }
  .difficulty__btn { padding: 7px 8px; font-size: 0.78rem; }
  .num-btn { font-size: 1.2rem; }
}
