/* B2 Reader prototype. Layout: one centred reading column; the scene sits above the text, never over it. */
:root {
  --bg: #eef0f3;
  --paper: #fbfbfc;
  --ink: #1d2130;
  --muted: #5d6273;
  --line: #d9dce3;
  --accent: #a8391f; /* Mars red */
  --scene-bg: #0b0d16;
  --font-read: "Literata", Georgia, "Times New Roman", serif;
  --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d0f15; --paper: #151821; --ink: #e4e2dc; --muted: #9a9fb0;
    --line: #2a2e3b; --accent: #e2714f; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d0f15; --paper: #151821; --ink: #e4e2dc; --muted: #9a9fb0;
  --line: #2a2e3b; --accent: #e2714f; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
}

.bar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: 0.02em; }
.brand small { font-weight: 500; color: var(--muted); margin-left: 6px; }
#book-title { color: var(--muted); flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tools { display: flex; flex-wrap: wrap; gap: 8px; }

button, .file-btn, select {
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
}
button:hover, .file-btn:hover { border-color: var(--muted); }
button:disabled { opacity: 0.4; cursor: default; }
button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.file-btn input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.file-btn { position: relative; }

main {
  max-width: 760px;
  margin: 0 auto;
  padding-block: 24px calc(120px + var(--corner-clear, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
#status { padding: 12px 14px; border: 1px solid var(--accent); border-radius: 8px; color: var(--accent); margin-bottom: 16px; }
#page { outline: none; }

.b2-page { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.b2-scene { margin: 0; background: var(--scene-bg); position: relative; }
.b2-scene svg { display: block; width: 100%; height: auto; max-width: 100%; }
.b2-alt { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

article, .title-page { padding: 28px clamp(18px, 5vw, 56px) 40px; }
article {
  font-family: var(--font-read);
  font-size: 1.15rem;
  line-height: 1.7;
  max-width: 68ch;
  margin: 0 auto;
}
article p { margin: 0 0 0.9em; }
article h2 {
  font-family: var(--font-read);
  font-weight: 500;
  font-size: 1.5rem;
  margin: 0.2em 0 1em;
  text-wrap: balance;
}
.place {
  display: block;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--accent);
  margin-bottom: 4px;
}
blockquote { margin: 1.2em 0; padding: 0.2em 0 0.2em 1.1em; border-left: 3px solid var(--line); }
blockquote.inscription { border-left-color: var(--accent); }
hr { border: 0; border-top: 1px solid var(--line); margin: 1.8em auto; width: 40%; }
.end { text-align: center; color: var(--muted); margin-top: 2em; }

.title-page { text-align: center; padding-block: 18vh 20vh; font-family: var(--font-read); }
.title-page h1 { font-weight: 500; font-size: clamp(2.4rem, 7vw, 3.6rem); margin: 0 0 0.2em; }
.title-page .subtitle { color: var(--muted); margin: 0 0 2.4em; }
.title-page .dedication { max-width: 34ch; margin: 0 auto; line-height: 1.6; }

.nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  /* --corner-clear: room for a host's badge in the bottom corner (set by b2-reader.js) */
  padding-block: 10px calc(10px + max(env(safe-area-inset-bottom, 0px), var(--corner-clear, 0px)));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
#counter { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 92px; text-align: center; }
#contents { max-width: min(46vw, 320px); }

@media (max-width: 520px) {
  #contents { display: none; }
  article { font-size: 1.05rem; }
}

/* ---------- Scene placements inside the text ---------- */
article .b2-scene { margin: 1.4em 0; background: transparent; }
article .b2-scene svg { border-radius: 4px; }
.b2-scene[data-frame="panel"] svg { border: 3px solid #111; border-radius: 2px; background: #fff; }
.b2-scene[data-placement="float-right"],
.b2-scene[data-placement="float-left"] { width: 42%; margin-top: 0.35em; margin-bottom: 0.8em; }
.b2-scene[data-placement="float-right"] { float: right; margin-left: 1.3em; }
.b2-scene[data-placement="float-left"] { float: left; margin-right: 1.3em; }
.b2-scene[data-placement="vignette"] { float: right; width: 22%; margin: 0.2em 0 0.6em 1.1em; }
article p { overflow-wrap: break-word; }
article hr, article blockquote, b2-strip { clear: both; }
b2-strip {
  display: grid;
  grid-template-columns: repeat(var(--panels, 3), minmax(0, 1fr));
  gap: 8px;
  margin: 1.4em 0;
}
b2-strip .b2-scene { margin: 0; }
@media (max-width: 560px) {
  .b2-scene[data-placement="float-right"],
  .b2-scene[data-placement="float-left"] { float: none; width: 100%; margin: 1.2em 0; }
  .b2-scene[data-placement="vignette"] { width: 34%; }
  b2-strip { grid-template-columns: minmax(0, 1fr); }
}

/* Video scenes */
.b2-scene video { display: block; width: 100%; height: auto; border-radius: 4px; background: #111; }

/* App notes: "ready offline", "new version", install help. Sits just above the page controls. */
#app-note {
  position: fixed;
  z-index: 6;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(70px + max(env(safe-area-inset-bottom, 0px), var(--corner-clear, 0px)));
  width: min(560px, calc(100% - 32px));
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 12px 14px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  font-size: 14px;
  line-height: 1.4;
}
#app-note[hidden] { display: none; }
#app-note-text { flex: 1 1 220px; }
#app-note-action { border-color: var(--accent); color: var(--accent); }
#install { border-color: var(--accent); color: var(--accent); }

/* Install help panel */
#install-help {
  width: min(560px, calc(100% - 32px));
  max-height: calc(100% - 48px);
  overflow: auto;
  padding: 0 20px 20px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  font-size: 15px;
  line-height: 1.5;
}
#install-help::backdrop { background: rgba(8, 10, 18, 0.55); }
#install-help .help-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 0 10px;
  background: var(--paper);
}
#install-help h2 { margin: 0; font-size: 18px; }
#install-help h3 { margin: 18px 0 6px; font-size: 15px; }
#install-help .help-intro { margin: 0 0 12px; color: var(--muted); }
#install-help details { border-top: 1px solid var(--line); padding: 10px 0; }
#install-help summary { cursor: pointer; font-weight: 600; }
#install-help details[open] summary { color: var(--accent); }
#install-help ol, #install-help ul { margin: 8px 0 0; padding-left: 22px; }
#install-help li { margin: 4px 0; }
#install-help .help-tip {
  margin: 10px 0 0;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 8px;
}

/* ---------- Library (bookshelf) ---------- */
body:not([data-view="book"]) .book-only,
body[data-view="book"] .library-only { display: none; }
#library[hidden], #page[hidden] { display: none; }
#to-library { padding-inline: 10px; }
.library-loading { color: var(--muted); }
.library-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 4px 0 18px; }
.library-head h1 { font-family: var(--font-read); font-weight: 500; font-size: 1.9rem; margin: 0; }
.library-intro { margin: 0; color: var(--muted); }

.shelf {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 28px 20px;
}
.shelf-item { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.book-card { display: flex; flex-direction: column; gap: 10px; color: inherit; text-decoration: none; border-radius: 8px; }
.cover {
  position: relative;
  display: block;
  aspect-ratio: 2 / 3;
  border-radius: 4px 8px 8px 4px;
  overflow: hidden;
  background: var(--scene-bg);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 8px 22px rgba(0, 0, 0, 0.18);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.cover::after { /* the spine */
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 6px;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.28), rgba(255, 255, 255, 0.08));
}
.cover img { display: block; width: 100%; height: 100%; object-fit: cover; }
.book-card:hover .cover, .book-card:focus-visible .cover { transform: translateY(-3px); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2), 0 14px 30px rgba(0, 0, 0, 0.24); }
.cover.plain {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
  padding: 18px 14px 18px 20px;
  background: linear-gradient(160deg, #2b2f3f, #12141d);
  color: #f4efe4;
  text-align: center;
}
.plain-title { font-family: var(--font-read); font-size: 1.25rem; line-height: 1.25; overflow-wrap: anywhere; }
.plain-author { font-size: 0.8rem; opacity: 0.75; }
.book-meta { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.book-name { font-family: var(--font-read); font-size: 1.05rem; font-weight: 600; line-height: 1.3; }
.book-by { color: var(--muted); font-size: 0.85rem; }
.book-progress { color: var(--muted); font-size: 0.8rem; margin-top: 4px; }
.book-bar { display: block; height: 3px; border-radius: 2px; background: var(--line); overflow: hidden; }
.book-bar i { display: block; height: 100%; background: var(--accent); }
.shelf-item .remove { align-self: flex-start; font-size: 12px; padding: 4px 10px; color: var(--muted); }
.shelf-item .remove.armed { color: var(--accent); border-color: var(--accent); }

.add-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  aspect-ratio: 2 / 3;
  padding: 14px;
  border: 2px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  cursor: pointer;
}
.add-tile:hover, .add-tile:focus-within { border-color: var(--accent); color: var(--accent); }
.add-tile input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.add-plus { font-size: 2.2rem; line-height: 1; }
.add-title { font-weight: 600; color: var(--ink); }
.add-help { font-size: 0.78rem; line-height: 1.35; }
@media (max-width: 520px) {
  .shelf { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px 14px; }
}
a.file-btn { text-decoration: none; display: inline-block; }
.add-tile.find-tile { text-decoration: none; border-style: solid; }
.add-tile:hover, .add-tile:focus-visible { border-color: var(--accent); color: var(--accent); }
.shelf-item .update { align-self: flex-start; font-size: 12px; padding: 4px 10px; color: var(--accent); border-color: var(--accent); }

/* ---------- Find books: the B2 Library inside the reader ---------- */
.find-back { color: var(--muted); font-size: 14px; }
.find-search {
  width: 100%; font: inherit; font-size: 16px; color: var(--ink);
  background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 11px 14px;
}
.find-note { color: var(--muted); font-size: 14px; margin: 12px 0 8px; }
.find-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: 12px; }
.find-item {
  display: grid; grid-template-columns: 76px 1fr; gap: 14px; align-items: start;
  padding: 12px; background: var(--paper); border: 1px solid var(--line); border-radius: 12px;
  scroll-margin: 90px;
}
.find-item[hidden] { display: none; }
.find-item.focus { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.find-cover { width: 76px; }
.find-sub { font-size: 0.85rem; line-height: 1.35; }
.find-action { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.find-btn { font-size: 14px; font-weight: 600; padding: 7px 14px; text-decoration: none; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 8px; }
.find-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.find-btn:disabled { opacity: 0.85; cursor: progress; }
.find-have { color: var(--muted); font-size: 13px; }

/* ---------- Timeline scenes: play, drag and speed controls ---------- */
.b2-controls {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px;
  margin-top: 8px; font-family: var(--font-ui); font-size: 14px;
}
.b2-controls .b2-play { min-width: 76px; }
.b2-controls input[type="range"] { flex: 1 1 160px; min-width: 120px; accent-color: var(--accent); height: 28px; }
.b2-speeds { display: inline-flex; gap: 4px; }
.b2-speeds button { padding: 6px 9px; min-width: 40px; }
.b2-readout { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 12ch; }
.b2-controls button { min-height: 36px; }
.lede { font-style: italic; color: var(--muted); }
.note { font-family: var(--font-ui); font-size: 0.85rem; color: var(--muted); border-left: 3px solid var(--line); padding-left: 0.8em; }
.kicker { display: block; font-family: var(--font-ui); font-size: 0.75rem; font-weight: 700; letter-spacing: 0.14em; color: #00656b; margin-bottom: 4px; }
:root[data-theme="dark"] .kicker { color: #83ded8; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .kicker { color: #83ded8; } }

/* ---------- Questions ---------- */
.b2-question { display: block; margin: 1.4em 0; padding: 1em 1.1em; border: 1px solid var(--line); border-radius: 10px; }
.b2-question > p:first-child { font-weight: 600; }
.b2-question .b2-scene { margin: 0.8em 0; }
.b2-choices { display: flex; flex-wrap: wrap; gap: 8px; margin: 0.6em 0; font-family: var(--font-ui); }
.b2-choices button { min-height: 40px; }
.b2-choices button[data-result="correct"] { border-color: #2f7d4f; color: #2f7d4f; opacity: 1; font-weight: 700; }
.b2-choices button[data-result="wrong"] { border-color: var(--accent); color: var(--accent); opacity: 1; text-decoration: line-through; }
.b2-reveal { font-family: var(--font-ui); margin-top: 0.4em; }
b2-answer { display: block; margin-top: 0.6em; padding-left: 0.8em; border-left: 3px solid #2f7d4f; }
b2-answer[hidden] { display: none; }

/* ---------- Cube scenes: moves list and step controls ---------- */
.b2-scene[data-kind="cube"] svg { background: #f3f5f9; border-radius: 10px; }
.b2-moves { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; margin: 10px 0 0; padding: 0; font-family: var(--font-ui); }
.b2-moves button {
  min-width: 44px; min-height: 36px; padding: 4px 10px; font-size: 16px; font-weight: 700;
  font-variant-numeric: tabular-nums; border-radius: 8px;
}
.b2-moves button[data-state="done"] { opacity: 0.55; }
.b2-moves button[data-state="active"] { background: var(--accent); border-color: var(--accent); color: #fff; opacity: 1; }
.b2-controls .b2-step { min-width: 40px; padding: 6px 8px; }
.moves-big { font-family: var(--font-ui); font-size: 1.3rem; font-weight: 700; letter-spacing: 0.04em; margin: 0.3em 0 0.6em; }
.moves-big small { font-size: 0.8rem; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.ref-list { list-style: none; padding: 0; }
.ref-list li { padding: 0.7em 0; border-bottom: 1px solid var(--line); }
.ref-moves { float: right; font-family: var(--font-ui); font-weight: 700; letter-spacing: 0.03em; margin-left: 1em; }

/* ---------- Form scenes: counts list and step controls ---------- */
.b2-scene[data-kind="form"] svg { border-radius: 12px; }
.b2-form-ui .b2-readout { display: block; margin-top: 6px; font-family: var(--font-ui); font-size: 0.85rem; min-height: 2.6em; }
.count-list { font-family: var(--font-ui); font-size: 0.92rem; list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; }
.count-list li { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.count-list b { display: block; font-size: 1rem; }
.count-list i { font-style: normal; color: var(--muted); }
.count-list .part { font-weight: 700; color: var(--muted); margin-right: 0.2em; }
.side-l { color: #138a8a; font-weight: 600; }
.side-r { color: #b5532e; font-weight: 600; }
:root[data-theme="dark"] .side-l { color: #5cc7c4; }
:root[data-theme="dark"] .side-r { color: #e48a63; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .side-l { color: #5cc7c4; }
  :root:not([data-theme="light"]) .side-r { color: #e48a63; }
}
