/* Page structure, ported from the Notes site's layout:
   - desktop / tablet: a powder-blue left sidebar (title, search pill, theme
     button, page list) next to the content column;
   - phones (≤ 800px): an iOS-style app with a floating Liquid Glass tab bar
     at the bottom and a full-screen search sheet (section 7).
   Sidebar markup: renderHeader() in js/components.js. Behaviour: js/ui.js. */

/* Frosted glass. Never put backdrop-filter on an element that contains
   position: fixed children (the sidebar holds the search sheet and the menu
   drawer): it would become their containing block and trap them inside it.
   The phone tab bar puts its glass on the capsule inside it instead. */

/* ═════════════ 1. Page grid ═════════════ */
html {
  --sb-left: max(0px, calc((100vw - 1500px) / 2));
  --sb-width: 290px;
}

body {
  display: grid;
  grid-template-columns: var(--sb-width) minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "warn warn"
    "side main"
    "side foot";
  column-gap: 5px;
  max-width: 1500px;
  margin: 0 auto;
}

/* Tablet: wider sidebar, and the content keeps a little room on the right. */
@media (min-width: 801px) and (max-width: 1200px) {
  html { --sb-width: 320px; }
  body { padding-right: 1rem; }
}

/* The sidebar's colour and border come from a fixed strip behind it, a full
   screen taller than the window at both ends, so it also fills the
   over-scroll area and there's no seam when the page bounces. */
@media (min-width: 801px) {
  body::before {
    content: "";
    position: fixed;
    top: -100vh;
    height: 300vh;
    left: var(--sb-left);
    width: var(--sb-width);
    border-right: 1px solid var(--ui-sidebar-edge);
    background: var(--ui-sidebar);
    z-index: -1;
    pointer-events: none;
    transition:
      background-color var(--dur-slow) var(--ease),
      border-color var(--dur-slow) var(--ease);
  }
}

main {
  grid-area: main;
  min-width: 0;
  padding: 6rem 2.5rem 4rem;
  /* Components size themselves to the content column, not the window */
  container: main / inline-size;
  animation: ui-page-enter var(--dur-slow) var(--ease-out) backwards;
}

.storage-warning { grid-area: warn; }

/* Cross-fade colours when toggling light/dark instead of snapping. */
body,
.site-sidebar,
.site-sidebar::before,
.search-button,
.darkmode,
.tab-capsule,
.explorer-content,
.site-footer,
.stat,
.tracklist,
.tracklist-head,
.home-aside .song-list,
#search-results,
.list-label,
.tag {
  transition:
    background-color var(--dur-slow) var(--ease),
    border-color var(--dur-slow) var(--ease),
    color var(--dur-slow) var(--ease),
    box-shadow var(--dur-slow) var(--ease);
}

/* Tap targets behave like app controls: no text selection on press */
.site-sidebar,
.button,
.song-row,
.track-row,
.album-card,
.result-card,
.stat,
.tag {
  -webkit-user-select: none;
  user-select: none;
}

/* ═════════════ 2. Left sidebar ═════════════ */
.site-sidebar {
  grid-area: side;
  align-self: start;
  position: sticky;
  top: 0;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding: 2.25rem 1.25rem 2rem;
  /* Scrolling over the sidebar never moves the page: overscroll is
     contained, and the 1px overhang below keeps it always scrollable
     (browsers only honour overscroll-behavior on scrollable boxes). */
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
}

.site-sidebar::-webkit-scrollbar { display: none; }

.site-sidebar::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: calc(100% + 1px);
  pointer-events: none;
}

.page-title {
  margin: 0;
  font-family: var(--headerFont);
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.2;
}

.page-title a {
  color: var(--ui-heading);
  font-weight: inherit;
  text-decoration: none;
}

