:root {
  --bg: #f1f1f4; --panel: #ffffff; --panel-2: #e9e9ee; --text: #1b1b1f; --muted: #686873;
  --border: #d2d2da; --accent: #2f62e8; --accent-text: #ffffff; --danger: #c62828; --ok: #2e7d32;
  --radius: 6px; color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #000000; --panel: #0d0d10; --panel-2: #1a1a20; --text: #e8e8ee; --muted: #9a9aa8;
  --border: #2b2b35; --accent: #6b92ff; --accent-text: #08101f; --danger: #ff6b6b; --ok: #6fd48a;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000; --panel: #0d0d10; --panel-2: #1a1a20; --text: #e8e8ee; --muted: #9a9aa8;
    --border: #2b2b35; --accent: #6b92ff; --accent-text: #08101f; --danger: #ff6b6b; --ok: #6fd48a;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* controls */
.btn { font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 6px 12px; cursor: pointer; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 8px; font-size: 12px; }
input[type="text"], input[type="password"], input[type="search"], input[type="number"], select {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 7px 9px; width: 100%; }
input:focus-visible, select:focus-visible, .btn:focus-visible, [role="tab"]:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: grid; gap: 4px; margin-bottom: 10px; }
label.check { display: flex; gap: 8px; align-items: center; margin: 4px 0 10px; }
.error { color: var(--danger); min-height: 1.4em; margin: 4px 0; }
.ok { color: var(--ok); }
code.block { display: block; padding: 8px; background: var(--panel-2); border-radius: var(--radius);
  overflow-wrap: anywhere; font-size: 13px; user-select: all; }

/* login */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; padding: 24px; }
.card h1 { font-size: 20px; margin-bottom: 4px; }
.card p { margin: 0 0 16px; }

/* shell */
.app { display: grid; height: 100dvh;
  grid-template-columns: var(--left-w, clamp(300px, 21vw, 380px)) 6px minmax(0, 1fr) 6px var(--right-w, clamp(480px, 38vw, 780px)); }
.splitter { background: var(--border); cursor: col-resize; touch-action: none; z-index: 5; }
.splitter:hover, .splitter.dragging { background: var(--accent); }
.splitter:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.panel { background: var(--panel); min-width: 0; min-height: 0; display: flex; flex-direction: column;
  overflow: auto; }
