/* ============================================================
   Hansei Ichiran source-page viewer — "archival" theme
   Deep-zoom (OpenSeadragon) page + linked extracted-data table.
   ============================================================ */

:root {
  /* paper + ink */
  --paper:        #f4ecd8;   /* aged washi background */
  --paper-2:      #faf4e6;   /* card / pane paper */
  --paper-3:      #fffdf6;   /* table surface */
  --ink:          #2a2520;   /* warm near-black text */
  --ink-soft:     #6a5f4f;   /* secondary text */
  --ink-faint:    #9a8d76;   /* tertiary / disabled */
  --rule:         #d9c8a4;   /* tan hairlines */
  --rule-soft:    #e7dcc2;
  /* accents */
  --cinnabar:     #a8362a;   /* 朱 seal-ink red — markers, active, value */
  --cinnabar-br:  #c8472f;   /* brighter cinnabar (hover) */
  --cinnabar-gl:  rgba(190, 70, 45, 0.55);
  --gold:         #9a7b1f;   /* ochre/gold secondary */
  --ink-surface:  #211c17;   /* dark warm reading surface (page pane) */
  --ink-surface2: #2c251d;
  /* fonts */
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --mincho: "Yu Mincho", "YuMincho", "Hiragino Mincho ProN", "Hiragino Mincho Pro", "Noto Serif JP", "MS Mincho", serif;
  --gothic: "Hiragino Sans", "Yu Gothic", "Noto Sans JP", "Segoe UI", sans-serif;
  --mono: "SFMono-Regular", "Consolas", "Liberation Mono", monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--serif);
  color: var(--ink);
  background:
    radial-gradient(120% 80% at 50% -10%, #fbf5e6 0%, var(--paper) 55%, #ecdfc2 100%);
  overflow: hidden;
}

