/* ── Variables (dark mode) ──────────────────────────────────────────────────── */
:root {
  --bg:          #1e1e1e;    /* dark gray, not black */
  --surface:     #2a2a2a;    /* cards, inputs, dropdown */
  --border:      #3d3d3d;    /* subtle separators */
  --text:        #e8e6e3;    /* warm near-white */
  --muted:       #8a8480;    /* medium warm gray */
  --accent:      #5b8ad9;    /* bright blue readable on dark bg */
  --accent-soft: #1e3258;    /* dark navy tint for hovers */
  --danger:      #e05c4a;    /* slightly brightened red */
  --disabled-bg: #333333;
  --disabled-fg: #666666;
  --radius:      6px;
  --shadow:      0 2px 8px rgba(0,0,0,0.4);
}
/* accent-soft audit on dark (#1e3258):
   dropdown-item:hover  → dark navy on surface #2a2a2a, near-white text legible ✓
   results-list li:hover → dark navy on bg #1e1e1e, text #e8e6e3 readable ✓
   voter-book.is-input  → dark navy bg + #5b8ad9 bright text (distinguishable from is-rec) ✓ */

/* ── Reset / base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
/* Ensure [hidden] wins over any author display rules (e.g. #main.post-run {display:grid}) */
[hidden] { display: none !important; }

body {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

/* ── Header ────────────────────────────────────────────────────────────────── */
header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;   /* tabs | centered title | empty balance */
  align-items: center;
  padding: 0 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: 3rem;
}

.site-name {
  font-size: 1.05rem;
  font-weight: normal;
  letter-spacing: 0.06em;
  color: var(--text);
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
  padding: 0;
}

/* ── Tab nav ───────────────────────────────────────────────────────────────── */
.nav-tabs {
  display: flex;
  align-items: stretch;
}

.nav-tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;   /* space for active indicator */
  cursor: pointer;
  font-family: inherit;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0.75rem 0.6rem;
  letter-spacing: 0.03em;
  line-height: 1;
  transition: color 0.15s;
}
.nav-tab:hover { color: var(--text); }
.nav-tab.is-active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

@media (max-width: 480px) {
  header {
    grid-template-columns: auto auto 1fr;   /* tabs at natural width; title follows; 1fr absorbs remainder */
  }
  .site-name {
    font-size: 0.85rem;
    letter-spacing: 0.03em;
  }
  .nav-tab {
    padding: 0.75rem 0.35rem;
    font-size: 0.72rem;
  }
}

/* ── Loading ───────────────────────────────────────────────────────────────── */
#loading {
  text-align: center;
  margin-top: 4rem;
  color: var(--muted);
  font-style: italic;
}

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

#main.post-run {
  display: grid;
  grid-template-columns: minmax(220px, 280px) 1fr;
  gap: 2rem;
  max-width: min(calc(100vw - 2.5rem), 1360px);
  align-items: start;
}

@media (max-width: 768px) {
  #main.post-run {
    grid-template-columns: 1fr;
  }
}

/* ── Input panel ───────────────────────────────────────────────────────────── */
#input-panel {
  position: sticky;
  top: 1rem;
  display: flex;
  flex-direction: column;
}

#main.pre-run #input-panel {
  position: static;
}

/* Flex order: pre-run keeps slider below search (order 5 > 4).
   Post-run: slider floats above book-entries (order 2 < 3). */
h1             { order: 0; }
.subtitle      { order: 1; }
#book-entries  { order: 3; }
#search-container { order: 4; }
#run-area      { order: 5; }
#main.post-run #run-area { order: -1; margin-bottom: 1rem; }

h1 {
  font-size: 1.4rem;
  font-weight: normal;
  margin-bottom: 0.25rem;
}

#main.post-run h1 {
  font-size: 1rem;
}

.subtitle {
  color: var(--muted);
  font-size: 0.9rem;
  margin-bottom: 1.25rem;
}

#main.post-run .subtitle { display: none; }

/* ── Book entries ──────────────────────────────────────────────────────────── */
#book-entries {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.book-entry {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
}

