:root {
  --ink: #1a120b;
  --night: #120b07;
  --panel: #241810;
  --panel-2: #2e2015;
  --line: #5a4126;
  --brass: #c9a25a;
  --gilt: #e6c988;
  --paper: #eadfc8;
  --muted: #a8977c;
  --serif: "Songti SC", "STSong", "Noto Serif CJK SC", "Source Han Serif SC", "SimSun", Georgia, "Times New Roman", serif;
  --app-height: 100dvh;
}
/* iOS standalone can report dvh/innerHeight shorter than the actual screen.
   lvh includes the status bar area; reserve safe areas in controls, not twice. */
:root.app-standalone { --app-height: 100lvh; height: var(--app-height); }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--night); color: var(--paper); font-family: var(--serif); -webkit-user-select: none; user-select: none; overscroll-behavior: none; }
/* A phone must not fight the controls: no page to scroll or rubber-band (the body is pinned), no double-tap zoom,
   no long-press callout, no grey flash on a tap. Pinch zoom on iOS ignores user-scalable=no: main.js stops it. */
body { position: fixed; inset: 0; touch-action: manipulation; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
kbd { display: inline-block; min-width: 1.6em; padding: 0 .4em; margin: 0 .12em; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: #1b120b; color: var(--gilt); font: 600 .82em/1.6 Georgia, serif; text-align: center; }

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
/* soft roaming (main.js): the mouse turns the view without pointer lock, so the cursor must not wander over the scene */
body[data-state=soft] #scene { cursor: none; }
#vignette { position: fixed; inset: 0; pointer-events: none; background: radial-gradient(ellipse at center, transparent 56%, rgba(23, 12, 3, .34) 100%); }
#fade { position: fixed; inset: 0; pointer-events: none; background: var(--night); opacity: 0; }

/* --- heads-up display --- */
#hud { position: fixed; inset: 0; pointer-events: none; }
#place { position: absolute; left: max(18px, env(safe-area-inset-left)); top: max(14px, env(safe-area-inset-top)); text-shadow: 0 1px 3px #000, 0 0 12px #000; }
#place-title { font-size: 21px; font-weight: 700; color: var(--gilt); letter-spacing: .04em; }
#place-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
#speed { position: absolute; left: 50%; bottom: max(22px, env(safe-area-inset-bottom)); transform: translateX(-50%); padding: 4px 14px; border-radius: 14px; background: rgba(20, 13, 8, .72); font-size: 13px; color: var(--gilt); letter-spacing: .05em; opacity: 0; transition: opacity .5s; white-space: nowrap; }
#speed.on { opacity: 1; transition: opacity .08s; }
body:not(.touch) #buttons { display: none; }      /* a keyboard has Tab, R and H for these */
#buttons { position: absolute; right: max(14px, env(safe-area-inset-right)); top: max(12px, env(safe-area-inset-top)); display: flex; gap: 8px; pointer-events: auto; }
#buttons button { min-width: 40px; height: 36px; padding: 0 12px; border: 1px solid var(--line); border-radius: 6px; background: rgba(26, 18, 11, .78); color: var(--gilt); font-size: 14px; backdrop-filter: blur(4px); }
#buttons button:hover { border-color: var(--brass); background: rgba(46, 32, 21, .9); }
body.touch #buttons button { min-width: 46px; height: 42px; font-size: 15px; }
@media (hover: none) { #buttons button:hover { border-color: var(--line); background: rgba(26, 18, 11, .78); } }
/* A plain vertical capsule with the movement stick's round thumb and palette. */
#lift { display: none; position: absolute; right: max(14px, env(safe-area-inset-right)); bottom: calc(max(18px, env(safe-area-inset-bottom)) + 28px);
  width: 58px; height: 186px; border: 1.5px solid rgba(230, 201, 136, .45); border-radius: 29px; background: rgba(20, 13, 8, .28);
  pointer-events: auto; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; cursor: ns-resize; }
body.touch #lift { display: block; }
#lift > span { pointer-events: none; }
.lift-thumb { position: absolute; left: calc(50% - 24px); top: calc(50% - 24px); width: 48px; height: 48px;
  transform: translateY(var(--lift-offset, 0px)); border-radius: 50%; background: rgba(230, 201, 136, .5);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .4); }
#lift:focus-visible { outline: 2px solid var(--brass); outline-offset: 3px; }
/* the joystick, drawn where the left thumb landed */
#stick { position: absolute; left: 0; top: 0; width: 0; height: 0; opacity: 0; transition: opacity .25s; }
#stick.on { opacity: 1; transition: opacity .06s; }
#stick::before { content: ""; position: absolute; left: -56px; top: -56px; width: 112px; height: 112px; border: 1.5px solid rgba(230, 201, 136, .45); border-radius: 50%; background: rgba(20, 13, 8, .28); }
#stick i { position: absolute; left: -24px; top: -24px; width: 48px; height: 48px; border-radius: 50%; background: rgba(230, 201, 136, .5); box-shadow: 0 0 0 1px rgba(0, 0, 0, .4); }
#crosshair { position: absolute; left: 50%; top: 50%; width: 6px; height: 6px; margin: -3px; border-radius: 50%; background: rgba(240, 220, 170, .75); box-shadow: 0 0 0 1px rgba(0, 0, 0, .5); transition: transform .12s, background .12s; }
#crosshair.on { transform: scale(2.1); background: transparent; box-shadow: 0 0 0 1.5px var(--gilt), 0 0 6px rgba(0, 0, 0, .8); }
#hint { position: absolute; left: 50%; top: calc(50% + 34px); transform: translateX(-50%); max-width: min(560px, 86vw); padding: 7px 14px; border: 1px solid var(--line); border-radius: 6px; background: rgba(20, 13, 8, .86); text-align: center; opacity: 0; transition: opacity .12s; }
#hint.on { opacity: 1; }
#hint em { display: block; margin-top: 5px; font-style: normal; font-size: 13px; color: var(--gilt); letter-spacing: .1em; }
#hint em::after { content: " ›"; }
/* on a touch screen: a caption over the book a tap picked out, which takes it when tapped */
body.touch #crosshair { display: none; }
#hint.placed { transform: none; max-width: min(340px, calc(100vw - 16px)); transition: opacity .12s; }
body.touch #hint.on { pointer-events: auto; cursor: pointer; border-color: var(--brass); }
#hint b { display: block; font-size: 16px; color: var(--gilt); }
#hint span { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
#hint i { display: block; margin-top: 2px; font-style: normal; font-size: 14px; color: var(--paper); }   /* an English title's Chinese */
#status { position: absolute; left: max(18px, env(safe-area-inset-left)); bottom: max(14px, env(safe-area-inset-bottom)); font-size: 12.5px; color: var(--muted); text-shadow: 0 1px 3px #000; }
#toasts { position: absolute; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
/* thumbs: the bottom corners are the controls'; toasts come down from the top */
body.touch #toasts { left: 50%; right: auto; bottom: auto; top: calc(max(12px, env(safe-area-inset-top)) + 96px); transform: translateX(-50%); width: min(360px, calc(100vw - 32px)); align-items: stretch; }
body.touch .toast { max-width: none; }
.toast { max-width: 340px; padding: 9px 14px; border: 1px solid var(--brass); border-radius: 6px; background: rgba(30, 20, 12, .94); font-size: 13.5px; pointer-events: auto; cursor: pointer; animation: rise .35s ease-out; }
.toast b { color: var(--gilt); }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }

