/* ============================================================
   APP SHELL — bottom tab bar + screen host + full-screen reader overlay.
   The native-app spine. Builds on the foundation.css design tokens; adds a
   little extra "juice": a floating, elevated tab bar, an animated active pill,
   a tactile center "+" FAB, and spring-y screen transitions.
   ============================================================ */

:root {
  --tabbar-h: 64px;
  --shell-max: 720px;            /* content column cap on big screens */
  --spring: var(--dur-slow) var(--ease-spring);
  --fab-grad: var(--accent-2); /* flat gold — no gradient (flat fills read modern) */
}

/* The shell owns the viewport; screens scroll inside, the bar stays put. */
#app-shell {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}
/* Hidden until the app reveals (boot cover) + while the reader overlay is up. */
html.app-booting #app-shell { visibility: hidden; }
body.reader-open #app-shell { visibility: hidden; pointer-events: none; }

/* ---- Screen host: the active tab mounts here, scrolls independently ---- */
#screen-host {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;            /* each .screen owns its own scroll */
}
.screen {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: calc(env(safe-area-inset-top) + 14px) 18px calc(var(--tabbar-h) + 24px);
  /* Off-state: shifted + faded, non-interactive. Active screen springs in. */
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 220ms ease, transform var(--spring);
}
.screen.active {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}
/* Centre the content column on tablets/desktop. */
.screen > .screen-inner {
  width: 100%;
  max-width: var(--shell-max);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
/* ScreenHeader — THE one canonical screen title/header (DESIGN-SYSTEM.md §3 /
   §5 P2). Title + optional trailing action. Every screen (Home, Library, Stats,
   Settings, Add) uses this; `.ds-screen-header` is the canonical alias name,
   `.screen-head` the established class kept so no call site regresses. Per-screen
   tweaks (the Stats gradient title, the Home logotype) layer on top of this base. */
.screen-head, .ds-screen-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 2px 2px;
}
.screen-head h2, .ds-screen-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

/* ============================================================
   TAB BAR — floating, elevated, with an animated active pill + center FAB.
   ============================================================ */
#tab-bar {
  flex-shrink: 0;
  position: relative;
  z-index: 30;
  display: flex;
  align-items: stretch;
  justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(18px);
  -webkit-backdrop-filter: saturate(1.4) blur(18px);
  border-top: 1px solid var(--border-soft);
  box-shadow: 0 -12px 34px rgba(0, 0, 0, 0.34);
}
.tab {
  flex: 1;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: transparent;
  color: var(--faint);
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  position: relative;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--tap);
}
.tab > svg { width: 24px; height: 24px; transition: transform var(--spring), color var(--tap); }
.tab > .tab-label { transition: opacity var(--tap); opacity: 0.9; }
/* Tabs are <button>s, so the generic foundation `button`/`button:hover` gold fill
   would bleed onto them (a gold blob on hover/active). Keep them transparent. */
.tab, .tab:hover, .tab.active { background: transparent; }
.tab:hover { color: var(--muted); }
.tab.active { color: var(--accent-2-strong); }
.tab.active > svg { color: var(--accent-2-strong); transform: translateY(-1px); }
/* Active indicator: a clean 4px gold dot under the label — restrained, like a
   real app's tab bar (the gold icon+label already carries the active state). No
   glowing pill (it read as a heavy gold blob). */
.tab.active::before {
  content: "";
  position: absolute;
  bottom: 5px;
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--accent-2);
  animation: tab-pop var(--spring);
}
@keyframes tab-pop { from { transform: scale(0); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.tab:active > svg { transform: scale(0.86); }

/* Center "+" FAB — the Add/capture action; lifts above the bar. */
.tab-add {
  flex: 0 0 auto;
  align-self: center;
  width: 56px; height: 56px;
  margin: 0 6px;
  transform: translateY(-14px);
  border-radius: 50%;
  border: none;
  background: var(--fab-grad);
  color: var(--on-accent);
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255,255,255,0.22);
  transition: transform var(--spring), filter var(--tap), box-shadow var(--tap);
  -webkit-tap-highlight-color: transparent;
}
.tab-add > svg { width: 26px; height: 26px; }
.tab-add:hover { filter: brightness(1.06); box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5); }
.tab-add:active { transform: translateY(-14px) scale(0.92); }
.tab-add:focus-visible, .tab:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--accent-2-soft); }

/* ============================================================
   ACCOUNT — folded INTO the tab bar so navigation is ONE system.
   The widget (#sr-auth-status) is reparented into #tab-bar by
   js/cloud/bootstrap.js (placeAccountStatus). On mobile it becomes the
   last bottom-tab item (avatar + "Account" label), matching every other
   tab's metrics + gold language; its menu opens UPWARD out of the bar.
   The desktop treatment lives in the ≥860px block below.
   ============================================================ */
