:root {
  --bg: #0d1117;
  --card-bg: #161b22;
  --input-bg: #0d1117;
  --code-bg: #0d1117;
  --border: #30363d;
  --border-hover: #484f58;
  --text: #c9d1d9;
  --text-bright: #e6edf3;
  --text-muted: #8b949e;
  --text-dim: #484f58;
  --accent: #58a6ff;
  --button-bg: #238636;
  --button-hover: #2ea043;
  --button-disabled-bg: #21262d;
  --button-disabled-fg: #484f58;
  --ghost-hover-bg: #21262d;
  --error-bg: #2d1b1b;
  --error-fg: #f85149;
  --warn-bg: #2b2004;
  --warn-fg: #d29922;
  --progress-bg: #21262d;
  --tok-fill: #238636;
}

[data-theme="light"] {
  --bg: #ffffff;
  --card-bg: #f6f8fa;
  --input-bg: #ffffff;
  --code-bg: #eaeef2;
  --border: #d0d7de;
  --border-hover: #8c959f;
  --text: #1f2328;
  --text-bright: #1f2328;
  --text-muted: #59636e;
  --text-dim: #818b98;
  --accent: #0969da;
  --button-bg: #1f883d;
  --button-hover: #1a7f37;
  --button-disabled-bg: #eaeef2;
  --button-disabled-fg: #818b98;
  --ghost-hover-bg: #eaeef2;
  --error-bg: #ffebe9;
  --error-fg: #cf222e;
  --warn-bg: #fff8c5;
  --warn-fg: #9a6700;
  --progress-bg: #eaeef2;
  --tok-fill: #1f883d;
}

*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 2rem;
  max-width: 900px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  transition: background 0.2s, color 0.2s;
}
h1 { color: var(--accent); font-size: 1.75rem; margin: 0 0 0.25rem; }
.subtitle { color: var(--text-muted); margin: 0 0 2rem; font-size: 0.95rem; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}
.card-title {
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 1rem;
}

label { display: block; color: var(--text-muted); font-size: 0.85rem; margin-bottom: 0.3rem; }

select {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 0.5rem 0.75rem;
  font-size: 0.95rem;
  cursor: pointer;
  margin-bottom: 1rem;
}
select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

textarea {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 0.75rem;
  font-size: 0.95rem;
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  line-height: 1.5;
  resize: vertical;
  min-height: 110px;
  margin-bottom: 0.75rem;
}
textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

.text-input {
  width: 100%;
  background: var(--input-bg);
  color: var(--text-bright);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}
.text-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

button {
  background: var(--button-bg);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0.5rem 1.25rem;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
  font-weight: 500;
}
button:hover:not(:disabled) { background: var(--button-hover); }
button:disabled { background: var(--button-disabled-bg); color: var(--button-disabled-fg); cursor: not-allowed; }
button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}
button.ghost:hover:not(:disabled) { background: var(--ghost-hover-bg); color: var(--text); border-color: var(--border-hover); }

.row { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; margin-bottom: 0.9em }
.model-row > select { flex: 1; min-width: 0; margin-bottom: 0; }

/* Theme toggle */
.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  width: 38px;
  height: 38px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--card-bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 50%;
  z-index: 100;
}
.theme-toggle:hover:not(:disabled) {
  background: var(--ghost-hover-bg);
  color: var(--accent);
  border-color: var(--border-hover);
}
.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
[data-theme="light"] .theme-toggle .icon-sun { display: block; }
[data-theme="light"] .theme-toggle .icon-moon { display: none; }

/* Progress */
.progress-wrap { margin-bottom: 0.9rem; }
.progress-status {
  color: var(--text-muted);
  font-size: 0.82rem;
  min-height: 1.1em;
  margin-bottom: 0.5rem;
}
.progress-status.error { color: var(--error-fg); }
.progress-files { display: flex; flex-direction: column; gap: 0.5rem; }
.progress-file:last-child .progress-bar { margin-bottom: 0; }
.progress-bar {
  background: var(--progress-bg);
  border-radius: 4px;
  height: 6px;
  overflow: hidden;
  margin-bottom: 0.4rem;
}
.progress-fill {
  background: linear-gradient(90deg, var(--button-bg), var(--button-hover));
  height: 100%;
  width: 0%;
  transition: width 0.25s ease;
}
.progress-label { color: var(--text-muted); font-size: 0.78rem; min-height: 1.1em; }

/* Accumulated text */
#acc-text {
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 1rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-all;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.9rem 1rem;
  min-height: 56px;
  margin-bottom: 1.1rem;
  color: var(--text-bright);
}

/* Token grid */
#token-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
  transition: opacity 0.15s;
}
#token-grid.loading { opacity: 0.35; pointer-events: none; }

.tok {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.45rem 0.7rem;
  background: var(--input-bg);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.2rem;
  min-width: 64px;
  transition: border-color 0.12s, transform 0.1s;
  color: inherit;
}
.tok::before {
  content: '';
  position: absolute;
  inset: 0;
  width: var(--fill, 0%);
  background: var(--color, var(--tok-fill));
  opacity: 0.18;
  pointer-events: none;
  z-index: 0;
}
.tok:hover { border-color: var(--accent); transform: translateY(-1px); }
.tok:active { transform: translateY(0); }
.tok-text {
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 0.9rem;
  position: relative;
  z-index: 1;
  color: var(--text-bright);
  white-space: pre;
}
.tok-prob {
  font-size: 0.7rem;
  color: var(--text-muted);
  position: relative;
  z-index: 1;
}

.explorer-footer { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.hint { color: var(--text-dim); font-size: 0.8rem; flex: 1; }

/* Error / Warning */
.alert {
  border-radius: 8px;
  padding: 0.9rem 1.1rem;
  margin-bottom: 1.25rem;
  font-size: 0.9rem;
  line-height: 1.5;
}
.alert-error { background: var(--error-bg); border: 1px solid var(--error-fg); color: var(--error-fg); }
.alert-warn  { background: var(--warn-bg); border: 1px solid var(--warn-fg); color: var(--warn-fg); }
code { background: var(--code-bg); border-radius: 3px; padding: 0.1em 0.35em; font-family: monospace; font-size: 0.88em; }

[hidden] { display: none !important; }

/* Tabs */
.tab-bar { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tab-btn {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  padding: 0.35rem 0.9rem;
  font-size: 0.85rem;
  cursor: pointer;
  font-weight: 500;
  border-radius: 0;
  margin-bottom: -1px;
  transition: color 0.12s;
}
.tab-btn:hover { background: transparent !important; color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