/* --- overlays --- */
.overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(10, 6, 3, .66); z-index: 10; }
#intro { background: radial-gradient(ellipse at center, rgba(18, 11, 7, .55), rgba(10, 6, 3, .93)); }
#intro { overflow-y: auto; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.intro-box { width: min(560px, 90vw); margin: auto; text-align: center; }
.ornament { font-size: 30px; color: var(--brass); }
.intro-box h1 { margin: 6px 0 22px; font-size: clamp(38px, 8vw, 62px); font-weight: 700; letter-spacing: .22em; text-indent: .22em; color: var(--gilt); text-shadow: 0 2px 18px rgba(0, 0, 0, .8); }
.quote { margin: 0; font-size: 16px; line-height: 1.9; color: var(--paper); }
.quote-by { margin: 6px 0 22px; font-size: 13px; color: var(--muted); }
.count { font-size: 14px; color: var(--muted); min-height: 1.5em; }
.who { margin: -4px 0 4px; font-size: 12.5px; color: var(--muted); }
.who a, #help-who a { color: var(--gilt); text-underline-offset: 3px; }
#enter { margin: 8px 0 26px; padding: 11px 44px; border: 1px solid var(--brass); border-radius: 4px; background: linear-gradient(#3a2816, #24170c); color: var(--gilt); font-size: 18px; letter-spacing: .3em; text-indent: .3em; }
#enter:hover:not(:disabled) { background: linear-gradient(#4a341d, #2e1e10); }
#enter:disabled { opacity: .45; cursor: default; }
.keys { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 18px; font-size: 13px; color: var(--muted); }
/* thumbs or a mouse: body.touch follows the input used last (main.js), not a media query */
#keys-touch, .help-panel .for-touch { display: none; }
body.touch #keys-touch { display: flex; } body.touch #keys-desktop { display: none; }
body.touch .help-panel .for-touch { display: table; } body.touch .help-panel .for-mouse { display: none; }
#pause { background: rgba(10, 6, 3, .4); cursor: pointer; }
.pause-box { padding: 16px 34px; border: 1px solid var(--line); border-radius: 6px; background: rgba(20, 13, 8, .9); font-size: 18px; color: var(--gilt); text-align: center; }
.pause-box small { display: block; margin-top: 6px; font-size: 12.5px; color: var(--muted); }