#tab-bar #sr-auth-status {
  /* Sit in the bar like a tab: same flex weight + column rhythm as .tab. */
  position: relative;
  flex: 1;
  align-self: stretch;
  z-index: 31;            /* above the bar so the upward menu isn't clipped visually */
}
#tab-bar #sr-auth-status.show { display: flex; }
#tab-bar .sr-account-toggle {
  /* Mirror .tab exactly: full-height column, faint until active, gold on open. */
  width: 100%;
  height: 100%;
  min-height: 0;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--tap);
}
/* Foundation's generic button:hover gold-fill would blob this — keep it flat. */
#tab-bar .sr-account-toggle,
#tab-bar .sr-account-toggle:hover { background: transparent; }
#tab-bar .sr-account-toggle:hover { color: var(--muted); }
#tab-bar #sr-auth-status.open .sr-account-toggle { color: var(--accent-2-strong); }
/* The avatar = the tab "icon": a 24px gold-ringed initial chip, tab-icon sized. */
#tab-bar .sr-avatar {
  width: 24px; height: 24px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%;
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  line-height: 1;
  transition: border-color var(--tap), color var(--tap), transform var(--spring), box-shadow var(--tap);
}
#tab-bar #sr-auth-status.open .sr-avatar {
  border-color: var(--accent-2);
  color: var(--accent-2-strong);
  transform: translateY(-1px);
  box-shadow: 0 0 0 3px var(--accent-2-soft);
}
#tab-bar .sr-account-tab-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.01em;
  opacity: 0.9;
}
#tab-bar .sr-account-toggle:active .sr-avatar { transform: scale(0.86); }
#tab-bar .sr-account-toggle:focus-visible { outline: none; }
#tab-bar .sr-account-toggle:focus-visible .sr-avatar { box-shadow: 0 0 0 3px var(--accent-2-soft); }
/* Menu opens UPWARD out of the bottom bar (right-anchored so it never clips the
   right edge), styled as a panel popover in the tab-bar's own surface language. */
#tab-bar .sr-account-menu {
  position: absolute;
  bottom: calc(100% + 10px);
  right: 8px;
  min-width: 224px;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.52);
}
#tab-bar .sr-account-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
#tab-bar .sr-auth-email { font-size: 13.5px; color: var(--text); max-width: 232px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tab-bar .sr-signout,
#tab-bar .sr-open-settings {
  margin-top: 2px; font-size: 13px; padding: 8px 12px; cursor: pointer; border-radius: 8px; text-align: left;
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border-soft);
  transition: border-color var(--tap), background var(--tap);
}
#tab-bar .sr-signout:hover,
#tab-bar .sr-open-settings:hover { border-color: var(--accent-2); background: var(--elevate); }
/* Checkbox-style account menu items ("Resume last book on launch", "Show load times").
   The leading box fills gold with a checkmark when on. */
#tab-bar .sr-resume-last,
#tab-bar .sr-show-load-times {
  margin-top: 2px; font-size: 13px; padding: 8px 12px 8px 34px; cursor: pointer; border-radius: 8px; text-align: left;
  position: relative;
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border-soft);
  transition: border-color var(--tap), background var(--tap);
}
#tab-bar .sr-resume-last:hover,
#tab-bar .sr-show-load-times:hover { border-color: var(--accent-2); background: var(--elevate); }
#tab-bar .sr-resume-last::before,
#tab-bar .sr-show-load-times::before {
  content: ""; position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
  width: 15px; height: 15px; border-radius: 4px;
  border: 1.5px solid var(--border); background: transparent;
  transition: background var(--tap), border-color var(--tap);
}
#tab-bar .sr-resume-last.on::before,
#tab-bar .sr-show-load-times.on::before { background: var(--accent-2); border-color: var(--accent-2); }
#tab-bar .sr-resume-last.on::after,
#tab-bar .sr-show-load-times.on::after {
  content: ""; position: absolute; left: 16.5px; top: 48%; transform: translateY(-60%) rotate(45deg);
  width: 4px; height: 8px; border: solid var(--bg, #0e1320); border-width: 0 2px 2px 0;
}

/* ============================================================
   ADD bottom sheet — capture without leaving your place.
   ============================================================ */
#add-sheet-backdrop {
  position: fixed; inset: 0; z-index: 38;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none;
  transition: opacity 220ms ease;
}
body.add-open #add-sheet-backdrop { opacity: 1; pointer-events: auto; }
#add-sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 39;
  max-height: 86dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--panel);
  border-top-left-radius: 22px; border-top-right-radius: 22px;
  border-top: 1px solid var(--border);
  box-shadow: 0 -18px 50px rgba(0, 0, 0, 0.55);
  padding: 10px 18px calc(22px + env(safe-area-inset-bottom));
  transform: translateY(110%);
  transition: transform var(--spring);
}
body.add-open #add-sheet { transform: translateY(0); }
/* The grip is the drag handle (shell.js drag-to-dismiss): a generous, touch-only
   hit area around the visible pill so it's easy to grab; the pill itself is the
   ::before. touch-action:none lets the drag own the vertical gesture cleanly. */
#add-sheet .sheet-grip {
  position: relative;
  height: 26px; margin: -4px auto 8px;
  display: block; width: 100%;
  cursor: grab; touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
#add-sheet .sheet-grip:active { cursor: grabbing; }
#add-sheet .sheet-grip::before {
  content: ""; position: absolute; left: 50%; top: 11px; transform: translateX(-50%);
  width: 40px; height: 4px; border-radius: 999px; background: var(--border);
}

