/* ── Custom properties ──────────────────────────────────────────────────── */

:root {
  --bg: #f5f5f5;
  --bg-header: #ffffff;
  --bg-card: #ffffff;
  --bg-search-result-hover: #e8e8e8;
  --text: #222222;
  --text-muted: #666666;
  --text-number: #666666;
  --border: #dddddd;
  --accent: #4a6fa5;
  --accent-hover: #3a5a8a;
  --btn-bg: #4a6fa5;
  --btn-text: #ffffff;
  --btn-disabled-bg: #cccccc;
  --btn-disabled-text: #888888;
  --shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
  --header-height: 60px;
  --transcript-bg: #f9f9f9;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --bg-header: #1e1e1e;
    --bg-card: #1e1e1e;
    --bg-search-result-hover: #333333;
    --text: #e0e0e0;
    --text-muted: #999999;
    --text-number: #999999;
    --border: #333333;
    --accent: #6a9fd8;
    --accent-hover: #8ab8e8;
    --btn-bg: #3a5a8a;
    --btn-text: #ffffff;
    --btn-disabled-bg: #2a2a2a;
    --btn-disabled-text: #555555;
    --shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
    --transcript-bg: #181818;
  }
}

/* ── Reset & base ────────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Ensure the hidden attribute works even on elements with explicit display values */
[hidden] { display: none !important; }

/* Visually hidden but available to screen readers */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html { scroll-behavior: smooth; overflow-x: clip; }

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

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

/* ── Loading overlay ─────────────────────────────────────────────────────── */

#loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  color: var(--text-muted);
  z-index: 1000;
}
#loading-overlay[hidden] { display: none; }

/* ── Progress bar ────────────────────────────────────────────────────────── */

#progress-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  z-index: 9999;
  background: transparent;
}
#progress-container[hidden] { display: none; }

#progress-bar {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.2s linear;
}

/* ── Header ──────────────────────────────────────────────────────────────── */

#site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1.5rem;
}

#logo-link { display: flex; align-items: center; flex-shrink: 0; }

#logo { height: 40px; width: auto; display: block; }

#nav-index {
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── Search ──────────────────────────────────────────────────────────────── */

#search-container { position: relative; flex: 1; max-width: 480px; }

#search-input {
  width: 100%;
  padding: 0.45rem 0.8rem;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
#search-input:focus { border-color: var(--accent); }

/* ── Focus indicators ────────────────────────────────────────────────────── */

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Suppress the default outline on elements that already have bespoke focus styles */
#search-input:focus-visible { outline: none; }
.search-result:focus-visible { outline: none; }

#search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: var(--shadow);
  max-height: min(520px, 70vh);
  overflow-y: auto;
  z-index: 200;
}
#search-results[hidden] { display: none; }

.search-result {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  padding: 0.5rem 0.8rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.search-result:last-child { border-bottom: none; }
.search-result:hover,
.search-result:focus { background: var(--bg-search-result-hover); outline: none; }

/* Title-match result */
.result-number {
  font-size: 0.8rem;
  color: var(--text-number);
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}
.result-title {
  font-size: 0.9rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Text/transcript-match result */
.search-result--text {
  flex-direction: column;
  align-items: flex-start;
  gap: 0.2rem;
}
.search-result--text .result-header {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}
.search-result--text .result-title {
  font-size: 0.82rem;
  color: var(--text-muted);
}
.result-excerpt {
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.4;
  word-break: break-word;
}
.result-excerpt strong {
  color: var(--text);
  font-weight: 600;
}
.search-result strong { font-weight: 600; }

.search-result--focused {
  background: var(--bg-search-result-hover);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.search-empty {
  padding: 0.75rem 0.8rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* ── Views ───────────────────────────────────────────────────────────────── */

.view { flex: 1; display: flex; flex-direction: column; }
.view[hidden] { display: none; }

/* ── Comic view ──────────────────────────────────────────────────────────── */

#comic-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1.5rem 1rem 2rem;
  gap: 1.25rem;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
}

/* ── Comic nav ───────────────────────────────────────────────────────────── */

#comic-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 1rem;
}

#btn-prev,
#btn-next {
  padding: 0.5rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 500;
  border-radius: 6px;
  background: var(--btn-bg);
  color: var(--btn-text);
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  text-decoration: none;
  transition: background 0.15s;
  cursor: pointer;
}
#btn-prev:hover,
#btn-next:hover { background: var(--accent-hover); color: var(--btn-text); text-decoration: none; }
#btn-prev[aria-disabled="true"],
#btn-next[aria-disabled="true"] {
  background: var(--btn-disabled-bg);
  color: var(--btn-disabled-text);
  pointer-events: none;
  cursor: default;
}

