/* Phone-first dark UI. Everything above the grid is fixed-position chrome so
   the scroller can own the whole viewport and never reflow while scrolling. */

:root {
  --bg: #0d0d0f;
  --bg-elev: #17171b;
  --bg-elev2: #232329;
  --fg: #f2f2f5;
  --fg-dim: #9b9ba6;
  --accent: #7aa2ff;
  --danger: #ff6b6b;
  --tile-gap: 3px;
  --topbar-h: 56px;
  --nav-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --bg-elev: #f4f4f7;
    --bg-elev2: #e7e7ee;
    --fg: #16161a;
    --fg-dim: #6a6a76;
    --accent: #2f5fe0;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

.hidden { display: none !important; }
.muted { color: var(--fg-dim); }
.err { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; font-size: 13px; }

svg {
  width: 22px; height: 22px; display: block;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

button, input { font: inherit; color: inherit; }

.icon-btn {
  background: none; border: 0; padding: 11px; border-radius: 50%;
  color: var(--fg); display: grid; place-items: center; cursor: pointer;
  flex: 0 0 auto; text-decoration: none;
}
.icon-btn:active { background: var(--bg-elev2); }
.icon-btn.on { color: var(--accent); }
.icon-btn.on svg { fill: currentColor; stroke: currentColor; }

/* ------------------------------------------------------------------ gate */

.gate {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; background: var(--bg); padding: 24px;
}
.gate-card { width: min(340px, 100%); text-align: center; }
.gate-logo {
  width: 60px; height: 60px; margin: 0 auto 18px; border-radius: 18px;
  background: linear-gradient(135deg, #7aa2ff, #b06bff 60%, #ff8fab);
}
.gate h1 { margin: 0 0 4px; font-size: 26px; font-weight: 600; }
.gate p { margin: 0 0 22px; font-size: 14px; }
#pin {
  width: 100%; padding: 14px; text-align: center; letter-spacing: 8px;
  font-size: 22px; border-radius: 12px; border: 1px solid var(--bg-elev2);
  background: var(--bg-elev); outline: none;
}
#pin:focus { border-color: var(--accent); }
.btn-primary {
  width: 100%; margin-top: 12px; padding: 14px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; font-weight: 600; cursor: pointer;
}
.btn-primary:active { filter: brightness(0.9); }

/* ---------------------------------------------------------------- topbar */

.app { height: 100%; }

.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 30;
  padding-top: var(--safe-top);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  border-bottom: 1px solid color-mix(in srgb, var(--fg) 8%, transparent);
}
.topbar-row {
  height: var(--topbar-h); display: flex; align-items: center; gap: 2px;
  padding: 0 6px 0 14px;
}
.title {
  flex: 1 1 auto; font-size: 20px; font-weight: 600; letter-spacing: -0.2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#search-input {
  flex: 1 1 auto; min-width: 0; height: 40px; padding: 0 14px;
  border-radius: 20px; border: 0; background: var(--bg-elev); outline: none;
}
#search-input::-webkit-search-cancel-button { filter: invert(0.6); }

.scanbar {
  position: relative; height: 22px; display: flex; align-items: center;
  padding: 0 14px; font-size: 11px; color: var(--fg-dim);
  background: var(--bg-elev); overflow: hidden;
}
#scanfill {
  position: absolute; inset: 0 auto 0 0; width: 0;
  background: color-mix(in srgb, var(--accent) 28%, transparent);
  transition: width .4s ease;
}
#scantext { position: relative; }

/* -------------------------------------------------------------- scroller */

.scroller {
  position: absolute; inset: 0;
  padding-top: calc(var(--topbar-h) + var(--safe-top));
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom));
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  /* The grid is absolutely positioned inside .canvas, so the browser never
     has to lay out 300k nodes - only the ~60 that are on screen. */
  contain: layout style paint;
}
.scroller.has-scan { padding-top: calc(var(--topbar-h) + var(--safe-top) + 22px); }
.canvas { position: relative; width: 100%; }

.day-head {
  position: absolute; left: 0; right: 0; display: flex; align-items: flex-end;
  padding: 12px 14px 6px; font-size: 13px; font-weight: 600; color: var(--fg);
}
.day-head .count { margin-left: 8px; font-weight: 400; color: var(--fg-dim); font-size: 12px; }

