:root {
  --bg: #faf7f2;
  --ink: #1f2024;
  --muted: #6b6f78;
  --accent: #c2410c;
  --line: #2b2d33;
  --grid: #c9c5bd;
  --ok: #137a3b;
  --err: #b91c1c;
  --card-border: #e6e1d8;
  --card-bg: #ffffff;
  --hover: #f3efe7;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ------- header ------- */

header {
  border-bottom: 1px solid var(--card-border);
  background: var(--bg);
}

.header-inner {
  display: grid;
  grid-template-columns: 36px 1fr 36px;
  align-items: center;
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px;
  gap: 8px;
}

.header-inner h1 {
  margin: 0;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--card-border);
  background: var(--card-bg);
  color: var(--ink);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
}

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

.header-spacer { display: block; }

/* ------- main ------- */

main {
  max-width: 520px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
}

.modes {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.mode {
  flex: 1;
  padding: 10px 14px;
  background: var(--card-bg);
  border: 1px solid #d8d3c9;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  color: var(--muted);
}

.mode.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--muted);
  min-height: 20px;
  padding: 0 4px;
}

.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--ink);
}

.status-line {
  min-height: 18px;
  font-size: 13px;
  text-align: center;
  font-weight: 500;
}

.status-line.ok  { color: var(--ok); }
.status-line.err { color: var(--err); }

#board-wrap {
  display: flex;
  justify-content: center;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  padding: 16px;
}

#board {
  width: 100%;
  height: auto;
  max-width: 440px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.controls {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.controls button {
  flex: 1;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid #d8d3c9;
  background: var(--card-bg);
  color: var(--ink);
  font-size: 14px;
  cursor: pointer;
}

.controls button:hover { background: var(--hover); }

#btn-check {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

/* ------- SVG ------- */

.grid-line  { stroke: var(--grid); stroke-width: 1; stroke-dasharray: 3 3; }
.path-line  { stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; fill: none; }
.firefly-circle { fill: var(--card-bg); stroke: var(--ink); stroke-width: 1.6; }
.firefly-text {
  font: bold 14px -apple-system, sans-serif;
  text-anchor: middle;
  dominant-baseline: central;
  fill: var(--ink);
}
.firefly-tail { fill: var(--ink); }

.solved .path-line { stroke: var(--ok); }

/* ------- modals ------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(31, 32, 36, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
  animation: modal-fade 140ms ease-out;
}

.modal.show {
  display: flex;
}

@keyframes modal-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal-card {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 24px 22px 20px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.18);
  animation: modal-pop 180ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes modal-pop {
  from { transform: scale(0.94) translateY(6px); opacity: 0; }
  to   { transform: scale(1) translateY(0);     opacity: 1; }
}

.modal-card h2 {
  margin: 0 0 8px;
  font-size: 22px;
  letter-spacing: 0.01em;
}

.modal-card h3 {
  margin: 18px 0 6px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.modal-lead {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 14px;
}

.modal-card ul.rules {
  margin: 6px 0 0 18px;
  padding: 0;
  font-size: 14px;
  color: var(--ink);
}

.modal-card ul.rules li { margin: 4px 0; }
.modal-card em      { font-style: normal; font-weight: 600; }
.modal-card strong  { font-weight: 600; }

.modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  border: 0;
  background: transparent;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

.modal-close:hover { background: var(--hover); color: var(--ink); }

/* ------- win modal ------- */

.win-card {
  text-align: center;
  padding: 28px 22px 20px;
}

.win-emoji {
  font-size: 48px;
  line-height: 1;
  margin-bottom: 6px;
}

.win-title {
  font-size: 30px;
  margin: 0 0 4px;
  letter-spacing: 0.01em;
}

.win-sub {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 14px;
}

.win-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 18px;
}

.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-bottom: 4px;
}

.stat-value {
  font-size: 26px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.win-actions {
  display: flex;
  gap: 8px;
}

.win-actions button {
  flex: 1;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid #d8d3c9;
  background: var(--card-bg);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.win-actions button:hover { background: var(--hover); }

.win-actions .primary {
  background: var(--ok);
  border-color: var(--ok);
  color: #fff;
}

.win-actions .primary:hover { background: #0e6630; border-color: #0e6630; }

.share-toast {
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--ok);
  min-height: 18px;
  font-weight: 600;
}

/* ------- footer ------- */

footer {
  border-top: 1px solid var(--card-border);
  margin-top: 12px;
  padding: 14px 16px 22px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

footer p { margin: 0; }

footer a {
  color: var(--muted);
  text-decoration: underline;
  text-decoration-color: #d8d3c9;
  text-underline-offset: 2px;
}

footer a:hover { color: var(--ink); }

@media (max-width: 420px) {
  .header-inner h1 { font-size: 20px; }
  .modal-card { padding: 22px 18px 18px; }
  .win-title { font-size: 26px; }
  .stat-value { font-size: 22px; }
}
