/* ── Reset & Base ── */
*, *::before, *::after { margin:0; padding:0; box-sizing:border-box; }
:root {
  --bg:       #0b0e17;
  --surface:  #141825;
  --surface2: #1c2133;
  --border:   #2a3050;
  --text:     #e2e6f0;
  --muted:    #7a82a0;
  --accent:   #6c5ce7;
  --accent2:  #a29bfe;
  --dict:     #3b82f6;          /* blue – already seen / in dictionary */
  --dict-bg:  rgba(59,130,246,.15);
  --new:      #f97316;          /* orange – new phrase being formed */
  --new-bg:   rgba(249,115,22,.15);
  --danger:   #ef4444;
  --radius:   12px;
  --font:     'Inter', system-ui, sans-serif;
  --mono:     'JetBrains Mono', 'Fira Code', monospace;
  --header-h: 56px;
}
html { font-size:16px; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.45;
  min-height: 100dvh;
  padding-top: calc(var(--header-h) + 12px);
  -webkit-tap-highlight-color: transparent;
}

/* ── Header ── */
#top-header {
  position: fixed; top:0; left:0; right:0; z-index:100;
  height: var(--header-h);
  display: flex; align-items: center; gap:12px;
  padding: 0 16px;
  background: rgba(11,14,23,.88);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
#btn-back {
  display:inline-flex; align-items:center; gap:4px;
  background:none; border:none; color:var(--accent2);
  font: 600 .9rem var(--font); cursor:pointer;
  padding:6px 10px; border-radius:8px;
  transition: background .2s;
}
#btn-back:hover { background:rgba(108,92,231,.15); }
#header-title {
  flex:1; text-align:center;
  font-size:1.05rem; font-weight:700;
  letter-spacing:.02em;
  background: linear-gradient(135deg, var(--accent2), var(--dict));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.header-spacer { width:68px; }

/* ── Main ── */
#app { max-width:960px; margin:0 auto; padding:0 14px 40px; }

/* ── Stats Bar ── */
#stats-bar {
  display:flex; gap:10px; margin-bottom:18px;
}
.stat-card {
  flex:1; text-align:center;
  background: var(--surface);
  border:1px solid var(--border);
  border-radius: var(--radius);
  padding:12px 8px;
  transition: transform .2s, box-shadow .2s;
}
.stat-card:hover { transform:translateY(-2px); box-shadow:0 4px 18px rgba(0,0,0,.35); }
.stat-card.accent { border-color: var(--accent); }
.stat-label { display:block; font-size:.72rem; text-transform:uppercase; letter-spacing:.08em; color:var(--muted); margin-bottom:4px; }
.stat-value { font:700 1.35rem var(--mono); }
.stat-value small { font-size:.7rem; font-weight:500; color:var(--muted); }

/* ── Input ── */
#input-section { margin-bottom:16px; }
.input-row { display:flex; align-items:center; gap:10px; flex-wrap:wrap; margin-bottom:8px; }
.input-label { font-weight:600; font-size:.88rem; white-space:nowrap; }
.preset-chips { display:flex; gap:6px; flex-wrap:wrap; }
.chip {
  font: 600 .72rem var(--mono);
  padding:5px 10px; border-radius:20px;
  background: var(--surface2); border:1px solid var(--border);
  color:var(--accent2); cursor:pointer;
  transition: background .2s, border-color .2s;
}
.chip:hover, .chip.active { background:var(--accent); border-color:var(--accent); color:#fff; }
#input-text {
  width:100%; font: 500 1rem var(--mono);
  padding:12px 14px; border-radius:var(--radius);
  background:var(--surface); border:1px solid var(--border);
  color:var(--text); outline:none;
  transition: border-color .2s;
}
#input-text:focus { border-color:var(--accent); }

/* ── Controls ── */
#controls-section {
  display:flex; gap:10px; margin-bottom:18px; justify-content:center;
}
.ctrl-btn {
  display:inline-flex; align-items:center; gap:6px;
  font: 600 .88rem var(--font);
  padding:10px 18px; border-radius:10px;
  border:1px solid var(--border); background:var(--surface);
  color:var(--text); cursor:pointer;
  transition: background .2s, transform .15s, border-color .2s;
}
.ctrl-btn:hover { background:var(--surface2); transform:translateY(-1px); }
.ctrl-btn.primary { background:var(--accent); border-color:var(--accent); color:#fff; }
.ctrl-btn.primary:hover { background:#5b4bd6; }

/* ── Legend ── */
#legend-section { margin-bottom:18px; }
#legend { display:flex; gap:18px; justify-content:center; flex-wrap:wrap; }
.legend-item { display:flex; align-items:center; gap:5px; font-size:.76rem; font-weight:600; color:var(--muted); }
.legend-swatch { width:14px; height:14px; border-radius:4px; }
.dict-legend .legend-swatch { background: var(--dict-bg); border:2px solid var(--dict); }
.new-legend  .legend-swatch { background: var(--new-bg);  border:2px solid var(--new); }

/* ── Main Content Two-Col Layout ── */
#main-content {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 18px;
  align-items: start;
}

/* ── Visualization ── */
#viz-section { margin-bottom:18px; }
#viz-section h2, #output-section h2, #dict-section h2 {
  font-size:.82rem; font-weight:700; margin-bottom:8px;
  color:var(--muted); text-transform:uppercase; letter-spacing:.06em;
}
#output-section h2 small { font-size:.72rem; font-weight:500; }

