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

:root {
  --bg:        #0e0f13;
  --bg2:       #16181f;
  --bg3:       #1d2029;
  --border:    rgba(255,255,255,0.08);
  --border2:   rgba(255,255,255,0.14);
  --text:      #e8e6df;
  --text2:     #9b9890;
  --text3:     #5c5a56;
  --accent:    #7f77dd;
  --accent2:   #534ab7;
  --teal:      #1d9e75;
  --teal-bg:   rgba(29,158,117,0.15);
  --amber:     #ef9f27;
  --amber-bg:  rgba(239,159,39,0.15);
  --purple-bg: rgba(127,119,221,0.15);
  --blue:      #378add;
  --blue-bg:   rgba(55,138,221,0.12);
  --mono:      'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  --sans:      'IBM Plex Sans', 'Inter', system-ui, sans-serif;
  --radius:    8px;
  --radius-lg: 14px;
}

html { font-size: 15px; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

/* ── Google Fonts ── */
@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500&family=IBM+Plex+Sans:wght@400;500&display=swap');

/* ── Header ── */
header {
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  padding: 2.5rem 2rem 2rem;
}
.header-inner { max-width: 900px; margin: 0 auto; }
.algo-tag {
  display: inline-block;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  background: var(--purple-bg);
  border: 1px solid rgba(127,119,221,0.3);
  border-radius: 4px;
  padding: 2px 8px;
  margin-bottom: 10px;
}
h1 {
  font-size: clamp(1.4rem, 3vw, 2rem);
  font-weight: 500;
  letter-spacing: -0.02em;
  color: var(--text);
  margin-bottom: 6px;
}
.subtitle { font-size: 0.9rem; color: var(--text2); }

/* ── Main layout ── */
main { max-width: 900px; margin: 0 auto; padding: 2rem; }

/* ── Input section ── */
.input-section {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
}
.input-row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.input-group { display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 140px; }
.input-group label {
  font-size: 12px;
  color: var(--text2);
  font-weight: 500;
}
.input-group label code {
  font-family: var(--mono);
  background: var(--purple-bg);
  color: var(--accent);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 11px;
}
.input-group input {
  font-family: var(--mono);
  font-size: 14px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text);
  padding: 8px 12px;
  outline: none;
  letter-spacing: 0.05em;
  transition: border-color 0.2s;
}
.input-group input:focus { border-color: var(--accent); }
.input-hint { font-size: 11px; color: var(--text3); margin-top: 10px; }

/* ── Buttons ── */
.btn-primary {
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
  padding: 9px 18px;
  background: var(--accent2);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
  align-self: flex-end;
}
.btn-primary:hover { background: var(--accent); }
.btn-primary:active { transform: scale(0.97); }

.btn-ctrl {
  font-family: var(--sans);
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 13px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.btn-ctrl:hover { background: var(--bg2); border-color: var(--border2); }
.btn-ctrl:active { transform: scale(0.97); }
.btn-play-main {
  background: var(--accent2);
  color: #fff;
  border-color: transparent;
  font-weight: 500;
  padding: 7px 18px;
}
.btn-play-main:hover { background: var(--accent); border-color: transparent; }
.btn-ghost { color: var(--text2); }

/* ── Controls bar ── */
.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.speed-group {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-left: auto;
  font-size: 12px;
  color: var(--text2);
}
.speed-group input[type=range] {
  width: 80px;
  accent-color: var(--accent);
}
#speed-val { font-family: var(--mono); font-size: 12px; color: var(--text2); min-width: 12px; }
.step-count { font-size: 11px; color: var(--text3); font-family: var(--mono); }

/* ── Progress bar ── */
.progress-bar-wrap {
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  margin-bottom: 14px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 0.2s;
}

/* ── Info bar ── */
.info-bar {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  min-height: 44px;
  display: flex;
  align-items: center;
  margin-bottom: 14px;
  line-height: 1.6;
  color: var(--text);
  transition: border-color 0.2s;
}
.info-bar code {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--bg3);
  padding: 1px 5px;
  border-radius: 3px;
}
.info-placeholder { color: var(--text2); }
.info-bar.phase-1 { border-color: rgba(55,138,221,0.4); }
.info-bar.phase-2 { border-color: rgba(29,158,117,0.4); }
.info-bar.phase-3 { border-color: rgba(239,159,39,0.4); }

