:root {
  --bg: #fafbfc;
  --panel: #ffffff;
  --line: #e0e3e6;
  --text: #1d1f22;
  --muted: #677;
  --green: #2a7;
  --green-bg: #e8f6ee;
  --grey: #ccc;
  --grey-bg: #f3f4f6;
  --accent: #2962ff;
}
* { box-sizing: border-box; }
body {
  margin: 0; padding: 0;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
/* Header + pagination nav are wrapped in .sticky-top so both pin to the
   viewport top together while the main page scrolls underneath. */
.sticky-top {
  position: sticky;
  top: 0;
  z-index: 100;
  background: white;
  box-shadow: 0 1px 0 var(--line), 0 4px 16px rgba(0,0,0,0.04);
}
header {
  background: white;
  border-bottom: 1px solid var(--line);
  padding: 12px 24px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 16px;
  align-items: center;
}
header .title { font-weight: 700; font-size: 16px; }
header input#search {
  padding: 8px 12px;
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  width: 100%;
  max-width: 480px;
}
header input#search:focus { border-color: var(--accent); }
header .count { color: var(--muted); font-size: 13px; }

nav#nav {
  padding: 10px 24px;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  background: white;
}
nav .pager-btn {
  padding: 4px 10px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
  font: inherit;
  color: var(--text);
  text-decoration: none;
  display: inline-block;
}
nav .pager-btn:hover { background: var(--grey-bg); }
nav .pager-btn.disabled { opacity: 0.4; pointer-events: none; }
nav .pager-current { color: var(--muted); padding: 0 8px; }

main { padding: 16px 24px 80px; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* Grid index page */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}
.grid a.thumb {
  display: block;
  text-decoration: none;
  color: inherit;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: white;
  overflow: hidden;
  transition: transform .12s, box-shadow .12s;
}
.grid a.thumb:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  border-color: var(--accent);
}
.grid a.thumb img { width: 100%; aspect-ratio: 3 / 2; object-fit: cover; display: block; background: var(--grey-bg); }
.grid a.thumb .name {
  padding: 6px 8px;
  font-family: monospace;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Source-detail layout (lives inside the modal). Two-column grid: source
   image on the left, phase-rows stacked vertically on the right with
   wrap-grids of large thumbs (no hover-zoom — they're big enough to read). */
.source {
  display: grid;
  grid-template-columns: minmax(360px, 480px) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}
.source-info {
  position: sticky;
  top: 0;
  /* Stickies the source image to the top of its column while phases scroll. */
}
.source-info img {
  width: 100%;
  border-radius: 4px;
  border: 1px solid var(--line);
  display: block;
  background: var(--grey-bg);
  aspect-ratio: 3 / 2;
  object-fit: cover;
}

.phases {
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.phase-row { display: block; }
.phase-label {
  width: auto;
  padding: 0;
  margin-bottom: 8px;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  display: flex;
  align-items: baseline;
  gap: 10px;
}
.phase-label .stat {
  font-weight: 400;
  font-size: 12px;
  color: var(--muted);
}
.candidates {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 12px;
  padding: 0;
  overflow: visible;
}
.candidate {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 4px;
  border: 2px solid var(--grey);
  background: white;
  border-radius: 4px;
  font-family: monospace;
  font-size: 11px;
  color: #333;
  text-decoration: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
  position: relative;
}
.candidate:hover {
  border-color: var(--accent);
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  transform: translateY(-1px);
}
.candidate img {
  width: 100%;
  aspect-ratio: 3 / 2;
  height: auto;
  object-fit: cover;
  border-radius: 2px;
  background: var(--grey-bg);
  display: block;
}
.candidate .meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 2px 0;
  gap: 6px;
}
.candidate .rank {
  font-weight: 700;
  color: var(--text);
}
.candidate .filename {
  flex: 1;
  font-size: 10px;
  color: #555;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}
.candidate .score { color: var(--muted); font-size: 10px; }
/* Keyboard-active highlight (no scale — just an accent ring) */
.candidate.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(41, 98, 255, 0.18), 0 6px 18px rgba(0,0,0,0.14);
}

.kb-hint {
  position: fixed; bottom: 16px; right: 16px;
  padding: 6px 12px;
  background: rgba(0,0,0,0.78);
  color: white;
  font-size: 12px;
  border-radius: 4px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
  z-index: 9999;
}
body.kb-mode .kb-hint { opacity: 1; }

/* Stack source above phases on narrow viewports (≤ 1100 px) */
@media (max-width: 1100px) {
  .source { grid-template-columns: 1fr; }
  .source-info { position: static; }
  .source-info img { max-width: 720px; }
}

/* Casual password gate (only rendered when --password was set at build time).
   Baked sha256 in app.js — anyone with View Source can bypass; this is a
   "no random visitor stumbles in" gate, not real security. */
body.gated #app { display: none; }
body.unlocked #app { display: block; }
body.unlocked #gate { display: none; }
.gate {
  position: fixed; inset: 0;
  background: linear-gradient(160deg, #1a1d22 0%, #2b313a 100%);
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.gate-card {
  background: white;
  border-radius: 10px;
  padding: 28px 32px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}
.gate-title {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
}
.gate-sub {
  font-size: 13px;
  color: var(--muted);
}
#gate-input {
  padding: 10px 12px;
  font: inherit;
  font-size: 15px;
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  margin-top: 4px;
}
#gate-input:focus { border-color: var(--accent); }
.gate-btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 10px 16px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}
.gate-btn:hover { filter: brightness(1.05); }
.gate-error {
  color: #c33;
  font-size: 13px;
  margin-top: -4px;
}
.gate-card.shake { animation: gate-shake 0.4s ease; }
@keyframes gate-shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

/* Search dropdown */
.search-results {
  position: absolute;
  top: 52px;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  border: 1px solid var(--line);
  border-radius: 4px;
  max-width: 480px;
  width: 100%;
  max-height: 300px;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  z-index: 200;
}
.search-results a {
  display: block;
  padding: 6px 12px;
  text-decoration: none;
  color: var(--text);
  font-family: monospace;
  font-size: 13px;
}
.search-results a:hover, .search-results a.active { background: var(--grey-bg); }

@media (max-width: 720px) {
  .source { grid-template-columns: 1fr; }
  .source-info img { max-width: 100%; }
}

/* Modal overlay */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 18, 22, 0.55);
  z-index: 500;
  cursor: pointer;
}
.modal-backdrop[hidden] { display: none; }
body.modal-open { overflow: hidden; }
.modal {
  position: fixed;
  top: 32px; left: 32px; right: 32px; bottom: 32px;
  background: white;
  border-radius: 8px;
  z-index: 501;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 16px 48px rgba(0,0,0,0.3);
}
.modal[hidden] { display: none; }
.modal-header {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: white;
  flex-shrink: 0;
}
.modal-header .filename {
  font-family: monospace;
  font-weight: 600;
  font-size: 14px;
}
.modal-header .modal-nav {
  display: flex;
  gap: 6px;
  align-items: center;
}
.modal-header button.modal-close {
  background: none;
  border: 1px solid var(--line);
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  color: var(--text);
}
.modal-header button.modal-close:hover { background: var(--grey-bg); }
.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px 24px;
}
/* Source-block inside modal: same shape as before but no outer border (modal already provides it). */
.modal-body .source { margin-bottom: 0; border: none; padding: 0; }

@media (max-width: 720px) {
  .modal { top: 0; left: 0; right: 0; bottom: 0; border-radius: 0; }
}
