:root {
  --bg: #0f1115;
  --bg-elev: #171a21;
  --bg-elev2: #1e222b;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --text-dim: #9aa3b2;
  --accent: #e8743b;       /* Mazda-ish orange */
  --accent-soft: #e8743b22;
  --ok: #4ec9a3;
  --radius: 12px;
  --maxw: 1200px;
  --header-h: 56px;
  font-synthesis: none;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9; --bg-elev: #fff; --bg-elev2: #f0f2f5; --border: #e2e5ea;
    --text: #1a1d24; --text-dim: #5c6573; --accent-soft: #e8743b18;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: inherit; text-decoration: none; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--header-h);
  display: flex; align-items: center; gap: 12px; padding: 0 14px;
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 800; letter-spacing: -0.02em; font-size: 18px; white-space: nowrap; }
.brand span { color: var(--accent); }
.search-wrap { position: relative; flex: 1; max-width: 640px; margin: 0 auto; }
#search {
  width: 100%; height: 38px; padding: 0 38px 0 14px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg-elev2); color: var(--text); font-size: 14px;
}
#search:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-hint {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  font-size: 11px; color: var(--text-dim); border: 1px solid var(--border);
  border-radius: 5px; padding: 1px 6px; background: var(--bg-elev);
}
.manual-picker {
  height: 36px; max-width: 180px; background: var(--bg-elev2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 0 8px; font-size: 13px;
}
.iconbtn {
  background: var(--bg-elev2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; height: 36px; min-width: 36px; padding: 0 9px; cursor: pointer; font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center;
}
.iconbtn:hover { border-color: var(--accent); }
.menu-toggle { display: none; }

/* ---------- layout ---------- */
.layout { display: flex; max-width: var(--maxw); margin: 0 auto; }
.sidebar {
  width: 250px; flex: none; padding: 18px 12px 60px; position: sticky;
  top: var(--header-h); align-self: flex-start; height: calc(100vh - var(--header-h));
  overflow-y: auto; border-right: 1px solid var(--border);
}
.view { flex: 1; min-width: 0; padding: 22px 22px 80px; }
.loading { color: var(--text-dim); padding: 40px; text-align: center; }

/* ---------- sidebar nav ---------- */
.nav-title { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-dim); margin: 4px 8px 10px; }
.sec-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-dim); font-size: 14px; cursor: pointer;
}
.sec-link:hover { background: var(--bg-elev2); color: var(--text); }
.sec-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sec-badge {
  flex: none; width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  color: #fff; font-weight: 700; font-size: 12px; display: grid; place-items: center;
}
.sec-link .count { margin-left: auto; font-size: 11px; color: var(--text-dim); }

/* ---------- home / hero ---------- */
.hero { padding: 8px 0 22px; }
.hero h1 { font-size: 26px; margin: 0 0 4px; letter-spacing: -0.02em; }
.hero p { color: var(--text-dim); margin: 0; }
.pill {
  display: inline-block; font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--bg-elev2); border: 1px solid var(--border); color: var(--text-dim); margin: 6px 6px 0 0;
}
/* home "fancy contents" — full-width rows */
.toc-head { margin: 26px 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-dim); }
.toc-list { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--bg-elev); }
.toc-row { display: flex; align-items: center; gap: 14px; padding: 13px 16px; border-bottom: 1px solid var(--border); color: var(--text); position: relative; transition: background .12s; }
.toc-row:last-child { border-bottom: none; }
.toc-row::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: transparent; transition: background .12s; }
.toc-row:hover { background: var(--accent-soft); }
.toc-row:hover::before { background: var(--accent); }
.toc-row .sec-badge { width: 30px; height: 30px; font-size: 13px; }
.toc-title { flex: 1; font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.toc-meta { color: var(--text-dim); font-size: 13px; white-space: nowrap; }
.toc-chev { color: var(--text-dim); font-size: 15px; transition: transform .12s, color .12s; }
.toc-row:hover .toc-title, .toc-row:hover .toc-chev { color: var(--accent); }
.toc-row:hover .toc-chev { transform: translateX(3px); }

.sec-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin-top: 18px; }
.sec-card {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; cursor: pointer; transition: border-color .15s, transform .15s;
}
.sec-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.sec-card .sec-badge { margin-bottom: 10px; }
.sec-card h3 { margin: 0 0 2px; font-size: 14px; }
.sec-card small { color: var(--text-dim); }

