:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --accent: #2f6fde;
  --accent-soft: #e6eefc;
  --ok: #1f8a4c;
  --ok-soft: #e3f4ea;
  --warn: #b26a00;
  --warn-soft: #fbf0dc;
  --bad: #c62f2f;
  --bad-soft: #fbe5e5;
  --neutral-soft: #eceff3;
  --flash: #fff4c2;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .06);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #181c21;
    --surface-2: #20252c;
    --border: #2c323a;
    --text: #e7eaee;
    --muted: #97a1ad;
    --accent: #6c9cf5;
    --accent-soft: #1d2a42;
    --ok: #4cc27f;
    --ok-soft: #173325;
    --warn: #e0a346;
    --warn-soft: #3a2c14;
    --bad: #f07272;
    --bad-soft: #3d1d1d;
    --neutral-soft: #262c33;
    --flash: #3b3416;
    --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 4px 16px rgb(0 0 0 / .3);
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 16px; }
h2 { font-size: 15px; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ── Top bar ─────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 18px; height: 18px; border-radius: 5px;
  background: linear-gradient(135deg, var(--accent), var(--ok));
}
.topbar-right { display: flex; align-items: center; gap: 12px; min-width: 0; }
.viewer { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.live {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--neutral-soft); color: var(--muted); font-size: 12.5px; white-space: nowrap;
}
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.live[data-state="live"] { background: var(--ok-soft); color: var(--ok); }
.live[data-state="live"] .live-dot { animation: pulse 2s ease-in-out infinite; }
.live[data-state="polling"] { background: var(--warn-soft); color: var(--warn); }
.live[data-state="offline"] { background: var(--bad-soft); color: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }

