/* ── Reset & base ───────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0d1117;
  --surface:   #161b22;
  --border:    #30363d;
  --text:      #e6edf3;
  --muted:     #8b949e;
  --accent:    #58a6ff;
  --green:     #3fb950;
  --green-dim: #1a4a22;
  --red:       #f85149;
  --yellow:    #d29922;
  --radius:    8px;
  --font-mono: "Fira Code", "Cascadia Code", "Consolas", monospace;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  padding: 24px 16px 60px;
}

.container {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* ── Header ─────────────────────────────────────────────────── */
header { text-align: center; }
header h1 { font-size: 2rem; letter-spacing: -.5px; }
.subtitle { color: var(--muted); margin-top: 4px; font-size: .9rem; }

/* ── GitHub link ────────────────────────────────────────────── */
.github-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: .82rem;
  text-decoration: none;
  transition: color .15s, border-color .15s, background .15s;
}

.github-link:hover {
  color: var(--text);
  border-color: var(--text);
  background: var(--surface);
}

.github-icon {
  width: 16px;
  height: 16px;
  fill: currentColor;
  flex-shrink: 0;
}

/* ── Config panel ───────────────────────────────────────────── */
.config-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 24px;
}

.config-row { display: flex; flex-direction: column; gap: 4px; }
.config-row label { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }

.config-row:first-child { grid-column: 1 / -1; }

input[type="text"],
input[type="number"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: .95rem;
  padding: 6px 10px;
  width: 100%;
  transition: border-color .15s;
}
input[type="text"]:focus,
input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
}

.speed-row { display: flex; align-items: center; gap: 8px; }
.speed-label { font-size: .8rem; color: var(--muted); white-space: nowrap; }
input[type="range"] { flex: 1; accent-color: var(--accent); cursor: pointer; }

/* ── Stage ──────────────────────────────────────────────────── */
.stage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.string-labels {
  display: flex;
  justify-content: space-between;
  font-size: .75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
}

.string-display {
  font-family: var(--font-mono);
  font-size: clamp(1rem, 3vw, 1.6rem);
  letter-spacing: .08em;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  min-height: 2.4em;
}

.string-display.target { opacity: .5; }

.char {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1ch;
  transition: color .2s, text-shadow .2s;
  position: relative;
}

.char.space { width: .6ch; }

/* matched character */
.char.matched {
  color: var(--green);
  text-shadow: 0 0 8px rgba(63, 185, 80, .5);
}

/* just-changed character */
.char.flash {
  animation: flash-anim .35s ease-out forwards;
}

@keyframes flash-anim {
  0%   { color: #fff; text-shadow: 0 0 16px #fff; transform: scale(1.3); }
  100% { color: var(--green); text-shadow: 0 0 8px rgba(63,185,80,.5); transform: scale(1); }
}

/* unmatched */
.char.unmatched { color: var(--red); }

/* ── Stats bar ──────────────────────────────────────────────── */
.stats-bar {
  display: flex;
  gap: 16px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 20px;
  flex-wrap: wrap;
}

.stat { display: flex; align-items: center; gap: 8px; }
.stat-label { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-family: var(--font-mono); font-size: .95rem; min-width: 4ch; }

.progress-stat { flex: 1; }
.progress-wrap {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  height: 10px;
  overflow: hidden;
  min-width: 80px;
}
.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--green);
  border-radius: 999px;
  transition: width .3s ease, background-color .3s;
}
.progress-bar.complete { background: var(--accent); }

/* ── Controls ───────────────────────────────────────────────── */
.controls { display: flex; gap: 10px; justify-content: center; }

.btn {
  padding: 8px 24px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: .9rem;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--border); }
.btn.btn-primary { background: var(--accent); border-color: var(--accent); color: #0d1117; font-weight: 600; }
.btn.btn-primary:hover { background: #79b8ff; border-color: #79b8ff; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

/* ── Population view ────────────────────────────────────────── */
.population-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
}
.population-view h2,
.history-panel h2,
.chart-section h2 {
  font-size: .85rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  margin-bottom: 12px;
}

.population-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 200px;
  overflow-y: auto;
}

.pop-row {
  font-family: var(--font-mono);
  font-size: .75rem;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
  border-bottom: 1px solid transparent;
}

.pop-row.is-winner {
  border-bottom-color: var(--green-dim);
}

.pop-row .pop-score {
  color: var(--muted);
  font-size: .7rem;
  min-width: 5ch;
  text-align: right;
}

.pop-char { color: var(--muted); }
.pop-char.matched { color: var(--green); }
.pop-char.winner-char { color: var(--accent); }

/* ── History ─────────────────────────────────────────────────── */
.history-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
}

.history-log {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 220px;
  overflow-y: auto;
}

.history-entry {
  display: flex;
  gap: 12px;
  align-items: baseline;
  font-family: var(--font-mono);
  font-size: .78rem;
  padding: 3px 0;
  border-bottom: 1px solid var(--border);
  animation: fadein .25s ease;
}

@keyframes fadein {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.hist-gen { color: var(--muted); min-width: 7ch; text-align: right; }
.hist-str { flex: 1; }
.hist-score { color: var(--yellow); min-width: 5ch; text-align: right; }

.hist-char { color: var(--muted); }
.hist-char.matched { color: var(--green); }

/* ── Chart ──────────────────────────────────────────────────── */
.chart-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
}

canvas { width: 100%; display: block; border-radius: 4px; }

/* ── Footer ─────────────────────────────────────────────────── */
footer {
  text-align: center;
  padding: 24px 0 8px;
  font-size: .78rem;
  color: var(--muted);
}

footer a {
  color: var(--muted);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
  transition: color .15s, border-color .15s;
}

footer a:hover {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ── Scrollbar styling ───────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 999px; }