/* ═════════════ 3. Toolbar: search pill + round theme button ═════════════ */
.toolbar {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.search {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
}

.search-button,
.darkmode {
  border: 1px solid var(--ui-border);
  background: var(--ui-sheen), var(--ui-surface);
  box-shadow: var(--ui-shadow-sm), var(--ui-highlight);
  cursor: pointer;
  transition:
    transform var(--dur) var(--spring),
    box-shadow var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    background-color var(--dur-slow) var(--ease),
    color var(--dur-slow) var(--ease);
}

.search-button:hover,
.darkmode:hover {
  border-color: var(--ui-border-strong);
  box-shadow: var(--ui-shadow), var(--ui-highlight);
}

.search-button {
  display: flex;
  align-items: center;
  width: 100%;
  height: 2.25rem;
  margin: 0;
  padding: 0 1rem 0 0;
  border-radius: 999px;
  color: var(--ui-muted);
  font: inherit;
  text-align: inherit;
  white-space: nowrap;
}

.search-button > p {
  display: inline;
  margin: 0;
  color: var(--ui-faint);
  font-weight: 450;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-button svg {
  width: 18px;
  min-width: 18px;
  margin: 0 0.5rem;
}

.search-button svg .search-path {
  stroke: var(--ui-muted);
  stroke-width: 1.5px;
  transition: stroke 0.5s ease;
}

.search-button:active { transform: scale(0.97); }

.darkmode {
  position: relative;
  flex-shrink: 0;
  width: 2.25rem;
  height: 2.25rem;
  margin: 0;
  padding: 0;
  border-radius: 50%;
}

.darkmode svg {
  position: absolute;
  width: 17px;
  height: 17px;
  top: calc(50% - 8.5px);
  left: calc(50% - 8.5px);
  fill: var(--ui-muted);
  stroke: var(--ui-muted);
  animation: ui-icon-in var(--dur-slow) var(--spring);
  transition:
    opacity var(--dur) var(--ease),
    transform var(--dur-slow) var(--spring);
}

.darkmode:hover svg {
  fill: var(--ui-heading);
  stroke: var(--ui-heading);
}

.darkmode:active { transform: scale(0.92); }

/* Sun in light mode, moon in dark mode. */
.darkmode > .nightIcon { display: none; }
:root[saved-theme="dark"] .darkmode > .dayIcon { display: none; }
:root[saved-theme="dark"] .darkmode > .nightIcon { display: inline; }

/* ═════════════ 4. Explorer: the page list ═════════════ */
.explorer {
  display: flex;
  flex-direction: column;
  min-height: 1.2rem;
  flex: 0 1 auto;
}

.explorer-title {
  margin: 0.25rem 0 0;
  font-family: var(--headerFont);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ui-faint);
}


.explorer-content {
  margin-top: 0.5rem;
  position: relative;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
}

.explorer-content::-webkit-scrollbar { display: none; }

.explorer-content::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: calc(100% + 1px);
  pointer-events: none;
}

.explorer-ul {
  list-style: none;
  margin: 0;
  padding: 0;
  overscroll-behavior: contain;
}

.explorer-ul li { margin: 0; }

/* Page rows: the link is the whole row, so the click area matches the hover. */
.explorer-ul li > a {
  display: block;
  width: 100%;
  padding: 0.24rem 0.55rem;
  border-radius: var(--radius-sm);
  font-size: 0.91rem;
  font-weight: 400;
  color: var(--ui-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-decoration: none;
  transition:
    background-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease),
    transform var(--dur) var(--spring);
}

.explorer-ul li > a:hover {
  background-color: var(--ui-hover);
  color: var(--ui-heading);
  transform: translateX(2px);
}

/* Current page: a glossy pill (raven in light mode, poppy in dark) */
.explorer-ul li > a.active {
  background: var(--ui-sheen), var(--ui-pill-bg);
  background-size: 100% 100%, auto;
  color: var(--ui-pill-text);
  font-weight: 550;
  box-shadow:
    var(--ui-shadow-sm),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  transform: none;
}

/* ═════════════ 5. Search sheet (frosted glass) ═════════════ */
.search-container {
  position: fixed;
  contain: layout;
  z-index: 999;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: none;
  background: var(--ui-scrim);
  --ui-backdrop: blur(12px) saturate(140%);
  -webkit-backdrop-filter: var(--ui-backdrop);
  backdrop-filter: var(--ui-backdrop);
}

.search-container.active {
  display: block;
  animation: ui-fade-in var(--dur) var(--ease) both;
}

.search-container.active > .search-space {
  animation: ui-sheet-in var(--dur-slow) var(--ease-out) both;
}

