:root {
  color-scheme: dark;
  --bg: #0f1115;
  --fg: #e7e9ee;
  --muted: #9aa3b2;
  --card: #171a21;
  --border: #242936;
  --border-strong: #2f3647;
  --accent: #7cc4ff;
  --accent-warm: #ffb86b;
  --good: #6be39a;
  --bad: #ff6b6b;
  --radius: 8px;
  --maxw: 980px;
  font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); height: 100%; }
body { display: flex; flex-direction: column; min-height: 100vh; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  max-width: var(--maxw); width: 100%; margin: 0 auto;
}
.topbar .brand { color: var(--muted); font-size: 0.9rem; }
.topbar .title { font-weight: 700; }
.topbar .title a { color: inherit; text-decoration: none; }
.topbar .title a:hover { color: var(--accent); text-decoration: none; }
.topbar .stats { margin-left: auto; color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }

main {
  flex: 1; width: 100%; max-width: var(--maxw); margin: 0 auto;
  padding: 1rem; display: flex; flex-direction: column; gap: 1rem; min-height: 0;
}

.controls { display: flex; flex-direction: column; gap: 0.75rem; }

.mode { display: inline-flex; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 3px; align-self: flex-start; }
.mode-btn {
  background: transparent; color: var(--muted); border: 0;
  padding: 0.45rem 0.9rem; border-radius: 6px; cursor: pointer;
  font: inherit; line-height: 1.1;
}
.mode-btn.active { background: var(--border-strong); color: var(--fg); }
.mode-btn:hover:not(.active) { color: var(--fg); }

.search-wrap { position: relative; }
#query {
  width: 100%; padding: 0.7rem 2.2rem 0.7rem 0.85rem; font: inherit; font-size: 1.05rem;
  background: var(--card); color: var(--fg); border: 1px solid var(--border-strong); border-radius: var(--radius);
}
#query:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
#clear-query {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 28px; height: 28px; border-radius: 6px;
}

.ghost {
  background: transparent; color: var(--muted); border: 1px solid transparent;
  border-radius: 6px; cursor: pointer; font: inherit; padding: 0.25rem 0.55rem;
}
.ghost:hover { color: var(--fg); border-color: var(--border-strong); }
.ghost.small { font-size: 0.8rem; padding: 0.1rem 0.4rem; }

.letter-map { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.5rem 0.75rem 0.75rem; }
.letter-map legend { padding: 0 0.4rem; color: var(--muted); font-size: 0.85rem; display: inline-flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.legend-help { display: inline-flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.legend-help .hint { white-space: nowrap; }
.legend-help .sep { color: var(--border-strong); opacity: 0.6; }
.legend-help em { font-style: normal; }
.legend-help em.req { color: var(--good); }
.legend-help em.forb { color: var(--bad); text-decoration: line-through; }
.letters {
  display: grid;
  grid-template-columns: repeat(13, minmax(0, 1fr));
  gap: 4px;
  margin-top: 0.4rem;
}
.ltr {
  appearance: none; border: 1px solid var(--border-strong); background: var(--card); color: var(--fg);
  font: inherit; font-weight: 600; cursor: pointer; padding: 0.45rem 0; border-radius: 6px;
  text-transform: lowercase; transition: background 80ms ease, color 80ms ease, border-color 80ms ease;
}
.ltr:hover { border-color: var(--accent); }
.ltr.req { background: rgba(107, 227, 154, 0.18); border-color: var(--good); color: var(--good); }
.ltr.forb { background: rgba(255, 107, 107, 0.15); border-color: var(--bad); color: var(--bad); text-decoration: line-through; }

.length-filter { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.5rem 0.75rem 0.75rem; }
.length-filter legend { padding: 0 0.4rem; color: var(--muted); font-size: 0.85rem; display: inline-flex; align-items: center; gap: 0.5rem; }

/* ---- Dual-thumb range slider ---- */
.range-dual {
  position: relative;
  height: 44px;
  margin: 1.5rem 0.5rem 0.25rem;
  --thumb-size: 20px;
  --track-h: 6px;
}
.range-track {
  position: absolute; top: 50%; left: 0; right: 0;
  height: var(--track-h); transform: translateY(-50%);
  background: var(--border-strong); border-radius: 999px;
  pointer-events: none;
}
.range-fill {
  position: absolute; top: 0; bottom: 0;
  background: var(--accent);
  border-radius: 999px;
  pointer-events: none;
}
/* Both range inputs overlap on top of the visual track. They are transparent;
   the track and fill above provide the visuals. Only their thumbs receive pointer events. */
.range-dual input[type="range"] {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%; margin: 0;
  -webkit-appearance: none; appearance: none;
  background: transparent;
  pointer-events: none;
  outline: none;
}
.range-dual input[type="range"]::-webkit-slider-runnable-track { background: transparent; border: 0; height: 100%; }
.range-dual input[type="range"]::-moz-range-track { background: transparent; border: 0; height: 100%; }
.range-dual input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: var(--thumb-size); height: var(--thumb-size);
  border-radius: 50%;
  background: var(--fg);
  border: 2px solid var(--accent);
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  cursor: grab;
  pointer-events: auto;
  margin-top: 0; /* centered via 100% input height */
}
.range-dual input[type="range"]::-moz-range-thumb {
  width: var(--thumb-size); height: var(--thumb-size);
  border-radius: 50%;
  background: var(--fg);
  border: 2px solid var(--accent);
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  cursor: grab;
  pointer-events: auto;
}
.range-dual input[type="range"]:active::-webkit-slider-thumb { cursor: grabbing; transform: scale(1.08); }
.range-dual input[type="range"]:active::-moz-range-thumb     { cursor: grabbing; transform: scale(1.08); }
.range-dual input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px rgba(124,196,255,0.35); }
.range-dual input[type="range"]:focus-visible::-moz-range-thumb     { box-shadow: 0 0 0 3px rgba(124,196,255,0.35); }