/* ── Phase legend ── */
.phase-legend { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.phase-badge {
  font-size: 11px;
  font-weight: 500;
  padding: 3px 10px;
  border-radius: 20px;
  opacity: 0.45;
  transition: opacity 0.25s;
}
.phase-badge.active { opacity: 1; }
.p1 { background: var(--blue-bg); color: var(--blue); border: 1px solid rgba(55,138,221,0.25); }
.p2 { background: var(--teal-bg); color: var(--teal); border: 1px solid rgba(29,158,117,0.25); }
.p3 { background: var(--amber-bg); color: var(--amber); border: 1px solid rgba(239,159,39,0.25); }

/* ── Table ── */
.table-outer {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  margin-bottom: 14px;
}
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; font-family: var(--mono); font-size: 13px; }
td, th {
  width: 38px;
  height: 38px;
  text-align: center;
  vertical-align: middle;
  border: 1px solid var(--border);
  transition: background 0.18s, color 0.18s, outline 0.18s;
}
th {
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 500;
  color: var(--text2);
  border: none;
  background: transparent;
}
th.char-header { color: var(--text); font-family: var(--mono); font-size: 13px; }

/* Cell states */
.cell-empty  { color: var(--text3); background: transparent; }
.cell-done   { color: var(--text2); background: var(--bg3); }
.cell-current{
  background: var(--purple-bg);
  color: #afa9ec;
  font-weight: 500;
}
.cell-match  {
  background: var(--teal-bg);
  color: #5dcaa5;
  font-weight: 500;
}
.cell-back   {
  background: var(--amber-bg);
  color: var(--amber);
  font-weight: 500;
  outline: 2px solid rgba(239,159,39,0.5);
  outline-offset: -2px;
}

/* ── Cell legend ── */
.cell-legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text2);
  margin-bottom: 16px;
}
.legend-item { display: flex; align-items: center; gap: 6px; }
.ldot { display: inline-block; width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.dot-current { background: var(--purple-bg); outline: 1px solid #afa9ec; }
.dot-match   { background: var(--teal-bg);   outline: 1px solid #5dcaa5; }
.dot-done    { background: var(--bg3);        outline: 1px solid var(--border2); }
.dot-back    { background: var(--amber-bg);   outline: 2px solid rgba(239,159,39,0.5); }

/* ── Result ── */
.result-area {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  font-size: 14px;
  color: var(--text2);
  min-height: 50px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
  transition: border-color 0.3s;
}
.result-area.has-result { border-color: rgba(29,158,117,0.4); }
.result-label { color: var(--text2); }
.result-word {
  font-family: var(--mono);
  font-size: 20px;
  font-weight: 500;
  color: var(--teal);
  letter-spacing: 0.08em;
}
.result-len {
  font-size: 12px;
  background: var(--teal-bg);
  color: var(--teal);
  border: 1px solid rgba(29,158,117,0.25);
  border-radius: 20px;
  padding: 2px 10px;
}

/* ── Code section ── */
.code-section {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: 2rem;
}
.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.code-title { font-size: 12px; font-weight: 500; color: var(--text2); }
.btn-copy {
  font-family: var(--sans);
  font-size: 11px;
  padding: 4px 10px;
  background: var(--bg3);
  color: var(--text2);
  border: 1px solid var(--border2);
  border-radius: 5px;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}
.btn-copy:hover { color: var(--text); background: var(--bg); }
.code-block {
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.75;
  color: var(--text2);
  padding: 1.25rem 1.5rem;
  overflow-x: auto;
  margin: 0;
  white-space: pre;
}
.hl-p1 { color: #85b7eb; }
.hl-p2 { color: #5dcaa5; }
.hl-p3 { color: var(--amber); }

/* ── Footer ── */
footer {
  border-top: 1px solid var(--border);
  text-align: center;
  padding: 1.25rem 2rem;
  font-size: 12px;
  color: var(--text3);
  font-family: var(--mono);
}

/* ── Responsive ── */
@media (max-width: 600px) {
  main { padding: 1rem; }
  header { padding: 1.5rem 1rem 1.25rem; }
  td, th { width: 30px; height: 30px; font-size: 11px; }
  .controls { gap: 6px; }
  .speed-group { margin-left: 0; width: 100%; }
}