/* ---- Reader overlay sits above the shell, edge-to-edge ---- */
.reader { z-index: 50; }

/* ============================================================
   Wide screens (≥860px): the bottom tab bar becomes a horizontal TOP NAV BAR —
   a real catalogue site's header (Letterboxd/Goodreads/StoryGraph): wordmark
   left, a few nav items, a clear primary "New" action on the right, content
   below. Restrained, tokens only.
   ============================================================ */
@media (min-width: 860px) {
  /* #tab-bar is AFTER #screen-host in the DOM; column-reverse lifts the bar to
     the TOP and lets the app fill the space below it. */
  #app-shell { flex-direction: column-reverse; }

  /* ---- The horizontal top bar ---- */
  #tab-bar {
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
    height: 60px;
    width: 100%;
    flex: 0 0 60px;
    /* The account avatar now sits IN the bar flow (far right), not as a fixed
       overlay, so no reserved gutter is needed — just even horizontal padding. */
    padding: 0 20px 0 24px;
    background: color-mix(in srgb, var(--panel) 92%, transparent);
    backdrop-filter: saturate(1.4) blur(18px);
    -webkit-backdrop-filter: saturate(1.4) blur(18px);
    border-top: none;
    border-bottom: 1px solid var(--border-soft);
    box-shadow: none;
  }

  /* ---- enrapt wordmark on the LEFT of the bar (CSS-only, Berkshire Swash) ---- */
  #tab-bar::before {
    content: "enrapt";
    /* Web-safe serif fallback, not `cursive` (= Comic Sans MS on Windows). */
    font-family: "Berkshire Swash", Georgia, "Times New Roman", serif;
    font-size: 22px;
    line-height: 1;
    color: var(--text);
    margin-right: 28px;
    padding: 0;
    letter-spacing: 0.01em;
  }

  /* ---- Each nav item: icon + label inline, sitting left after the wordmark ---- */
  .tab {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: 8px;
    height: 38px;
    padding: 0 14px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    letter-spacing: -0.005em;
    color: var(--muted);
    transition: background var(--tap), color var(--tap);
  }
  .tab > svg { width: 20px; height: 20px; flex: 0 0 auto; }
  .tab > .tab-label { opacity: 1; }
  .tab:hover { background: var(--elevate); color: var(--text); }
  .tab:hover > svg { color: var(--text); }

  /* Active item: a quiet gold text treatment (icon + label) — no heavy fill. */
  .tab.active { background: transparent; color: var(--accent-2-strong); }
  .tab.active > svg { color: var(--accent-2-strong); transform: none; }
  /* The mobile gold dot is meaningless in the top bar — the gold text carries it. */
  .tab.active::before { display: none; }

  /* ---- "+" add action → a clear PRIMARY gold button pinned to the RIGHT ---- */
  .tab-add {
    flex: 0 0 auto;
    align-self: center;
    margin-left: auto;        /* push the primary action to the far right */
    width: auto;
    height: 38px;
    padding: 0 16px;
    transform: none;
    border-radius: var(--radius-sm);
    background: var(--accent-2);
    color: var(--on-accent);
    display: inline-flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 650;
    box-shadow: var(--shadow-sm);
  }
  .tab-add > svg { width: 20px; height: 20px; }
  .tab-add::after { content: "New"; }
  .tab-add:hover { filter: brightness(1.06); box-shadow: var(--shadow-sm); transform: none; }
  .tab-add:active { transform: scale(0.98); }

  #add-sheet { left: auto; right: 24px; bottom: 24px; width: 420px; border-radius: 18px; border: 1px solid var(--border); }
  body.add-open #add-sheet { transform: none; }

  /* ---- Account on DESKTOP: a clean circular avatar aligned at the far RIGHT of
     the same top chrome — part of the bar, not a floating overlay. It's the last
     #tab-bar child, so it naturally trails the primary "New" action. ---- */
  #tab-bar #sr-auth-status {
    flex: 0 0 auto;
    align-self: center;
    margin-left: 14px;        /* small, even gap after the New button */
    z-index: 31;
  }
  #tab-bar .sr-account-toggle {
    flex-direction: row;       /* avatar only — label hidden below */
    width: 36px;
    height: 36px;
    border-radius: 50%;
    gap: 0;
  }
  /* On desktop the wordmark + tabs carry identity; the avatar speaks for itself. */
  #tab-bar .sr-account-tab-label { display: none; }
  #tab-bar .sr-avatar {
    width: 36px;
    height: 36px;
    font-size: 13px;
    background: var(--panel-2);
    border-color: var(--border);
  }
  #tab-bar .sr-account-toggle:hover .sr-avatar { border-color: var(--accent-2); color: var(--text); }
  #tab-bar #sr-auth-status.open .sr-avatar { transform: none; }
  /* Desktop bar is at the TOP, so the menu drops DOWN from the avatar. */
  #tab-bar .sr-account-menu {
    bottom: auto;
    top: calc(100% + 10px);
    right: 0;
  }
}
