:root {
  --paper: #EEF1F5;
  --surface: #FFFFFF;
  --ink: #182230;
  --ink-2: #4E5B6B;
  --ink-3: #8A95A3;
  --line: #D6DCE4;
  --grid: #E6EAF0;
  --blue: #2457A6;
  --blue-soft: #DCE6F6;
  --orange: #E4642A;
  --orange-soft: #FBE7DC;
  --teal: #178A7B;
  --tunnel: #10161F;
  --radius: 14px;
  --font: 'Instrument Sans', -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--paper);
  font-variant-numeric: tabular-nums;
  padding-inline: max(16px, env(safe-area-inset-left)) max(16px, env(safe-area-inset-right));
  padding-block: 0 48px;
}
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 1.25rem; }
p { margin: 0; }
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

/* header */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding-block: 18px 14px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 20px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.35rem; letter-spacing: -0.02em; }
.brand-mark { width: 44px; height: 18px; fill: var(--blue); }
.who { color: var(--ink-2); font-size: 0.95rem; }
.who strong { color: var(--ink); font-weight: 600; }
.link { background: none; border: 0; padding: 0 2px; color: var(--blue); text-decoration: underline; text-underline-offset: 2px; }

/* layout */
.layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 20px; max-width: 1400px; margin: 0 auto; }
.panel { min-width: 0; }
@media (min-width: 1000px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
  #editor-panel { grid-column: 1; grid-row: 1; }
  #viewer-panel { grid-column: 2; grid-row: 1; }
  #results-panel { grid-column: 1; grid-row: 2; }
  #leaderboard-panel { grid-column: 2; grid-row: 2; }
}

/* panels */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.panel-head { margin-bottom: 14px; }
.hint { color: var(--ink-2); font-size: 0.95rem; margin-top: 4px; max-width: 60ch; }

/* canvases */
.canvas-wrap { position: relative; border-radius: 10px; overflow: hidden; }
.editor-wrap { aspect-ratio: 3 / 2; background: var(--surface); border: 1px solid var(--line); }
.viz-wrap { aspect-ratio: 11 / 7; background: var(--tunnel); }
.canvas-wrap canvas {
  display: block; width: 100%; height: 100%;
  touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
#editor { cursor: crosshair; }

.viz-overlay {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 10px 12px 12px; color: #E9EEF5;
  background: linear-gradient(to top, rgba(16,22,31,0.92), rgba(16,22,31,0));
  pointer-events: none;
}
.viz-stats { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 0.9rem; color: #B6C2D1; }
.viz-stats b { color: #FFFFFF; font-weight: 600; margin-left: 4px; }
.viz-progress { height: 4px; background: rgba(255,255,255,0.18); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.viz-progress-bar { height: 100%; width: 0; background: var(--orange); transition: width 0.3s linear; }
.viz-legend { position: absolute; top: 10px; right: 12px; font-size: 0.8rem; color: #B6C2D1; display: flex; align-items: center; gap: 6px; pointer-events: none; }
.viz-legend .swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.viz-legend .low { background: #3B82F6; }
.viz-legend .high { background: #F97316; margin-left: 8px; }

/* toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.tool {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 14px; min-height: 44px; font-weight: 500; color: var(--ink);
}
.tool:hover { border-color: var(--ink-3); }
.tool:disabled { opacity: 0.45; cursor: default; }
.tool.accent { border-color: var(--blue); color: var(--blue); background: var(--blue-soft); }
.tool[hidden] { display: none; }
.status { min-height: 1.4em; margin-top: 10px; font-size: 0.95rem; color: var(--ink-2); }
.status.error { color: #B3261E; }
.status.good { color: var(--teal); }

.readout { color: var(--ink-3); font-size: 0.85rem; margin-top: 10px; }
.readout span { color: var(--ink); font-weight: 600; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.primary {
  background: var(--orange); color: #fff; border: 0; border-radius: 10px;
  padding: 12px 22px; min-height: 48px; font-weight: 600; font-size: 1.05rem; flex: 1 1 auto;
}
.primary:hover { filter: brightness(0.95); }
.primary:disabled { opacity: 0.5; cursor: default; }

/* results */
.hero-stat { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; margin: 4px 0 16px; }
.hero-value { font-size: 3.6rem; font-weight: 700; letter-spacing: -0.03em; line-height: 1; color: var(--orange); }
.hero-label { color: var(--ink-2); max-width: 26ch; }
.hero-label b { color: var(--ink); font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px 16px; margin: 0 0 16px; padding: 14px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.stats div { display: flex; flex-direction: column; }
.stats dt { color: var(--ink-3); font-size: 0.85rem; }
.stats dd { margin: 0; font-weight: 600; font-size: 1.15rem; }
.stats dd small { font-weight: 400; color: var(--ink-3); font-size: 0.85rem; }
.chart-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 12px; }
.seg { background: var(--surface); border: 0; padding: 8px 14px; min-height: 40px; color: var(--ink-2); }
.seg.active { background: var(--ink); color: #fff; }
.charts { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 640px) { .charts { grid-template-columns: 1fr 1fr; } }
.chart { margin: 0; }
.chart figcaption { font-size: 0.9rem; color: var(--ink-2); margin-bottom: 4px; }
.chart canvas { display: block; width: 100%; height: 220px; touch-action: pan-y; }

/* leaderboard */
.lb-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.lb { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.lb th { text-align: left; color: var(--ink-3); font-weight: 500; font-size: 0.85rem; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.lb td { padding: 8px; border-bottom: 1px solid var(--grid); vertical-align: middle; white-space: nowrap; }
.lb .num, .lb td.num { text-align: right; }
.lb tr.me td { background: var(--orange-soft); }
.lb tr.first td:first-child { color: var(--orange); font-weight: 700; }
.lb .empty { color: var(--ink-3); text-align: center; padding: 18px; white-space: normal; }

/* dialog */
dialog {
  border: 0; border-radius: 18px; padding: 28px; max-width: min(92vw, 420px); width: 100%;
  background: var(--surface); color: var(--ink); box-shadow: 0 30px 80px rgba(16,22,31,0.25);
}
dialog::backdrop { background: rgba(16,22,31,0.55); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
.dialog-mark { width: 64px; height: 26px; fill: var(--blue); }
dialog h1 { font-size: 1.8rem; letter-spacing: -0.02em; }
dialog p { color: var(--ink-2); }
dialog label { font-weight: 500; margin-top: 6px; }
dialog input[type=text] {
  font: inherit; font-size: 1.1rem; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; min-height: 48px;
}
dialog .primary { margin-top: 6px; }

@media (prefers-reduced-motion: reduce) {
  .viz-progress-bar { transition: none; }
}