/* ── Buttons & inputs ────────────────────────────────────── */
.btn {
  font: inherit; color: var(--text); cursor: pointer;
  padding: 6px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .search:focus-visible, tr:focus-visible, .thumb:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.btn-ghost { border-color: transparent; background: transparent; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.icon { padding: 4px 9px; }
.btn-danger { color: var(--bad); border-color: var(--border); }
.btn-danger:hover { background: var(--bad-soft); }
.btn-danger-solid { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger-solid:hover { background: var(--bad); filter: brightness(1.08); }
.btn:disabled { opacity: .6; cursor: progress; }
.detail-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.search {
  font: inherit; color: var(--text);
  width: min(320px, 100%); padding: 6px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
}

/* ── Layout ──────────────────────────────────────────────── */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px;
  padding: 16px 20px; max-width: 1600px; margin: 0 auto;
}
.layout.has-detail { grid-template-columns: minmax(0, 1fr) minmax(360px, 40%); }
.list-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 10px; flex-wrap: wrap;
}
.count { color: var(--muted); font-weight: 400; }
.list-foot { display: flex; justify-content: center; padding: 12px 0; }
.banner {
  margin-bottom: 10px; padding: 8px 12px; border-radius: 8px;
  background: var(--warn-soft); color: var(--warn);
}

/* ── Upload table ────────────────────────────────────────── */
.table-wrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.uploads { width: 100%; border-collapse: collapse; }
.uploads th {
  text-align: left; font-weight: 500; font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
  padding: 10px 14px; background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.uploads th.num { text-align: right; }
.uploads td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.uploads tbody tr:last-child td { border-bottom: 0; }
.uploads tbody tr { cursor: pointer; }
.uploads tbody tr:hover { background: var(--surface-2); }
.uploads tbody tr[aria-selected="true"] { background: var(--accent-soft); }
.uploads tbody tr.fresh { animation: flash 3s ease-out; }
@keyframes flash { from { background: var(--flash); } }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.name { font-weight: 500; }
.name.untitled { color: var(--muted); font-style: italic; font-weight: 400; }
.sub { display: block; color: var(--muted); font-family: var(--mono); font-size: 11.5px; }
.col-status { width: 190px; }
.empty { margin: 0; padding: 36px 16px; text-align: center; color: var(--muted); }

/* ── Status ──────────────────────────────────────────────── */
.status { display: flex; flex-direction: column; gap: 5px; }
.badge {
  align-self: flex-start;
  display: inline-block; padding: 1px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 500; background: var(--neutral-soft); color: var(--muted);
}
.badge[data-status="processing"] { background: var(--accent-soft); color: var(--accent); }
.badge[data-status="completed"] { background: var(--ok-soft); color: var(--ok); }
.badge[data-status="failed"] { background: var(--bad-soft); color: var(--bad); }
.progress { height: 4px; border-radius: 2px; background: var(--neutral-soft); overflow: hidden; }
.progress > span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .4s ease; }
.progress[data-status="completed"] > span { background: var(--ok); }
.progress[data-status="failed"] > span { background: var(--bad); }

/* ── Detail pane ─────────────────────────────────────────── */
.detail {
  align-self: start; position: sticky; top: 64px;
  max-height: calc(100vh - 80px); overflow: auto;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px;
}
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.detail-titles { min-width: 0; }
.detail-titles h2 { overflow-wrap: anywhere; }
.detail-titles p { margin: 2px 0 0; overflow-wrap: anywhere; }
.meta {
  display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px;
  margin: 14px 0; padding: 12px; border-radius: 8px; background: var(--surface-2);
}
.meta dt { color: var(--muted); }
.meta dd { margin: 0; overflow-wrap: anywhere; }
.job-error {
  margin: 0 0 14px; padding: 10px 12px; border-radius: 8px;
  background: var(--bad-soft); color: var(--bad); white-space: pre-wrap; overflow-wrap: anywhere;
}
.section-title { font-size: 13px; color: var(--muted); font-weight: 500; margin: 6px 0 10px; }

.gallery {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px;
}
.gallery li {
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface);
}
.thumb {
  display: block; width: 100%; aspect-ratio: 4 / 3; padding: 0; border: 0; cursor: zoom-in;
  background: var(--surface-2) center / contain no-repeat;
}
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb.failed { cursor: default; }
.thumb-info { padding: 6px 8px; font-size: 12px; }
.thumb-info .fname { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.thumb-info .hash { display: block; color: var(--muted); font-family: var(--mono); font-size: 11px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── AI analysis ─────────────────────────────────────────── */
.status .ai-badge { margin-top: 2px; }
.ai { margin: 6px 0 16px; }
.ai-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ai-head .section-title { margin: 6px 0; }
.ai-actions, .json-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-small { padding: 3px 10px; font-size: 12.5px; }
.ai-status { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.ai-status .line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--muted); }
.ai-status .warn { margin: 0; padding: 8px 12px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); }
.ai-toolbar { display: flex; justify-content: space-between; align-items: flex-end; gap: 8px; flex-wrap: wrap; }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; }
.tab {
  font: inherit; font-size: 12.5px; cursor: pointer; color: var(--muted);
  padding: 5px 11px; border: 1px solid transparent; border-bottom: 0;
  border-radius: 8px 8px 0 0; background: transparent;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--text); background: var(--surface-2); border-color: var(--border); }
.json {
  margin: 0; padding: 12px; overflow: auto; max-height: 460px;
  border: 1px solid var(--border); border-radius: 0 8px 8px 8px; background: var(--surface-2);
  font-family: var(--mono); font-size: 12px; line-height: 1.5; tab-size: 2;
  white-space: pre-wrap; overflow-wrap: anywhere; /* long values wrap, no sideways scrolling */
}
.j-key { color: var(--accent); }
.j-str { color: var(--ok); }
.j-num { color: var(--warn); }
.j-lit { color: var(--bad); font-style: italic; }