/* ---------- result cards ---------- */
.results-head { color: var(--text-dim); font-size: 13px; margin-bottom: 12px; }
.result {
  display: flex; gap: 14px; background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px; margin-bottom: 10px; cursor: pointer;
  transition: border-color .15s;
}
.result:hover { border-color: var(--accent); }
.result .thumb { flex: none; width: 64px; height: 88px; border-radius: 6px; overflow: hidden; background: var(--bg-elev2); border: 1px solid var(--border); }
.result .thumb img { width: 100%; height: 100%; object-fit: cover; }
.result .r-body { min-width: 0; }
.result .r-top { display: flex; align-items: center; gap: 8px; margin-bottom: 3px; }
.result .r-sec { font-size: 11px; font-weight: 700; color: var(--accent); }
.result .r-pg { font-size: 11px; color: var(--text-dim); }
.result h3 { margin: 0 0 4px; font-size: 15px; }
.result .snippet { color: var(--text-dim); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.result mark, .snippet mark { background: var(--accent-soft); color: var(--accent); border-radius: 3px; padding: 0 2px; }

/* ---------- page view ---------- */
.crumbs { font-size: 13px; color: var(--text-dim); margin-bottom: 10px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.crumbs a:hover { color: var(--accent); }

/* compact prev/next jump bar under the breadcrumbs */
.pager-bar { display: flex; justify-content: space-between; gap: 10px; margin: 0 0 14px; }
.pager-bar .pg { display: inline-flex; align-items: center; gap: 6px; max-width: 48%; font-size: 13px; color: var(--text-dim); padding: 7px 11px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-elev); }
.pager-bar a.pg:hover { border-color: var(--accent); color: var(--accent); }
.pager-bar .pg-txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pager-bar .pg-arrow { flex: none; font-size: 15px; line-height: 1; }
.pager-bar .pg.disabled { visibility: hidden; }
.page-grid { display: grid; grid-template-columns: 1fr 290px; gap: 26px; align-items: start; }
.page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 8px; }
.page-head .sec-badge { width: 34px; height: 34px; font-size: 14px; }
.page-head h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.page-head .ph-sub { color: var(--text-dim); font-size: 13px; }

.content { font-size: 14px; line-height: 1.55; }
.content h2 { font-size: 16px; margin: 18px 0 7px; padding-bottom: 5px; border-bottom: 1px solid var(--border); text-transform: uppercase; letter-spacing: .01em; }
.content h3 { font-size: 14.5px; margin: 15px 0 5px; }
.content h4 { font-size: 13px; margin: 12px 0 4px; }
.content p { margin: 7px 0; text-align: justify; hyphens: auto; }
.content li { text-align: justify; hyphens: auto; }
.content ul, .content ol { margin: 8px 0; padding-left: 22px; }
.content li { margin: 3px 0; }
.content blockquote {
  margin: 10px 0; padding: 10px 14px; border-left: 3px solid var(--accent);
  background: var(--accent-soft); border-radius: 0 8px 8px 0;
}
.content blockquote p:first-child { margin-top: 0; }
.content .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; margin: 12px 0; }
.content table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 13px; }
.content .table-wrap table { margin: 0; }
.content th, .content td { border: 1px solid var(--border); padding: 6px 9px; text-align: left; vertical-align: top; }
.content th { background: var(--bg-elev2); font-weight: 600; }
.content a.xref { color: var(--accent); border-bottom: 1px dotted var(--accent); white-space: nowrap; }