#main.post-run .book-entry {
  padding: 0.35rem 0.6rem;
}

.book-main {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  align-items: baseline;
}

.book-title { font-weight: bold; font-size: 0.9rem; }
.book-sep   { color: var(--muted); font-size: 0.85rem; }
.book-author { color: var(--muted); font-size: 0.85rem; }

.book-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.1rem;
}

.voter-count {
  font-size: 0.75rem;
  color: var(--muted);
  flex: 1;
}

.remove-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.15rem;
  border-radius: 3px;
  flex-shrink: 0;
}
.remove-btn:hover { color: var(--danger); }

/* ── Search box ────────────────────────────────────────────────────────────── */
.search-wrapper { position: relative; }

#search-input {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
#search-input:focus { border-color: var(--accent); }

/* ── Dropdown ──────────────────────────────────────────────────────────────── */
.dropdown {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  min-width: max(100%, 320px);   /* wider than input; at least 320px */
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  list-style: none;
  max-height: 320px;
  overflow-y: auto;
  z-index: 100;
}

.dropdown-item {
  padding: 0.45rem 0.75rem;
  cursor: pointer;
  font-size: 0.875rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}
.dropdown-item:hover,
.dropdown-item.active { background: var(--accent-soft); }

.dropdown-item.disabled {
  color: var(--disabled-fg);
  cursor: default;
  background: none;
}

.dropdown-no-matches {
  padding: 0.45rem 0.75rem;
  font-size: 0.875rem;
  color: var(--muted);
  font-style: italic;
  list-style: none;
}

.item-left   { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 0.25rem; overflow: hidden; }
.item-title  { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sep    { color: var(--muted); font-size: 0.85rem; flex-shrink: 0; }
.item-author { color: var(--muted); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-count  { font-size: 0.75rem; color: var(--muted); white-space: nowrap; flex-shrink: 0; }

/* ── Blend slider (Popular ↔ Distinctive) ───────────────────────────────────── */
#run-area { margin-top: 1rem; }

#blend-control {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
}

.blend-label {
  font-size: 0.8rem;
  color: var(--muted);
  user-select: none;
  text-align: center;
}

.blend-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.blend-slider-wrapper {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;   /* vertically center slider input within wrapper */
}

#blend-slider {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.blend-tooltip {
  position: absolute;
  bottom: calc(100% + 0.3rem);
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.1rem 0.3rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
.blend-tooltip.visible { opacity: 1; }

.blend-reset {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.3rem;
  padding: 0.2rem 0.5rem;
  border-radius: var(--radius);
  line-height: 1;
  flex-shrink: 0;
  align-self: center;
  font-family: sans-serif;
}
.blend-reset:hover { color: var(--text); }

/* ── Mode toggle (segmented control) ─────────────────────────────────────── */
.mode-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  align-self: flex-start;
  order: -1;
  margin-bottom: 0.5rem;
}
.mode-btn {
  background: none;
  border: none;
  border-right: 1px solid var(--border);
  padding: 0.3rem 0.9rem;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode-btn:last-child { border-right: none; }
.mode-btn.is-active  { background: var(--accent); color: #fff; cursor: default; }
.mode-btn:not(.is-active):hover { background: var(--accent-soft); color: var(--text); }
#main.post-run .mode-toggle { align-self: center; }

/* ── Results panel ─────────────────────────────────────────────────────────── */
#results-panel {
  transition: opacity 0.2s;
}

#results-header {
  font-size: 1rem;
  color: var(--text);
  margin-bottom: 1rem;
}

#results-list {
  list-style: none;
  counter-reset: result-counter;
}

#results-list li {
  display: grid;
  grid-template-columns: 2rem 1fr auto;
  gap: 0.1rem 0.6rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
  counter-increment: result-counter;
}

#results-list li::before {
  content: counter(result-counter);
  color: var(--muted);
  font-size: 0.85rem;
  text-align: right;
  padding-top: 0.15rem;
  grid-column: 1;
  grid-row: 1;
}

