:root {
  --bg: #f4f3fb;
  --card: #ffffff;
  --text: #1d1b2e;
  --muted: #6b6880;
  --line: #e4e2f0;
  --cell: #f7f7fd;
  --cell-filled: #e6e5ff;
  --accent: #6364ff;
  --accent-strong: #4b4ad8;
  --accent-soft: #ecebff;
  --dot: #1d1b2e;
  --dot-text: #ffffff;
  --wall: #1d1b2e;
  --success: #17a673;
  --gold: #f5b301;
  --danger: #d93a4a;
  --shadow: 0 1px 2px rgba(29, 27, 46, 0.06), 0 6px 24px rgba(29, 27, 46, 0.06);
  --radius: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131220;
    --card: #1d1c2e;
    --text: #ecebf7;
    --muted: #9c99b5;
    --line: #393757;
    --cell: #232238;
    --cell-filled: #34327a;
    --accent: #8384ff;
    --accent-strong: #a2a3ff;
    --accent-soft: #2a2950;
    --dot: #ecebf7;
    --dot-text: #131220;
    --wall: #ecebf7;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent-strong); }

h1, h2 { line-height: 1.2; margin: 0 0 0.6rem; }
h1 { font-size: clamp(1.6rem, 6vw, 2.2rem); letter-spacing: -0.02em; }
h2 { font-size: 1.15rem; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.center { text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* --- layout --------------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
  padding: 14px 16px 6px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 800;
  font-size: 1.25rem;
  letter-spacing: -0.02em;
  color: var(--text);
  text-decoration: none;
}

.account { display: flex; align-items: center; gap: 0.75rem; min-width: 0; }
.account-link {
  display: flex; align-items: center; gap: 0.4rem; min-width: 0;
  color: var(--muted); text-decoration: none; font-size: 0.85rem;
}
.account-link span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 34vw; }
.account-link img { border-radius: 8px; flex: none; }

.tabs {
  display: flex;
  gap: 4px;
  max-width: 560px;
  width: calc(100% - 32px);
  margin: 8px auto 0;
  padding: 4px;
  background: var(--card);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.tabs a {
  flex: 1;
  text-align: center;
  padding: 8px 4px;
  border-radius: 9px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.9rem;
}
.tabs a.active { background: var(--accent-soft); color: var(--accent-strong); }

main {
  flex: 1;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px 24px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 14px;
}

.footer { text-align: center; padding: 16px; }
.footer p { margin: 0.25rem 0; }
.footer span, .footer a { white-space: nowrap; }
.footer a { color: inherit; }

.notice { padding: 12px 16px; border-radius: 12px; margin-bottom: 14px; }
.notice.error { background: color-mix(in srgb, var(--danger) 14%, var(--card)); color: var(--text); }

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

button, select, input, textarea { font: inherit; color: inherit; }

button {
  border: 1.5px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 9px 18px;
  font-weight: 600;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
button.big { padding: 12px 40px; font-size: 1.1rem; }
.link-button { border: 0; background: none; padding: 0; color: var(--muted); font-weight: 500; font-size: 0.85rem; }
.link-button:hover { color: var(--text); }
.icon-button { padding: 4px 14px; font-size: 1.2rem; line-height: 1; }

input[type="text"], select, textarea {
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  padding: 10px 12px;
  width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible, .board:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.login-form { display: flex; gap: 8px; margin: 12px 0; }
.login-form input { flex: 1; min-width: 0; }

.toggle { display: flex; gap: 10px; align-items: center; cursor: pointer; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }

/* --- landing -------------------------------------------------------------- */

.hero .lede { color: var(--muted); margin: 0 0 1rem; }
.demo { display: flex; justify-content: center; margin: 8px 0 12px; }
.demo-svg { width: 132px; height: 132px; background: var(--cell); border-radius: 14px; }
.demo-path {
  fill: none; stroke: var(--accent); stroke-width: 0.42; stroke-linecap: round; stroke-linejoin: round;
  stroke-dasharray: 9; stroke-dashoffset: 9; animation: draw 2.8s ease-in-out infinite;
}
.demo-dots circle { fill: var(--dot); }
.demo-dots text { fill: var(--dot-text); font-size: 0.32px; font-weight: 700; text-anchor: middle; dominant-baseline: central; }
@keyframes draw { 0% { stroke-dashoffset: 9; } 60%, 100% { stroke-dashoffset: 0; } }
.today-line { text-align: center; color: var(--muted); margin: 0; font-size: 0.9rem; }

/* --- game ----------------------------------------------------------------- */

.game-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.puzzle-title { font-weight: 700; font-size: 1.1rem; }
.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 1.4rem;
  background: var(--accent-soft);
  color: var(--accent-strong);
  padding: 4px 14px;
  border-radius: 999px;
}

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

.board {
  --n: 6;
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  aspect-ratio: 1;
  width: 100%;
  border: 2px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  background: var(--cell);
}
.board.concealed .cell { filter: blur(2px); }
.cell {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  transition: background-color 120ms;
}
.cell.filled { background: var(--cell-filled); }
.cell.hint { animation: hint-pulse 1s ease-in-out infinite; }
@keyframes hint-pulse { 50% { background: color-mix(in srgb, var(--gold) 55%, var(--cell)); } }
.result-hints { margin: -8px 0 12px; min-height: 1em; }

.path-layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.path-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 0.46;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.92;
}
.path-head { fill: var(--accent-strong); }
.dot circle { fill: var(--dot); transition: fill 150ms; }
.dot.passed circle { fill: var(--accent-strong); }
.dot text {
  fill: var(--dot-text);
  font-size: 0.32px;
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: central;
}
.dot.passed text { fill: #fff; }
.wall { stroke: var(--wall); stroke-width: 0.12; stroke-linecap: round; }
.board.complete .path-line { stroke: var(--success); }
.board.complete .dot circle { fill: var(--success); }
.board.complete .path-head { fill: var(--success); }
.board.complete { animation: pulse 600ms ease-out; }
@keyframes pulse { 50% { transform: scale(1.02); } }

.board-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--card) 55%, transparent);
  border-radius: 14px;
  backdrop-filter: blur(3px);
}
.overlay-box { text-align: center; padding: 16px; max-width: 300px; }
.overlay-title { font-size: 1.5rem; font-weight: 800; margin: 0 0 4px; }