.tile {
  position: absolute; overflow: hidden; background: var(--bg-elev);
  cursor: pointer; contain: strict;
}
.tile img {
  width: 100%; height: 100%; object-fit: cover; display: block;
  opacity: 0; transition: opacity .18s ease;
}
.tile img.in { opacity: 1; }
.tile .dur {
  position: absolute; right: 4px; bottom: 3px; font-size: 11px; font-weight: 600;
  color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,.8); pointer-events: none;
}
.tile .fav {
  position: absolute; left: 4px; bottom: 3px; width: 14px; height: 14px;
  color: #fff; filter: drop-shadow(0 1px 2px rgba(0,0,0,.8)); pointer-events: none;
}
.tile .fav svg { width: 14px; height: 14px; fill: #fff; stroke: none; }
.tile.sel img { transform: scale(.82); border-radius: 8px; transition: transform .12s ease; }
.tile .check {
  position: absolute; right: 4px; top: 4px; width: 20px; height: 20px;
  border-radius: 50%; border: 2px solid rgba(255,255,255,.9);
  background: rgba(0,0,0,.25); display: none;
}
.selecting .tile .check { display: block; }
.tile.sel .check { background: var(--accent); border-color: var(--accent); }
.tile.sel .check::after {
  content: ""; position: absolute; left: 5px; top: 2px; width: 5px; height: 10px;
  border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(42deg);
}

.empty {
  position: absolute; inset: 0; display: grid; place-items: center;
  text-align: center; padding: 40px; color: var(--fg-dim); pointer-events: none;
}

/* ------------------------------------------------------------- list views */

.list { padding: 8px 0 20px; }
.row {
  display: flex; align-items: center; gap: 14px; padding: 14px 16px;
  cursor: pointer; border-bottom: 1px solid color-mix(in srgb, var(--fg) 6%, transparent);
}
.row:active { background: var(--bg-elev); }
.row .grow { flex: 1 1 auto; min-width: 0; }
.row .grow b { display: block; font-weight: 500; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.row .grow small { color: var(--fg-dim); }
.row .thumb {
  width: 52px; height: 52px; border-radius: 10px; object-fit: cover;
  background: var(--bg-elev2); flex: 0 0 auto;
}
.section-title {
  padding: 18px 16px 6px; font-size: 12px; font-weight: 700; letter-spacing: .08em;
  text-transform: uppercase; color: var(--fg-dim);
}
.album-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px; padding: 14px;
}
.album-card { cursor: pointer; }
.album-card .cover {
  width: 100%; aspect-ratio: 1; border-radius: 14px; object-fit: cover;
  background: var(--bg-elev2); display: block;
}
.album-card b { display: block; margin-top: 7px; font-weight: 500; font-size: 14px; }
.album-card small { color: var(--fg-dim); }

/* --------------------------------------------------------------- scrubber */

.scrub {
  position: fixed; right: 0; z-index: 25; width: 44px;
  top: calc(var(--topbar-h) + var(--safe-top) + 8px);
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 8px);
  touch-action: none;
}
.scrub-thumb {
  position: absolute; right: 6px; width: 32px; height: 32px; margin-top: -16px;
  border-radius: 16px; background: var(--bg-elev2);
  box-shadow: 0 2px 10px rgba(0,0,0,.4);
  opacity: 0; transition: opacity .25s ease; display: grid; place-items: center;
}
.scrub-thumb::after {
  content: ""; width: 12px; height: 2px; border-radius: 2px; background: var(--fg-dim);
  box-shadow: 0 -4px 0 var(--fg-dim), 0 4px 0 var(--fg-dim);
}
.scrub.show .scrub-thumb, .scrub.drag .scrub-thumb { opacity: 1; }
.scrub.drag .scrub-thumb { background: var(--accent); }
.scrub.drag .scrub-thumb::after { background: #fff; box-shadow: 0 -4px 0 #fff, 0 4px 0 #fff; }
.scrub-label {
  position: absolute; right: 46px; margin-top: -17px; padding: 7px 12px;
  border-radius: 17px; background: var(--accent); color: #fff;
  font-weight: 600; font-size: 14px; white-space: nowrap;
  box-shadow: 0 3px 14px rgba(0,0,0,.45);
}

/* -------------------------------------------------------------------- nav */

.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  border-top: 1px solid color-mix(in srgb, var(--fg) 8%, transparent);
}
.nav-btn {
  flex: 1; height: var(--nav-h); border: 0; background: none; cursor: pointer;
  display: grid; place-items: center; gap: 2px; color: var(--fg-dim);
  font-size: 10.5px; padding: 6px 0;
}
.nav-btn.active { color: var(--accent); }
.nav-btn.active svg { fill: color-mix(in srgb, var(--accent) 18%, transparent); }

/* ----------------------------------------------------------------- viewer */

