:root {
  --paper: #f3eee4;
  --paper-dark: #e8e0d0;
  --ink: #2b2622;
  --red: #6d1b1b;
  --red-light: #8a2b2b;
  --gold: #c9a227;
  --line: #d8cfbd;
  --header-h: 56px;
  --footer-h: 52px;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--paper);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- header ---------- */
header {
  height: var(--header-h);
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px;
  background: linear-gradient(180deg, var(--red-light), var(--red));
  color: #f7f1e3;
  box-shadow: 0 2px 6px rgba(0,0,0,.25);
  z-index: 30;
}
header h1 {
  margin: 0;
  font-family: "Times New Roman", Georgia, "Liberation Serif", serif;
  font-size: 20px;
  font-weight: 600;
  white-space: nowrap;
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
header h1 a { color: inherit; text-decoration: none; }
header h1 .sub { font-size: 12px; opacity: .8; font-weight: normal; font-style: italic; }
header button {
  flex: none;
  background: rgba(255,255,255,.12);
  color: inherit;
  border: 1px solid rgba(255,255,255,.25);
  border-radius: 6px;
  font-size: 17px;
  line-height: 1;
  padding: 7px 11px;
  cursor: pointer;
}
header button:hover { background: rgba(255,255,255,.22); }
#about { font-family: "Times New Roman", Georgia, serif; font-style: italic; font-weight: bold; }
.searchbox { position: relative; flex: 1; max-width: 560px; margin-left: auto; }
.searchbox input {
  width: 100%;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,.3);
  background: rgba(255,255,255,.95);
  color: var(--ink);
  font-size: 14px;
  outline: none;
}
.searchbox input:focus { box-shadow: 0 0 0 3px rgba(201,162,39,.45); }

.results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  max-height: min(70vh, 560px);
  overflow-y: auto;
  background: #fffdf8;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.28);
  z-index: 100;
}
.results .count {
  padding: 8px 12px;
  font-size: 12px;
  color: #7a7060;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: #fffdf8;
}
.results a {
  display: block;
  padding: 9px 12px;
  text-decoration: none;
  color: var(--ink);
  border-bottom: 1px solid #f0ead9;
  font-size: 13.5px;
  line-height: 1.45;
}
.results a:hover, .results a.sel { background: #f6efdd; }
.results a .pg {
  display: inline-block;
  min-width: 58px;
  font-weight: 700;
  color: var(--red);
  font-size: 12.5px;
}
.results mark { background: #ffe9a8; padding: 0 1px; border-radius: 2px; }
.results .none { padding: 14px; color: #7a7060; font-size: 13px; }

/* ---------- layout ---------- */
main { flex: 1; display: flex; min-height: 0; position: relative; }

/* ---------- sidebar ---------- */
#sidebar {
  width: 232px;
  flex: none;
  border-right: 1px solid var(--line);
  background: var(--paper-dark);
  overflow-y: auto;
  overflow-x: hidden;
}
#thumbs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  padding: 10px;
}
.thumb {
  display: block;
  position: relative;
  border: 2px solid transparent;
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
  text-decoration: none;
}
.thumb img { display: block; width: 100%; height: auto; background: #f7f4ee; min-height: 60px; }
.thumb span {
  position: absolute;
  bottom: 0; right: 0;
  background: rgba(43,38,34,.78);
  color: #fff;
  font-size: 10.5px;
  padding: 1px 5px;
  border-top-left-radius: 5px;
}
.thumb.active { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(201,162,39,.5); }

/* ---------- viewer ---------- */
#viewer {
  flex: 1;
  overflow: auto;
  padding: 18px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background:
    radial-gradient(circle at 50% 0%, rgba(255,255,255,.6), transparent 60%),
    var(--paper);
}
#page {
  display: block;
  margin: 0 auto;
  background: #fff;
  box-shadow: 0 3px 18px rgba(0,0,0,.25);
  cursor: zoom-in;
}
#page.fit { max-width: 100%; max-height: calc(100vh - var(--header-h) - var(--footer-h) - 40px); width: auto; height: auto; cursor: zoom-out; }
#page:not([src]) { display: none; }

#textview {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  background: #fffdf8;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 3px 18px rgba(0,0,0,.18);
  font-family: "Times New Roman", Georgia, "Liberation Serif", serif;
  font-size: 17px;
  line-height: 1.8;
}
#textview article { padding: 26px 30px 40px; }
#textview h3 { color: var(--red); font-size: 19px; margin: 1.6em 0 .6em; }
#textview p { margin: 0 0 .85em; text-align: justify; }
#textview p.entry { padding-left: 1.7em; text-indent: -1.7em; }
#textview mark { background: #ffe9a8; }
#textview .pagemark { color: #9a9080; font-size: 12px; font-style: italic; }
body.textmode .zoom { display: none; }
#loading {
  position: fixed;
  top: calc(var(--header-h) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(43,38,34,.85);
  color: #fff;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 20px;
  z-index: 20;
}
.hidden { display: none !important; }

/* ---------- footer ---------- */
footer {
  height: var(--footer-h);
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 0 12px;
  border-top: 1px solid var(--line);
  background: var(--paper-dark);
  z-index: 20;
  font-size: 14px;
}
footer button {
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fffdf8;
  color: var(--ink);
  font-size: 14px;
  cursor: pointer;
}
footer button:hover { background: #fff; border-color: var(--gold); }
.pager { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.pager input {
  width: 66px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  text-align: center;
  background: #fffdf8;
}
.zoom { display: flex; align-items: center; gap: 6px; margin-left: 8px; }
.zoom button { padding: 7px 11px; }
.zoomlabel { font-size: 12px; color: #7a7060; min-width: 38px; text-align: center; }

/* ---------- dialog ---------- */
dialog {
  border: none;
  border-radius: 12px;
  padding: 22px 26px;
  max-width: 560px;
  background: #fffdf8;
  color: var(--ink);
  box-shadow: 0 12px 48px rgba(0,0,0,.4);
  font-size: 14px;
  line-height: 1.6;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin: 0 0 8px; font-family: "Times New Roman", Georgia, serif; color: var(--red); }
dialog a { color: var(--red); }
dialog form { text-align: right; margin-top: 14px; }
dialog button { padding: 7px 18px; border-radius: 8px; border: 1px solid var(--line); background: var(--paper-dark); cursor: pointer; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  header h1 { font-size: 16px; }
  header h1 .sub { display: none; }
  #sidebar {
    position: absolute;
    top: 0; bottom: 0; left: 0;
    z-index: 40;
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 4px 0 18px rgba(0,0,0,.3);
    width: 240px;
  }
  #sidebar.open { transform: translateX(0); }
  #viewer { padding: 8px; }
  #page.fit { max-height: calc(100vh - var(--header-h) - var(--footer-h) - 16px); }
  footer { gap: 6px; padding: 0 6px; }
  footer button { padding: 7px 9px; font-size: 13px; }
  .pager input { width: 52px; }
  .zoomlabel { display: none; }
  .zoom { margin-left: 0; }
}