#comic-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-width: 0;
  text-align: center;
}
#comic-number { font-size: 0.85rem; color: var(--text-number); font-variant-numeric: tabular-nums; }
#comic-title {
  font-size: 1.1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* ── Comic display ───────────────────────────────────────────────────────── */

#comic-display {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  box-shadow: var(--shadow);
  min-height: 200px;
  position: relative;
}

#comic-image {
  display: block;
  max-width: min(900px, 100%);
  height: auto;
}

#comic-missing {
  padding: 2rem;
  color: var(--text-muted);
  text-align: center;
  font-style: italic;
}

/* ── Spinner ─────────────────────────────────────────────────────────────── */

#comic-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#comic-spinner[hidden] { display: none; }

.spinner-ring {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Buttons (generic) ───────────────────────────────────────────────────── */

button {
  padding: 0.5rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 500;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: var(--btn-bg);
  color: var(--btn-text);
  transition: background 0.15s;
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 40px;
}
button:hover { background: var(--accent-hover); }
button:disabled { background: var(--btn-disabled-bg); color: var(--btn-disabled-text); cursor: not-allowed; }

/* ── Info tabs ───────────────────────────────────────────────────────────── */

#info-tabs { width: 100%; }

#tab-bar { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.tab-btn {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}
.tab-btn:hover { background: var(--accent); color: var(--btn-text); }
.tab-btn--active { background: var(--accent); color: var(--btn-text); }

/* Height is managed by JS so all transitions (open/close/resize) animate */
#tab-panel {
  overflow: hidden;
  height: 0;
  transition: height 0.25s ease;
}

#tab-panel-inner {
  margin-top: 0.75rem;
  padding: 1.25rem;
  background: var(--transcript-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.7;
}

/* Comment tab */
.tab-comment { text-align: center; }

/* Transcript/description markdown content */
#tab-panel-inner p { margin-bottom: 0.6em; }
#tab-panel-inner p:last-child { margin-bottom: 0; }
#tab-panel-inner h3, #tab-panel-inner h4 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-number);
  margin: 0.8em 0 0.3em;
}
#tab-panel-inner strong { color: var(--text); font-weight: 600; }
#tab-panel-inner em { font-style: italic; }
#tab-panel-inner code {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.85em;
  background: var(--border);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}

.transcript-section { margin-bottom: 1rem; }
.transcript-section:last-child { margin-bottom: 0; }

.tab-empty { font-style: italic; }

/* ── Keyboard hints ──────────────────────────────────────────────────────── */

kbd {
  display: inline-block;
  font-size: 0.72em;
  font-family: inherit;
  border: 1px solid currentColor;
  border-radius: 3px;
  padding: 0.05em 0.35em;
  line-height: 1.4;
  opacity: 0.7;
  vertical-align: baseline;
}

@media (max-width: 600px) { kbd { display: none; } }

/* ── Keyboard shortcuts dialog ───────────────────────────────────────────── */

#shortcuts-dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-card);
  color: var(--text);
  padding: 0;
  width: min(460px, calc(100vw - 2rem));
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  /* Centre via margin since <dialog> ignores flexbox/grid on the viewport */
  margin: auto;
}

#shortcuts-dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

#shortcuts-inner {
  padding: 1.5rem;
}

#shortcuts-inner h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1rem;
}

#shortcuts-table {
  width: 100%;
  border-collapse: collapse;
}

#shortcuts-table td {
  padding: 0.4rem 0.5rem;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

#shortcuts-table tr:last-child td { border-bottom: none; }