/* ── Dialogs ─────────────────────────────────────────────── */
dialog { border: 0; padding: 0; color: var(--text); background: transparent; }
dialog::backdrop { background: rgb(0 0 0 / .55); }
.signin form {
  width: min(380px, 90vw); padding: 22px; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
}
.signin p { margin: 0; }
.signin input {
  font: inherit; color: var(--text); padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.signin .actions { display: flex; justify-content: flex-end; gap: 8px; }
.error { color: var(--bad); }
.signin label { font-size: 13px; color: var(--muted); margin-bottom: -4px; }
.signin input:disabled { opacity: .7; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ── Users (admin) ───────────────────────────────────────── */
.users-dialog { width: min(1000px, calc(100vw - 32px)); max-width: none; }
.users-panel {
  padding: 20px; border-radius: var(--radius); background: var(--bg); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 64px); overflow: auto;
}
.users-head { display: flex; align-items: center; gap: 10px; }
.users-head h2 { margin: 0; margin-right: auto; font-size: 18px; }
.users-table { width: 100%; border-collapse: collapse; }
.users-table th {
  text-align: left; font-weight: 500; font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
  padding: 10px 14px; background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.users-table th.num { text-align: right; }
.users-table td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.users-table tbody tr:last-child td { border-bottom: 0; }
.users-table tr.inactive .name, .users-table tr.inactive .role { opacity: .6; }
.role-list { display: flex; flex-wrap: wrap; gap: 4px; }
.role {
  padding: 1px 8px; border-radius: 999px; font-size: 12px;
  background: var(--accent-soft); color: var(--accent);
}
.user-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
.users-legend { margin: 0; font-size: 12.5px; }
.roles { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin: 0; }
.roles legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.role-option { display: flex; gap: 8px; align-items: baseline; padding: 4px 0; color: var(--text) !important; }
.signin .role-option input { padding: 0; }
@media (max-width: 700px) {
  .users-table th:nth-child(4), .users-table td:nth-child(4),
  .users-table th:nth-child(5), .users-table td:nth-child(5) { display: none; }
  .users-table td, .users-table th { padding: 8px; }
}
.lightbox {
  width: 100vw; height: 100vh; max-width: none; max-height: none;
  margin: 0; color: #fff;
}
.lightbox[open] { display: flex; flex-direction: column; }
.lightbox::backdrop { background: rgb(0 0 0 / .88); }
.lb-bar { display: flex; align-items: center; gap: 14px; padding: 10px 14px; }
.lb-caption { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.lb-counter { font-variant-numeric: tabular-nums; font-size: 13px; opacity: .8; white-space: nowrap; }
.lb-stage {
  flex: 1; min-height: 0; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 0 8px 16px; touch-action: pan-y;
}
.lb-stage img {
  flex: 1; min-width: 0; height: 100%; object-fit: contain;
  transition: opacity .15s ease; user-select: none;
}
.lb-stage img.loading { opacity: .25; }
.lb-btn {
  font: inherit; color: #fff; cursor: pointer;
  border: 0; border-radius: 999px; background: rgb(255 255 255 / .1);
  width: 36px; height: 36px; flex-shrink: 0;
}
.lb-btn:hover { background: rgb(255 255 255 / .2); }
.lb-btn:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.lb-btn:disabled { opacity: .25; cursor: default; background: rgb(255 255 255 / .1); }
.lb-nav { width: 48px; height: 48px; font-size: 28px; line-height: 1; }
.lb-text { width: auto; padding: 0 14px; font-size: 13px; }
.lb-text[aria-pressed="true"] { background: #fff; color: #111; }
.lb-data {
  flex: 0 0 min(440px, 40vw); height: 100%; max-height: none; border-radius: 8px;
  color: #e7eaee; background: #181c21; border-color: #2c323a;
}
.lb-data .j-key { color: #6c9cf5; }
.lb-data .j-str { color: #4cc27f; }
.lb-data .j-num { color: #e0a346; }
.lb-data .j-lit { color: #f07272; }
@media (max-width: 900px) {
  .lb-stage:has(.lb-data:not([hidden])) { flex-direction: column; }
  .lb-stage:has(.lb-data:not([hidden])) img { height: auto; min-height: 0; flex: 1 1 50%; }
  .lb-data { flex: 1 1 50%; width: 100%; }
}
@media (max-width: 600px) {
  .lb-nav { position: absolute; bottom: 16px; }
  #lightbox-prev { left: 16px; }
  #lightbox-next { right: 16px; }
}

/* ── Narrow screens ──────────────────────────────────────── */
@media (max-width: 900px) {
  .layout.has-detail { grid-template-columns: minmax(0, 1fr); }
  .layout.has-detail .list-pane { display: none; }
  .detail { position: static; max-height: none; }
  .col-owner, .col-size, .uploads td:nth-child(2), .uploads td:nth-child(4) { display: none; }
  .col-status { width: 120px; }
  .topbar, .layout { padding-left: 12px; padding-right: 12px; }
}
