:root {
  --bg: #ffffff;
  --fg: #1a1a1b;
  --muted: #787c7e;
  --line: #d3d6da;
  --ring: #c4c7cc;
  --ring-active: #1a1a1b;
  --accent: #6aaa64;
  --hint: #c9b458;
  --btn-bg: #d3d6da;
  --btn-fg: #1a1a1b;
  --modal-bg: #ffffff;
  --overlay: rgba(255, 255, 255, 0.5);
  --toast-bg: #1a1a1b;
  --toast-fg: #ffffff;
  --point-edge: rgba(0, 0, 0, 0.35);
}

html.dark {
  --bg: #121213;
  --fg: #f8f8f8;
  --muted: #818384;
  --line: #3a3a3c;
  --ring: #4a4a4d;
  --ring-active: #f8f8f8;
  --accent: #538d4e;
  --hint: #b59f3b;
  --btn-bg: #818384;
  --btn-fg: #f8f8f8;
  --modal-bg: #121213;
  --overlay: rgba(0, 0, 0, 0.5);
  --toast-bg: #f8f8f8;
  --toast-fg: #121213;
  --point-edge: rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }

html, body {
  /* no double-tap zoom on fast taps (pinch zoom still works) */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.header-wrap { flex: none; }

.app {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin: 0 auto;
}

/* ---------- header ---------- */
.header-wrap { border-bottom: 1px solid var(--line); }
header {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px;
}
.header-side { display: flex; align-items: center; gap: 2px; min-width: 120px; }
.header-side.right { justify-content: flex-end; }
header h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  font-size: 30px;
  letter-spacing: 0.01em;
}
.icon-btn {
  background: none;
  border: 0;
  color: var(--fg);
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  text-decoration: none;
}
.icon-btn:focus-visible, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn svg { width: 24px; height: 24px; }

/* ---------- info row ---------- */
.info {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 16px 0;
  font-size: 14px;
  color: var(--muted);
}
.info .puzzle-label { justify-self: start; }
.info .puzzle-label strong { color: var(--fg); }
.info .moves {
  font-variant-numeric: tabular-nums;
  font-size: 20px;
  font-weight: 700;
  color: var(--fg);
  justify-self: center;
}
.info .target { justify-self: end; font-variant-numeric: tabular-nums; }
.archive-banner {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  padding-top: 4px;
  min-height: 20px;
}
.archive-banner a { color: var(--fg); font-weight: 600; }

/* ---------- board ---------- */
.board-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 8px;
}
#board {
  touch-action: none;
  display: block;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
#board.dragging { cursor: grabbing; }