/* result-title-row: title + author·year byline on one flex line */
.result-title-row {
  grid-column: 2; grid-row: 1;
  display: flex; align-items: baseline; gap: 0.4rem;
}
.result-title  { font-weight: bold; font-size: 0.95rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-byline { color: var(--muted); font-size: 0.85rem; white-space: nowrap; flex-shrink: 0; }
.result-chevron {
  grid-column: 3;
  grid-row: 1 / span 2;   /* span title+byline and count rows */
  align-self: center;
  padding: 0 0.75rem 0 0.25rem;
  color: var(--muted); font-size: 1rem;
  display: inline-block; transition: transform 0.15s ease;
}
#results-list li.result-expanded .result-chevron { transform: rotate(90deg); }

.result-count  { grid-column: 2; grid-row: 2; color: var(--muted); font-size: 0.8rem; margin-top: 0.05rem; }
.result-diag   { grid-column: 2; grid-row: 3; color: var(--muted); font-size: 0.75rem; font-style: italic; margin-top: 0.05rem; }

#results-list li:hover { background: var(--accent-soft); }

/* ── Detail panel ───────────────────────────────────────────────────────────── */
.result-detail {
  grid-column: 2 / -1;
  background: #252525;          /* subtle resting elevation: between --bg #1e1e1e and --surface #2a2a2a */
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 0.75rem 0.75rem;
  margin: 0.35rem 0.75rem 0.75rem 0;  /* top/bottom gap; right=0.75rem containment buffer (left is 0 — grid-column handles the left indent) */
  box-shadow: var(--shadow);
}

/* Scores row */
.detail-scores { display: flex; gap: 2rem; margin-bottom: 0.6rem; flex-wrap: wrap; }
.detail-score  { display: flex; align-items: baseline; gap: 0.4rem; }
.detail-score-label { font-size: 0.72rem; color: var(--muted); }
.detail-score-value { font-size: 1.05rem; font-weight: bold; color: var(--text); }

/* Description */
.detail-description {
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.5;
  margin: 0 0 0.75rem;
  font-style: italic;
}

/* Voter strip */
.detail-strip-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  /* margin: 0 0.5rem insets the wrapper within .result-detail's content box —
     both wrapper edges are 0.5rem from the content-box edges (verifiable via
     getBoundingClientRect). The ::after spacer on .detail-strip handles the
     scroll-end right buffer within the scroll content. Chrome drops padding-right
     from scrollWidth; margin sidesteps that entirely. */
  margin: 0 0.5rem;
  padding: 0 0 0.1rem;
}
.detail-strip {
  display: flex;
  align-items: stretch;   /* uniform height: all cards = tallest card */
  gap: 0.6rem;
  padding-bottom: 0.4rem;   /* clears horizontal scrollbar appearance */
  min-width: min-content;
}
/* ::after spacer removed: wrapper margin-right already provides 8px right-side
   buffer between scroll viewport and card inner border — spacer was redundant
   and stacked with the flex gap to create 18px internal buffer vs 8px on left */

.voter-card {
  flex-shrink: 0;
  width: 160px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}
