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

:root {
  --bg:       #f8f7f2;
  --surface:  #ffffff;
  --primary:  #3b6fa0;
  --primary-dark: #2c5480;
  --accent:   #c0392b;
  --text:     #222222;
  --muted:    #666666;
  --border:   #ddd;
  --radius:   8px;
  --shadow:   0 2px 8px rgba(0,0,0,.10);
  --max-w:    860px;
}

html { scroll-behavior: smooth; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ────────────────────────────────────────────────────── */
header {
  background: var(--primary);
  color: #fff;
  padding: 2rem 1rem 1.5rem;
  text-align: center;
}

header h1 {
  font-size: clamp(1.4rem, 4vw, 2.2rem);
  font-weight: 700;
  letter-spacing: -.5px;
  margin-bottom: .4rem;
}

header p {
  font-size: .95rem;
  opacity: .88;
}

header p a { color: #aed6f1; }

/* ── Main content ──────────────────────────────────────────────── */
main {
  flex: 1;
  width: 100%;
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
}

/* ── Search box ────────────────────────────────────────────────── */
.search-section {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: 1rem 0 .75rem;
}

#search-input {
  width: 100%;
  padding: .75rem 1rem;
  font-size: 1.05rem;
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  outline: none;
  background: var(--surface);
  transition: box-shadow .2s;
}

#search-input:focus {
  box-shadow: 0 0 0 3px rgba(59,111,160,.25);
}

/* ── Results bar (stats + sort) ───────────────────────────────── */
#results-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .5rem;
  margin-bottom: 1rem;
}

#stats {
  font-size: .85rem;
  color: var(--muted);
  min-height: 1.2em;
}

#sort-controls {
  display: flex;
  align-items: center;
  gap: .4rem;
  font-size: .85rem;
  color: var(--muted);
}

#sort-controls.hidden { display: none; }

#sort-select {
  font-size: .85rem;
  padding: .25rem .5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
}

/* ── Group-by toggle switch ───────────────────────────────────── */
#group-toggle {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  margin-left: .75rem;
}

#group-toggle.hidden { display: none; }

#group-toggle .toggle-text {
  font-size: .85rem;
  color: var(--muted);
}

#group-toggle .toggle-label {
  font-size: .8rem;
  color: var(--muted);
}

.switch {
  position: relative;
  display: inline-block;
  width: 34px;
  height: 18px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch .slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #ccc;
  border-radius: 18px;
  transition: background-color .2s;
}

.switch .slider::before {
  content: "";
  position: absolute;
  height: 14px;
  width: 14px;
  left: 2px;
  top: 2px;
  background-color: #fff;
  border-radius: 50%;
  transition: transform .2s;
}

.switch input:checked + .slider {
  background-color: var(--primary);
}

.switch input:checked + .slider::before {
  transform: translateX(16px);
}

.switch input:focus-visible + .slider {
  box-shadow: 0 0 0 3px rgba(59,111,160,.35);
}

/* ── Reference-group card ─────────────────────────────────────── */
.ref-group-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
  overflow: hidden;
}

.ref-group-header {
  background: var(--primary);
  color: #fff;
  padding: .5rem .9rem;
  font-weight: 600;
  font-size: .95rem;
}

.ref-entry {
  border-top: 1px solid var(--border);
  padding: .4rem .25rem;
}

.ref-entry:first-of-type { border-top: none; }

.ref-entry-header {
  display: flex;
  align-items: baseline;
  gap: .5rem;
  padding: .25rem .75rem;
}

.ref-hit-line {
  flex-wrap: wrap;
}

.ref-hit-title {
  font-size: .8rem;
  font-weight: 600;
  color: var(--primary-dark);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 1;
  min-width: 0;
}

/* ── Video card ───────────────────────────────────────────────── */
.video-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
  overflow: hidden;
}

.video-header {
  display: flex;
  gap: .75rem;
  align-items: center;
  padding: .75rem 1rem;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--border);
  transition: background .15s;
}

.video-header:hover { background: #f0f5fb; }

.thumbnail {
  width: 80px;
  height: 45px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}

.video-header-info {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  min-width: 0;
}

.video-title {
  font-weight: 600;
  font-size: .95rem;
  color: var(--primary-dark);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.video-meta {
  font-size: .78rem;
  color: var(--muted);
}

.hit-count {
  font-size: .78rem;
  color: var(--accent);
  font-weight: 600;
}

/* ── Hit lines (ref + timestamp + KWIC rows within a video) ───── */
.hit-lines {
  padding: .25rem .75rem .5rem;
}

.hit-line {
  display: flex;
  gap: .5rem;
  align-items: baseline;
  padding: .2rem .25rem;
  text-decoration: none;
  color: inherit;
  transition: background .15s;
  border-radius: 4px;
}

.hit-line:hover { background: #f0f5fb; }

.hit-ref {
  font-size: .8rem;
  font-weight: 600;
  color: var(--primary-dark);
  white-space: nowrap;
  flex-shrink: 0;
}

.timestamp {
  font-size: .8rem;
  color: var(--accent);
  font-weight: 600;
  flex-shrink: 0;
}

.snippet {
  font-size: .82rem;
  color: var(--muted);
  font-style: italic;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* ── Feedback messages ─────────────────────────────────────────── */
.no-results, .error, .hint {
  padding: 1.5rem 1rem;
  color: var(--muted);
  text-align: center;
  font-size: .95rem;
}

.error { color: var(--accent); }

/* ── Footer ────────────────────────────────────────────────────── */
footer {
  background: #222;
  color: #aaa;
  font-size: .8rem;
  text-align: center;
  padding: .9rem 1rem;
}

footer a { color: #aed6f1; }

/* ── Responsive ────────────────────────────────────────────────── */
@media (max-width: 500px) {
  .thumbnail { width: 60px; height: 34px; }
  .video-title { font-size: .88rem; }
  .hit-line { flex-wrap: wrap; }
  #results-bar { flex-direction: column; align-items: flex-start; }
}
