/* ============================================================================
   base.css — reset, typography, app shell layout, navigation.
   ========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: var(--lh-body);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-wrap: break-word;
}

h1, h2, h3, h4 { margin: 0; line-height: var(--lh-tight); font-weight: var(--fw-semi); }
h1 { font-size: var(--fs-2xl); letter-spacing: -0.02em; text-wrap: balance; }
h2 { font-size: var(--fs-xl); letter-spacing: -0.01em; text-wrap: balance; }
h3 { font-size: var(--fs-lg); }
p  { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }

a { color: var(--primary); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-color: var(--primary); }

img, svg { max-width: 100%; }
svg { fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
button:disabled { cursor: not-allowed; }

/* Focus: never removed, always 3:1 against both adjacent surfaces. */
:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-radius: var(--r-xs);
}
:focus:not(:focus-visible) { outline: none; }

/* Sticky headers must not hide the focused control (WCAG 2.2 — focus not obscured). */
html { scroll-padding-top: calc(var(--topbar-h) + var(--sp-4)); scroll-behavior: smooth; }

::selection { background: var(--primary-soft); color: var(--primary-soft-fg); }

/* Scrollbars — subtle, themed, never layout-shifting. */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: var(--r-full); border: 3px solid transparent; background-clip: content-box; }
*::-webkit-scrollbar-track { background: transparent; }

.skip-link {
  position: fixed; top: var(--sp-2); left: var(--sp-2); z-index: 100;
  transform: translateY(-200%);
  background: var(--primary); color: var(--primary-fg);
  padding: var(--sp-3) var(--sp-4); border-radius: var(--r-md);
  font-weight: var(--fw-semi); text-decoration: none;
  transition: transform var(--dur-fast) var(--ease-out);
}
.skip-link:focus { transform: translateY(0); }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* ============================================================================
   App shell
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "top" "main";
  min-height: 100dvh;
}

/* ---- Top bar ------------------------------------------------------------ */
.topbar {
  grid-area: top;
  position: sticky; top: 0; z-index: var(--z-nav);
  display: flex; align-items: center; gap: var(--sp-2);
  height: var(--topbar-h);
  padding: 0 var(--sp-3);
  padding-left: max(var(--sp-3), env(safe-area-inset-left));
  padding-right: max(var(--sp-3), env(safe-area-inset-right));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--border);
}
@supports not (backdrop-filter: blur(1px)) { .topbar { background: var(--surface); } }

.topbar__spacer { flex: 1 1 auto; }

.brand { display: flex; align-items: center; gap: var(--sp-3); text-decoration: none; color: inherit; padding: var(--sp-1) var(--sp-2); border-radius: var(--r-md); min-width: 0; }
.brand:hover { background: var(--surface-hover); }
.brand__mark {
  display: grid; place-items: center; flex: none;
  width: 32px; height: 32px; border-radius: var(--r-sm);
  background: var(--primary); color: var(--primary-fg);
}
.brand__mark svg { width: 20px; height: 20px; }
.brand__text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand__text strong { font-size: var(--fs-base); font-weight: var(--fw-bold); letter-spacing: -0.01em; }
.brand__text small { font-size: var(--fs-xs); color: var(--text-subtle); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 420px) { .brand__text { display: none; } }

.search-trigger {
  display: flex; align-items: center; gap: var(--sp-2);
  height: 36px; padding: 0 var(--sp-2) 0 var(--sp-3);
  border: 1px solid var(--border); border-radius: var(--r-md);
  background: var(--surface-2); color: var(--text-subtle);
  font-size: var(--fs-sm); min-width: 180px;
  transition: border-color var(--dur-fast), background var(--dur-fast), color var(--dur-fast);
}
.search-trigger:hover { border-color: var(--border-strong); color: var(--text-muted); background: var(--surface-hover); }
.search-trigger svg { width: 16px; height: 16px; flex: none; }
.search-trigger span { flex: 1; text-align: left; }
.search-trigger kbd {
  font-family: var(--font-sans); font-size: 11px; font-weight: var(--fw-semi);
  padding: 2px 6px; border-radius: var(--r-xs);
  background: var(--surface); border: 1px solid var(--border); color: var(--text-subtle);
}
@media (max-width: 720px) {
  .search-trigger { min-width: 0; width: 36px; padding: 0; justify-content: center; }
  .search-trigger span, .search-trigger kbd { display: none; }
}

.icon-btn {
  display: grid; place-items: center; flex: none;
  width: 40px; height: 40px; border-radius: var(--r-md);
  color: var(--text-muted);
  transition: background var(--dur-fast), color var(--dur-fast);
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 20px; height: 20px; }

.icon-moon { display: none; }
:root.is-dark .icon-sun { display: none; }
:root.is-dark .icon-moon { display: block; }

.topbar__menu { display: grid; }

/* ---- Side navigation ---------------------------------------------------- */
.sidenav {
  grid-area: nav;
  position: fixed; inset: var(--topbar-h) auto 0 0; z-index: var(--z-drawer);
  width: var(--sidenav-w);
  display: flex; flex-direction: column;
  padding: var(--sp-3) var(--sp-3) var(--sp-4);
  padding-left: max(var(--sp-3), env(safe-area-inset-left));
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto; overscroll-behavior: contain;
  transform: translateX(-100%);
  visibility: hidden;
  /* visibility must not be interpolated: a transitioned `visibility` stays
     `hidden` partway through the animation, and a hidden element silently
     refuses focus — so the drawer would open unfocusable for keyboard and
     screen-reader users. Instead flip it instantly on open, and delay the
     flip to hidden until the slide-out has finished. */
  transition: transform var(--dur-slow) var(--ease-out),
              visibility 0s linear var(--dur-slow);
}
.sidenav.is-open {
  transform: translateX(0);
  visibility: visible;
  transition: transform var(--dur-slow) var(--ease-out), visibility 0s;
}