.voter-card-header {
  flex-shrink: 0;
  background: var(--disabled-bg);
  padding: 0.3rem 0.5rem;
  font-size: 0.72rem;
  font-weight: bold;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.voter-card-books {
  flex: 1;                /* fills remaining card height */
  padding: 0.25rem 0.35rem;
  overflow: hidden;       /* no internal scroll; uniform via flex stretch */
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.voter-book {
  font-size: 0.68rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0.1rem 0.3rem;
  border-radius: 2px;
  line-height: 1.3;
}
.voter-book.is-input {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: bold;
}
.voter-book.is-rec {
  background: var(--accent);
  color: #fff;
  font-weight: bold;
}

.detail-empty {
  font-size: 0.8rem;
  color: var(--muted);
  font-style: italic;
}

/* ── About page ─────────────────────────────────────────────────────────────── */
#about {
  padding: 3rem 1.25rem 5rem;
}
.about-content {
  max-width: min(calc(100vw - 2.5rem), 820px);
  margin: 0 auto;
}
.about-lead {
  font-size: 1.35rem;
  line-height: 1.6;
  color: var(--text);
  margin-bottom: 2rem;
}
.about-content p {
  font-size: 1rem;
  line-height: 1.85;
  color: var(--text);
  margin-bottom: 1.25rem;
}
.about-tips {
  list-style: decimal;
  padding-left: 1.5rem;
  margin-top: 0.25rem;
}
.about-tips > li {
  font-size: 1rem;
  line-height: 1.85;
  color: var(--text);
  margin-bottom: 1rem;
}
.about-tips > li::marker {
  color: var(--text);
}
.about-tips-sub {
  list-style: lower-alpha;
  padding-left: 1.25rem;
  margin-top: 0.5rem;
}
.about-tips-sub > li {
  font-size: 0.9rem;
  line-height: 1.7;
  color: var(--text);
  margin-bottom: 0.35rem;
}
.about-tips-sub > li::marker {
  color: var(--text);
}

/* ── Mobile (≤600px) ─────────────────────────────────────────────────────────── */
/* Additive-only block. No base rules modified. Desktop (>600px) is byte-for-byte unaffected. */
@media (max-width: 600px) {
  /* Header: tabs left, title right (flex replaces grid; 480px grid-template-columns
     override has no effect on a flex container, so existing 480px block is harmless) */
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 1rem;
  }

  /* Input panel: cancel sticky on mobile so it scrolls away instead of overlaying results */
  #input-panel {
    position: static;
    top: auto;
  }

  /* Search input: ≥16px prevents iOS Safari auto-zoom on focus (was 0.9rem = 14.4px) */
  #search-input {
    font-size: 1rem;
  }

  /* Grid items default to min-width: auto, which prevents the 1fr track in
     #main.post-run from shrinking below items' min-content (~646px), overflowing
     the page by ~296px at 390px viewport. min-width: 0 lets the track resolve
     correctly to the available ~310px. Voter-strip overflow-x: auto is unaffected. */
  #input-panel,
  #results-panel {
    min-width: 0;
  }

  /* Inside each result <li> (grid: 2rem 1fr auto), result-title-row and result-count
     sit in the 1fr column with min-width: auto. The byline (flex-shrink: 0,
     white-space: nowrap) forces result-title-row's min-content to ~302px, pushing
     1fr beyond the available ~236px and causing scrollWidth 646 per row. min-width: 0
     lets 1fr resolve correctly; overflow: hidden clips any byline edge case. */
  /* min-width: 0 keeps 1fr grid track correct; overflow: visible overridden below */
  .result-title-row {
    min-width: 0;
    overflow: hidden;
  }
  .result-count {
    min-width: 0;
  }

  /* Fix 1: stack title on its own line, byline beneath — removes the single-line
     clip that was showing only ~2 words. Byline becomes a block below the title. */
  .result-title-row {
    display: block;
    overflow: visible;
  }
  .result-title {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }
  .result-byline {
    display: block;
    white-space: normal;
  }

  /* Fix 2: expanded card spans full row (removes 41.6px desktop left indent +
     12px right buffer that squeezes card to ~256px on mobile) */
  .result-detail {
    grid-column: 1 / -1;
    margin-right: 0;
  }

  /* Fix 3: dropdown matches search input width exactly; removes the 320px
     minimum-width floor that extends dropdown past viewport on 320px phones
     (measured: count.right=327 > iw=320 at 320px with no fix) */
  .dropdown {
    min-width: 100%;
  }

  /* Inline the list count with the author text instead of right-aligning.
     Changes: item-left stops growing (flex: 0 1 auto) so item-count follows
     immediately after it; gap removed; · separator added via ::before. */
  .dropdown-item {
    justify-content: flex-start;
    gap: 0;
  }
  .item-left {
    flex: 0 1 auto;
  }
  .item-count::before {
    content: '\00b7';       /* dot only; spacing via margin, not content whitespace */
    margin: 0 0.25rem;      /* matches gap: 0.25rem inside .item-left */
    color: var(--muted);
  }
}