.viewer { position: fixed; inset: 0; z-index: 60; background: #000; color: #fff; }
.viewer-track {
  position: absolute; inset: 0;
  /* touch-action:none, not pan-y: with pan-y the browser holds each touch
     back until it has decided the gesture is not a vertical scroll, which
     shows up as a dead zone at the start of every swipe. Vertical drag is
     handled in JS anyway. */
  touch-action: none;
  /* Promote to its own compositor layer so dragging moves an existing
     texture instead of repainting a full-screen photo every frame. */
  will-change: transform;
  transform: translate3d(0, 0, 0);
}
.viewer-page {
  position: absolute; top: 0; bottom: 0; width: 100%;
  display: grid; place-items: center; overflow: hidden;
  contain: layout paint;
}
.viewer-page img, .viewer-page video {
  max-width: 100%; max-height: 100%; display: block;
  object-fit: contain;
}
.viewer-page .spin {
  position: absolute; width: 30px; height: 30px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.2); border-top-color: #fff;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.viewer-top, .viewer-bottom {
  position: absolute; left: 0; right: 0; z-index: 2; display: flex;
  align-items: center; gap: 4px; transition: opacity .2s ease;
}
.viewer-top {
  top: 0; padding: var(--safe-top) 8px 8px;
  background: linear-gradient(rgba(0,0,0,.65), transparent);
}
.viewer-bottom {
  bottom: 0; padding: 10px 8px calc(10px + var(--safe-bottom));
  justify-content: space-around;
  background: linear-gradient(transparent, rgba(0,0,0,.65));
}
.viewer.chrome-off .viewer-top, .viewer.chrome-off .viewer-bottom,
.viewer.chrome-off .v-nav { opacity: 0; pointer-events: none; }

/* Previous/next arrows. Also the visible hint that the left and right edges
   of the screen are tappable. */
.v-nav {
  position: absolute; top: 50%; z-index: 2; margin-top: -24px;
  width: 48px; height: 48px; padding: 0; border: 0; border-radius: 50%;
  display: grid; place-items: center; cursor: pointer;
  background: rgba(0, 0, 0, .38); color: #fff;
  backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
  transition: opacity .2s ease, background .15s ease;
}
.v-nav:active { background: rgba(0, 0, 0, .62); }
.v-nav svg { width: 26px; height: 26px; }
.v-nav-prev { left: 10px; }
.v-nav-next { right: 10px; }
.v-nav[disabled] { opacity: 0; pointer-events: none; }
.v-title { flex: 1; font-size: 14px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; }
.v-panel {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 3; max-height: 62%;
  overflow: auto; padding: 18px 20px calc(24px + var(--safe-bottom));
  background: #141418; border-radius: 18px 18px 0 0; font-size: 14px;
}
.v-panel dl { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; }
.v-panel dt { color: var(--fg-dim); }
.v-panel dd { margin: 0; overflow-wrap: anywhere; }

/* ------------------------------------------------------------ sheet/toast */

.sheet { position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.55); }
.sheet-body {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 76%; overflow: auto;
  background: var(--bg-elev); border-radius: 18px 18px 0 0;
  padding: 10px 0 calc(14px + var(--safe-bottom));
}
.sheet-body h3 { margin: 8px 18px 10px; font-size: 16px; }
.sheet-body .row { border: 0; }
.sheet-form { padding: 4px 18px 8px; }
.sheet-form p { margin: 0 0 16px; font-size: 14px; line-height: 1.5; }
.sheet-form input {
  width: 100%; padding: 13px 14px; border-radius: 12px; background: var(--bg);
  border: 1px solid var(--bg-elev2); outline: none;
}
.sheet-form input:focus { border-color: var(--accent); }
.sheet-actions { display: flex; gap: 10px; margin-top: 14px; }
.sheet-actions .btn-primary, .sheet-actions .btn-ghost { margin: 0; flex: 1; }
.btn-ghost {
  padding: 14px; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--bg-elev2); color: var(--fg); font-weight: 600;
}
.btn-primary.danger { background: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-bottom) + 18px);
  transform: translate(-50%, 20px); z-index: 90; opacity: 0; pointer-events: none;
  background: var(--bg-elev2); color: var(--fg); padding: 11px 18px;
  border-radius: 24px; font-size: 14px; box-shadow: 0 4px 20px rgba(0,0,0,.5);
  transition: opacity .2s ease, transform .2s ease; max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Wider screens: the same layout, just roomier tiles and a centred column. */
@media (min-width: 900px) {
  .nav { top: 0; bottom: 0; right: auto; width: 92px; flex-direction: column;
    padding-top: calc(var(--topbar-h) + var(--safe-top)); border-top: 0;
    border-right: 1px solid color-mix(in srgb, var(--fg) 8%, transparent); }
  .nav-btn { flex: 0 0 auto; height: 68px; width: 100%; font-size: 11px; }
  .scroller { left: 92px; padding-bottom: 20px; }
  .topbar { left: 0; }
  .scrub { bottom: 8px; }
  .toast { bottom: 24px; left: calc(50% + 46px); }
}