/* Search open: lock the page, and stop the root rubber-band so dragging the
   backdrop doesn't bounce the page underneath. */
html:has(.search-container.active) {
  overflow-y: hidden;
  overscroll-behavior: none;
}

.search-container > .search-space {
  position: relative;
  width: 65%;
  max-width: 760px;
  margin: 12vh auto 0;
}

@media not all and (min-width: 1200px) {
  .search-container > .search-space { width: 90%; }
}

/* The search box and the results card. Radius, shadow, blur and gap are
   custom properties so the phone sheet (section 7) can change them. */
.search-container > .search-space > * {
  display: block;
  width: 100%;
  border: 1px solid var(--ui-border);
  border-radius: var(--sheet-radius, var(--radius-lg));
  background: var(--ui-sheen), var(--ui-glass-strong);
  background-size: 100% 80px, auto;
  background-repeat: no-repeat;
  --ui-backdrop: var(--sheet-blur, blur(30px) saturate(180%));
  -webkit-backdrop-filter: var(--ui-backdrop);
  backdrop-filter: var(--ui-backdrop);
  box-shadow: var(--sheet-shadow, var(--ui-shadow-lg)), var(--ui-highlight);
  overflow: hidden;
  margin-bottom: var(--sheet-item-gap, 1rem);
}

.search-container > .search-space > input.search-bar {
  font-family: var(--bodyFont);
  font-size: 1.15rem;
  padding: 0.9rem 1.2rem;
  color: var(--ui-heading);
  outline: none;
}

.search-container > .search-space > input.search-bar:focus {
  border-color: var(--ui-border-strong);
  box-shadow: var(--sheet-shadow, var(--ui-shadow-lg)), var(--ui-highlight);
}

.search-container > .search-space > input.search-bar::placeholder {
  color: var(--ui-faint);
}

.search-container > .search-space > .search-layout { display: none; }

.search-container > .search-space > .search-layout.display-results { display: block; }

.results-container {
  max-height: 63vh;
  overflow-y: auto;
  overscroll-behavior: contain;
}

/* Cancel button: phones only (section 7). Desktop closes with Escape or a
   click on the backdrop. */
.search-cancel { display: none; }

/* ═════════════ 6. Footer ═════════════ */
.site-footer {
  grid-area: foot;
  min-width: 0;
  padding: 0 2.5rem calc(1.5rem + env(safe-area-inset-bottom));
  color: var(--ui-muted);
  font-size: 0.8rem;
}

.footer-inner {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid;
  border-image: linear-gradient(90deg, transparent, var(--ui-border-strong), transparent) 1;
}

/* ═════════════ 7. Phones: an iOS app ═════════════ */
/* No top bar and no menu drawer. Each page leads with its large title; a
   floating Liquid Glass tab bar sits at the bottom (the pages, plus a round
   search button beside it, like iOS 26); the theme button sits at the top
   right of the page like a navigation-bar button and scrolls away with it.
   Search opens as a full-screen sheet. */
.tab-bar { display: none; }