/* ---------------- Header ---------------- */
.viewer-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 22px;
  background: linear-gradient(180deg, #36302a 0%, #2b2620 100%);
  color: #f0e6d2;
  border-bottom: 3px solid var(--cinnabar);
  box-shadow: 0 2px 10px rgba(0,0,0,0.25);
  height: 60px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  font-family: var(--mincho);
  font-size: 30px; line-height: 1;
  width: 42px; height: 42px; flex: 0 0 42px;
  display: flex; align-items: center; justify-content: center;
  color: #fff; background: var(--cinnabar);
  border-radius: 5px;
  box-shadow: inset 0 0 0 1.5px rgba(255,255,255,0.25), 0 1px 4px rgba(0,0,0,0.4);
}
.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0; font-size: 19px; font-weight: 600; letter-spacing: 0.01em;
  font-family: var(--serif); color: #fdf6e6;
}
.brand-year { color: var(--cinnabar-br); font-variant-numeric: oldstyle-nums; font-style: italic; }
.brand-sub { margin: 1px 0 0; font-size: 12px; color: #c8b89a; font-style: italic; }

/* language toggle */
.lang-toggle { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.lang-toggle-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em;
  color: #b6a585; margin-right: 2px;
}
.lang-btn {
  font-family: var(--mincho); font-size: 14px;
  padding: 5px 11px; cursor: pointer;
  background: rgba(255,255,255,0.06); color: #e8dcc4;
  border: 1px solid rgba(255,255,255,0.18); border-radius: 4px;
  transition: background .15s, color .15s, border-color .15s;
}
.lang-btn small { font-family: var(--gothic); font-size: 9px; opacity: 0.7; }
.lang-btn:hover { background: rgba(255,255,255,0.14); }
.lang-btn.is-active {
  background: var(--cinnabar); color: #fff; border-color: var(--cinnabar-br);
  box-shadow: 0 1px 5px rgba(168,54,42,0.5);
}

/* ---------------- Page nav ---------------- */
.page-nav {
  display: flex; align-items: center; gap: 12px;
  padding: 7px 22px; background: var(--paper-2);
  border-bottom: 1px solid var(--rule);
  height: 46px;
}
.nav-arrow {
  font-size: 20px; line-height: 1; width: 32px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  background: #fff; color: var(--cinnabar);
  border: 1px solid var(--rule); border-radius: 4px; cursor: pointer;
  transition: background .12s, color .12s, transform .08s;
}
.nav-arrow:hover:not(:disabled) { background: var(--cinnabar); color: #fff; }
.nav-arrow:active:not(:disabled) { transform: translateY(1px); }
.nav-arrow:disabled { color: var(--ink-faint); cursor: not-allowed; opacity: 0.5; }
.page-nav select {
  /* Fills the row between the arrows and the counter and truncates its option
     text with an ellipsis. Width depends only on the window, never on the page's
     text — so the whole nav bar is identical on every page and never overflows. */
  flex: 0 1 520px; min-width: 0;
  padding: 5px 9px; font-size: 13.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: var(--serif); color: var(--ink);
  background: #fff; border: 1px solid var(--rule); border-radius: 4px;
}
/* current-domain anchor filling the right side of the nav — layout-safe: shrinks and the
   romaji ellipsizes, so it never overflows or pushes the counter off. */
.nav-context {
  margin-left: auto; flex: 0 1 auto; min-width: 0;
  display: flex; align-items: baseline; gap: 8px;
  overflow: hidden; white-space: nowrap;
}
.nav-domain { font-family: var(--mincho); font-size: 16px; font-weight: 600; color: var(--cinnabar); flex: 0 0 auto; }
.nav-domain-en { font-family: var(--serif); font-size: 13px; font-style: italic; color: var(--ink-soft); overflow: hidden; text-overflow: ellipsis; }
/* stable "N / 182" position counter, pinned far right — fixed content, never resizes the bar */
.page-count {
  flex: 0 0 auto; margin-left: 14px;
  font-family: var(--serif); font-size: 12.5px; color: var(--ink-faint);
  font-variant-numeric: tabular-nums; letter-spacing: 0.03em; white-space: nowrap;
}

/* ---------------- Main split ---------------- */
.viewer-main { display: flex; height: calc(100vh - 106px); }

/* LEFT — deep-zoom page */
.page-pane {
  flex: 1 1 52%; position: relative;
  background:
    repeating-linear-gradient(0deg, rgba(255,255,255,0.015) 0 2px, transparent 2px 4px),
    radial-gradient(130% 100% at 50% 0%, var(--ink-surface2) 0%, var(--ink-surface) 70%);
  border-right: 1px solid #000;
  min-width: 0;
}
.osd-canvas { position: absolute; inset: 0; }
/* OSD draws the page on a canvas; give the image a parchment frame feel */
.openseadragon-canvas { outline: none; }
.osd-canvas .openseadragon-canvas canvas { image-rendering: auto; }

/* annotation overlays (positioned by OSD in image coords) */
.hl-box {
  border: 1.5px solid var(--cinnabar-gl);
  background: rgba(200, 90, 50, 0.10);
  border-radius: 2px;
  cursor: pointer; pointer-events: auto;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.hl-box:hover, .hl-box.active {
  background: rgba(210, 80, 45, 0.30);
  border-color: var(--cinnabar-br);
  box-shadow: 0 0 0 1px rgba(255,240,210,0.5), 0 0 16px var(--cinnabar-gl);
}
.hl-box.is-marker {           /* the ○domain markers get a distinct cue */
  border-style: dashed; border-color: rgba(230,180,90,0.7);
  background: rgba(220,170,70,0.10);
}
.hl-box.is-marker:hover, .hl-box.is-marker.active {
  background: rgba(220,170,70,0.28); border-color: var(--gold);
  box-shadow: 0 0 0 1px rgba(255,240,210,0.5), 0 0 16px rgba(220,170,70,0.6);
}

/* custom zoom controls */
.osd-controls {
  position: absolute; top: 12px; left: 12px; z-index: 6;
  display: flex; flex-direction: column; gap: 5px;
}
.osd-controls button {
  width: 34px; height: 34px; font-size: 17px; line-height: 1;
  background: rgba(34,28,23,0.78); color: #f0e6d2;
  border: 1px solid rgba(240,230,210,0.25); border-radius: 5px;
  cursor: pointer; backdrop-filter: blur(2px);
  transition: background .12s;
}
.osd-controls button:hover { background: var(--cinnabar); border-color: var(--cinnabar-br); }

/* floating tooltip over an annotation */
.osd-tip {
  position: absolute; z-index: 8; max-width: 320px;
  padding: 8px 11px; font-size: 13px; line-height: 1.45;
  background: rgba(28,23,18,0.96); color: #f3ead5;
  border: 1px solid var(--cinnabar); border-radius: 5px;
  box-shadow: 0 6px 22px rgba(0,0,0,0.5);
  pointer-events: none;
}
.osd-tip .tip-kanji { font-family: var(--mincho); font-size: 15px; color: #fff; }
.osd-tip .tip-en { font-family: var(--serif); }
.osd-tip .tip-val { display:block; margin-top: 4px; color: var(--cinnabar-br); font-family: var(--mono); font-size: 12px; }

.image-meta {
  position: absolute; bottom: 8px; right: 12px; z-index: 6;
  color: #c8b89a; opacity: 0.75;
  font-family: var(--mono); font-size: 10.5px; text-align: right;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
}

/* RIGHT — data pane */
.data-pane {
  flex: 1 1 48%; max-width: 620px;
  overflow-y: auto; padding: 16px 20px 40px;
  background: var(--paper-2);
}
.card {
  background: var(--paper-3); border: 1px solid var(--rule);
  border-left: 4px solid var(--cinnabar);
  border-radius: 4px; padding: 12px 15px;
  box-shadow: 0 1px 3px rgba(120,90,40,0.10);
}
.page-summary { margin-bottom: 18px; font-size: 13.5px; line-height: 1.6; }
.page-summary h3 {
  margin: 0 0 6px; font-size: 13px; font-weight: 700;
  color: var(--cinnabar); letter-spacing: 0.02em;
  font-family: var(--serif); text-transform: uppercase;
}
.summary-title {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 6px 14px; flex-wrap: wrap; margin-bottom: 2px;
}
.summary-domain { font-family: var(--mincho); font-size: 19px; font-weight: 600; color: var(--cinnabar); line-height: 1.25; }
.summary-domain-en { font-family: var(--serif); font-size: 14px; font-weight: 600; color: var(--ink-soft); font-style: italic; }
.summary-bookpage {
  font-variant-numeric: oldstyle-nums; font-style: italic;
  color: var(--ink-faint); font-size: 12.5px; white-space: nowrap; flex: 0 0 auto;
}
.summary-topic { font-family: var(--serif); font-size: 12.5px; color: var(--ink-soft); font-style: italic; margin-bottom: 9px; }
.page-summary .summary-kanji {
  font-family: var(--mincho); font-size: 16px; color: var(--ink);
  display: block; margin-bottom: 2px;
}
.page-summary .summary-en-head { color: var(--ink-soft); font-style: italic; }
.page-summary p { margin: 6px 0 0; color: var(--ink); }

.data-head h2 {
  margin: 0 0 4px; font-size: 16px; font-weight: 700; color: var(--ink);
  font-family: var(--serif); letter-spacing: 0.01em;
}
.data-head h2::after {
  content: ""; display: block; width: 46px; height: 2px;
  background: var(--cinnabar); margin-top: 5px;
}
.data-help { font-size: 11.5px; color: var(--ink-soft); margin: 8px 0 12px; line-height: 1.55; }
.data-help strong { color: var(--cinnabar); }
.data-help .muted { color: var(--ink-faint); font-style: italic; }
.dot-anno {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--cinnabar); vertical-align: middle;
}

/* extract table */
.extract-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.extract-table thead th {
  text-align: left; padding: 7px 9px;
  background: linear-gradient(180deg, #efe4cc, #e7dabd);
  border-bottom: 2px solid var(--gold);
  font-family: var(--serif); font-weight: 700; font-size: 11.5px;
  text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft);
  white-space: nowrap; position: sticky; top: -16px; z-index: 5;
}
.extract-table th.num { text-align: right; }
.extract-table td { padding: 7px 9px; border-bottom: 1px solid var(--rule-soft); vertical-align: middle; }
.extract-table tbody tr { transition: background .1s; }
.extract-table tr.has-anno { cursor: pointer; }
.extract-table tr.has-anno td:first-child {
  border-left: 3px solid transparent;
}
.extract-table tr:not(.has-anno) td { color: var(--ink-faint); }
.extract-table tr:hover { background: #fbf2dc; }
.extract-table tr.active {
  background: #f7e7c8;
}
.extract-table tr.active td:first-child { border-left-color: var(--cinnabar); }
.extract-table tr.active td { color: var(--ink); }

.extract-table .field-jp {
  font-family: var(--mincho); font-size: 16px; white-space: nowrap;
  line-height: 1.7; padding-top: 9px; color: var(--ink);
}
.extract-table .field-jp ruby { ruby-position: over; ruby-align: center; }
.extract-table .field-jp rt {
  font-family: var(--gothic); font-size: 9px; color: var(--ink-soft);
  font-weight: normal; line-height: 1.1;
}
.extract-table .field-en {
  font-size: 12px; color: var(--ink-soft); line-height: 1.4;
  font-style: italic; max-width: 250px; white-space: normal;
}
.extract-table .value {
  font-family: var(--mono); text-align: right; white-space: nowrap;
  font-size: 13px; font-weight: 700; color: var(--cinnabar);
}
.extract-table .unit { color: var(--ink-faint); font-size: 11px; white-space: nowrap; font-style: italic; }

/* ---------------- Detail modal ---------------- */
.detail-modal {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(30, 24, 18, 0.74);
  display: flex; align-items: center; justify-content: center; padding: 30px;
  backdrop-filter: blur(2px);
}
.detail-modal-inner {
  background: var(--paper-3); border-radius: 6px;
  border-top: 4px solid var(--cinnabar);
  max-width: 900px; width: 100%; max-height: 88vh; overflow-y: auto;
  position: relative; padding: 26px 30px;
  box-shadow: 0 18px 60px rgba(0,0,0,0.5);
}
.detail-close {
  position: absolute; top: 8px; right: 14px;
  background: transparent; border: none; font-size: 28px;
  color: var(--ink-faint); cursor: pointer; line-height: 1; padding: 4px 8px;
}
.detail-close:hover { color: var(--cinnabar); }
.detail-grid { display: grid; grid-template-columns: 1fr 200px; gap: 28px; }
.detail-text { min-width: 0; }
.detail-field { margin-bottom: 15px; }
.detail-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--gold); font-weight: 700; margin-bottom: 4px; font-family: var(--serif);
}
.detail-label-jp { font-family: var(--mincho); margin-left: 4px; color: var(--ink-faint); }
.detail-master { font-family: var(--mincho); font-size: 20px; font-weight: 600; color: var(--cinnabar); }
.detail-kanji { font-family: var(--mincho); font-size: 18px; line-height: 1.65; color: var(--ink); }
.detail-modern-jp { font-family: var(--gothic); font-size: 14px; line-height: 1.55; color: var(--ink-soft); }
.detail-en { font-family: var(--serif); font-size: 14.5px; line-height: 1.6; color: var(--ink); }
.detail-value {
  font-family: var(--mono); font-size: 16px; font-weight: 700; color: #fff;
  background: var(--cinnabar); padding: 5px 12px; border-radius: 3px;
  display: inline-block;
}
.detail-crop-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; }
#detail-crop {
  max-width: 200px; max-height: 60vh;
  border: 1px solid var(--rule); background: #fff; padding: 4px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.25);
}
.detail-zoom-link {
  font-family: var(--serif); font-size: 12px; cursor: pointer;
  background: #fff; color: var(--cinnabar);
  border: 1px solid var(--rule); border-radius: 4px; padding: 5px 10px;
  transition: background .12s, color .12s;
}
.detail-zoom-link:hover { background: var(--cinnabar); color: #fff; }

/* ---------------- Responsive ---------------- */
@media (max-width: 920px) {
  body { overflow: auto; }
  .viewer-header { height: auto; flex-wrap: wrap; gap: 10px; }
  .viewer-main { flex-direction: column; height: auto; }
  .page-pane { flex: 0 0 auto; height: 62vh; border-right: none; border-bottom: 1px solid #000; }
  .data-pane { max-width: none; height: auto; }
  .extract-table thead th { top: 0; }
}
@media (max-width: 620px) {
  .detail-grid { grid-template-columns: 1fr; }
  #detail-crop { max-height: 40vh; }
  .brand-sub { display: none; }
  .nav-context { display: none; }
}