.panel { position: relative; border: 1px solid var(--line); border-radius: 8px; background: linear-gradient(var(--panel-2), var(--panel)); box-shadow: 0 18px 60px rgba(0, 0, 0, .7), inset 0 0 0 1px rgba(201, 162, 90, .12); }
.close { position: absolute; right: 10px; top: 8px; width: 34px; height: 34px; border: 0; background: none; color: var(--muted); font-size: 26px; line-height: 1; z-index: 2; }
.close:hover { color: var(--gilt); }
h2 { margin: 0; font-size: 22px; color: var(--gilt); letter-spacing: .08em; }
.help-panel { width: min(640px, 92vw); max-height: 88vh; max-height: 88dvh; overflow: auto; overscroll-behavior: contain; padding: 22px 26px; }
.logout { margin-top: 14px; }
.logout a { color: var(--gilt); text-underline-offset: 3px; }
.help-panel table { width: 100%; margin: 14px 0; border-collapse: collapse; font-size: 14.5px; }
.help-panel td { padding: 7px 4px; border-bottom: 1px solid rgba(90, 65, 38, .5); vertical-align: top; }
.help-panel td:first-child { white-space: nowrap; color: var(--paper); padding-right: 18px; }
.help-panel td:last-child { color: var(--muted); }
.note { font-size: 13.5px; line-height: 1.8; color: var(--muted); }
.help-panel h3 { margin: 18px 0 6px; font-size: 15px; font-weight: 600; color: var(--paper); letter-spacing: .2em; }
.office p { margin: 4px 0; font-size: 13.5px; line-height: 1.75; color: var(--muted); }
.settings { display: grid; gap: 12px; margin: 8px 0 4px; }
.setting { display: block; font-size: 14px; color: var(--paper); }
.setting .name { display: flex; justify-content: space-between; gap: 12px; }
.setting output { font-size: 13px; color: var(--gilt); }
.setting small { display: block; margin-top: 2px; font-size: 12px; color: var(--muted); }
.setting input[type=range] { display: block; width: 100%; height: 30px; margin: 2px 0 0; accent-color: var(--brass); touch-action: manipulation; cursor: pointer; }
.setting.switch { display: flex; align-items: center; gap: 10px; min-height: 34px; cursor: pointer; }
.setting.switch input { width: 20px; height: 20px; margin: 0; accent-color: var(--brass); }
.settings > .link { justify-self: start; }

