:root {
  color-scheme: light;
  --bg: #f4f7f3;
  --ink: #1f2933;
  --muted: #637083;
  --panel: #ffffff;
  --line: #d9e1d6;
  --primary: #1f7a6a;
  --primary-strong: #165f53;
  --accent: #d45067;
  --shadow: 0 18px 45px rgba(31, 41, 51, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(244, 247, 243, 0.94), rgba(246, 240, 229, 0.86)),
    #f4f7f3;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

[hidden] { display: none !important; }

.mode-nav {
  position: fixed;
  top: 36px;
  left: 50%;
  z-index: 40;
  display: flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255,255,255,.94);
  box-shadow: 0 8px 24px rgba(31,41,51,.12);
  transform: translateX(-50%);
}

.mode-nav button { min-height: 38px; padding: 0 14px; background: transparent; color: var(--muted); }
.mode-nav button[aria-current="page"] { background: var(--primary); color: white; }

.parent-gate { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 20px; background: rgba(31,41,51,.48); backdrop-filter: blur(8px); }
.parent-gate-card { display: grid; gap: 14px; width: min(420px, 100%); padding: 24px; border: 1px solid var(--line); border-radius: 14px; background: #fff; box-shadow: var(--shadow); }
.parent-gate-card h1, .parent-gate-card p { margin: 0; }
.parent-gate-card > p:not(.library-kicker, .library-status) { color: var(--muted); line-height: 1.45; }
.parent-gate-card label { display: grid; gap: 6px; color: var(--muted); font-size: .82rem; font-weight: 850; }
.parent-gate-card input { width: 100%; min-height: 48px; border: 1px solid var(--line); border-radius: 8px; padding: 0 12px; font: inherit; font-size: 1.15rem; letter-spacing: .2em; }
.parent-gate-card input:focus { border-color: var(--primary); outline: 3px solid rgba(31,122,106,.2); }
.parent-gate-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.parent-gate-actions button:first-child { background: #edf1ee; color: var(--primary-strong); }
.parent-gate-link { min-height: 34px; padding: 0; background: transparent; color: var(--primary-strong); font-size: .86rem; text-decoration: underline; }
.parent-gate-link:hover { background: transparent; color: var(--primary); }

.library-view { min-height: 100vh; padding: 112px max(24px, calc((100vw - 1100px) / 2)) 48px; }
.library-header { display: flex; align-items: end; justify-content: space-between; gap: 24px; margin-bottom: 20px; }
.library-header h1, .library-header p, .library-card h2, .library-card p { margin: 0; }
.library-header h1 { margin: 4px 0 8px; font-size: clamp(2rem, 5vw, 3.5rem); }
.library-header > div > p:last-child { max-width: 650px; color: var(--muted); line-height: 1.5; }
.library-kicker { color: var(--primary-strong); font-size: .78rem; font-weight: 900; text-transform: uppercase; }
.library-count { flex: 0 0 auto; padding: 10px 14px; border-radius: 999px; background: #e5f3ee; color: var(--primary-strong); font-weight: 850; }
.library-status { min-height: 24px; margin: 0 0 12px; color: var(--primary-strong); font-weight: 750; }
.library-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.library-section { display: grid; gap: 16px; margin-top: 26px; }
.library-section + .library-section { padding-top: 28px; border-top: 1px solid var(--line); }
.library-section-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; }
.library-section-heading h2, .library-section-heading p { margin: 0; }
.library-section-heading h2 { margin-top: 4px; font-size: 1.5rem; }
.library-section-heading > p { color: var(--muted); font-size: .88rem; font-weight: 700; }
.library-heading-action { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .88rem; font-weight: 700; }
.library-heading-action p { margin: 0; }
.family-library { padding-bottom: 24px; }
.add-family-book { width: 48px; min-height: 48px; padding: 0; border-radius: 999px; font-size: 1.8rem; line-height: 1; }
.family-book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.family-book-card { display: grid; gap: 9px; min-height: 150px; padding: 18px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255,255,255,.9); box-shadow: 0 8px 24px rgba(31,41,51,.08); }
.family-book-card h3, .family-book-card p { margin: 0; }
.family-book-card h3 { font-size: 1rem; line-height: 1.35; }
.family-book-card p { color: var(--muted); font-size: .82rem; }
.family-book-status { align-self: end; width: max-content; padding: 5px 9px; border-radius: 999px; background: #edf1ee; color: var(--primary-strong); font-size: .75rem; font-weight: 850; text-transform: capitalize; }
.family-book-status[data-status="failed"] { background: #fbe8eb; color: #9f2438; }
.family-book-status[data-status="succeeded"] { background: #e5f3ee; color: var(--primary-strong); }
.delete-family-job-button { align-self: end; margin-top: 4px; background: #fbe8eb; color: #9f2438; }
.family-book-empty { margin: 0; padding: 22px; border: 1px dashed var(--line); border-radius: 12px; color: var(--muted); font-weight: 750; text-align: center; }
.family-book-popover { position: fixed; inset: 50% auto auto 50%; z-index: 70; width: min(680px, calc(100vw - 28px)); max-height: calc(100vh - 40px); overflow: auto; padding: 20px; border: 1px solid var(--line); border-radius: 16px; background: #fff; box-shadow: 0 26px 80px rgba(31,41,51,.3); transform: translate(-50%, -50%); }
.family-book-popover::backdrop { background: rgba(31,41,51,.5); }
.family-popover-heading { display: flex; align-items: start; justify-content: space-between; gap: 20px; margin-bottom: 16px; }
.family-popover-heading h3, .family-popover-heading p { margin: 0; }
.family-popover-heading h3 { margin-top: 4px; font-size: 1.4rem; }
.family-popover-heading > button { width: 38px; min-height: 38px; padding: 0; border-radius: 999px; background: #edf1ee; color: var(--primary-strong); font-size: 1.5rem; }
.family-tabs { display: flex; gap: 5px; padding: 5px; border-radius: 12px; background: #edf1ee; }
.family-tabs button { flex: 1; min-height: 42px; padding: 7px; background: transparent; color: var(--muted); font-size: .82rem; }
.family-tabs button[aria-selected="true"] { background: var(--primary); color: white; }
.family-tab-panel { margin-top: 14px; }
.family-tab-panel form { display: grid; gap: 13px; }
.family-tab-panel label { display: grid; gap: 6px; color: var(--muted); font-size: .84rem; font-weight: 850; }
.family-tab-panel input, .family-tab-panel textarea { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 11px 12px; color: var(--ink); font: inherit; }
.family-tab-panel textarea { min-height: 120px; resize: vertical; }
.family-tab-panel output { min-height: 1.2em; color: var(--primary-strong); font-size: .84rem; font-weight: 700; }
.family-form-hint { margin: -5px 0 0; color: var(--muted); font-size: .8rem; }
.family-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.family-actions button { padding: 0 18px; }
.family-actions button + button { background: #fff; border: 1px solid var(--primary); color: var(--primary-strong); }
.story-draft-form { display: grid; gap: 14px; width: min(680px, 100%); padding: 18px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255,255,255,.9); }
.story-draft-form label { display: grid; gap: 7px; color: var(--muted); font-size: .84rem; font-weight: 850; }
.story-draft-form input, .story-draft-form textarea { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 11px 12px; background: #fff; color: var(--ink); font: inherit; resize: vertical; }
.story-draft-form input:focus, .story-draft-form textarea:focus { border-color: var(--primary); outline: 3px solid rgba(31,122,106,.18); }
.story-form-actions { display: flex; justify-content: flex-end; gap: 10px; }
.story-form-actions button { padding: 0 18px; }
.story-form-actions button:first-child { background: #edf1ee; color: var(--primary-strong); }
.library-card { display: grid; grid-template-columns: 92px 1fr; gap: 14px; padding: 14px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255,255,255,.9); box-shadow: 0 8px 24px rgba(31,41,51,.08); }
.library-card img { width: 92px; height: 118px; border-radius: 7px; object-fit: cover; background: #fdfbf6; }
.library-card img:not([src]) { border: 1px dashed var(--line); }
.library-card > div { display: grid; align-content: start; gap: 8px; }
.library-card h2 { font-size: 1rem; line-height: 1.25; }
.library-card p { color: var(--muted); font-size: .8rem; line-height: 1.4; }
.library-card button { align-self: end; width: 100%; margin-top: auto; }
.library-card .return-button { background: #edf1ee; color: var(--primary-strong); }
.library-card-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(88px, 1fr)); gap: 7px; align-self: end; margin-top: auto; }
.library-card-actions .delete-book-button { background: #fbe8eb; color: #9f2438; }
.profile-role { margin: 0; color: var(--primary-strong); font-size: .8rem; font-weight: 850; }

button {
  font: inherit;
}

.reader-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 24px;
  min-height: 100vh;
  padding: 24px;
}

.bookshelf,
.stage {
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.bookshelf {
  position: fixed;
  top: 24px;
  left: 24px;
  z-index: 30;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 16px;
  width: min(340px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  min-height: 0;
  padding: 18px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transform: translateX(calc(-100% - 32px));
  transition: opacity 160ms ease, transform 180ms ease;
}

.is-shelf-open .bookshelf {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0);
}

.bookshelf-toggle {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 3;
  min-width: 92px;
  padding: 0 16px;
  box-shadow: 0 10px 24px rgba(31, 41, 51, 0.16);
}

.profile-control { position: fixed; top: 36px; right: 24px; z-index: 45; }
.profile-toggle { min-width: 76px; min-height: 44px; padding: 0 14px; box-shadow: 0 10px 24px rgba(31, 41, 51, 0.16); }
.profile-menu { position: absolute; top: 52px; right: 0; display: grid; gap: 10px; width: min(280px, calc(100vw - 48px)); padding: 18px; border: 1px solid var(--line); border-radius: 10px; background: rgba(255,255,255,.96); box-shadow: var(--shadow); user-select: text; }
.profile-menu[hidden] { display: none; }
.profile-label, .profile-name, .profile-email { margin: 0; }
.profile-label { color: var(--primary-strong); font-size: .75rem; font-weight: 900; text-transform: uppercase; }
.profile-name { font-size: 1.05rem; font-weight: 850; overflow-wrap: anywhere; }
.profile-email { color: var(--muted); font-size: .85rem; overflow-wrap: anywhere; }
.profile-level { display: grid; gap: 6px; color: var(--muted); font-size: .82rem; font-weight: 800; }
.profile-level select { min-height: 40px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; background: #fff; color: var(--ink); font: inherit; }
.profile-menu form button { width: 100%; background: #edf4ef; color: var(--primary-strong); }
.profile-secondary-button { width: 100%; background: #edf4ef; color: var(--primary-strong); }
.reset-pin-form { display: grid; gap: 9px; padding-top: 10px; border-top: 1px solid var(--line); }
.reset-pin-form label { display: grid; gap: 5px; color: var(--muted); font-size: .78rem; font-weight: 850; }
.reset-pin-form input { width: 100%; min-height: 40px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; font: inherit; letter-spacing: .15em; }
.profile-form-status { min-height: 1em; margin: 0; color: #9f2438; font-size: .78rem; font-weight: 750; }

.bookshelf-scrim {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(31, 41, 51, 0.18);
}

.bookshelf-scrim[hidden] {
  display: none;
}

.bookshelf-heading {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 36px;
  align-items: baseline;
  gap: 12px;
}

.bookshelf h1,
.bookshelf p {
  margin: 0;
}

.bookshelf h1 {
  font-size: 1.25rem;
}

#bookShelfCount {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
  white-space: nowrap;
}

.bookshelf-close {
  align-self: center;
  width: 36px;
  min-height: 36px;
  border-radius: 999px;
  background: #edf4ef;
  color: var(--primary-strong);
  font-size: 1.5rem;
  line-height: 1;
}

.bookshelf-close:hover {
  background: #dfeee8;
}

.book-grid {
  display: grid;
  align-content: start;
  gap: 12px;
  min-height: 0;
  max-height: calc(100vh - 200px);
  overflow: auto;
  padding-right: 2px;
  overscroll-behavior: contain;
}

.login-body {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.login-shell {
  width: min(420px, 100%);
}

.login-panel {
  display: grid;
  gap: 16px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.login-kicker,
.login-panel h1,
.login-hint,
.login-error,
.login-success {
  margin: 0;
}

.login-kicker {
  color: var(--primary-strong);
  font-size: 0.82rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.login-panel h1 {
  font-size: 1.65rem;
  line-height: 1.15;
}

.login-hint,
.login-error,
.login-success {
  font-size: 0.95rem;
  font-weight: 750;
  line-height: 1.45;
}

.login-hint,
.login-hint a {
  color: var(--muted);
}

.login-error {
  color: #9f2438;
}

.login-success {
  color: var(--primary-strong);
}

.login-actions {
  display: grid;
  gap: 10px;
}

.local-login {
  display: grid;
  gap: 12px;
}

.local-login label {
  display: grid;
  gap: 6px;
}

.local-login span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 850;
}

.local-login input {
  min-height: 44px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: #fffdf8;
  color: var(--ink);
  font: inherit;
}

.local-login input:focus {
  border-color: var(--primary);
  outline: 3px solid rgba(31, 122, 106, 0.22);
}

.local-login button {
  width: 100%;
}

.login-button {
  display: grid;
  min-height: 48px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-weight: 850;
  text-decoration: none;
}

.login-button:hover {
  border-color: rgba(31, 122, 106, 0.34);
  background: #f7fbf8;
}

.login-button.is-disabled {
  pointer-events: none;
  opacity: 0.48;
}

.book-card {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr);
  grid-template-rows: 1fr auto;
  gap: 4px 12px;
  min-height: 86px;
  padding: 10px;
  border: 1px solid transparent;
  background: #fffdf8;
  color: var(--ink);
  text-align: left;
}

.book-card:hover {
  border-color: rgba(31, 122, 106, 0.32);
  background: #ffffff;
}

.book-card[aria-pressed="true"] {
  border-color: var(--primary);
  background: #eef8f5;
}

.book-card img {
  grid-row: 1 / 3;
  width: 58px;
  height: 66px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fdfbf6;
  object-fit: cover;
}

.book-card-title,
.book-card-meta {
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-card-title {
  align-self: end;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 850;
  line-height: 1.2;
}

.book-card-meta {
  align-self: start;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.stage {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  min-width: 0;
  min-height: calc(100vh - 48px);
  padding: 22px;
  border-radius: 8px;
}

.book-meta {
  position: absolute;
  top: 10px;
  right: 102px;
  z-index: 3;
  display: grid;
  grid-template-columns: auto 0fr;
  align-items: center;
  max-width: min(440px, calc(100% - 136px));
  min-height: 44px;
  overflow: hidden;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  box-shadow: none;
  color: var(--primary-strong);
  cursor: default;
  transition: grid-template-columns 180ms ease, background 180ms ease, box-shadow 180ms ease;
  backdrop-filter: blur(10px);
}

.book-meta:hover,
.book-meta:focus {
  grid-template-columns: auto minmax(0, 1fr);
  background: transparent;
  box-shadow: none;
  outline: 0;
}

.book-meta:focus-visible {
  outline: 3px solid rgba(31, 122, 106, 0.24);
}

.book-meta-label {
  display: grid;
  place-items: center;
  width: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 1.45rem;
  font-weight: 850;
}

.book-meta-details {
  display: grid;
  gap: 2px;
  min-width: 0;
  overflow: hidden;
  padding-right: 14px;
  opacity: 0;
  transform: translateX(8px);
  transition: opacity 160ms ease, transform 180ms ease;
}

.book-meta:hover .book-meta-details,
.book-meta:focus .book-meta-details {
  opacity: 1;
  transform: translateX(0);
}

.book-title,
.page-count {
  margin: 0;
}

.book-title {
  color: var(--primary-strong);
  overflow: hidden;
  font-size: 1rem;
  font-weight: 800;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-count {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}

.image-frame {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fdfbf6;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#pageImage {
  display: block;
  width: 100%;
  height: 100%;
  max-height: calc(100vh - 94px);
  object-fit: contain;
}

.playback-pane {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 2;
  display: grid;
  grid-template-columns: 52px minmax(78px, 118px) 52px;
  grid-template-rows: auto auto;
  align-items: center;
  justify-content: center;
  gap: 8px 12px;
  width: min(278px, calc(100% - 32px));
  padding: 10px;
  border: 1px solid rgba(255, 255, 255, 0.48);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.2);
  box-shadow: 0 14px 34px rgba(31, 41, 51, 0.12);
  transform: translate(-50%, -50%);
  transition: opacity 160ms ease, transform 180ms ease, visibility 180ms ease;
  backdrop-filter: blur(12px);
  -webkit-tap-highlight-color: transparent;
}

.is-playback-pane-hidden .playback-pane {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -46%) scale(0.96);
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: rgba(31, 122, 106, 0.82);
  color: white;
  font-weight: 800;
  cursor: pointer;
}

button:hover {
  background: rgba(22, 95, 83, 0.92);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

#prevButton,
#playButton,
#nextButton {
  border: 1px solid rgba(255, 255, 255, 0.56);
  background: rgba(255, 255, 255, 0.22);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.48),
    0 8px 20px rgba(31, 41, 51, 0.16);
  color: rgba(31, 41, 51, 0.88);
  backdrop-filter: blur(12px);
}

#prevButton:hover,
#playButton:hover,
#nextButton:hover {
  background: rgba(255, 255, 255, 0.34);
}

#prevButton,
#nextButton {
  width: 52px;
  font-size: 2rem;
  line-height: 1;
}

#playButton {
  font-size: 3rem;
  line-height: 1;
  min-height: 54px;
  min-width: 78px;
}

.playback-options {
  grid-column: 1 / -1;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 14px;
}

.playback-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 28px;
  color: white;
  font-weight: 800;
  white-space: nowrap;
}

.playback-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

#playbackStatus {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
  min-height: 1.2em;
  text-align: center;
}

@media (max-width: 900px) {
  .reader-shell {
    grid-template-columns: 1fr;
    padding: 14px;
  }

  .stage {
    min-height: calc(100vh - 28px);
    padding: 14px;
  }

  .bookshelf {
    top: 14px;
    left: 14px;
    max-height: calc(100vh - 28px);
  }

  .playback-pane {
    grid-template-columns: 48px minmax(72px, 104px) 48px;
    width: min(260px, calc(100% - 28px));
  }

  #pageImage {
    max-height: calc(100vh - 58px);
  }
}

@media (max-width: 520px) {
  .mode-nav { top: 18px; }
  .mode-nav button { padding: 0 10px; font-size: .82rem; }
  .library-view { padding: 88px 14px 28px; }
  .library-header { align-items: start; flex-direction: column; gap: 14px; }
  .bookshelf-toggle {
    top: 10px;
    left: 10px;
    min-width: 78px;
    padding: 0 12px;
  }

  .book-meta {
    top: 6px;
    right: 96px;
    max-width: calc(100% - 112px);
  }

  .profile-control { top: 10px; right: 10px; }

  .book-meta-label {
    width: 40px;
    min-width: 40px;
    min-height: 40px;
    padding: 0;
  }

  .playback-pane {
    grid-template-columns: 44px 1fr 44px;
    width: min(280px, calc(100% - 20px));
    gap: 8px;
    padding: 8px;
  }

  .playback-options {
    justify-content: center;
  }
}
