/* ── Variables & theming ─────────────────────────────────────────────── */
:root {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --surface-alt: #f0efee;
  --border: #d6d3d1;
  --text: #1c1917;
  --text-muted: #78716c;
  --accent: #b45309;
  --accent-dim: #fef3c7;
  --highlight-bg: #fde68a;
  --highlight-fg: #1c1917;
  --sidebar-w: 260px;
  --radius: 7px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .10), 0 1px 2px rgba(0, 0, 0, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1917;
    --surface: #292524;
    --surface-alt: #312e2b;
    --border: #44403c;
    --text: #fafaf9;
    --text-muted: #a8a29e;
    --accent: #f59e0b;
    --accent-dim: #451a03;
    --highlight-bg: #92400e;
    --highlight-fg: #fef3c7;
  }
}

[data-theme="light"] {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --surface-alt: #f0efee;
  --border: #d6d3d1;
  --text: #1c1917;
  --text-muted: #78716c;
  --accent: #b45309;
  --accent-dim: #fef3c7;
  --highlight-bg: #fde68a;
  --highlight-fg: #1c1917;
}

[data-theme="dark"] {
  --bg: #1c1917;
  --surface: #292524;
  --surface-alt: #312e2b;
  --border: #44403c;
  --text: #fafaf9;
  --text-muted: #a8a29e;
  --accent: #f59e0b;
  --accent-dim: #451a03;
  --highlight-bg: #92400e;
  --highlight-fg: #fef3c7;
}

::highlight(search-match) {
  background-color: var(--highlight-bg);
  color: var(--highlight-fg);
}

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

html, body {
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  direction: rtl;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

button {
  cursor: pointer;
  font: inherit;
}

/* ── Top bar (header + search, fixed height, no scroll) ─────────────── */
#top-bar {
  flex-shrink: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: .75rem 1.25rem .5rem;
  z-index: 10;
}

#top-bar-inner {
  display: flex;
  align-items: center;
  gap: .85rem;
}

h1 {
  font-size: 1.25rem;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── Search bar (inside top-bar) ─────────────────────────────────────── */
#search-bar {
  width: 30%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: .6rem;
  min-width: 0;
}

.query-wrap {
  flex: 1;
  position: relative;
  display: flex;
  min-width: 0;
}

#query {
  flex: 1;
  min-width: 0;
  padding: .45rem .8rem .45rem 2rem;
  font-size: .95rem;
  font-family: inherit;
  direction: rtl;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}

#query:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

#query::placeholder {
  color: var(--text-muted);
}

#query::-webkit-search-cancel-button {
  display: none;
}

#query-clear {
  position: absolute;
  left: .5rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: .8rem;
  line-height: 1;
  padding: .2rem .3rem;
  border-radius: 50%;
  transition: color .15s, background .15s;
}

#query-clear:hover {
  color: var(--text);
  background: var(--border);
}

#search-status {
  font-size: .8rem;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

#search-status:empty {
  display: none;
}

/* ── Theme toggle ────────────────────────────────────────────────────── */
#theme-toggle {
  margin-inline-start: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .3rem .55rem;
  font-size: .95rem;
  line-height: 1;
  color: var(--text);
  flex-shrink: 0;
  transition: background .15s;
}

#theme-toggle:hover {
  background: var(--surface-alt);
}

/* ── Sidebar toggle (mobile only) ───────────────────────────────────── */
#sidebar-toggle {
  display: none; /* shown only on mobile */
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .3rem .55rem;
  font-size: .95rem;
  line-height: 1;
  color: var(--text);
  flex-shrink: 0;
  transition: background .15s;
}

#sidebar-toggle:hover {
  background: var(--surface-alt);
}

/* ── Backdrop (mobile only) ──────────────────────────────────────────── */
#sidebar-backdrop {
  display: none;
}

/* Default (light): show sun, hide moon */
#theme-toggle .icon-dark {
  display: none;
}

#theme-toggle .icon-light {
  display: inline;
}

/* Explicit dark theme */
[data-theme="dark"] #theme-toggle .icon-dark {
  display: inline;
}

[data-theme="dark"] #theme-toggle .icon-light {
  display: none;
}

/* System dark preference (no explicit theme set) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) #theme-toggle .icon-light {
    display: none;
  }

  :root:not([data-theme]) #theme-toggle .icon-dark {
    display: inline;
  }
}

/* ── Breadcrumb ──────────────────────────────────────────────────────── */
#breadcrumb {
  margin-top: .3rem;
  font-size: .8rem;
  color: var(--text-muted);
  min-height: 1.2em;
}

#breadcrumb:empty {
  display: none;
}

#breadcrumb a {
  color: var(--text-muted);
}

#breadcrumb a:hover {
  color: var(--accent);
  text-decoration: underline;
}

#breadcrumb .sep {
  margin: 0 .35em;
}

/* ── Two-pane layout ─────────────────────────────────────────────────── */
#layout {
  flex: 1;
  display: flex;
  overflow: hidden;
  /* RTL: first child (aside#sidebar) is on the right */
}

/* ── Sidebar ─────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  overflow-y: auto;
  border-inline-start: 1px solid var(--border); /* left border in RTL */
  background: var(--surface-alt);
  padding: .5rem 0;
}

