:root {
  color-scheme: light;
  --accent: #3584e4;
  --accent-hover: #2a76d2;
  --accent-fg: #ffffff;
  --success: #2ec27e;
  --warning: #f5c211;
  --error: #d81920;
  --bg: #fafafb;
  --surface: #ffffff;
  --surface-2: #f0f0f2;
  --header: rgba(250, 250, 251, 0.92);
  --dialog: #ffffff;
  --fg: #323239;
  --fg-strong: #101014;
  --fg-weak: #8b8b93;
  --border: rgba(0, 0, 0, 0.11);
  --line: rgba(50, 50, 70, 0.16);
  --line-strong: rgba(50, 50, 70, 0.55);
  --tint-weak: rgba(53, 132, 228, 0.13);
  --tint-strong: rgba(53, 132, 228, 0.28);
  --peer-tint: rgba(40, 40, 70, 0.055);
  --shadow-1: 0 1px 2px rgba(20, 20, 30, 0.10), 0 1px 6px rgba(20, 20, 30, 0.06);
  --shadow-2: 0 12px 40px rgba(10, 10, 20, 0.30);
  --radius: 12px;
}

[data-theme="dark"] {
  color-scheme: dark;
  --accent: #4a90e8;
  --accent-hover: #5b9cf0;
  --success: #33d17a;
  --warning: #f8e45c;
  --error: #ff7b74;
  --bg: #1e1e23;
  --surface: #2b2b31;
  --surface-2: #36363d;
  --header: rgba(30, 30, 35, 0.92);
  --dialog: #38383f;
  --fg: #f2f2f4;
  --fg-strong: #ffffff;
  --fg-weak: #97979f;
  --border: rgba(255, 255, 255, 0.10);
  --line: rgba(255, 255, 255, 0.13);
  --line-strong: rgba(255, 255, 255, 0.45);
  --tint-weak: rgba(74, 144, 232, 0.24);
  --tint-strong: rgba(74, 144, 232, 0.42);
  --peer-tint: rgba(255, 255, 255, 0.065);
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-2: 0 12px 40px rgba(0, 0, 0, 0.55);
}

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

html {
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body {
  margin: 0;
  font-family: "Cantarell", "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.45;
  touch-action: manipulation;
}

.headerbar,
.play-card,
.alt-card,
.btn,
.icon-btn,
.tool,
.num-btn,
.chip,
.diff-opt,
.hist-item {
  -webkit-touch-callout: none;
  user-select: none;
}

button {
  font: inherit;
  color: inherit;
}

::selection {
  background: var(--tint-strong);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.headerbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  padding-top: calc(0.55rem + env(safe-area-inset-top));
  background: var(--header);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}

.hdr-side {
  display: flex;
  gap: 0.15rem;
  min-width: 92px;
}

.hdr-side.right {
  justify-content: flex-end;
}

.hdr-title {
  flex: 1;
  margin: 0;
  text-align: center;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: var(--fg-strong);
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn:hover {
  background: var(--peer-tint);
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn.small {
  width: 34px;
  height: 34px;
}

.icon-btn.small svg {
  width: 17px;
  height: 17px;
}

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

.btn {
  border: none;
  border-radius: 999px;
  padding: 0.6rem 1.3rem;
  font-weight: 700;
  font-size: 0.95rem;
  background: var(--surface-2);
  color: var(--fg);
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.1s ease, background 0.15s ease;
}

.btn:hover {
  filter: brightness(0.97);
}

.btn:active {
  transform: scale(0.97);
}

.btn.suggested {
  background: var(--accent);
  color: var(--accent-fg);
}

.btn.suggested:hover {
  background: var(--accent-hover);
  filter: none;
}

.btn.destructive {
  background: #e01b24;
  color: #fff;
}

.btn.destructive:hover {
  background: #c7161e;
  filter: none;
}

.btn.text {
  background: transparent;
  padding: 0.35rem 0.8rem;
  color: var(--accent);
}

.btn.text.destructive-text {
  color: var(--error);
}

.btn.text:hover {
  background: var(--peer-tint);
  filter: none;
}

.btn.block {
  width: 100%;
}

a.btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.view {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem 1rem calc(1.75rem + env(safe-area-inset-bottom));
  animation: view-in 0.25s ease;
}

@keyframes view-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

.status-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.32rem 0.75rem;
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}

.chip .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
}

.chip-ic {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: var(--fg-weak);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.mono {
  font-variant-numeric: tabular-nums;
}

.board-wrap {
  position: relative;
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
}

#board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1;
  background: var(--surface);
  border: 2px solid var(--line-strong);
  border-radius: 14px;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

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

.cell.eb {
  border-bottom: none;
}

.cell.er {
  border-right: none;
}

.cell.bt {
  box-shadow: inset 0 2px 0 var(--line-strong);
}

.cell.bl {
  box-shadow: inset 2px 0 0 var(--line-strong);
}

.cell.peer {
  background: var(--peer-tint);
}

.cell.same {
  background: var(--tint-weak);
}

.cell.selected {
  background: var(--tint-strong);
}

.cell.selected::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 2px var(--accent);
  pointer-events: none;
}

.cell .val {
  font-size: clamp(1.05rem, 4.6vw, 1.85rem);
  font-weight: 500;
  color: var(--accent);
  line-height: 1;
}

.cell.given .val {
  font-weight: 700;
  color: var(--fg-strong);
}

.cell.error .val {
  color: var(--error);
}

.notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}

