:root {
  --ink: #0b1220;        /* deep blue-black, lets media carry the color */
  --panel: #121a2b;
  --line: #1f2a40;
  --text: #dbe4f3;
  --dim: #7c8aa5;
  --accent: #2fa8e0;     /* saved-messages blue */
  --danger: #e05b5b;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* The UA rule [hidden]{display:none} loses to any class that sets display
   (grid/flex/block), so hiding via .hidden silently failed on several
   elements. This makes the attribute authoritative everywhere. */
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--ink); color: var(--text); }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 14px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--ink) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-name { font-weight: 700; letter-spacing: .02em; }
.brand-count {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px; color: var(--accent);
}
#search {
  flex: 1; min-width: 0;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; font-size: 14px; outline: none;
}
#search:focus-visible { border-color: var(--accent); }

.chips {
  display: flex; gap: 8px; overflow-x: auto;
  padding: 10px 16px; scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  background: var(--panel); color: var(--dim);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 14px; font-size: 13px; cursor: pointer;
}
.chip.active { color: #fff; background: var(--accent); border-color: var(--accent); }
.chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.grid {
  display: grid; gap: 4px; padding: 0 4px;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
}
@media (min-width: 700px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 6px; padding: 0 6px; }
}
.tile {
  position: relative; aspect-ratio: 1;
  background: var(--panel); border-radius: 6px; overflow: hidden;
  border: none; padding: 0; cursor: pointer;
}
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tile .badge {
  position: absolute; right: 6px; bottom: 6px;
  background: rgba(0,0,0,.65); color: #fff;
  font-size: 11px; padding: 2px 7px; border-radius: 999px;
}
.tile .filecard {
  height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px;
  color: var(--dim); font-size: 12px; padding: 10px; text-align: center;
  word-break: break-all;
}
.filecard .glyph { font-size: 28px; }

#sentinel { height: 1px; }
.empty { text-align: center; color: var(--dim); padding: 60px 20px; }

.lightbox {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(4, 8, 16, .96);
  display: flex; flex-direction: column;
}
.lightbox[hidden] { display: none; }
.lb-close {
  position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 12px;
  background: rgba(255,255,255,.1); color: #fff; border: 0;
  width: 38px; height: 38px; border-radius: 50%; font-size: 16px; cursor: pointer;
}
.lb-stage {
  flex: 1; position: relative;
  display: flex; align-items: center; justify-content: center;
  min-height: 0; padding: 8px;
}
.lb-stage img, .lb-stage video {
  max-width: 100%; max-height: 100%; border-radius: var(--radius);
}
.lb-stage audio { width: min(420px, 90%); }
.lb-meta {
  display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center;
  padding: 12px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); font-size: 13px;
}
.lb-meta .mono { color: var(--dim); }
.lb-actions { margin-left: auto; display: flex; gap: 14px; }
.lb-actions a { color: var(--accent); text-decoration: none; }
.lb-actions button {
  background: none; border: 0; color: var(--danger);
  font-size: 13px; cursor: pointer; padding: 0;
}