.sidenav__list { display: flex; flex-direction: column; gap: 2px; flex: 1; }

.navlink {
  display: flex; align-items: center; gap: var(--sp-3);
  min-height: var(--row-h); padding: 0 var(--sp-3);
  border-radius: var(--r-md);
  color: var(--text-muted); text-decoration: none;
  font-size: var(--fs-base); font-weight: var(--fw-medium);
  transition: background var(--dur-fast), color var(--dur-fast);
}
.navlink svg { width: 19px; height: 19px; flex: none; }
.navlink:hover { background: var(--surface-hover); color: var(--text); }
.navlink[aria-current="page"] {
  background: var(--primary-soft); color: var(--primary-soft-fg); font-weight: var(--fw-semi);
}
.navlink__badge {
  margin-left: auto; flex: none;
  min-width: 22px; height: 20px; padding: 0 6px;
  display: grid; place-items: center;
  border-radius: var(--r-full);
  background: var(--surface-2); color: var(--text-muted);
  font-size: var(--fs-xs); font-weight: var(--fw-semi);
  font-variant-numeric: tabular-nums;
}
.navlink[aria-current="page"] .navlink__badge { background: var(--primary); color: var(--primary-fg); }
.navlink__badge.is-alert { background: var(--danger); color: var(--danger-fg); }

.sidenav__foot { padding-top: var(--sp-3); border-top: 1px solid var(--border); margin-top: var(--sp-3); }
.storage-note { font-size: var(--fs-xs); color: var(--text-subtle); line-height: var(--lh-snug); }

.scrim {
  /* Starts below the top bar so the menu button stays visible and clickable
     while the drawer is open — the same control opens and closes it. */
  position: fixed; inset: var(--topbar-h) 0 0 0; z-index: var(--z-scrim);
  background: var(--scrim-bg);
  animation: fade-in var(--dur-base) var(--ease-out);
}
.scrim[hidden] { display: none; }

/* ---- Main --------------------------------------------------------------- */
.main {
  grid-area: main;
  min-width: 0;
  padding: var(--sp-5) var(--sp-4);
  padding-left: max(var(--sp-4), env(safe-area-inset-left));
  padding-right: max(var(--sp-4), env(safe-area-inset-right));
  padding-bottom: calc(var(--tabbar-h) + var(--sp-8) + env(safe-area-inset-bottom));
}
.main:focus { outline: none; }
.view-host { max-width: var(--content-max); margin: 0 auto; }

.boot { display: grid; place-items: center; gap: var(--sp-4); padding: var(--sp-16) 0; color: var(--text-subtle); }
.boot__spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--primary);
  animation: spin 720ms linear infinite;
}

/* ---- Bottom tab bar (small screens) ------------------------------------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: var(--z-nav);
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
@supports not (backdrop-filter: blur(1px)) { .tabbar { background: var(--surface); } }

.tab {
  flex: 1 1 0; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; padding: var(--sp-2) 2px;
  color: var(--text-subtle); text-decoration: none;
  font-size: 11px; font-weight: var(--fw-medium);
  transition: color var(--dur-fast);
  position: relative;
}
.tab svg { width: 22px; height: 22px; }
.tab span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.tab:hover { color: var(--text-muted); }
.tab[aria-current="page"] { color: var(--primary); font-weight: var(--fw-semi); }
.tab[aria-current="page"]::before {
  content: ""; position: absolute; top: 0; left: 50%; width: 26px; height: 3px;
  transform: translateX(-50%); border-radius: 0 0 var(--r-full) var(--r-full);
  background: var(--primary);
}
.tab__dot {
  position: absolute; top: 6px; left: 50%; margin-left: 6px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--danger); border: 2px solid var(--surface);
}

/* ============================================================================
   Responsive: ≥1024px promotes the drawer to a permanent sidebar and drops
   the bottom bar (Material adaptive navigation).
   ========================================================================== */
@media (min-width: 1024px) {
  .app {
    grid-template-columns: var(--sidenav-w) 1fr;
    grid-template-areas: "top top" "nav main";
  }
  .sidenav {
    position: sticky; inset: auto; height: calc(100dvh - var(--topbar-h));
    transform: none; visibility: visible; transition: none;
  }
  .topbar__menu { display: none; }
  .tabbar { display: none; }
  .main { padding: var(--sp-6) var(--sp-8) var(--sp-16); }
  .scrim { display: none !important; }
}

@media (min-width: 640px) and (max-width: 1023.98px) {
  .main { padding-left: var(--sp-6); padding-right: var(--sp-6); }
}

/* ============================================================================
   Motion preferences — honour the OS setting everywhere at once.
   ========================================================================== */
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
:root[data-motion="off"] *, :root[data-motion="off"] *::before, :root[data-motion="off"] *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

.noscript {
  max-width: 40ch; margin: 20vh auto; padding: var(--sp-6);
  text-align: center; color: var(--text);
}

/* ============================================================================
   Sign-in and friends stand alone — no nav, no tab bar, nothing to wander off
   into before you have an account.
   ========================================================================== */
.app.is-auth .sidenav,
.app.is-auth .tabbar,
.app.is-auth .topbar__menu,
.app.is-auth .search-trigger { display: none !important; }

.app.is-auth { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
.app.is-auth .main { padding-bottom: var(--sp-8); }