.cell.filled .notes {
  display: none;
}

.notes span {
  display: grid;
  place-items: center;
  font-size: clamp(0.42rem, 1.75vw, 0.72rem);
  font-weight: 700;
  color: var(--fg-weak);
  opacity: 0;
}

.notes span.on {
  opacity: 1;
}

.pause-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  background: rgba(250, 250, 251, 0.55);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.pause-overlay.show {
  opacity: 0.96;
  pointer-events: auto;
}

.pause-overlay .big-icon {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: var(--surface-2);
  display: grid;
  place-items: center;
}

.pause-overlay .big-icon svg {
  width: 34px;
  height: 34px;
  fill: none;
  stroke: var(--fg);
  stroke-width: 2;
  stroke-linecap: round;
}

.pause-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--fg-strong);
}

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 0.4rem;
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
}

.num-btn {
  aspect-ratio: 1;
  max-height: 54px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface);
  font-size: clamp(1rem, 3.9vw, 1.35rem);
  font-weight: 800;
  cursor: pointer;
  transition: background 0.12s ease, transform 0.1s ease, opacity 0.2s ease;
  font-variant-numeric: tabular-nums;
}

.num-btn:hover {
  background: var(--surface-2);
}

.num-btn:active {
  transform: scale(0.94);
}

.num-btn[data-done] {
  opacity: 0.3;
  pointer-events: none;
}

.tools {
  display: flex;
  justify-content: center;
  gap: 1.1rem;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  background: none;
  border: none;
  padding: 0.25rem;
  border-radius: 14px;
  cursor: pointer;
}

.tool .ic {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  display: grid;
  place-items: center;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.tool .ic svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tool .lbl {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--fg-weak);
}

.tool:hover .ic {
  background: var(--surface-2);
}

.tool:active .ic {
  transform: scale(0.94);
}

.tool.active .ic {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-fg);
}

.tool.active .lbl {
  color: var(--accent);
}

.tool:disabled {
  opacity: 0.4;
  pointer-events: none;
}

.kbd-hint {
  margin: 0;
  text-align: center;
  font-size: 0.75rem;
  color: var(--fg-weak);
}

@media (pointer: coarse) {
  .kbd-hint {
    display: none;
  }
}

.section-title {
  margin: 0.5rem 0 0;
  font-size: 1rem;
  font-weight: 800;
  color: var(--fg-strong);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.55rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 0.4rem;
  text-align: center;
}

.stat-card.wide {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  text-align: left;
  padding: 0.8rem 1rem;
}

.stat-card .stat-ic {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--tint-weak);
  color: var(--accent);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.stat-card .stat-ic svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.stat-num {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--fg-strong);
  font-variant-numeric: tabular-nums;
}

.stat-lbl {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--fg-weak);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.hist-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.hist-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.5rem 0.6rem 0.9rem;
}

.hist-item .dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
}

.hist-main {
  flex: 1;
  min-width: 0;
}

.hist-title {
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--fg-strong);
}

.hist-sub {
  font-size: 0.78rem;
  color: var(--fg-weak);
}

.hist-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.hist-time {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
}

.best-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  background: rgba(245, 194, 17, 0.22);
  color: #8a6d00;
  font-size: 0.68rem;
  font-weight: 800;
  padding: 0.14rem 0.5rem;
  border-radius: 999px;
}

[data-theme="dark"] .best-chip {
  color: var(--warning);
}

.best-chip svg {
  width: 11px;
  height: 11px;
  fill: currentColor;
  stroke: none;
}

