* {
  box-sizing: border-box;
}

:root {
  color-scheme: light;
  --ink: #132b2a;
  --line: #c8cbc2;
  --gold: #dfbc70;
}

html,
body {
  height: 100%;
  margin: 0;
  background: #263837;
  color: var(--ink);
  font: 16px/1.65 Arial, sans-serif;
}

body {
  overflow: hidden;
}

.reader {
  display: grid;
  grid-template-columns: 1fr min(33.333vw, max(320px, calc(100vw - 100dvh * var(--page-ratio, 0.7335))));
  height: 100dvh;
  width: 100%;
  direction: ltr;
  background: #fff;
}

.canvas {
  min-width: 0;
  height: 100%;
  background: #263837;
  color: white;
  display: flex;
  flex-direction: column;
}

#viewport {
  flex: 1;
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  background: #283b39;
  direction: ltr;
  touch-action: pan-y;
}

#scan {
  position: absolute;
  left: 0;
  top: 0;
  max-width: none;
  transform-origin: 0 0;
  transition: transform 0.48s ease;
  width: auto;
  height: auto;
}

#scanStatus {
  position: absolute;
  inset: auto 12px 10px;
  padding: 6px 12px;
  border-radius: 4px;
  text-align: center;
  color: white;
  background: rgba(27, 48, 47, 0.92);
}

#scanStatus:empty {
  display: none;
}

.reader aside {
  direction: rtl;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: #f8f8f3;
  border-right: 1px solid var(--line);
}

.sidebar-head {
  background: var(--ink);
  color: white;
  padding: 10px 16px 12px;
  flex-shrink: 0;
}

.sidebar-head nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding-bottom: 8px;
  margin-bottom: 8px;
  border-bottom: 1px solid #45605d;
  font-size: 0.88rem;
}

.sidebar-head nav a {
  white-space: nowrap;
  text-decoration: none;
  color: #d4ded6;
  padding: 5px 10px;
  border-radius: 4px;
  border-bottom: 2px solid transparent;
}

.sidebar-head nav a[aria-current] {
  border-color: var(--gold);
  color: #ffe4a9;
  background: rgba(255, 255, 255, 0.06);
}

.pub-meta h1,
.pub-meta p,
h2,
.section-label {
  margin: 0;
}

.pub-meta h1,
h2 {
  font-family: Georgia, "Times New Roman", serif;
}

.pub-meta h1 {
  font-size: 1.45rem;
  line-height: 1.25;
}

.pub-meta p {
  font-size: 0.85rem;
  color: #d4ded6;
  margin-top: 2px;
}

.sidebar-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 12px 16px 14px;
  gap: 8px;
}

.sidebar-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
  font-size: 0.86rem;
  flex-shrink: 0;
}

#position {
  color: #526661;
  font-weight: 600;
}

.sidebar-tools button {
  background: #fff;
  border-color: #7d9690;
  font-size: 0.85rem;
  padding: 5px 10px;
}

.sidebar-tools button[aria-pressed="true"] {
  background: var(--ink);
  color: white;
  border-color: var(--ink);
}

.section-label {
  font-size: 0.85rem;
  color: #526661;
  flex-shrink: 0;
}

#section-list {
  display: grid;
  gap: 4px;
  max-height: 22vh;
  overflow-y: auto;
  flex-shrink: 0;
  padding-left: 2px;
}

#section-list button {
  text-align: right;
  background: #fff;
  font-size: 0.88rem;
  padding: 6px 10px;
}

#section-list button[aria-current="true"] {
  background: var(--ink);
  color: white;
  border-color: var(--ink);
}

article {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px 14px;
}

article p {
  white-space: pre-line;
  line-height: 1.9;
  margin: 8px 0 0;
}

h2 {
  font-size: 1.25rem;
  line-height: 1.4;
}

button {
  font: inherit;
  color: inherit;
  padding: 7px 13px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.42;
  cursor: default;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  padding-top: 6px;
  flex-shrink: 0;
}

.controls button {
  flex: 1;
}

.controls button:last-child {
  background: var(--ink);
  color: white;
  border-color: var(--ink);
}

:focus-visible {
  outline: 3px solid #cb872d;
  outline-offset: -3px;
}

[hidden] {
  display: none !important;
}

#highlight {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  background: rgba(255, 213, 74, 0.2);
  border: 2px solid rgba(233, 184, 61, 0.9);
  border-radius: 5px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.4), 0 0 16px rgba(255, 213, 74, 0.35);
  transition: all 0.48s ease;
}

#highlight.pulse {
  animation: mark-pulse 1.1s ease-out 0.35s;
}

@keyframes mark-pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(233, 184, 61, 0.7), 0 0 16px rgba(255, 213, 74, 0.35);
  }
  100% {
    box-shadow: 0 0 0 16px rgba(233, 184, 61, 0), 0 0 16px rgba(255, 213, 74, 0.35);
  }
}

#minimap {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 3;
  border: 2px solid rgba(255, 255, 255, 0.88);
  border-radius: 4px;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  overflow: hidden;
  opacity: 0.92;
  transition: opacity 0.2s;
}

#minimap:hover {
  opacity: 1;
}

#minimap img {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
  user-select: none;
}

#minimap .view {
  position: absolute;
  pointer-events: none;
  border: 2px solid #e9b83d;
  background: rgba(233, 184, 61, 0.14);
  transition: all 0.48s ease;
}

#minimap .dot {
  position: absolute;
  pointer-events: none;
  min-width: 5px;
  min-height: 3px;
  background: #d9382b;
  border-radius: 2px;
  transition: all 0.48s ease;
}

#progress {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  height: 3px;
  width: 0;
  background: var(--gold);
  pointer-events: none;
  transition: width 0.48s ease;
}

@media (max-width: 820px) {
  body {
    overflow: auto;
  }
  .reader {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100dvh;
  }
  .canvas {
    height: 52dvh;
    min-height: 280px;
  }
  .reader aside {
    height: auto;
    border-right: none;
    border-top: 1px solid var(--line);
  }
  #section-list {
    max-height: 22vh;
  }
  article {
    overflow: visible;
  }
  #minimap {
    left: 6px;
    bottom: 6px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #scan,
  #highlight,
  #minimap .view,
  #minimap .dot,
  #progress {
    transition: none;
  }
  #highlight.pulse {
    animation: none;
  }
}
