:root {
  --bg: #f4efe4;
  --panel: #fffaf0;
  --ink: #2b2620;
  --muted: #7a7062;
  --line: #cfc4b0;
  --cell: #fbf7ee;
  --cell-given: #ece2cf;
  --cell-hint: #e3ecd9;
  --cell-wrong: #f6d3cc;
  --grid: #b9ad97;
  --frame: #3a332a;
  --rail: #5b6470;
  --rail-done: #b8862b;
  --sleeper: #8a5a34;
  --ok: #3f8a4f;
  --over: #c2412d;
  --accent: #c2412d;
  --accent-ink: #fff;
  --shadow: 0 6px 24px rgba(60, 45, 20, .12);
  --font: "Vazirmatn", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1a17;
    --panel: #26231f;
    --ink: #eee6d8;
    --muted: #a89d8c;
    --line: #3d3832;
    --cell: #2b2823;
    --cell-given: #3a342b;
    --cell-hint: #2f3a2b;
    --cell-wrong: #5a2a24;
    --grid: #4a443b;
    --frame: #8d826f;
    --rail: #b7c0cb;
    --rail-done: #e2b553;
    --sleeper: #8e6040;
    --ok: #74c086;
    --over: #f07a63;
    --accent: #e0644c;
    --shadow: 0 6px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1a17; --panel: #26231f; --ink: #eee6d8; --muted: #a89d8c; --line: #3d3832;
  --cell: #2b2823; --cell-given: #3a342b; --cell-hint: #2f3a2b; --cell-wrong: #5a2a24;
  --grid: #4a443b; --frame: #8d826f; --rail: #b7c0cb; --rail-done: #e2b553; --sleeper: #8e6040;
  --ok: #74c086; --over: #f07a63; --accent: #e0644c; --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

.app {
  max-width: 620px;
  margin: 0 auto;
  padding-block: 14px 32px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.title { margin: 0; font-size: 1.45rem; font-weight: 800; letter-spacing: -.01em; }
.logo { display: inline-block; }
.top-actions { display: flex; gap: 8px; }
.icon-btn, .lang-btn {
  min-width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  font-weight: 700;
}
.lang-btn { padding: 0 12px; }
.icon-btn:hover, .lang-btn:hover { border-color: var(--muted); }

.levels {
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 4px; gap: 4px;
}
.levels button {
  border: 0; background: transparent; padding: 8px 6px; border-radius: 10px; cursor: pointer; font-weight: 600;
  color: var(--muted);
}
.levels button[aria-selected="true"] { background: var(--ink); color: var(--bg); }

.status { display: flex; justify-content: space-between; gap: 8px; }
.pill {
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 4px 12px; font-size: .92rem; color: var(--muted);
}
.pill b { color: var(--ink); font-variant-numeric: tabular-nums; }

.board-wrap {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 8px;
}
.board {
  display: block; width: 100%; height: auto;
  touch-action: none; user-select: none; -webkit-user-select: none;
  direction: ltr;
}
.loading {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: 10px;
  align-items: center; justify-content: center; background: color-mix(in srgb, var(--panel) 85%, transparent);
  border-radius: 18px; font-weight: 600;
}
.loading[hidden] { display: none; }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Board SVG */
.cell { fill: var(--cell); stroke: var(--grid); stroke-width: 1; }
.cell.given { fill: var(--cell-given); }
.cell.hinted { fill: var(--cell-hint); }
.cell.wrong { fill: var(--cell-wrong); transition: fill .3s; }
.frame { fill: none; stroke: var(--frame); stroke-width: 3; }
.count { font-family: var(--font); font-size: 26px; font-weight: 600; fill: var(--ink); text-anchor: middle; dominant-baseline: central; transition: fill .2s; }
.count.ok { fill: var(--ok); }
.count.over { fill: var(--over); }
.mark { stroke: var(--muted); stroke-width: 3; stroke-linecap: round; opacity: .7; }
.sleepers { fill: none; stroke: var(--sleeper); stroke-width: 24; stroke-dasharray: 4 7; }
.rails { fill: none; stroke: var(--rail); stroke-width: 15; transition: stroke .5s; }
.board.done .rails { stroke: var(--rail-done); }
.given-rails .rails { stroke: var(--ink); }
.ends { fill: var(--accent); }
.end-label { font-family: var(--font); font-size: 13px; font-weight: 800; fill: var(--accent-ink); text-anchor: middle; dominant-baseline: central; }
.smoke { fill: var(--muted); }

.toolbar {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
}
.toolbar button, .dialog button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 12px;
  padding: 10px 8px; cursor: pointer; font-weight: 600; white-space: nowrap;
}
.toolbar button:hover, .dialog button:hover { border-color: var(--muted); }
.toolbar button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.06); border-color: var(--accent); }

.tip { margin: 0; text-align: center; color: var(--muted); font-size: .88rem; line-height: 1.7; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; font-weight: 600;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.dialog {
  border: 1px solid var(--line); border-radius: 18px; background: var(--panel); color: var(--ink);
  padding: 22px; width: min(440px, calc(100vw - 32px)); box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(20, 16, 10, .45); }
.dialog h2 { margin: 0 0 12px; font-size: 1.25rem; }
.dialog p, .dialog li { line-height: 1.8; }
.dialog ul { padding-inline-start: 20px; margin: 0 0 16px; }
.dialog form, .dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.dialog.win { text-align: center; }
.dialog.win .dialog-actions { justify-content: center; }
.win-emoji { font-size: 48px; }
.share-url {
  width: 100%; direction: ltr; font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--cell); color: var(--ink);
}
.stats-table { width: 100%; border-collapse: collapse; }
.stats-table th, .stats-table td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: start; }
.stats-table td { font-variant-numeric: tabular-nums; }

@media (max-width: 420px) {
  .title { font-size: 1.2rem; }
  .toolbar button { font-size: .9rem; padding: 10px 4px; }
}