.sidebar-list {
  list-style: none;
  padding: 0;
}

.sidebar-item {
  border-radius: 0;
  transition: background .1s, color .1s;
  color: var(--text);
}

.sidebar-item:hover {
  background: var(--surface);
}

.sidebar-item.current {
  background: var(--accent-dim);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-item.no-match {
  opacity: .35;
}

.sidebar-item.has-match {
  font-weight: 600;
}

.sidebar-link {
  display: block;
  padding: .45rem .85rem;
  font-size: .82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: inherit;
  text-decoration: none;
}

/* ── Main pane ───────────────────────────────────────────────────────── */
#main-pane {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem 3rem;
  min-width: 0;
}

/* ── Welcome state ───────────────────────────────────────────────────── */
.welcome-message {
  color: var(--text-muted);
  padding: 3rem 1rem;
  text-align: center;
  font-size: 1rem;
}

/* ── Search results ──────────────────────────────────────────────────── */
.results-summary {
  font-size: .85rem;
  color: var(--text-muted);
  margin-bottom: 1.25rem;
}

.results-episode {
  margin-bottom: 1.75rem;
  content-visibility: auto;
  contain-intrinsic-size: auto 8rem;
}

.results-episode-header {
  display: flex;
  align-items: baseline;
  gap: .6rem;
  margin-bottom: .5rem;
  padding-bottom: .3rem;
  border-bottom: 1px solid var(--border);
}

.results-episode-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
}

.results-episode-title:hover {
  color: var(--accent);
}

.results-episode-count {
  font-size: .8rem;
  color: var(--text-muted);
}

.result-entry {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .4rem .75rem;
  margin-bottom: .4rem;
  overflow: hidden;
}

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

.result-line:hover {
  background: var(--surface-alt);
}

.result-line.context {
  color: var(--text-muted);
  font-size: .88rem;
}

.result-line.match {
  color: var(--text);
}

.result-line .ts {
  font-size: .75rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  flex-shrink: 0;
}

.entries-overflow.hidden {
  display: none;
}

.show-more-btn {
  background: none;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: .82rem;
  padding: .35rem .75rem;
  width: 100%;
  margin-top: .25rem;
  transition: border-color .15s, color .15s;
}

.show-more-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ── Episode view ────────────────────────────────────────────────────── */
.episode-header {
  margin-bottom: 1rem;
}

.episode-header h2 {
  font-size: 1.2rem;
  font-weight: 700;
}

.episode-header h2 a {
  color: inherit;
  text-decoration: none;
}

.episode-header h2 a:hover {
  text-decoration: underline;
}

.transcript-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.transcript-line {
  display: flex;
  gap: .85rem;
  align-items: baseline;
  padding: .28rem .4rem;
  border-radius: 5px;
  transition: background .1s;
  content-visibility: auto;
  contain-intrinsic-size: auto 2rem;
}

.transcript-line:hover {
  background: var(--surface-alt);
}

.transcript-line.hidden {
  display: none;
}

.transcript-line.highlighted {
  background: var(--accent-dim);
}

.transcript-line .ts {
  font-size: .75rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  flex-shrink: 0;
  padding-top: .1em;
}

.transcript-line .text {
  flex: 1;
}

.chapter-block {
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: .75rem;
  overflow: hidden;
}

.chapter-block-header {
  align-items: baseline;
  color: var(--text-muted);
  display: flex;
  padding: .35rem .4rem;
  font-size: .8rem;
  font-variant-numeric: tabular-nums;
  text-decoration: none;
  gap: .85rem;
}

a.chapter-block-header:hover {
  text-decoration: underline;
}

.chapter-block-header strong {
  color: var(--text);
}

/* ── Generic states ──────────────────────────────────────────────────── */
.state-message {
  text-align: center;
  color: var(--text-muted);
  padding: 3rem 1rem;
  font-size: 1rem;
}

#site-footer {
  text-align: right;
  padding: .6rem 1rem;
  font-size: .8rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

#site-footer p {
  margin: 0;
}

#site-footer a {
  color: var(--text-muted);
  text-decoration: underline;
}

#site-footer a:hover {
  color: var(--accent);
}

/* ── Mobile layout (≤640px) ──────────────────────────────────────────── */
@media (max-width: 640px) {
  /* Top bar: two rows
     Row 1: h1 (flex: 1) | theme-toggle | sidebar-toggle
     Row 2: search-bar (full width, pushed to row 2 via order: 1)        */
  #top-bar-inner {
    flex-wrap: wrap;
  }

  #top-bar-inner h1 {
    flex: 1;
  }

  #search-bar {
    order: 1;
    width: 100%;
    flex-shrink: 0;
  }

  #sidebar-toggle {
    display: inline-flex;
  }

  /* Sidebar: fixed drawer, slides in from right (RTL) */
  #sidebar {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 80%;
    max-width: 300px;
    overflow-y: auto;
    transform: translateX(100%);
    transition: transform .25s ease;
    z-index: 20;
  }

  body.sidebar-open #sidebar {
    transform: translateX(0);
  }

  /* Backdrop */
  #sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .4);
    z-index: 19;
  }

  body.sidebar-open #sidebar-backdrop {
    display: block;
  }
}