/* ---------- controls ---------- */
.controls {
  display: flex;
  gap: 8px;
  justify-content: center;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
}
.btn, .icon-btn, .link-btn { touch-action: manipulation; }
.btn {
  flex: 1;
  max-width: 180px;
  height: 52px;
  border: 0;
  border-radius: 6px;
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-size: 15px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn svg { width: 20px; height: 20px; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); color: #fff; }
.btn:active:not(:disabled) { filter: brightness(0.92); }

/* ---------- toast ---------- */
.toasts {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 12px 16px;
  border-radius: 4px;
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  transition: opacity 0.3s;
}
.toast.fade { opacity: 0; }

/* ---------- modals ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 40;
  display: none;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
}
.overlay.open { display: flex; }
.modal {
  position: relative;
  background: var(--modal-bg);
  color: var(--fg);
  border-radius: 8px;
  box-shadow: 0 4px 23px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 500px;
  max-height: 100%;
  overflow-y: auto;
  padding: 16px 20px 24px;
  animation: slideIn 0.2s ease-out;
}
html.dark .modal { border: 1px solid #1a1a1b; }
@keyframes slideIn { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal .close { position: absolute; top: 12px; right: 12px; }
.modal h2 {
  font-size: 16px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: center;
  margin: 8px 0 16px;
}
.modal h3 { font-size: 28px; font-family: Georgia, serif; margin: 12px 0 4px; }
.modal p, .modal li { font-size: 15px; line-height: 1.45; }
.modal ul { padding-left: 20px; }
.modal hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.help-goal { margin: 4px 0 8px; }
.tutorial { text-align: center; background: rgba(127, 127, 127, 0.08); border-radius: 8px; padding: 8px 8px 12px; }
#tut-board { display: block; margin: 0 auto; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
#tut-board.dragging { cursor: grabbing; }
.modal .tut-msg { margin: 4px 0 0; font-size: 14px; font-weight: 600; min-height: 20px; }
.tut-actions { display: flex; gap: 8px; justify-content: center; margin-top: 8px; }
.tut-actions:empty { display: none; }
.tut-actions .btn { height: 40px; max-width: 200px; font-size: 13px; }
.help-list { margin: 12px 0 0; }
.modal .help-list li { font-size: 14px; margin-bottom: 4px; }
.small { font-size: 13px; color: var(--muted); }

/* stats */
.stats-row { display: flex; justify-content: center; gap: 2px; margin-bottom: 4px; }
.stat { flex: 1; max-width: 80px; text-align: center; }
.stat .num { font-size: 24px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 11px; color: var(--muted); }
#stats h2 { margin: 4px 0 10px; font-size: 13px; }
#stats h2.sub { margin-top: 16px; color: var(--muted); font-size: 11px; }
#stats .modal { padding: 14px 18px 16px; }

/* result summary at the top of the stats sheet */
.result { text-align: center; padding: 6px 0 14px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
.result-title { font-size: 13px; font-weight: 600; color: var(--muted); }
.result-score { font-size: 44px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.result-score span { font-size: 16px; font-weight: 600; color: var(--muted); margin-left: 6px; }
#global { margin-bottom: 4px; }
#global h2.sub { margin-top: 0; }
.global-note { font-size: 13px; color: var(--muted); text-align: center; margin: 8px 0 12px; }
.global-dist .dist-count { min-width: 28px; font-weight: 600; color: var(--accent); }
.result-meta { font-size: 14px; }
.result-note { font-size: 12px; color: var(--muted); margin-top: 4px; }
.result-actions { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.result-actions .btn { height: 44px; max-width: 170px; font-size: 14px; }
.result-next { font-size: 13px; color: var(--muted); margin-top: 10px; }
.result-next span { font-variant-numeric: tabular-nums; color: var(--fg); font-weight: 600; }
/* label | bar | count: labels right-aligned, every bar starts at the same edge */
.dist {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr) max-content;
  column-gap: 8px;
  row-gap: 4px;
  align-items: center;
  margin: 0;
  font-size: 13px;
}
.dist-label { text-align: right; }
.dist-track { display: flex; justify-content: flex-start; min-width: 0; }
.dist-count { font-size: 13px; color: var(--muted); white-space: nowrap; min-width: 56px; }
.dist .bar {
  background: var(--muted);
  color: #fff;
  font-weight: 700;
  text-align: right;
  padding: 2px 8px;
  min-width: 28px;
  box-sizing: border-box;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.dist .bar.today { background: var(--accent); }

/* feedback */
.field { display: block; margin: 0 0 14px; font-size: 14px; font-weight: 600; }
.field textarea, .field input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px;
  font: inherit;
  font-weight: 400;
  font-size: 16px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  resize: vertical;
}
.field textarea:focus, .field input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.fb-status { min-height: 20px; font-size: 14px; text-align: center; margin: 10px 0 0; }
.fb-status.error { color: #e5484d; }
.link-btn {
  background: none; border: 0; padding: 0; font: inherit; color: var(--fg);
  text-decoration: underline; cursor: pointer;
}
.feedback-row { text-align: center; margin: 12px 0 0; font-size: 13px; }

/* archive */
.modal .archive-list { list-style: none; padding: 0; margin: 0; }
.archive-list li { border-bottom: 1px solid var(--line); }
.archive-list button {
  width: 100%;
  background: none;
  border: 0;
  color: var(--fg);
  font: inherit;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 4px;
  cursor: pointer;
  text-align: left;
}
.archive-list button:hover { background: rgba(127, 127, 127, 0.08); }
.archive-list .when { color: var(--muted); font-size: 13px; }
.archive-list .status { font-variant-numeric: tabular-nums; font-weight: 600; }
.archive-list .status.done { color: var(--accent); }
.archive-month { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 16px 0 4px; }

/* settings */
.setting {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}
.setting .desc { font-size: 12px; color: var(--muted); }
.switch { position: relative; width: 32px; height: 20px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute; inset: 0; border-radius: 999px; background: var(--muted); cursor: pointer; transition: background 0.2s;
}
.switch span::before {
  content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; border-radius: 50%; background: #fff; transition: transform 0.2s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(12px); }

@media (max-height: 600px) {
  header { height: 44px; }
  header h1 { font-size: 24px; }
  .btn { height: 44px; }
}
@media (prefers-reduced-motion: reduce) {
  .modal { animation: none; }
}

.free-levels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.free-levels .btn { height: 40px; font-size: 13px; text-decoration: none; max-width: none; }
.result-next a { color: var(--fg); font-weight: 600; }

.privacy { margin-top: 14px; }
.privacy-title { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.modal .privacy p { font-size: 12px; color: var(--muted); margin: 0 0 6px; line-height: 1.4; }

.controls .btn { font-size: 14px; gap: 6px; padding: 0 6px; }
.controls .btn svg { width: 18px; height: 18px; flex: none; }
#reset-btn.armed { background: #e5484d; color: #fff; }