.tagview { padding: 12px; gap: 10px; }
.contentview { background: var(--content-bg, #000); color: #8a8a95; position: relative; overflow: hidden; }
.browseview { overflow: hidden; }
.toolbar { display: flex; gap: 6px; align-items: center; padding: 8px; border-bottom: 1px solid var(--border);
  flex-wrap: nowrap; overflow-x: auto; }
.toolbar > * { flex: none; }
.toolbar .spacer { flex: 1 0 0; }
[role="tab"] { font: inherit; color: var(--text); background: transparent; border: 1px solid transparent;
  border-bottom: 2px solid transparent; padding: 6px 10px; cursor: pointer; border-radius: var(--radius) var(--radius) 0 0; }
[role="tab"][aria-selected="true"] { border-bottom-color: var(--accent); background: var(--panel-2); }
.tabbody { padding: 16px; flex: 1; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 8px 0 4px; }
.placeholder-box { border: 1px dashed var(--border); border-radius: var(--radius); padding: 12px; color: var(--muted); }
.bottomnav { display: none; }

@media (max-width: 900px) {
  .splitter { display: none; }
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .panel { display: none; grid-row: 1; border: 0; }
  .app[data-panel="tags"] .tagview, .app[data-panel="content"] .contentview,
  .app[data-panel="browse"] .browseview { display: flex; }
  .bottomnav { display: flex; grid-row: 2; border-top: 1px solid var(--border); background: var(--panel);
    padding-bottom: env(safe-area-inset-bottom, 0); }
  .bottomnav button { flex: 1; font: inherit; color: var(--text); background: transparent; border: 0; padding: 12px 0; }
  .bottomnav button[aria-current="true"] { color: var(--accent); font-weight: 600; box-shadow: inset 0 2px 0 var(--accent); }
}

/* settings dialog */
dialog { color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 0; width: min(560px, 94vw); max-height: 90dvh; }
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); }
.dlg-body { padding: 8px 16px 16px; overflow: auto; max-height: calc(90dvh - 56px); }
.dlg-body section { padding: 12px 0; border-bottom: 1px solid var(--border); }
.dlg-body section:last-child { border-bottom: 0; }
.dlg-body h3 { margin-bottom: 8px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
ul.devices { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 8px; }
ul.devices li { display: flex; justify-content: space-between; gap: 8px; align-items: center;
  border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 10px; }
ol.codes { columns: 2; padding-left: 20px; margin: 8px 0; font-family: ui-monospace, monospace; }

/* ---- browse: search form, tag input ---- */
.tabbody.browse-body { padding: 0; display: flex; flex-direction: column; min-height: 0; }
.browse { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.search-form { display: grid; gap: 8px; padding: 8px; border-bottom: 1px solid var(--border); flex: none; }
details.tag-filters > summary, details.more-filters > summary { cursor: pointer; font-weight: 600; }
details.tag-filters[open] > summary { margin-bottom: 6px; }
.more-body { display: grid; gap: 8px; padding-top: 8px; max-height: 45vh; overflow: auto; }
.more-body .row > input[type="search"] { flex: 1; min-width: 0; }
.search-form > .row { padding: 2px 0; }
.cat-rows { display: grid; gap: 6px; }
.cat-row { display: grid; grid-template-columns: 76px minmax(0, 1fr) minmax(0, 1fr); gap: 6px; align-items: start; }
.cat-name { padding-top: 7px; color: var(--muted); font-size: 12px; }
.cat-head span { font-size: 12px; text-align: center; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 14px; }
label.check.inline { display: inline-flex; margin: 0; }
.narrow { width: 76px; flex: none; }
.tag-input { position: relative; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; min-width: 0;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 3px; }
.tag-input:focus-within { border-color: var(--accent); }
.tag-input .ti-input { flex: 1; min-width: 56px; border: 0; padding: 3px 4px; background: transparent; width: auto; outline: none; }
.chips { display: contents; }
.chip { display: inline-flex; align-items: center; gap: 2px; background: var(--chip, #444); color: #fff; border-radius: 10px;
  padding: 0 3px 0 8px; font-size: 12px; max-width: 100%; }
.chip-x { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0 4px; }
.suggest { position: absolute; z-index: 30; left: 0; right: 0; top: 100%; margin: 2px 0 0; padding: 0; list-style: none;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); max-height: 220px; overflow: auto;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .45); }
.suggest li { padding: 6px 10px; cursor: pointer; }
.suggest li.more-hint { color: var(--muted); cursor: default; font-size: 12px; }
.suggest li.active, .suggest li:hover { background: var(--accent); color: var(--accent-text); }

/* ---- browse: results ---- */
.results-bar { display: grid; gap: 6px; padding: 8px; border-bottom: 1px solid var(--border); flex: none; }
.results-bar select { width: auto; padding: 3px 6px; }
.results-bar input[type="range"] { width: 110px; }
.btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.status { padding: 8px 10px; color: var(--muted); flex: none; }
.result-list { flex: 1; min-height: 0; overflow: auto; position: relative; outline: none; user-select: none; -webkit-user-select: none; }
.result-list:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
.list-inner { position: relative; }
.tile { position: absolute; background: var(--panel-2); border-radius: 6px; overflow: hidden; cursor: pointer;
  border: 2px solid transparent; box-sizing: border-box; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.tile-icon { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 12px; }
.badge { position: absolute; right: 4px; bottom: 4px; background: rgba(0, 0, 0, .72); color: #fff; font-size: 11px;
  padding: 1px 6px; border-radius: 10px; }
.tile.selected, .lrow.selected { border-color: var(--accent); }
.lrow.selected { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.tile.active { box-shadow: 0 0 0 2px var(--text); }
.lrow.active { box-shadow: inset 3px 0 0 var(--accent); }
.lrow { position: absolute; left: 0; right: 0; height: 30px; display: grid; grid-template-columns: var(--cols);
  align-items: center; cursor: pointer; border: 0 solid transparent; border-bottom: 1px solid var(--border); box-sizing: border-box; }
.cell { padding: 0 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell.num { text-align: right; }
.table-head { position: sticky; top: 0; z-index: 2; display: grid; grid-template-columns: var(--cols);
  background: var(--panel-2); border-bottom: 1px solid var(--border); }
.th { position: relative; min-width: 0; }
.th-label { display: block; width: 100%; text-align: left; font: inherit; font-weight: 600; color: var(--text);
  background: none; border: 0; padding: 6px 8px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.resize-handle { position: absolute; right: 0; top: 0; bottom: 0; width: 8px; cursor: col-resize; touch-action: none; }
.resize-handle:hover { background: var(--accent); opacity: .5; }
.menu { position: relative; }
.menu > summary { list-style: none; cursor: pointer; display: inline-block; }
.menu-body { position: absolute; z-index: 20; left: 0; margin-top: 4px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px; min-width: 170px; box-shadow: 0 6px 18px rgba(0, 0, 0, .45); }
.menu-body .check { margin: 2px 0; }

/* ---- viewer ---- */
.stage { position: absolute; inset: 0; display: flex; overflow: hidden; }
.contentview.has-strip .stage { bottom: 84px; }
.stage { touch-action: none; }          /* the app handles swipes, pinch and drag itself, so the browser must not */
.media { margin: auto; display: block; }
.media.fit { width: 100%; height: 100%; object-fit: contain; will-change: transform; user-select: none; -webkit-user-select: none; }  /* small content scales up to fit */
.stage.zoomed .media.fit { cursor: grab; }
.stage.zoomed.panning .media.fit { cursor: grabbing; }
video.media { width: 100%; height: 100%; object-fit: contain; }
.empty { margin: auto; text-align: center; padding: 16px; color: #8a8a95; }
.nav-btn { position: absolute; top: 50%; transform: translateY(-50%); z-index: 3; font-size: 30px; line-height: 1;
  color: #fff; background: rgba(0, 0, 0, .45); border: 0; padding: 10px 12px; cursor: pointer; opacity: .35; border-radius: 6px; }
.nav-btn.prev { left: 6px; } .nav-btn.next { right: 6px; }
.contentview:hover .nav-btn, .nav-btn:focus-visible { opacity: 1; }
@media (hover: none) { .nav-btn { opacity: .75; } }
.viewer-info { position: absolute; top: 8px; left: 8px; z-index: 3; background: rgba(0, 0, 0, .6); color: #ddd;
  font-size: 12px; padding: 2px 8px; border-radius: 4px; pointer-events: none; }
.filmstrip { position: absolute; left: 0; right: 0; bottom: 0; z-index: 3; height: 84px; display: flex; gap: 6px;
  overflow-x: auto; padding: 6px; background: rgba(0, 0, 0, .75); }
.film-item { flex: none; width: 72px; height: 72px; padding: 0; border: 2px solid transparent; border-radius: 4px;
  background: #1a1a20; color: #aaa; cursor: pointer; overflow: hidden; }
.film-item.active { border-color: var(--accent); }
.film-item img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ---- viewer options ---- */
.contentview[data-arrows="off"] .nav-btn { display: none; }
.contentview[data-index="off"] .viewer-info { display: none; }
.fs-btn { position: absolute; top: 6px; right: 6px; z-index: 4; font-size: 18px; line-height: 1; color: #fff;
  background: rgba(0, 0, 0, .45); border: 0; border-radius: 6px; padding: 6px 9px; cursor: pointer; opacity: .45; }
.contentview:hover .fs-btn, .fs-btn:focus-visible { opacity: 1; }
@media (hover: none) { .fs-btn { opacity: .8; } }
.contentview:fullscreen .fs-btn { display: none; }              /* desktop: Esc or F leaves fullscreen */
@media (max-width: 900px), (hover: none) {
  .contentview:fullscreen .fs-btn { display: block; opacity: .22; }
  .contentview:fullscreen .fs-btn:hover { opacity: .22; }
}
.app.pseudo-fs .fs-btn { display: block; opacity: .22; }        /* the full-window fallback has no Esc on a phone */
.filmstrip { background: color-mix(in srgb, var(--content-bg, #000) 65%, transparent); transition: opacity .3s ease; }
.filmstrip.faded { opacity: 0; }
.filmstrip.faded:hover, .filmstrip.faded:focus-within { opacity: 1; }
.contentview[data-filmstrip="hidden"] .filmstrip { display: none; }

/* ---- full-window fallback where the Fullscreen API is missing (iPhone) ---- */
.app.pseudo-fs { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); position: fixed; inset: 0; z-index: 50; }
.app.pseudo-fs > .panel:not(.contentview), .app.pseudo-fs > .splitter, .app.pseudo-fs > .bottomnav { display: none; }
.app.pseudo-fs > .contentview { display: flex; grid-row: 1; grid-column: 1; }

/* ---- Tag View ---- */
.tv-body { display: grid; gap: 4px; }
.tv-cat { margin: 8px 0 4px; }
.tv-cat-head { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 4px; }
.tv-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; min-height: 4px; }
.tchip { display: inline-flex; align-items: center; gap: 2px; background: var(--chip, #444); color: #fff; font-size: 13px;
  border: 1px solid transparent; border-radius: 13px; padding: 2px 4px 2px 10px; cursor: default; user-select: none; -webkit-user-select: none; }
.tchip.added { box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 70%, transparent); }
.tchip.new { border: 1px dashed rgba(255, 255, 255, .7); }
.tchip:not(:has(.tchip-x)) { padding-right: 10px; }
.tchip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tchip-x { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0 5px; border-radius: 8px; }
.tchip-x:hover { background: rgba(255, 255, 255, .25); }
.tag-picker { position: relative; }
.tp-input { width: 100%; }
.suggest li.recent .star { color: #e6b422; }
.suggest li.create { font-style: italic; border-top: 1px solid var(--border); }
.tv-rating { margin: 6px 0; display: grid; gap: 6px; }
.tv-rating input[type="range"] { width: 100%; }
.stars { display: inline-flex; }
.star-btn { font-size: 20px; line-height: 1; background: none; border: 0; color: var(--border); cursor: pointer; padding: 0 2px; }
.star-btn.on { color: #e6b422; }
.tv-actions { margin-top: 8px; }
.tv-status { min-height: 1.4em; font-size: 13px; color: var(--muted); }
.tv-status.ok { color: var(--ok); }
.tv-status.error { color: var(--danger); }
.tv-status.dirty { color: var(--accent); }
.ctxmenu { position: fixed; z-index: 100; min-width: 200px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5); }
.ctx-item { display: block; width: 100%; text-align: left; font: inherit; color: var(--text); background: none; border: 0;
  border-radius: 5px; padding: 7px 10px; cursor: pointer; }
.ctx-item:hover:not(:disabled), .ctx-item:focus-visible { background: var(--accent); color: var(--accent-text); outline: none; }
.ctx-item:disabled { color: var(--muted); cursor: default; }

/* ---- controls shown instead of the browser's while a video is zoomed ---- */
.mini-controls { position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 4; display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: 10px; color: #fff; background: color-mix(in srgb, var(--content-bg, #000) 70%, transparent); }
.contentview.has-strip .mini-controls { bottom: 92px; }
.mini-controls input[type="range"] { flex: 1; min-width: 0; }
.vc-btn { font: inherit; color: #fff; background: rgba(255, 255, 255, .15); border: 0; border-radius: 6px; padding: 4px 9px; cursor: pointer; }
.vc-time { font-size: 12px; white-space: nowrap; }
.stage.zoomed video.media { cursor: grab; }
