:root {
  --bg: #f5f4ef;
  --surface: rgba(255, 255, 255, 0.86);
  --surface-solid: #ffffff;
  --text: #1c1c1f;
  --muted: #6c6c73;
  --faint: rgba(28, 28, 31, 0.12);
  --border: rgba(28, 28, 31, 0.09);
  --ink: #1c1c1f;
  --ink-muted: rgba(28, 28, 31, 0.28);
  --accent: #3555e0;
  --accent-ink: #ffffff;
  --good: #0f9d72;
  --mid: #e2a100;
  --bad: #e0364e;
  --good-text: #0b8461;
  --mid-text: #9a6500;
  --bad-text: #cc2840;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 12px 40px rgba(20, 20, 40, 0.12);
  --radius: 20px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121316;
    --surface: rgba(30, 31, 36, 0.86);
    --surface-solid: #1e1f24;
    --text: #ececf0;
    --muted: #9a9aa3;
    --faint: rgba(236, 236, 240, 0.12);
    --border: rgba(236, 236, 240, 0.1);
    --ink: #f0f0f3;
    --ink-muted: rgba(240, 240, 243, 0.28);
    --accent: #7d93ff;
    --accent-ink: #0d0f1a;
    --good: #2fc98f;
    --mid: #f2b52a;
    --bad: #ff5d72;
    --good-text: #2fc98f;
    --mid-text: #f2b52a;
    --bad-text: #ff6b7e;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 16px 48px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  /* No text selection, magnifier loupe or long-press callout while drawing. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.app {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

/* ---------- Top bar ---------- */

.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 14px calc(16px + env(safe-area-inset-left));
  pointer-events: none;
}

.actions {
  pointer-events: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 28px;
  height: 28px;
  flex: none;
  color: var(--text);
}

.brand-name {
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
}

.chip strong {
  color: var(--text);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.chip-pencil {
  cursor: pointer;
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
}

.chip-pencil[aria-pressed="false"] {
  color: var(--muted);
  border-color: var(--border);
}

.chip-pencil svg {
  width: 16px;
  height: 16px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  font-size: 15px;
  font-weight: 650;
  cursor: pointer;
}

.icon-btn:hover,
.chip-pencil:hover {
  border-color: var(--faint);
  background: var(--surface-solid);
}

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

/* ---------- Idle hint ---------- */

.hint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 24px;
  text-align: center;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.hint.is-hidden {
  opacity: 0;
}

.hint-title {
  margin: 0;
  font-size: clamp(28px, 5vw, 44px);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--faint);
  color: color-mix(in srgb, var(--text) 22%, transparent);
}

.hint-sub {
  margin: 0;
  font-size: 15px;
  color: var(--muted);
}

/* ---------- Toast ---------- */

.toast {
  position: absolute;
  left: 50%;
  top: calc(70px + env(safe-area-inset-top));
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
  font-weight: 550;
  text-align: center;
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 0;
  transform: translateX(-50%);
  translate: 0 -8px;
  transition: opacity 0.2s ease, translate 0.2s ease;
}

.toast.is-visible {
  opacity: 1;
  translate: 0 0;
}

/* ---------- Result card ---------- */

.card {
  position: absolute;
  width: min(460px, calc(100% - 32px));
  padding: 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  -webkit-backdrop-filter: blur(18px) saturate(1.3);
  backdrop-filter: blur(18px) saturate(1.3);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  translate: 0 14px;
  transition: opacity 0.25s ease, translate 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.card.is-visible {
  opacity: 1;
  pointer-events: auto;
  translate: 0 0;
}

.card[data-place="bottom"] {
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
}

.card[data-place="top"] {
  left: 50%;
  top: calc(64px + env(safe-area-inset-top));
  transform: translateX(-50%);
}

.card[data-place="right"],
.card[data-place="left"] {
  top: 50%;
  width: 340px;
  transform: translateY(-50%);
}

.card[data-place="right"] {
  right: calc(20px + env(safe-area-inset-right));
}

.card[data-place="left"] {
  left: calc(20px + env(safe-area-inset-left));
}

.card-head {
  display: flex;
  align-items: center;
  gap: 16px;
}

.score {
  font-size: 56px;
  line-height: 1;
  font-weight: 750;
  letter-spacing: -0.035em;
  font-variant-numeric: tabular-nums;
  color: var(--score-color, var(--text));
  white-space: nowrap;
}

.score-unit {
  font-size: 0.5em;
  font-weight: 650;
  margin-left: 2px;
  letter-spacing: 0;
}

.verdict {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.grade {
  font-size: 19px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.method {
  font-size: 13px;
  color: var(--muted);
}

.new-best {
  align-self: flex-start;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.tip {
  margin: 14px 0 0;
  padding: 9px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  font-size: 14px;
  line-height: 1.4;
}

.tip[hidden] {
  display: none;
}

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

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1px;
  margin: 18px 0 14px;
  border-radius: 14px;
  overflow: hidden;
  background: var(--border);
  border: 1px solid var(--border);
}

.card[data-place="left"] .stats,
.card[data-place="right"] .stats {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.stat {
  padding: 10px 12px;
  background: var(--surface-solid);
  min-width: 0;
}

.stat dt {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat dt abbr {
  text-decoration: none;
}

.stat dd {
  margin: 3px 0 0;
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat dd small {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}

.miss-dot {
  width: 9px;
  height: 9px;
  flex: none;
  border-radius: 50%;
  border: 2px solid var(--text);
}

.legend {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  font-size: 12px;
  color: var(--muted);
}

.legend-bar {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--good), var(--mid) 45%, var(--bad));
}

.primary {
  display: block;
  width: 100%;
  height: 46px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 16px;
  font-weight: 650;
  cursor: pointer;
}

.primary:hover {
  filter: brightness(1.06);
}

.primary:active {
  filter: brightness(0.95);
}

/* ---------- Help dialog ---------- */

.help {
  width: min(560px, calc(100% - 32px));
  max-height: calc(100% - 48px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-solid);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-user-select: text;
  user-select: text;
}

.help::backdrop {
  background: rgba(10, 10, 20, 0.35);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.help-inner {
  padding: 24px;
}

.help h2 {
  margin: 0 0 14px;
  font-size: 21px;
  letter-spacing: -0.01em;
}

.help ol {
  margin: 0 0 14px;
  padding-left: 22px;
  line-height: 1.5;
  font-size: 15px;
}

.help li + li {
  margin-top: 8px;
}

.help-note {
  margin: 0 0 12px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--muted);
}

.help-note strong {
  color: var(--text);
}

.help .primary {
  margin-top: 18px;
}

kbd {
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--bg);
}

/* ---------- Small screens ---------- */

@media (max-width: 520px) {
  .brand-name {
    font-size: 16px;
  }

  .score {
    font-size: 46px;
  }

  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card {
    padding: 16px;
  }
}

@media (max-width: 380px) {
  .chip-best {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .card,
  .toast,
  .hint {
    transition: none;
  }
}