.login {
  position: fixed; inset: 0; z-index: 30;
  background: var(--ink);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.login[hidden] { display: none; }
.login-card {
  width: min(340px, 100%);
  display: flex; flex-direction: column; gap: 12px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px 24px;
}
.login-title { font-weight: 700; font-size: 18px; }
.login-hint { margin: 0; color: var(--dim); font-size: 13px; }
.login-card input {
  background: var(--ink); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 15px; outline: none;
}
.login-card input:focus-visible { border-color: var(--accent); }
.login-card button {
  background: var(--accent); color: #fff; border: 0;
  border-radius: 8px; padding: 10px; font-size: 15px;
  font-weight: 600; cursor: pointer;
}
.login-err { margin: 0; color: var(--danger); font-size: 13px; }
.fail-btn {
  flex: 0 0 auto;
  background: rgba(224, 91, 91, .14); color: var(--danger);
  border: 1px solid rgba(224, 91, 91, .4); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; cursor: pointer;
}
.fail-card { max-height: 80vh; }
.fail-list {
  list-style: none; margin: 0; padding: 0;
  min-height: 0; flex: 1 1 auto;
  overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
}
.fail-list li {
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 13px;
  display: flex; flex-direction: column; gap: 4px;
}
.fail-list .fail-name { font-weight: 600; word-break: break-all; }
.fail-list .fail-error {
  color: var(--dim); font-family: ui-monospace, Menlo, monospace;
  font-size: 11px; word-break: break-all;
}
.fail-list .fail-row { display: flex; justify-content: space-between; align-items: center; }
.fail-list .fail-meta { color: var(--dim); font-size: 11px; }
.fail-list button {
  background: none; border: 0; color: var(--danger);
  font-size: 12px; cursor: pointer; padding: 0;
}
.fail-close {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px; font-size: 14px; cursor: pointer;
}

.syncbar {
  display: flex; flex-direction: column; gap: 5px;
  width: calc(100% - 32px); margin: 0 16px 10px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px;
  color: var(--text); text-align: left; cursor: pointer;
}
.syncbar[hidden] { display: none; }
.syncbar-track {
  display: block; height: 5px; border-radius: 999px;
  background: var(--line); overflow: hidden;
}
.syncbar-fill {
  display: block; height: 100%; width: 0%;
  background: var(--accent); border-radius: 999px;
  transition: width .4s ease;
}
.syncbar-text {
  font-size: 12px; color: var(--dim);
  font-family: ui-monospace, Menlo, monospace;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.fab {
  position: fixed; right: 18px; bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 15;
  background: var(--accent); color: #fff; border: 0;
  border-radius: 999px; padding: 14px 22px;
  font-size: 15px; font-weight: 700; cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,.4);
}

.loop {
  position: fixed; inset: 0; z-index: 25;
  background: var(--ink);
  display: flex; flex-direction: column;
  touch-action: none; overscroll-behavior: none;
}
.loop[hidden] { display: none; }
.loop-top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  border-bottom: 1px solid var(--line);
}
.loop-top-spacer { flex: 1; }
.loop-icon {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 50%;
  width: 36px; height: 36px; font-size: 15px; cursor: pointer;
}
.loop-icon:disabled { opacity: .35; }
.loop-select {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 8px; font-size: 13px;
}
.loop-stage {
  flex: 1; min-height: 0; position: relative;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; padding: 6px;
}
.loop-stage img, .loop-stage video {
  max-width: 100%; max-height: 100%; border-radius: 8px;
  user-select: none; -webkit-user-drag: none;
}
.loop-stage .stage-media { will-change: transform; }
.play-badge {
  position: absolute; width: 64px; height: 64px; border-radius: 50%;
  background: rgba(0,0,0,.55); color: #fff; border: 0;
  font-size: 24px; line-height: 1; cursor: pointer;
}
.loop-empty { color: var(--dim); text-align: center; padding: 24px; }
.loop-stars {
  display: flex; justify-content: center; gap: 6px;
  padding: 8px 12px 2px;
}
.loop-stars button {
  background: none; border: 0; cursor: pointer;
  font-size: 30px; line-height: 1; padding: 4px 6px;
  color: #33415c;
}
.loop-stars button.on { color: #f5c04e; }
.loop-actions {
  display: flex; gap: 8px;
  padding: 8px 12px calc(14px + env(safe-area-inset-bottom));
}
.loop-actions button {
  flex: 1;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 12px 0; font-size: 15px; cursor: pointer;
}
.loop-actions .primary {
  flex: 1.6; background: var(--accent); border-color: var(--accent);
  color: #fff; font-weight: 700;
}
.loop-actions #loopJunk.on {
  background: rgba(224,91,91,.18); border-color: var(--danger); color: var(--danger);
}
.tg-step { display: flex; flex-direction: column; gap: 12px; }
.tg-step[hidden] { display: none; }
.tg-step input {
  background: var(--ink); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 15px; outline: none;
}
.tg-step input:focus-visible { border-color: var(--accent); }
.tg-step button {
  background: var(--accent); color: #fff; border: 0;
  border-radius: 8px; padding: 10px; font-size: 15px;
  font-weight: 600; cursor: pointer;
}
.tg-step button:disabled { opacity: .5; cursor: default; }

@media (prefers-reduced-motion: no-preference) {
  .tile img { transition: transform .18s ease; }
  .tile:hover img { transform: scale(1.04); }
}

