:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --border: #d9d7d2;
  --accent: #d9480f;
  --hover: #fff1e8;
  --mark: #ffd8a8;
}
@media (prefers-color-scheme: dark) {
  /* The logo's dark grey lettering needs a light backdrop in dark mode. */
  .logo { background: #f6f5f2; border-radius: 6px; padding: 4px 8px; }
  :root {
    --bg: #17171a;
    --surface: #222226;
    --text: #ececef;
    --muted: #9a9aa2;
    --border: #3a3a40;
    --accent: #ff8a3d;
    --hover: #3a2a20;
    --mark: #6b3d14;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

header {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  /* Side columns of equal width keep the search field centered next to the logo. */
  display: grid;
  grid-template-columns: 1fr minmax(0, 720px) 1fr;
  column-gap: 16px;
  align-items: center;
}
.logo {
  grid-column: 1;
  grid-row: 1 / span 2;
  justify-self: start;
  width: auto;
  height: 52px;
}
.search { position: relative; grid-column: 2; }
#q {
  width: 100%;
  padding: 12px 16px;
  font-size: 18px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
#q:focus { border-color: var(--accent); }
#status { grid-column: 2; margin: 6px 0 0; color: var(--muted); font-size: 13px; }

#results {
  position: absolute;
  left: 0; right: 0; top: calc(100% + 4px);
  max-height: 70vh;
  overflow-y: auto;
  margin: 0; padding: 4px 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.15);
}
#results li { padding: 8px 16px; cursor: pointer; }
#results li[aria-selected="true"], #results li:hover { background: var(--hover); }
#results .title { font-weight: 600; }
#results .byline { font-size: 14px; }
#results .meta { font-size: 13px; color: var(--muted); }
.uncertain {
  display: inline-grid; place-items: center;
  width: 18px; height: 18px; margin-left: 6px;
  border-radius: 50%;
  background: var(--border); color: var(--muted);
  font-size: 12px; font-weight: 700; vertical-align: 2px;
  cursor: help;
}
.book-title { font-size: 16px; font-weight: 600; color: var(--text); }
figcaption a { color: var(--accent); }
#results .publisher {
  display: inline-block;
  margin-right: 6px;
  padding: 0 6px;
  border-radius: 4px;
  background: var(--border);
  color: var(--text);
}
#results .empty { color: var(--muted); cursor: default; }
mark { background: var(--mark); color: inherit; border-radius: 2px; }

main {
  display: grid;
  grid-template-columns: 1fr 3fr 1fr;
  gap: 16px;
  align-items: start;
  padding: 16px;
}
figure { margin: 0; }
/* The frame must hug the image exactly, otherwise the SVG overlay coordinates drift. */
.frame {
  position: relative;
  width: fit-content;
  margin: 0 auto;
  line-height: 0;
  overflow: hidden;
  border-radius: 6px;
  cursor: zoom-in;
  touch-action: pan-y;
  user-select: none;
}
.frame.zoomed { cursor: grab; touch-action: none; }
.frame.dragging { cursor: grabbing; }
.stage { position: relative; transform-origin: 0 0; }
.stage.animate { transition: transform 0.25s ease-out; }

.zoom-controls {
  position: absolute;
  right: 8px;
  bottom: 8px;
  display: flex;
  gap: 4px;
  line-height: 1;
}
.zoom-controls button {
  width: 36px;
  height: 36px;
  font-size: 20px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 2px 6px rgb(0 0 0 / 0.2);
  cursor: pointer;
  opacity: 0.9;
}
.zoom-controls button:hover { opacity: 1; border-color: var(--accent); }
.zoom-controls button:disabled { opacity: 0.4; cursor: default; border-color: var(--border); }
.side img { width: 100%; height: auto; border-radius: 6px; }
.current .frame img { display: block; width: auto; max-width: 100%; max-height: calc(100vh - 150px); }
.current figcaption { text-align: center; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#overlay polygon { fill: none; stroke: var(--accent); stroke-width: calc(1px / var(--zoom, 1)); stroke-dasharray: calc(4px / var(--zoom, 1)) calc(3px / var(--zoom, 1)); vector-effect: non-scaling-stroke; }
#overlay .dim { fill: rgb(0 0 0 / 0.55); }
#overlay rect {
  fill: none;
  stroke: var(--accent);
  stroke-width: calc(3px / var(--zoom, 1));
  vector-effect: non-scaling-stroke;
  animation: pulse 1.2s ease-in-out 3;
}
@keyframes pulse { 50% { stroke-width: calc(7px / var(--zoom, 1)); } }

figcaption { margin-top: 8px; font-size: 13px; color: var(--muted); line-height: 1.4; }
.side { cursor: pointer; opacity: 0.75; transition: opacity 0.15s; }
.side:hover { opacity: 1; }
.side.empty { cursor: default; opacity: 1; }
.side .none {
  display: grid; place-items: center;
  aspect-ratio: 9 / 16;
  border: 1px dashed var(--border); border-radius: 6px;
  color: var(--muted); font-size: 13px; line-height: 1.4; text-align: center;
}

@media (max-width: 1100px) {
  header { grid-template-columns: auto minmax(0, 1fr); }
  .logo { height: 44px; }
}
@media (max-width: 760px) {
  header { grid-template-columns: minmax(0, 1fr); }
  .logo { grid-row: auto; grid-column: 1; height: 36px; margin-bottom: 10px; }
  .search, #status { grid-column: 1; }
  main { grid-template-columns: 1fr 1fr; }
  .current { grid-column: 1 / -1; order: -1; }
}