.controls { display: flex; gap: 10px; justify-content: center; margin: 16px 0 8px; }
.controls button { flex: 1; max-width: 200px; }
.status-line { text-align: center; margin: 8px 0 2px; min-height: 1.5em; }
.metrics { text-align: center; }

/* --- result --------------------------------------------------------------- */

.result-card { text-align: center; }
.result-kicker { margin: 0; font-weight: 700; color: var(--accent-strong); }
.result-time {
  font-size: clamp(3rem, 16vw, 4.5rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
  line-height: 1.1;
  margin: 4px 0 12px;
}
.result-facts, .stat-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.stat-row { grid-template-columns: repeat(4, 1fr); text-align: center; padding-bottom: 16px; border-bottom: 1px solid var(--line); }
.result-facts div, .stat-row div { display: flex; flex-direction: column; }
.result-facts b, .stat-row b { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.result-facts span, .stat-row span { color: var(--muted); font-size: 0.8rem; }

.share { text-align: left; border-top: 1px solid var(--line); padding-top: 14px; }
.share textarea { resize: vertical; margin: 6px 0 8px; font-size: 0.95rem; }
.share-row { display: flex; gap: 8px; flex-wrap: wrap; }
.share-row select { flex: 1 1 100px; width: auto; min-width: 0; }
.next-line { margin: 16px 0 0; color: var(--muted); }
.next-line b { color: var(--text); font-variant-numeric: tabular-nums; }

/* --- stats ---------------------------------------------------------------- */

.streak-block { padding-top: 14px; }
.week { display: flex; justify-content: space-between; gap: 4px; margin: 12px 0; }
.day { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: 1; }
.day .wd { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.dot-day {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--line); color: #fff; font-weight: 800;
}
.day.solved .dot-day { background: var(--gold); }
.day.today .dot-day { background: transparent; border: 2px dashed var(--gold); }
.day.started .dot-day { background: color-mix(in srgb, var(--danger) 35%, var(--line)); }

.badge-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.badge {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  padding: 12px 8px; border-radius: 12px; background: var(--bg); gap: 2px;
}
.badge-emoji { font-size: 1.8rem; line-height: 1.2; }
.badge-name { font-weight: 700; font-size: 0.9rem; }
.badge-desc { font-size: 0.75rem; color: var(--muted); }
.badge.locked .badge-emoji { filter: grayscale(1); opacity: 0.35; }
.badge.locked .badge-name { color: var(--muted); }
.badge.earned { background: color-mix(in srgb, var(--gold) 14%, var(--card)); }
.new-badges { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin-bottom: 16px; }
.badge.pop { animation: pop 500ms cubic-bezier(0.2, 1.6, 0.4, 1); }
@keyframes pop { from { transform: scale(0.6); opacity: 0; } to { transform: none; opacity: 1; } }

/* --- leaderboard & archive ------------------------------------------------ */

.lb-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.lb-head h2 { margin: 0; font-size: 1rem; text-align: center; }
.leaderboard, .archive { list-style: none; margin: 0; padding: 0; }
.leaderboard li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 6px; border-bottom: 1px solid var(--line);
}
.leaderboard li.me { background: var(--accent-soft); border-radius: 10px; }
.lb-rank { width: 2em; text-align: center; font-weight: 700; color: var(--muted); flex: none; }
.lb-avatar { border-radius: 8px; flex: none; }
.lb-name { display: flex; flex-direction: column; min-width: 0; flex: 1; color: var(--text); text-decoration: none; }
.lb-display, .lb-handle { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-display { font-weight: 600; }
.lb-handle { font-size: 0.8rem; color: var(--muted); }
.lb-time { font-weight: 700; font-variant-numeric: tabular-nums; flex: none; }

.archive li a {
  display: flex; gap: 10px; align-items: baseline; padding: 10px 6px;
  border-bottom: 1px solid var(--line); color: var(--text); text-decoration: none;
}
.archive li a:hover { background: var(--bg); }
.ar-num { font-weight: 700; width: 3.2em; flex: none; }
.ar-meta { flex: 1; color: var(--muted); font-size: 0.9rem; }
.ar-done { color: var(--success); font-weight: 600; font-size: 0.9rem; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  box-shadow: var(--shadow); font-size: 0.9rem; max-width: calc(100% - 32px);
}

@media (max-width: 420px) {
  .card { padding: 16px; }
  .result-facts b, .stat-row b { font-size: 1.25rem; }
  .dot-day { width: 30px; height: 30px; }
}