.empty {
  padding: 2.4rem 1rem;
  text-align: center;
  color: var(--fg-weak);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.empty p {
  margin: 0.2rem 0;
}

.empty strong {
  color: var(--fg);
}

.backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(15, 15, 22, 0.45);
  display: grid;
  place-items: center;
  padding: 1rem;
  animation: fade-in 0.18s ease;
}

[data-theme="dark"] .backdrop {
  background: rgba(0, 0, 0, 0.55);
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

.dialog {
  background: var(--dialog);
  border-radius: 20px;
  box-shadow: var(--shadow-2);
  width: min(94vw, 400px);
  padding: 1.35rem 1.25rem 1.15rem;
  animation: pop-in 0.22s cubic-bezier(0.2, 0.9, 0.3, 1.15);
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: scale(0.94) translateY(10px);
  }
}

.dialog h2 {
  margin: 0 0 0.2rem;
  font-size: 1.15rem;
  text-align: center;
  color: var(--fg-strong);
}

.dialog .sub {
  margin: 0 0 1rem;
  text-align: center;
  font-size: 0.88rem;
  color: var(--fg-weak);
}

.dialog .warn {
  margin: 0.8rem 0 0;
  font-size: 0.8rem;
  color: var(--warning);
  text-align: center;
  font-weight: 700;
}

.dialog-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: flex-end;
  margin-top: 1.3rem;
}

.dialog-actions.column {
  flex-direction: column;
}

.dialog-actions .btn {
  min-width: 110px;
}

.dialog-actions.column .btn {
  width: 100%;
}

.diff-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.diff-opt {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  cursor: pointer;
  text-align: left;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.diff-opt:hover {
  background: var(--surface-2);
}

.diff-opt.sel {
  border-color: var(--accent);
  background: var(--tint-weak);
}

.diff-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.diff-name {
  font-weight: 800;
  color: var(--fg-strong);
}

.diff-desc {
  font-size: 0.76rem;
  color: var(--fg-weak);
}

.diff-best {
  margin-left: auto;
  font-size: 0.76rem;
  font-weight: 800;
  color: var(--fg-weak);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.win-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.55rem;
  margin-bottom: 0.4rem;
}

.win-badge {
  width: 74px;
  height: 74px;
  border-radius: 50%;
  background: var(--success);
  color: #fff;
  display: grid;
  place-items: center;
  animation: badge-pop 0.45s cubic-bezier(0.2, 1.6, 0.4, 1);
}

.win-badge svg {
  width: 36px;
  height: 36px;
  fill: none;
  stroke: #fff;
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@keyframes badge-pop {
  from {
    transform: scale(0.4);
    opacity: 0;
  }
}

.win-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.45rem;
  margin-top: 0.35rem;
}

.stat-pill {
  background: var(--surface-2);
  border-radius: 999px;
  padding: 0.32rem 0.75rem;
  font-size: 0.82rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}

.stat-pill svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@media (max-width: 380px) {
  .hdr-side {
    min-width: 84px;
  }

  .numpad {
    gap: 0.28rem;
  }

  .tools {
    gap: 0.7rem;
  }
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  margin-left: 3px;
}

.brand-mark svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.play-card {
  display: flex;
  align-items: center;
  gap: 0.95rem;
  background: var(--accent);
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 72%, #7c4dff));
  color: #fff;
  border-radius: 18px;
  padding: 1.05rem 1.1rem;
  text-decoration: none;
  box-shadow: var(--shadow-1);
  transition: transform 0.12s ease, filter 0.15s ease;
}

.play-card:hover {
  filter: brightness(1.05);
}

.play-card:active {
  transform: scale(0.98);
}

.play-icon {
  width: 52px;
  height: 52px;
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.18);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.play-icon svg {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.play-text {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.play-text strong {
  font-size: 1.12rem;
  font-weight: 800;
  letter-spacing: 0.2px;
}

.play-text small {
  font-size: 0.8rem;
  font-weight: 600;
  opacity: 0.88;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.play-chev {
  margin-left: auto;
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.85;
}

.alt-card {
  display: flex;
  align-items: center;
  gap: 0.95rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 0.95rem 1.1rem;
  text-decoration: none;
  color: var(--fg);
  box-shadow: var(--shadow-1);
  transition: transform 0.12s ease, background 0.15s ease;
}

button.alt-card {
  width: 100%;
  text-align: left;
  cursor: pointer;
}

.alt-card:hover {
  background: var(--surface-2);
}

.alt-card:active {
  transform: scale(0.98);
}

.alt-icon {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: var(--tint-weak);
  color: var(--accent);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.alt-icon svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

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