@media (max-width: 800px) {
  :root {
    --tabbar-h: 62px;
    /* Floats just above the home indicator, never flush with the edge */
    --tabbar-gap: max(12px, calc(env(safe-area-inset-bottom) - 6px));
    scroll-padding-top: calc(env(safe-area-inset-top) + 1rem);
    scroll-padding-bottom: calc(var(--tabbar-h) + var(--tabbar-gap) + 1rem);
  }

  body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "warn"
      "main"
      "foot";
    padding: env(safe-area-inset-top) max(1rem, env(safe-area-inset-right)) 0
      max(1rem, env(safe-area-inset-left));
  }

  /* Room at the bottom so the last row can scroll clear of the tab bar */
  main { padding: 1rem 0 calc(var(--tabbar-h) + var(--tabbar-gap) + 2.5rem); }

  /* Apps don't have footers: the tab bar is the bottom of the screen. */
  .site-footer { display: none; }

  /* The sidebar steps out of the layout; its pieces are placed one by one. */
  .site-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    display: block;
    height: 0;
    padding: 0;
    overflow: visible;
  }

  .site-sidebar::after { content: none; }

  .page-title,
  .explorer,
  .search-button { display: none; }

  .toolbar { display: block; }

  /* Liquid Glass: frosted powder tint, lit from above, with a bright rim. */
  .tab-capsule,
  .tab-search,
  .darkmode {
    border: 1px solid var(--ui-liquid-rim);
    background: var(--ui-liquid-sheen), var(--ui-glass);
    --ui-backdrop: blur(24px) saturate(180%);
    -webkit-backdrop-filter: var(--ui-backdrop);
    backdrop-filter: var(--ui-backdrop);
    box-shadow: var(--ui-liquid-shadow), var(--ui-highlight);
  }

  .darkmode:hover {
    border-color: var(--ui-liquid-rim);
    box-shadow: var(--ui-liquid-shadow), var(--ui-highlight);
  }

  /* Theme button: top right of the page, like a navigation-bar button. */
  .darkmode {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 1rem);
    right: max(1rem, env(safe-area-inset-right));
    width: 2.5rem;
    height: 2.5rem;
  }

  .darkmode svg {
    width: 18px;
    height: 18px;
    top: calc(50% - 9px);
    left: calc(50% - 9px);
  }

  /* Large titles and the back button keep clear of it. */
  .hero h1,
  .page-head h1,
  .prose > h1:first-child { padding-right: 3rem; }

  /* ---------- Tab bar ---------- */
  .tab-bar {
    position: fixed;
    z-index: 10;
    left: max(14px, env(safe-area-inset-left));
    right: max(14px, env(safe-area-inset-right));
    bottom: var(--tabbar-gap);
    display: flex;
    gap: 10px;
    height: var(--tabbar-h);
    max-width: 520px;
    margin: 0 auto;
    -webkit-user-select: none;
    user-select: none;
  }

  /* While typing, the keyboard covers the bottom like in iOS apps, so the
     tab bar hides instead of riding up above the keyboard. */
  body:has(input:not([type="range"]):not([type="file"]):focus, textarea:focus, select:focus) .tab-bar {
    display: none;
  }

  .tab-capsule {
    position: relative;
    flex: 1;
    min-width: 0;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    padding: 4px;
    border-radius: 999px;
  }

  /* The lens behind the current tab glides to the new tab on a spring. The
     tab colours switch at once, so two tabs never look selected. */
  .tab-lens {
    position: absolute;
    top: 4px;
    bottom: 4px;
    left: 4px;
    width: calc((100% - 8px) / 4);
    border-radius: 999px;
    background: var(--ui-liquid-lens);
    box-shadow: var(--ui-shadow-sm), var(--ui-liquid-lens-edge);
    transform: translateX(calc(var(--tab-index, 0) * 100%));
    transition:
      transform var(--dur-slow) var(--spring),
      opacity var(--dur) var(--ease);
  }

  .tab-capsule.no-active .tab-lens { opacity: 0; }

  .tab {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-width: 0;
    border-radius: 999px;
    color: var(--ui-muted);
    font-size: 0.66rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    line-height: 1.1;
    text-decoration: none;
  }

  .tab span {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .tab svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  /* Current tab: tinted poppy, like an iOS app's accent colour */
  .tab.active { color: var(--ui-accent); }
  .tab.active svg { stroke-width: 2.1; }

  .tab-search {
    flex: 0 0 var(--tabbar-h);
    display: grid;
    place-items: center;
    padding: 0;
    border-radius: 50%;
    color: var(--ui-heading);
    cursor: pointer;
    transition:
      transform var(--dur) var(--spring),
      background-color var(--dur-slow) var(--ease),
      border-color var(--dur-slow) var(--ease),
      box-shadow var(--dur-slow) var(--ease);
  }

  .tab-search svg {
    width: 22px;
    height: 22px;
  }

  .tab-search svg .search-path {
    stroke: currentColor;
    stroke-width: 1.7px;
  }

  .tab-search:active,
  .darkmode:active { transform: scale(0.9); }

  /* ---------- Page transitions (direction set by js/nav.js) ---------- */
  html[data-nav="push"] main { animation: ios-push-in 440ms var(--ease-ios) backwards; }
  html[data-nav="pop"] main { animation: ios-pop-in 440ms var(--ease-ios) backwards; }
  html[data-nav="tab"] main { animation: ui-fade-in 200ms var(--ease) backwards; }
  html[data-nav="none"] main { animation: none; }

  /* ---------- Search: a full-screen, app-style sheet ----------
       [ search field ………………… ] Cancel
       ┌ results (only this scrolls) ┐
       └─────────────────────────────┘   ← ends above the keyboard
     The sheet always covers the whole screen; only its contents are inset
     to the visible area (--vv-top and --kb, set by js/ui.js while the
     keyboard is up). js/ui.js also cancels drags that would pan the page. */
  .search-container {
    --sheet-gap: 0.75rem;
    --sheet-top: calc(var(--sheet-gap) + env(safe-area-inset-top));
    --field-h: 2.75rem;
    --cancel-w: 4.75rem;
    --sheet-item-gap: var(--sheet-gap);
    --sheet-radius: 14px;
    --sheet-shadow: var(--ui-shadow-sm);
    --sheet-blur: none; /* the sheet itself is blurred; don't blur twice */
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: auto;
    height: auto;
    padding-top: var(--vv-top, 0px);
    /* Keyboard up: stop above it. Down: clear the home indicator. */
    padding-bottom: max(var(--kb, 0px), env(safe-area-inset-bottom));
    overflow: hidden;
    background: var(--ui-drawer-tint);
    --ui-backdrop: blur(40px) saturate(160%);
    touch-action: none;
  }

  .search-container > .search-space {
    display: flex;
    flex-direction: column;
    width: auto;
    max-width: none;
    height: calc(100% - var(--sheet-top));
    margin: var(--sheet-top) max(1rem, env(safe-area-inset-right)) 0
      max(1rem, env(safe-area-inset-left));
  }

  /* 17px: iOS zooms the page when focusing a field smaller than 16px. */
  .search-container > .search-space > input.search-bar {
    flex: none;
    max-width: calc(100% - var(--cancel-w));
    height: var(--field-h);
    padding: 0 0.9rem;
    font-size: 17px;
  }

  .search-container > .search-space > .search-layout {
    flex: 0 1 auto;
    min-height: 0;
  }

  .search-container > .search-space > .search-layout.display-results { display: flex; }

  .search-layout > .results-container {
    flex: 0 1 auto;
    width: 100%;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }

  .results-container .result-card { padding: 0.85rem 1rem; }
  .results-container .result-card:active { background: var(--ui-active); }

  .search-container > .search-cancel {
    position: absolute;
    /* Absolute boxes ignore the sheet's padding, so add the inset here too. */
    top: calc(var(--vv-top, 0px) + var(--sheet-top));
    right: max(1rem, env(safe-area-inset-right));
    display: flex;
    align-items: center;
    justify-content: flex-end;
    width: var(--cancel-w);
    height: var(--field-h);
    padding: 0;
    border: 0;
    background: none;
    color: var(--ui-link);
    font-family: var(--bodyFont);
    font-size: 17px;
    font-weight: 500;
    cursor: pointer;
    transition: opacity var(--dur-fast) var(--ease);
  }

  .search-container > .search-cancel:active { opacity: 0.45; }
}

/* ═════════════ 8. Page layouts ═════════════ */

/* Home */
.hero { padding-bottom: 2.5rem; }

.hero-sub {
  color: var(--ui-muted);
  font-size: 1.15rem;
}

.home-layout {
  display: grid;
  /* minmax(0, …) lets long titles truncate instead of widening the column */
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 3rem;
  align-items: start;
}

.home-aside {
  position: sticky;
  top: 2.25rem;
}

.home-aside h2 { margin-bottom: 1rem; }

.section-head,
.page-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.section-head h2,
.page-head h1 { margin: 0; }

.page-head p { margin: 0; }

/* Grids */
.album-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 1rem;
}

.song-list {
  display: flex;
  flex-direction: column;
}

/* Prose pages (About) */
.prose { max-width: calc(720px + 5rem); }

.prose p,
.prose li {
  line-height: 1.7;
}

.prose h2 { margin-top: 2.2rem; }

@container main (max-width: 860px) {
  .home-layout { grid-template-columns: minmax(0, 1fr); }
  .home-aside { position: static; }
}

/* Small columns: two albums per row, always fitting */
@container main (max-width: 540px) {
  .album-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
  }
}