/* --- catalogue --- */
.catalog-panel { width: min(1020px, 95vw); height: min(760px, 90vh); height: min(760px, 90dvh); display: flex; flex-direction: column; -webkit-user-select: text; user-select: text; }
.catalog-head { display: flex; align-items: center; gap: 18px; padding: 16px 56px 12px 22px; border-bottom: 1px solid var(--line); }
#search { flex: 1; height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 6px; background: #17100a; outline: none; font-size: 15px; }
#search:focus { border-color: var(--brass); }
.catalog-body { flex: 1; display: flex; min-height: 0; }
#classes { width: 250px; overflow-y: auto; overscroll-behavior: contain; padding: 8px; border-right: 1px solid var(--line); }
#classes button { display: flex; width: 100%; align-items: baseline; gap: 8px; padding: 6px 10px; border: 0; border-radius: 5px; background: none; text-align: left; font-size: 14px; color: var(--paper); }
#classes button:hover { background: rgba(201, 162, 90, .1); }
#classes button.on { background: rgba(201, 162, 90, .2); color: var(--gilt); }
#classes button.empty { color: #6f6250; }
#classes button i { font: 700 15px Georgia, serif; width: 1.5em; color: var(--brass); }
#classes button em { margin-left: auto; font-style: normal; font-size: 12px; color: var(--muted); }
#classes button span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#results { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 6px 12px 16px; }
.sec-head { position: sticky; top: 0; margin: 10px 0 4px; padding: 6px 8px; background: var(--panel-2); border-bottom: 1px solid var(--line); font-size: 14px; color: var(--brass); z-index: 1; }
.sec-head button { float: right; border: 0; background: none; color: var(--muted); font-size: 12.5px; }
.sec-head button:hover { color: var(--gilt); }
.book-row { display: flex; align-items: center; gap: 12px; width: 100%; padding: 7px 8px; border: 0; border-radius: 5px; background: none; text-align: left; }
.book-row:hover { background: rgba(201, 162, 90, .12); }
.book-row .swatch { flex: none; width: 9px; height: 34px; border-radius: 1px; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .4); }
.book-row .t { flex: 1; min-width: 0; }
.book-row .t b { display: block; font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-row .t span { display: block; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-row .side { flex: none; display: flex; flex-direction: row-reverse; align-items: baseline; gap: 12px; }
.book-row .call { font: 600 12.5px Georgia, serif; color: var(--brass); }
.book-row .when { font-size: 12px; color: var(--muted); white-space: nowrap; }
.book-row .fav { font-size: 13px; color: var(--gilt); }
.book-row .tags { flex: none; display: flex; gap: 4px; }
.tag { padding: 1px 6px; border: 1px solid var(--line); border-radius: 3px; font: 11px/1.5 Georgia, serif; color: var(--muted); text-transform: uppercase; }
.empty-note { padding: 40px 20px; text-align: center; color: var(--muted); font-size: 14.5px; line-height: 1.9; }
.more { display: block; margin: 10px auto; padding: 6px 18px; border: 1px solid var(--line); border-radius: 5px; background: none; color: var(--muted); }

/* --- a book in hand --- */
#card { position: fixed; right: 0; top: 0; bottom: 0; width: min(430px, 46vw); z-index: 8; border-left: 1px solid var(--line); background: linear-gradient(90deg, rgba(36, 24, 16, .93), rgba(26, 17, 10, .98)); box-shadow: -20px 0 60px rgba(0, 0, 0, .55); animation: slide .3s ease-out; -webkit-user-select: text; user-select: text; }
@keyframes slide { from { transform: translateX(40px); opacity: 0; } }
.card-scroll { height: 100%; overflow-y: auto; overscroll-behavior: none; padding: 26px max(26px, env(safe-area-inset-right)) max(30px, env(safe-area-inset-bottom)) 26px; }
#card-cover { display: none; max-width: 150px; max-height: 210px; margin: 0 0 14px; border-radius: 2px; box-shadow: 0 6px 22px rgba(0, 0, 0, .6); }
.card-call { font: 600 13.5px Georgia, var(--serif); color: var(--brass); letter-spacing: .04em; }
#card-title { margin: 6px 0 4px; font-size: 25px; line-height: 1.35; letter-spacing: .02em; }
#card-alt { font-size: 14.5px; font-style: italic; color: var(--muted); }
#card-zh { margin: -2px 0 4px; font-size: 17px; color: var(--paper); }   /* an English title's Chinese */
#card-zh:empty { display: none; }
#card-authors { margin-top: 8px; font-size: 15px; color: var(--paper); }
#card-summary { margin: 12px 0 0; font-size: 14px; line-height: 1.8; color: var(--muted); }
#card h3 { margin: 22px 0 8px; padding-bottom: 6px; border-bottom: 1px solid var(--line); font-size: 14px; font-weight: 600; color: var(--brass); letter-spacing: .15em; }
.version { display: flex; align-items: center; gap: 12px; width: 100%; margin-bottom: 8px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; background: rgba(18, 11, 7, .6); text-align: left; }
body:not(.touch) .version:hover, body.touch .version.pressed { border-color: var(--brass); background: rgba(58, 40, 22, .7); }
body.touch .version { -webkit-user-select: none; user-select: none; }
.version.last { border-color: var(--brass); }
.version .fmt { flex: none; width: 52px; padding: 3px 0; border-radius: 3px; background: var(--brass); color: var(--ink); font: 700 12px/1.5 Georgia, serif; text-align: center; }
.version .v { flex: 1; min-width: 0; }
.version .v b { display: block; font-size: 15px; font-weight: 600; }
.version .v span { display: block; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }   /* (wraps: the size must never be cut off) */
.version .keep { flex: none; padding: 3px 8px; border: 1px solid var(--line); border-radius: 4px; background: none; font-size: 12px; color: var(--muted); white-space: nowrap; touch-action: manipulation; }
body:not(.touch) .version .keep:hover { border-color: var(--brass); color: var(--gilt); }
.version .keep.here { border-color: transparent; color: var(--gilt); }
.version .keep.busy { border-color: var(--brass); color: var(--gilt); font-variant-numeric: tabular-nums; }
.office .actions { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.version .go { flex: none; font-size: 13px; color: var(--gilt); }
.card-actions { display: flex; align-items: center; gap: 18px; margin-top: 16px; }
.card-actions #card-back { margin-left: auto; }
.fav-toggle { padding: 5px 12px; border: 1px solid var(--line); border-radius: 5px; background: rgba(18, 11, 7, .6); font-size: 13.5px; color: var(--paper); }
.fav-toggle:hover { border-color: var(--brass); }
.fav-toggle.on { border-color: var(--brass); color: var(--gilt); }
.link { border: 0; background: none; padding: 4px 0; font-size: 13.5px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--gilt); }

/* --- reading --- */
#reader { position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; background: #1b130c; }
body, #scene, #vignette, #fade, #hud, .overlay, #reader, #fatal { height: var(--app-height); bottom: auto; }
/* Safari can extend ordinary page content behind its glass controls. The
   scene has a small overscan region; its controls stay in the visible area. */
html.native-scene, html.native-scene body { position: static; height: var(--scene-height); overflow-x: hidden; overflow-y: auto; }
html.native-scene #scene, html.native-scene #vignette, html.native-scene #fade { position: absolute; top: 0; height: var(--scene-height); }
html.native-scene #hud { position: absolute; top: var(--scene-inset); }
.reader-bar { flex: none; display: flex; align-items: center; gap: 12px; height: calc(46px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left)); border-bottom: 1px solid var(--line); background: linear-gradient(var(--panel-2), var(--panel)); }
.reader-bar button, .reader-bar a, .reader-bar select { height: 32px; padding: 0 12px; border: 1px solid var(--line); border-radius: 5px; background: #1b120b; color: var(--gilt); font-size: 13.5px; line-height: 30px; text-decoration: none; }
.reader-bar button:hover, .reader-bar a:hover { border-color: var(--brass); }
#reader-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15.5px; color: var(--paper); }
#reader-version { max-width: 36vw; }
.reader-bar button.docked { padding: 0 10px; }
.reader-bar button.docked.on { border-color: var(--brass); color: var(--paper); font-weight: 600; }
#reader-frame { flex: 1; min-height: 0; width: 100%; display: block; border: 0; background: #2a2a2e; }
#reader.pdf-docked > .reader-bar { display: none; }
.reader-more > summary { cursor: pointer; list-style: none; color: var(--gilt); border: 1px solid var(--line); border-radius: 5px; height: 32px; padding: 0 10px; line-height: 30px; background: #1b120b; }
.reader-more > summary::-webkit-details-marker { display: none; }
.reader-menu { position: absolute; top: 100%; right: 8px; z-index: 5; display: flex; flex-direction: column; gap: 8px; padding: 12px; max-width: min(300px, 95vw); background: var(--panel); border: 1px solid var(--line); border-radius: 5px; box-shadow: 0 8px 24px #0008; }
.reader-menu-title { overflow-wrap: anywhere; color: var(--paper); }
.reader-menu #reader-menu-version { max-width: 100%; }
html.reader-open, html.reader-open body { background: var(--reader-background, #120b07); }
/* Safari samples the whole page behind its glass bars, including during a
   reader transition. Remove the roaming canvas from that composited page. */
html.reader-open #scene, html.reader-open #vignette, html.reader-open #fade { display: none; }
html.reader-open #card, html.reader-open #catalog, html.reader-open #help, html.reader-open #babel, html.reader-open #hud { visibility: hidden; pointer-events: none; }
/* Meme pages fit the viewport. Suspend roaming's extra scrollable height so a
   vertical gesture cannot shrink Safari's chrome and reveal an empty edge. */
html.meme-reading, html.meme-reading body {
  position: fixed; inset: 0; width: 100%; height: var(--app-height);
  overflow: hidden; overflow: clip; overscroll-behavior: none; touch-action: pan-x;
  background: var(--panel);
}
html.meme-reading #reader { background: var(--panel); }
/* Safari extends the iframe element's own edge color into its browser chrome,
   even when the document inside that frame already has the correct palette. */
html.meme-reading #reader-frame { background: var(--panel); }
html.meme-reading .reader-bar { background: linear-gradient(var(--panel), var(--panel-2) 50%, var(--panel)); }
@media (min-width: 851px) {
  .reader-more, .reader-more[open] .reader-menu { display: contents; }
  .reader-more > summary, .reader-menu-title, #reader-menu-version, #reader-menu-download { display: none; }
}
@media (max-width: 850px) {
  .reader-more:not([open]) .reader-menu { display: none; }
  #reader.ebook-docked .reader-bar { gap: 6px; padding-inline: max(6px, env(safe-area-inset-left)) max(6px, env(safe-area-inset-right)); }
  #reader.ebook-docked #reader-version, #reader.ebook-docked #reader-download { display: none; }
  #reader.ebook-docked .reader-bar button.docked { padding-inline: 8px; white-space: nowrap; }
  #reader.ebook-docked .reader-bar { position: relative; z-index: 2; }
  #reader.ebook-docked #reader-close { width: 34px; padding: 0; flex: none; }
  #reader.ebook-docked #reader-close span { display: none; }
  body.touch .reader-more > summary { height: 38px; line-height: 36px; }
}
#reader.immersive { padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left); }
#reader.immersive .reader-bar { display: none; }
/* Cross-fade viewport snapshots without animating the very tall document's
   size. The current text/page is aligned before the new snapshot is revealed. */
::view-transition-group(root) { animation: none; }
::view-transition-old(root), ::view-transition-new(root) { animation-duration: 180ms; animation-timing-function: ease-out; }
@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(root), ::view-transition-new(root) { animation-duration: 0s; }
}

/* --- the Library's own books --- */
.babel-book { position: relative; width: min(820px, 94vw); max-height: 92vh; max-height: 92dvh; display: flex; flex-direction: column; padding: 26px 30px 18px; border-radius: 3px; background: #e9dfc6; color: #2b2015; box-shadow: 0 20px 70px rgba(0, 0, 0, .8), inset 0 0 60px rgba(120, 90, 40, .28); -webkit-user-select: text; user-select: text; }
.babel-book .close { color: #7a6748; }
.babel-head { display: flex; justify-content: space-between; padding-right: 34px; font-size: 13px; color: #7a6748; }
#babel-text { flex: 1; margin: 14px 0; overflow: auto; font: 13.5px/1.42 "Courier New", Courier, monospace; letter-spacing: .02em; white-space: pre; color: #2b2015; }
.babel-nav { display: flex; justify-content: space-between; }
.babel-nav button { padding: 6px 16px; border: 1px solid #a8926a; border-radius: 4px; background: none; color: #5a4526; font-size: 13.5px; }
.babel-nav button:hover { background: rgba(120, 90, 40, .15); }

#lost { z-index: 15; }
#lost .pause-box { font-family: inherit; cursor: pointer; max-width: min(420px, 88vw); }
#fatal { position: fixed; inset: 0; z-index: 99; display: flex; align-items: center; justify-content: center; padding: 30px; background: var(--night); color: var(--paper); font-size: 16px; line-height: 1.9; text-align: center; white-space: pre-wrap; }

@media (max-width: 760px), (orientation: portrait) {
  /* the card as a bottom sheet: the book in the hand stays in view above it */
  #card { top: auto; bottom: 0; left: 0; width: 100%; height: 56vh; height: 56dvh; border-left: 0; border-top: 1px solid var(--line); border-radius: 12px 12px 0 0; background: rgba(28, 18, 11, .97); animation-name: sheet; }
  #card::before { content: ""; position: absolute; left: 50%; top: 7px; width: 40px; height: 4px; margin-left: -20px; border-radius: 2px; background: var(--line); }
  #card-cover { display: none !important; }      /* the book itself is in view just above the card */
  .card-scroll { padding: 22px max(18px, env(safe-area-inset-right)) max(26px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left)); }
  #classes { width: 132px; }
  #classes button span { display: none; }
  .catalog-head { padding-left: 14px; gap: 10px; }
  #place-title { font-size: 17px; }
  #reader-version { max-width: 30vw; }
}

@keyframes sheet { from { transform: translateY(40px); opacity: 0; } }
#buttons button { touch-action: manipulation; user-select: none; -webkit-user-select: none; }
#card .close, .panel .close { touch-action: manipulation; }
body.touch .close { width: 44px; height: 44px; right: 6px; top: 4px; font-size: 30px; }
/* iOS zooms into any field with text under 16px when it takes the focus */
body.touch input, body.touch select { font-size: 16px !important; }
/* A narrow screen: the place name leaves room for the three buttons */
@media (max-width: 560px) {
  #place { max-width: calc(100vw - 200px - env(safe-area-inset-left) - env(safe-area-inset-right)); }
  #place-sub { font-size: 12px; }
  .book-row .tags { display: none; }
}

/* --- phones: the catalogue and the help panel fill the screen; classes become a strip of chips --- */
@media (max-width: 600px), (max-height: 500px) {
  #help, #catalog { align-items: stretch; }
  .catalog-panel, .help-panel { width: 100%; height: 100%; max-height: none; border: 0; border-radius: 0;
    padding-top: env(safe-area-inset-top); padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
  .help-panel { padding: max(18px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left)); }
  .help-panel td:first-child { white-space: normal; min-width: 5em; padding-right: 10px; }
  .panel .close { top: max(4px, env(safe-area-inset-top)); right: max(6px, env(safe-area-inset-right)); }
  .catalog-head { padding: 10px 56px 8px 14px; gap: 12px; }
  .catalog-head h2 { font-size: 19px; }
  .catalog-body { flex-direction: column; }
  #classes { width: auto; flex: none; display: flex; gap: 6px; overflow-x: auto; overflow-y: hidden; padding: 8px 12px; border-right: 0; border-bottom: 1px solid var(--line); scrollbar-width: none; }
  #classes::-webkit-scrollbar { display: none; }
  #classes button { flex: none; width: auto; max-width: 9.5em; padding: 6px 10px; border: 1px solid var(--line); border-radius: 16px; font-size: 13.5px; }
  #classes button span { display: block !important; }
  #classes button i { width: auto; font-size: 13.5px; }
  #classes button em { margin-left: 2px; }
  #results { padding: 4px 8px max(16px, env(safe-area-inset-bottom)); }
  .book-row { min-height: 56px; gap: 10px; padding: 8px 6px; }
  .book-row .t b { font-size: 15.5px; }
  .book-row .side { flex-direction: column; align-items: flex-end; gap: 3px; }
  .sec-head button { font-size: 13px; padding: 4px 0 4px 8px; }
}

/* a low screen (a phone on its side): the title page and the card tighten */
@media (max-height: 500px) {
  .intro-box h1 { margin: 2px 0 10px; font-size: 34px; }
  .ornament { font-size: 22px; }
  .quote { font-size: 14px; line-height: 1.7; }
  .quote-by { margin: 2px 0 8px; }
  #enter { margin: 4px 0 12px; padding: 9px 40px; }
  .card-scroll { padding-top: max(18px, env(safe-area-inset-top)); }
  #card-title { font-size: 21px; }
}

/* the Library's own volumes: forty lines of eighty letters, made to fit the width of a phone */
@media (max-width: 760px) {
  .babel-book { padding: 22px 12px 14px; }
  #babel-text { font-size: min(13.5px, calc((94vw - 30px) / 48.5)); }
  .babel-nav button { padding: 8px 12px; }
}
body.touch .reader-bar button, body.touch .reader-bar a, body.touch .reader-bar select { height: 38px; line-height: 36px; }

.icp-filing { position:fixed; bottom:calc(5px + env(safe-area-inset-bottom)); left:50%; transform:translateX(-50%); color:#b9a47b; font:11px Georgia,"Songti SC",serif; text-decoration:none; z-index:3; white-space:nowrap; }
body:has(#reader:not([hidden])) .icp-filing { display:none; }