/* Value tooltips floating above each thumb */
.range-bubble {
  position: absolute;
  bottom: calc(50% + var(--thumb-size) / 2 + 6px);
  transform: translateX(-50%);
  background: var(--border-strong);
  color: var(--fg);
  font-size: 0.78rem; font-weight: 600;
  font-variant-numeric: tabular-nums;
  padding: 2px 7px; border-radius: 999px;
  border: 1px solid var(--border-strong);
  pointer-events: none;
  white-space: nowrap;
  transition: background 100ms ease;
}
.range-bubble::after {
  content: ''; position: absolute; left: 50%; bottom: -4px;
  transform: translateX(-50%) rotate(45deg);
  width: 6px; height: 6px;
  background: var(--border-strong);
}
.range-dual.dragging .range-bubble.active {
  background: var(--accent); color: var(--bg); border-color: var(--accent);
}
.range-dual.dragging .range-bubble.active::after {
  background: var(--accent);
}

.range-legend {
  display: flex; justify-content: space-between;
  color: var(--muted); font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  margin-top: 0.25rem;
}
.range-legend strong { color: var(--fg); margin-left: 0.2rem; }

.sort-row { display: flex; align-items: center; gap: 0.5rem; }
.sort-label-text { color: var(--muted); font-size: 0.85rem; }
.sort { display: inline-flex; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 3px; }
.sort-btn {
  background: transparent; color: var(--muted); border: 0;
  padding: 0.35rem 0.7rem; border-radius: 6px; cursor: pointer;
  font: inherit; font-size: 0.9rem;
}
.sort-btn.active { background: var(--border-strong); color: var(--fg); }
.sort-btn:hover:not(.active):not(:disabled) { color: var(--fg); }
.sort-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.active-filters { color: var(--muted); font-size: 0.85rem; min-height: 1.2em; }
.active-filters .chip { display: inline-flex; align-items: center; gap: 0.25rem; padding: 0.1rem 0.45rem; border-radius: 999px; border: 1px solid var(--border-strong); margin-right: 0.35rem; }
.active-filters .chip.req { color: var(--good); border-color: rgba(107, 227, 154, 0.6); }
.active-filters .chip.forb { color: var(--bad); border-color: rgba(255, 107, 107, 0.5); text-decoration: line-through; }

.results { display: flex; flex-direction: column; gap: 0.4rem; flex: 1; min-height: 0; }
.results-header { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.results-body { display: flex; gap: 0.5rem; flex: 1; min-height: 0; }
.muted { color: var(--muted); }

.viewport {
  position: relative; overflow-y: auto; flex: 1;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  min-height: 280px; max-height: 60vh;
}
.viewport:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.sidebar-index {
  display: flex; flex-direction: column;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 4px 2px;
  min-width: 28px;
  /* Match viewport bounds so scroll stays in sync */
  max-height: 60vh; min-height: 280px;
  user-select: none;
}
.sidebar-index button {
  flex: 1 1 0; min-height: 14px;
  appearance: none; background: transparent; border: 0; padding: 0;
  color: var(--fg); font: inherit; font-size: 0.72rem; font-weight: 600; line-height: 1;
  text-transform: uppercase; cursor: pointer;
  border-radius: 3px;
}
.sidebar-index button:hover:not(:disabled) { background: var(--border-strong); color: var(--accent); }
.sidebar-index button:disabled, .sidebar-index button.empty {
  color: var(--muted); opacity: 0.35; cursor: default;
}
.sidebar-index button.active { color: var(--accent); background: rgba(124, 196, 255, 0.12); }
.spacer { width: 1px; }
.rows { position: absolute; top: 0; left: 0; right: 0; will-change: transform; }
.row {
  display: flex; align-items: baseline; gap: 0.5rem;
  padding: 0 0.85rem;
  height: 32px; line-height: 32px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.row:last-child { border-bottom: 0; }
.row .word { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 1rem; }
.row .word .hit { color: var(--accent); background: rgba(124, 196, 255, 0.12); border-radius: 3px; }
.row .meta { margin-left: auto; color: var(--muted); font-size: 0.78rem; }
.row.empty { justify-content: center; color: var(--muted); height: auto; padding: 1.5rem; border: 0; }

.foot { max-width: var(--maxw); margin: 0 auto; padding: 0.75rem 1rem 1.25rem; font-size: 0.8rem; }

@media (max-width: 560px) {
  main { padding: 0.75rem; gap: 0.75rem; }
  .topbar { padding: 0.5rem 0.75rem; gap: 0.5rem; }
  .topbar .title { font-size: 0.95rem; }
  .topbar .stats { font-size: 0.7rem; }
  .letters { grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 3px; }
  .ltr { padding: 0.6rem 0; font-size: 0.95rem; } /* taller hit area on touch */
  #query { font-size: 16px; padding: 0.7rem 2.2rem 0.7rem 0.75rem; } /* 16px prevents iOS zoom on focus */
  .range-dual { --thumb-size: 24px; height: 48px; margin-top: 1.75rem; }
  .sidebar-index { min-width: 22px; padding: 2px 1px; }
  .sidebar-index button { font-size: 0.65rem; }
  .row { padding: 0 0.6rem; }
  .viewport { max-height: 65vh; }
}

/* Touch devices: bigger thumbs regardless of width. */
@media (pointer: coarse) {
  .range-dual { --thumb-size: 24px; }
}