#viz-container {
  background: var(--surface);
  border:1px solid var(--border);
  border-radius: var(--radius);
  padding:16px 14px;
  min-height:56px;
}
#char-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}
.char-cell {
  display:inline-flex; align-items:center; justify-content:center;
  width:32px; height:40px;
  font: 700 1.05rem var(--mono);
  border-radius:6px; position:relative;
  transition: background .3s, color .3s, transform .3s, box-shadow .3s;
  flex-shrink:0;
  background: var(--surface2);
  color: var(--muted);
}
.char-cell.dict-match {
  background:var(--dict-bg);
  color:var(--dict);
  box-shadow: 0 0 0 2px var(--dict);
}
.char-cell.new-char {
  background:var(--new-bg);
  color:var(--new);
  box-shadow: 0 0 0 2px var(--new);
  transform: scale(1.08);
}
.char-cell.processed {
  opacity:.35;
}

#step-info {
  margin-top:10px; text-align:center;
  font: 500 .82rem var(--mono);
  color: var(--muted);
  min-height: 20px;
}

/* ── Output Tokens ── */
#output-section { margin-bottom:18px; }
#token-list { display:flex; flex-wrap:wrap; gap:8px; }
.token-card {
  background:var(--surface); border:1px solid var(--border);
  border-radius:10px; padding:8px 12px;
  font: 600 .8rem var(--mono);
  transition: opacity .3s, transform .3s;
  animation: tokenIn .35s ease-out;
}
.token-card .tok-index { color:var(--dict); }
.token-card .tok-char  { color:var(--new); }
@keyframes tokenIn {
  from { opacity:0; transform:translateY(8px) scale(.92); }
  to   { opacity:1; transform:translateY(0) scale(1); }
}

/* ── Dictionary Table ── */
#dict-section { position: sticky; top: calc(var(--header-h) + 12px); }
#dict-table-wrap {
  max-height: 420px;
  overflow-y: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  scrollbar-width: thin;
  scrollbar-color: var(--border) var(--surface);
}
#dict-table-wrap::-webkit-scrollbar { width:6px; }
#dict-table-wrap::-webkit-scrollbar-track { background: var(--surface); }
#dict-table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius:3px; }

#dict-table {
  width:100%;
  border-collapse: collapse;
  font: 500 .82rem var(--mono);
}
#dict-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
}
#dict-table th {
  background: var(--surface2);
  color: var(--muted);
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
#dict-table td {
  padding: 8px 14px;
  border-bottom: 1px solid rgba(42,48,80,.4);
  color: var(--text);
  transition: background .3s;
}
#dict-table tr.highlight-row {
  background: var(--dict-bg) !important;
}
#dict-table tr.highlight-row td {
  color: var(--dict);
  font-weight: 700;
}
#dict-table tbody tr:hover {
  background: rgba(108,92,231,.08);
}
#dict-table .idx-cell {
  color: var(--accent2);
  font-weight: 700;
  width: 60px;
}
#dict-table .phrase-cell {
  letter-spacing: .05em;
}
#dict-table tr.new-row {
  animation: rowIn .4s ease-out;
}
@keyframes rowIn {
  from { opacity:0; transform:translateX(-10px); }
  to   { opacity:1; transform:translateX(0); }
}

#dict-empty {
  text-align:center;
  font-size:.8rem;
  color:var(--muted);
  padding:28px 12px;
  font-style:italic;
}
#dict-empty.hidden { display:none; }

/* ── Error toast ── */
.error-toast {
  position:fixed; bottom:20px; left:50%; transform:translateX(-50%);
  background:var(--danger); color:#fff;
  font: 600 .85rem var(--font);
  padding:10px 20px; border-radius:10px;
  z-index:200; animation: toastIn .3s ease-out;
  box-shadow: 0 6px 24px rgba(239,68,68,.4);
}
@keyframes toastIn {
  from { opacity:0; transform:translate(-50%,12px); }
  to   { opacity:1; transform:translate(-50%,0);   }
}

/* ── Responsive ── */
@media (max-width:768px) {
  #main-content {
    grid-template-columns: 1fr;
  }
  #dict-section {
    position: static;
  }
  #dict-table-wrap {
    max-height: 300px;
  }
}

@media (max-width:560px) {
  :root { --header-h:48px; }
  #header-title { font-size:.88rem; }
  .stat-value { font-size:1.1rem; }
  .char-cell { width:26px; height:34px; font-size:.88rem; }
  #controls-section { flex-wrap:wrap; }
  .ctrl-btn { flex:1; justify-content:center; padding:10px 10px; }
  #viz-container { padding:12px 8px; }
  #app { padding:0 10px 32px; }
  #stats-bar { flex-wrap:wrap; }
  .stat-card { min-width: calc(50% - 6px); }
}