/* ---------- the manual page as a paper sheet ---------- */
.sheet {
  /* local light palette so content reads like printed paper in any theme */
  --text: #1b1d20; --text-dim: #6a6f78; --border: #e0e3e8; --bg-elev2: #f3f4f6;
  --accent-soft: #e8743b16;
  background: #fff; color: var(--text);
  max-width: 900px; margin: 4px auto 0; padding: 26px 34px 34px;
  border: 1px solid #e3e6eb; border-radius: 10px; box-shadow: 0 6px 28px rgba(0,0,0,.18);
}
.sheet .page-head { align-items: center; gap: 12px; margin: 0 0 4px; padding-bottom: 12px; border-bottom: 2px solid #1b1d20; }
.sheet .page-head .ph-titles { flex: 1; min-width: 0; }
.sheet .page-head h1 { font-size: 19px; }
.sheet .viewog {
  margin: 0; background: transparent; color: var(--text-dim); border: 1px solid var(--border);
  padding: 6px 12px; font-size: 12px; font-weight: 600; border-radius: 8px; white-space: nowrap;
}
.sheet .viewog:hover { color: var(--accent); border-color: var(--accent); filter: none; }

/* row-based layout: each figure sits beside its own text block */
.block-row { display: grid; grid-template-columns: minmax(220px, 330px) 1fr; gap: 26px; align-items: start; margin: 20px 0; }
.block-row:first-child { margin-top: 14px; }
.block-row + .block-row { border-top: 1px solid var(--border); padding-top: 18px; }
.block-content > :first-child { margin-top: 0; }
.block-content .fig-legend-side { margin-top: 12px; }

/* full-width block: figures arranged in a grid (source side-by-side), text below */
.block-grid { margin: 20px 0; }
.block-row + .block-grid, .block-grid + .block-row, .block-grid + .block-grid, .block-wide + .block-grid, .block-grid + .block-wide { border-top: 1px solid var(--border); padding-top: 18px; }
.fig-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 18px; margin-bottom: 12px; }
.fig-grid figure { margin: 0; display: flex; flex-direction: column-reverse; }  /* caption ABOVE the image, like the source */
.fig-grid img { width: 100%; display: block; background: #fff; border: 1px solid #e6e6e6; border-radius: 4px; cursor: zoom-in; }
.fig-grid figcaption { padding: 0 1px 5px; font-size: 12px; line-height: 1.45; color: var(--text); }

/* full-width block: wide diagram on top, its text below */
.block-wide { margin: 20px 0; }
.block-row + .block-wide, .block-wide + .block-row, .block-wide + .block-wide { border-top: 1px solid var(--border); padding-top: 18px; }
.block-wide figure { margin: 0 0 12px; }
.block-wide img { width: 100%; display: block; background: #fff; border: 1px solid #e6e6e6; border-radius: 4px; cursor: zoom-in; }
.block-wide figcaption { padding: 5px 1px 0; font-size: 11.5px; line-height: 1.45; color: var(--text-dim); }
.block-wide .content { margin-top: 6px; }

/* source-style: figures left, text right */
.facsimile { display: grid; grid-template-columns: 310px 1fr; gap: 24px; align-items: start; margin-top: 16px; }
.fig-col { display: flex; flex-direction: column; gap: 18px; }
.fig-col figure { margin: 0; }
.fig-col img { width: 100%; display: block; background: #fff; border: 1px solid #e6e6e6; border-radius: 4px; cursor: zoom-in; }
.fig-col figcaption { padding: 5px 1px 0; font-size: 11.5px; line-height: 1.45; color: var(--text-dim); }
.fig-label { font-weight: 700; color: var(--text); font-style: italic; }
.fig-desc { color: var(--text-dim); }
.fig-legend { margin: 5px 0 0; padding-left: 18px; columns: 2; font-size: 11.5px; color: var(--text); }
.fig-legend li { margin: 1px 0; }
.text-col { min-width: 0; }
.text-col > :first-child { margin-top: 0; }

/* wide figures (exploded diagrams) span the full page width */
.wide-figs { margin-top: 18px; display: flex; flex-direction: column; gap: 18px; }
.wide-figs figure { margin: 0; }
.wide-figs img { width: 100%; display: block; background: #fff; border: 1px solid #e6e6e6; border-radius: 4px; cursor: zoom-in; }
.wide-figs figcaption { padding: 6px 1px 0; font-size: 11.5px; line-height: 1.45; color: var(--text-dim); }

/* numbered-legend figures: image left, part list to the right (source-style) */
.fig-row { display: flex; gap: 22px; align-items: flex-start; }
.fig-row .fig-left { flex: 1 1 58%; min-width: 0; }
.fig-row .fig-legend-side { flex: 1 1 42%; }
.fig-legend-side { list-style: none; margin: 2px 0 0; padding: 0; font-size: 13px; color: var(--text); display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 4px 18px; }
.fig-legend-side li { display: flex; gap: 9px; margin: 3px 0; align-items: center; }
.fig-legend-side .cnum { flex: none; width: 18px; height: 18px; border: 1px solid currentColor; border-radius: 50%; font-size: 10.5px; display: inline-grid; place-items: center; }

/* cover / section-divider: show the page image centered */
.visual-page { text-align: center; margin: 16px 0; }
.visual-page img { max-width: 440px; width: 100%; border: 1px solid var(--border); border-radius: 4px; cursor: zoom-in; box-shadow: 0 2px 10px rgba(0,0,0,.08); }

.figstrip { display: flex; gap: 10px; flex-wrap: wrap; margin: 14px 0; }
.figchip {
  font-size: 12px; padding: 5px 10px; border-radius: 8px; background: var(--bg-elev2);
  border: 1px solid var(--border); cursor: pointer; color: var(--text-dim);
}
.figchip:hover { border-color: var(--accent); color: var(--accent); }
.figchip b { color: var(--text); }

.viewog {
  display: inline-flex; align-items: center; gap: 8px; margin-top: 6px;
  background: var(--accent); color: #fff; border: none; border-radius: 10px;
  padding: 10px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.viewog:hover { filter: brightness(1.06); }

/* meta sidebar */
.meta { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; position: sticky; top: calc(var(--header-h) + 16px); }
.meta .ogthumb { width: 100%; border-radius: 8px; border: 1px solid var(--border); display: block; margin-bottom: 12px; cursor: zoom-in; background: var(--bg-elev2); }
.meta h4 { margin: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); }
.meta h4:first-of-type { margin-top: 4px; }
.tag { display: inline-block; font-size: 12px; padding: 3px 8px; margin: 0 5px 5px 0; border-radius: 6px; background: var(--bg-elev2); border: 1px solid var(--border); }
.tag.tool, .tag.clickable { cursor: pointer; }
.tag.tool:hover, .tag.clickable:hover { border-color: var(--accent); color: var(--accent); }
.tag.spec { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }
.xreflist a { display: block; font-size: 13px; color: var(--accent); padding: 3px 0; }

/* metadata bar below the facsimile */
.metabar { display: flex; flex-wrap: wrap; gap: 20px 28px; margin-top: 26px; padding-top: 18px; border-top: 1px solid var(--border); }
.mb-group { min-width: 120px; }
.mb-group h4 { margin: 0 0 7px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); }

.pager { display: flex; justify-content: space-between; margin-top: 28px; gap: 10px; }
.pager a { flex: 1; padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg-elev); font-size: 13px; }
.pager a:hover { border-color: var(--accent); }
.pager a.next { text-align: right; }
.pager .muted { color: var(--text-dim); }

/* ---------- lightbox ---------- */
.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.92); display: flex; flex-direction: column; }
.lightbox[hidden] { display: none; }
.lb-bar { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; color: #fff; gap: 10px; }
.lb-title { font-size: 13px; opacity: .85; }
.lb-actions { display: flex; gap: 6px; }
.lb-actions .iconbtn { background: #ffffff14; border-color: #ffffff2a; color: #fff; }
.lb-stage { flex: 1; position: relative; overflow: hidden; cursor: grab; touch-action: none; }
.lb-stage.grabbing { cursor: grabbing; }
#lbImg { position: absolute; top: 0; left: 0; transform-origin: 0 0; user-select: none; max-width: none; will-change: transform; box-shadow: 0 8px 40px rgba(0,0,0,.6); }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .menu-toggle { display: inline-flex; }
  .sidebar {
    position: fixed; left: 0; top: var(--header-h); bottom: 0; width: 270px; z-index: 25;
    background: var(--bg-elev); transform: translateX(-100%); transition: transform .2s; height: auto;
  }
  .sidebar.open { transform: translateX(0); }
  .page-grid { grid-template-columns: 1fr; }
  .sheet { padding: 18px 16px 24px; border-radius: 0; margin-top: 0; border-left: none; border-right: none; }
  .facsimile { grid-template-columns: 1fr; gap: 14px; }
  .block-row { grid-template-columns: 1fr; gap: 10px; margin: 14px 0; }
  .block-row + .block-row { padding-top: 14px; }
  .fig-col { flex-direction: column; }
  .fig-col img { max-width: 460px; }
  .fig-row { flex-direction: column; gap: 8px; }
  .meta { position: static; order: -1; }
  .search-hint { display: none; }
  .manual-picker { display: none; }
}
@media (max-width: 520px) {
  .view { padding: 16px 14px 70px; }
  #search { font-size: 16px; } /* avoid iOS zoom */
}
.scrim { position: fixed; inset: var(--header-h) 0 0 0; background: rgba(0,0,0,.4); z-index: 24; }