/* Tag chips in loop */
.loop-tags {
  display: flex; gap: 8px; overflow-x: auto;
  padding: 6px 12px 0; scrollbar-width: none; min-height: 38px;
}
.loop-tags::-webkit-scrollbar { display: none; }
.loop-tags .tchip {
  flex: 0 0 auto;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 13px; font-size: 13px; cursor: pointer;
}
.loop-tags .tchip.person { border-color: #4e8f5f; }
.loop-tags .tchip.event { border-color: #8a6fc9; }
.loop-tags .tchip .x { color: var(--dim); margin-left: 6px; }
.loop-tags .tchip-add {
  flex: 0 0 auto;
  background: none; color: var(--accent);
  border: 1px dashed var(--accent); border-radius: 999px;
  padding: 6px 13px; font-size: 13px; cursor: pointer;
}
.chip-action { color: var(--accent); border-style: dashed; }

/* Bottom sheet picker */
.sheet {
  position: fixed; inset: 0; z-index: 35;
  background: rgba(4,8,16,.6);
  display: flex; align-items: flex-end;
}
.sheet[hidden] { display: none; }
.sheet-card {
  width: 100%; max-height: 72vh;
  background: var(--panel);
  border-radius: 16px 16px 0 0;
  border-top: 1px solid var(--line);
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px;
  overflow-y: auto;
}
.sheet-grab {
  width: 40px; height: 4px; border-radius: 2px;
  background: var(--line); margin: 4px auto 0;
}
.picker-quick { display: flex; flex-wrap: wrap; gap: 8px; }
.picker-quick:empty { display: none; }
.picker-groups { display: flex; flex-direction: column; gap: 10px; }
.picker-group-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--dim);
}
.picker-row { display: flex; flex-wrap: wrap; gap: 8px; }
.pchip {
  background: var(--ink); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 7px 14px; font-size: 14px; cursor: pointer;
}
.pchip.applied { opacity: .4; }
.picker-new { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--line); padding-top: 12px; }
.picker-new input {
  background: var(--ink); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 15px; outline: none;
}
.type-row { display: flex; gap: 6px; }
.type-row button {
  flex: 1; background: var(--ink); color: var(--dim);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 0; font-size: 13px; cursor: pointer;
}
.type-row button.sel { color: #fff; background: var(--accent); border-color: var(--accent); }
.picker-add {
  background: var(--accent); color: #fff; border: 0;
  border-radius: 8px; padding: 10px; font-size: 15px; font-weight: 600; cursor: pointer;
}

/* Settings sheet */
.set-btn { color: var(--dim); border-color: var(--line); background: var(--panel); }
.roots-list { display: flex; flex-direction: column; gap: 8px; }
.roots-list .root-row {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 12px; font-size: 13px;
}
.roots-list .root-path { flex: 1; word-break: break-all; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.roots-list .root-missing { color: var(--danger); font-size: 11px; }
.roots-list .root-del { background: none; border: 0; color: var(--danger); cursor: pointer; font-size: 14px; }
.roots-list:empty::after {
  content: "No folders yet — browse below and add one.";
  color: var(--dim); font-size: 13px;
}
.browse-head { display: flex; align-items: center; gap: 10px; border-top: 1px solid var(--line); padding-top: 12px; }
.browse-crumb { color: var(--dim); font-size: 12px; word-break: break-all; }

/* Kill all visible scrollbars (scrolling still works) */
* { scrollbar-width: none; -ms-overflow-style: none; }
*::-webkit-scrollbar { display: none; }

/* Lightbox stars + swipe */
.lb-stars { flex-basis: 100%; justify-content: flex-start; padding: 0 0 4px; }
.lb-stars button { font-size: 24px; }
.lb-stage { touch-action: none; overscroll-behavior: none; }
.lb-stage img, .lb-stage video { user-select: none; -webkit-user-drag: none; will-change: transform; }

.update-section {
  display: flex; flex-direction: column; gap: 8px;
  border-top: 1px solid var(--line); padding-top: 12px;
}

/* Face chips + People */
.loop-faces:empty { display: none; min-height: 0; }
.loop-faces .fchip {
  display: flex; align-items: center; gap: 7px;
  flex: 0 0 auto;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 4px 12px 4px 4px; font-size: 13px; cursor: pointer;
}
.loop-faces .fchip img {
  width: 26px; height: 26px; flex: none; border-radius: 50%; object-fit: cover;
}
.loop-faces .fchip.named { border-color: #4e8f5f; }
.loop-faces .fchip.unknown { border-style: dashed; color: var(--dim); }

.people-grid {
  /* min-height:0 lets this become a real scroll area inside the flex column;
     without it the child can't shrink, never scrolls, and the rows get
     crushed. grid-auto-rows pins row height so nothing can compress them. */
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid; gap: 8px; padding: 10px;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  grid-auto-rows: 138px;
  align-content: start;
}
.people-grid .pcard {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; overflow: hidden; cursor: pointer;
  padding: 0; display: block; text-align: left;
}
.people-grid .pcard img {
  width: 100%; height: 110px; object-fit: cover; object-position: center 25%;
  display: block; flex: none;
}
.people-grid .pcard .plabel {
  display: block;
  padding: 6px 8px; font-size: 12px; color: var(--dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.people-grid .pcard .plabel b { color: var(--text); }
.person-detail { flex: 1 1 auto; display: flex; flex-direction: column;
  min-height: 0; overflow-y: auto; }
.person-detail #personSamples { flex: 0 1 auto; }
.person-detail[hidden] { display: none; }
.person-head {
  display: flex; gap: 8px; align-items: center; padding: 10px 12px;
}
.person-head input {
  flex: 1; min-width: 0;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 12px; font-size: 15px; outline: none;
}
.person-head .picker-add { flex: 0 0 auto; padding: 9px 16px; }
.person-ignore {
  margin: 10px 12px calc(14px + env(safe-area-inset-bottom));
  background: rgba(224,91,91,.12); color: var(--danger);
  border: 1px solid rgba(224,91,91,.4); border-radius: 10px;
  padding: 11px; font-size: 14px; cursor: pointer;
}
#nameSheet input {
  background: var(--ink); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 15px; outline: none;
}

/* Lightbox nav arrows — always reachable, even over playing video */
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  z-index: 2;
  width: 42px; height: 42px; border-radius: 50%;
  background: rgba(0,0,0,.55); color: #fff; border: 0;
  font-size: 24px; line-height: 1; cursor: pointer;
}
.lb-nav:disabled { opacity: .25; }
.lb-prev { left: 8px; }
.lb-next { right: 8px; }
.lb-stage { position: relative; }

/* Filters + suggestion chips */
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.filter-grid label {
  display: flex; flex-direction: column; gap: 5px;
  font-size: 12px; color: var(--dim);
}
.filter-grid .loop-select { width: 100%; padding: 9px 8px; }
.filter-tagfrom { background: var(--panel); color: var(--accent); border: 1px solid var(--accent); }
.loop-tags .schip {
  display: flex; align-items: center; gap: 8px;
  flex: 0 0 auto;
  background: none; color: var(--accent);
  border: 1px dashed var(--accent); border-radius: 999px;
  padding: 6px 10px 6px 13px; font-size: 13px; cursor: pointer;
}
.loop-tags .schip .sx { color: var(--dim); padding: 0 2px; }

/* Channel browser */
#channelSearch {
  background: var(--ink); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 12px; font-size: 14px; outline: none;
}
.roots-list .chan-row {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 14px; cursor: pointer;
  background: none; color: var(--text); text-align: left; width: 100%;
}
.roots-list .chan-row .chan-type {
  font-size: 11px; color: var(--dim); flex: 0 0 auto;
}
.roots-list .chan-row .chan-title {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.roots-list .chan-row .chan-check {
  flex: 0 0 auto; width: 22px; height: 22px; border-radius: 6px;
  border: 1px solid var(--line); display: flex;
  align-items: center; justify-content: center; font-size: 14px;
  color: transparent;
}
.roots-list .chan-row.on { border-color: var(--accent); }
.roots-list .chan-row.on .chan-check {
  background: var(--accent); border-color: var(--accent); color: #fff;
}

/* iOS img-in-button collapse fix + sheet close + centered play badge */
.pimg { height: 110px; overflow: hidden; }
.pimg img {
  width: 100%; height: 100%; object-fit: cover;
  object-position: center 30%; display: block;
}
.person-quick:empty { display: none; }
.person-quick { padding: 0 12px; }
.sheet-close {
  position: absolute; top: 10px; right: 12px;
  background: var(--ink); color: var(--dim);
  border: 1px solid var(--line); border-radius: 50%;
  width: 32px; height: 32px; font-size: 14px; cursor: pointer;
}
.sheet-card { position: relative; }
.lb-close { z-index: 3; }
/* centred on whichever stage contains it; both stages are position:relative */
.play-badge { left: 50%; top: 50%; transform: translate(-50%, -50%); }

.loop-select { min-width: 0; max-width: 42vw; }

/* Skip layout/paint for offscreen tiles — keeps 10k+ item grids smooth */
.tile { content-visibility: auto; contain-intrinsic-size: auto 150px; }