#shortcuts-table td:first-child {
  white-space: nowrap;
  color: var(--text-muted);
  width: 1%;
  padding-right: 1.25rem;
}

/* Always show kbd inside the dialog, even on mobile */
#shortcuts-dialog kbd { display: inline-block !important; }

#btn-close-shortcuts {
  display: block;
  margin-top: 1.25rem;
  margin-left: auto;
}

/* ── Nav arrow / label visibility ───────────────────────────────────────── */

/* Desktop: hide the plain arrow span, show kbd + label */
.nav-arrow { display: none; }
.nav-label { display: inline; }

/* Desktop: hide shuffle icon, show text label */
.shuffle-icon { display: none; }

/* ── 404 view ─────────────────────────────────────────────────────────────── */

#not-found-main {
  flex: 1;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
  padding: 4rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
}
#not-found-main h1 { font-size: 2rem; }
#not-found-main p { color: var(--text-muted); }

/* ── Index view ──────────────────────────────────────────────────────────── */

#index-main {
  flex: 1;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 1rem;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "heading heading"
    "about   labels"
    "list    list";
  column-gap: 2rem;
  row-gap: 1.5rem;
  align-items: start;
}

#index-main > h1 { grid-area: heading; }
#index-about    { grid-area: about; margin-bottom: 0; min-width: 0; }
#ai-labels      { grid-area: labels; align-self: start; display: flex; flex-direction: column; gap: 1rem; }
#index-list     { grid-area: list; min-width: 0; }

#ai-labels figure     { display: flex; flex-direction: column; align-items: center; gap: 0.35rem; margin: 0; }
#ai-labels figcaption { font-size: 0.78rem; color: var(--text-muted); text-align: center; }
#ai-labels a          { display: block; line-height: 0; }
#ai-labels img        { display: block; width: 104px; height: auto; }

#index-main h1 {
  font-size: 1.5rem;
  margin-bottom: 1rem;
}

#index-about {
  font-size: 0.88rem;
  color: var(--text-muted);
  line-height: 1.75;
  max-width: 680px;
}
#index-about p { margin-bottom: 0.65em; }
#index-about p:last-child { margin-bottom: 0; }
#index-about strong { color: var(--text); font-weight: 600; }

#index-count { font-size: 1rem; color: var(--text-number); font-weight: 400; }

#index-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr));
  gap: 0.5rem;
}

.index-item {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  font-size: 0.9rem;
  color: var(--text);
  text-decoration: none;
  border: 1px solid transparent;
  transition: background 0.1s, border-color 0.1s;
}
.index-item:hover {
  background: var(--bg-card);
  border-color: var(--border);
  text-decoration: none;
  color: var(--text);
}

.index-number {
  font-size: 0.8rem;
  color: var(--text-number);
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
  min-width: 3rem;
}

.index-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Site footer ─────────────────────────────────────────────────────────── */

#site-footer {
  text-align: center;
  padding: 1.25rem 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

/* ── Responsive ──────────────────────────────────────────────────────────── */

@media (max-width: 600px) {
  #site-header { padding: 0 1rem; gap: 0.6rem; }
  #logo-link { flex-shrink: 1; min-width: 0; max-width: 40%; }
  #logo { height: 32px; width: 100%; object-fit: contain; object-position: left center; }
  #nav-index { display: none; }

  /* Random: icon-only on mobile */
  #btn-random { padding: 0 0.65rem; }
  .shuffle-icon { display: block; }
  .random-label { display: none; }

  /* Show plain arrow, hide kbd + label — keeps nav in a single row */
  .nav-arrow { display: inline; }
  .nav-label { display: none; }

  #comic-title { font-size: 1rem; }
  #btn-prev, #btn-next, button { min-height: 44px; }

  #index-list { grid-template-columns: 1fr; }

  #index-main {
    grid-template-columns: 1fr;
    grid-template-areas:
      "heading"
      "about"
      "list"
      "labels";
  }
  #ai-labels { flex-direction: row; justify-content: center; }
}

/* ── Print ───────────────────────────────────────────────────────────────── */

@media print {
  #site-header, #comic-nav, #info-tabs, #site-footer { display: none; }
  #comic-display { border: none; box-shadow: none; padding: 0; }
}
