:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #efefec;
  --line: #e2e2de;
  --fg: #18181b;
  --muted: #62626b;
  --accent: #2f5bea;
  --accent-fg: #ffffff;
  --accent-soft: #e6ecfd;
  --danger: #c4312b;
  --danger-soft: #fbe9e8;
  --ok: #1f7a4a;
  --ok-soft: #e3f3ea;
  --warn: #9a6400;
  --warn-soft: #fbf1dc;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.06);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #111113;
    --surface: #19191c;
    --surface-2: #232328;
    --line: #2d2d33;
    --fg: #ececef;
    --muted: #9d9da8;
    --accent: #7b9bff;
    --accent-fg: #0b1230;
    --accent-soft: #1d2645;
    --danger: #ff7a72;
    --danger-soft: #3a1c1b;
    --ok: #5fd39a;
    --ok-soft: #15301f;
    --warn: #f0b955;
    --warn-soft: #33270f;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 4px 16px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0; text-wrap: balance; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
svg.icon { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.boot { padding: 40px; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
[hidden] { display: none !important; }

/* ---------- controls ---------- */
button, .btn {
  font: inherit;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); color: var(--fg);
  cursor: pointer; white-space: nowrap;
}
button:hover:not(:disabled), .btn:hover { background: var(--surface-2); text-decoration: none; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); }
button.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover:not(:disabled) { background: var(--surface-2); }
button.icon-only { padding: 0; width: 32px; min-height: 32px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input[type='text'], input[type='email'], input[type='password'], input[type='search'], select, textarea {
  font: inherit; color: var(--fg);
  width: 100%; min-height: 38px; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input:disabled, select:disabled, textarea:disabled { background: var(--surface-2); color: var(--muted); }
label.field { display: grid; gap: 6px; font-weight: 600; font-size: 0.88rem; }
label.field > span.hint { font-weight: 400; color: var(--muted); }
.warn-text { color: var(--warn); margin: 0; font-weight: 500; }
.form-error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 0.9rem; }
.stack { display: grid; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.auth-card h1 { margin-bottom: 4px; }
.auth-card p.lead { margin: 0 0 20px; color: var(--muted); }

/* ---------- shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  height: 56px; padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--fg); margin-right: 12px; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; color: var(--accent-fg); }
.brand-mark svg { width: 14px; height: 14px; fill: currentColor; stroke: none; }
.nav a { color: var(--muted); padding: 6px 10px; border-radius: 6px; font-weight: 500; }
.nav a.active, .nav a:hover { color: var(--fg); background: var(--surface-2); text-decoration: none; }
.user-chip { color: var(--muted); font-size: 0.9rem; }

.library { display: grid; grid-template-columns: 230px minmax(0, 1fr); min-height: calc(100vh - 56px); }
.sidebar { border-right: 1px solid var(--line); padding: 18px 12px; display: grid; align-content: start; gap: 22px; }
.side-section { display: grid; gap: 2px; }
.side-section h3 { padding: 0 8px 6px; display: flex; align-items: center; }
.side-link {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 7px; color: var(--fg);
  cursor: pointer; font-size: 0.93rem;
}
.side-link:hover { background: var(--surface-2); text-decoration: none; }
.side-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side-link .count { margin-left: auto; color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.side-link .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-link .actions { display: none; margin-left: auto; gap: 2px; }
.side-link:hover .actions, .side-link:focus-within .actions { display: flex; }
.side-link:hover .actions + .count, .side-link:focus-within .actions + .count { display: none; }
.side-link .actions button { width: 24px; min-height: 24px; padding: 0; border: 0; background: transparent; color: var(--muted); }
.side-link .actions button:hover { color: var(--fg); background: var(--line); }
.side-link .actions svg { width: 14px; height: 14px; }
.tag-cloud { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 8px; }

.content { padding: 22px 24px 80px; min-width: 0; position: relative; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.toolbar h1 { margin-right: auto; }
.search { position: relative; width: 260px; max-width: 100%; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 16px; height: 16px; }
.search input { padding-left: 32px; }
.toolbar select { width: auto; }
.active-filters { display: flex; gap: 8px; margin: -6px 0 16px; flex-wrap: wrap; }

.grid { display: grid; gap: 20px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.card { display: block; color: inherit; border-radius: var(--radius); }
.card:hover { text-decoration: none; }
.card:hover .thumb img { transform: scale(1.02); }
.thumb {
  position: relative; aspect-ratio: 16 / 9; border-radius: var(--radius); overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--line);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform 0.2s; }
.thumb .placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 0.85rem; text-align: center; padding: 8px; }
.thumb .placeholder svg { width: 28px; height: 28px; margin: 0 auto 4px; display: block; }
.badge-time { position: absolute; right: 8px; bottom: 8px; background: rgb(0 0 0 / 0.75); color: #fff; font-size: 0.75rem; padding: 1px 6px; border-radius: 4px; font-variant-numeric: tabular-nums; }
.badge-vis { position: absolute; left: 8px; top: 8px; background: rgb(0 0 0 / 0.65); color: #fff; border-radius: 4px; padding: 3px; display: flex; }
.badge-vis svg { width: 13px; height: 13px; }
.card-title { font-weight: 600; margin-top: 8px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card-meta { color: var(--muted); font-size: 0.82rem; margin-top: 2px; }

.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 0.76rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.ready { background: var(--ok-soft); color: var(--ok); }
.pill.processing, .pill.uploading { background: var(--warn-soft); color: var(--warn); }
.pill.error { background: var(--danger-soft); color: var(--danger); }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 0.8rem; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--fg); border: 1px solid var(--line); cursor: pointer; }
.tag:hover { border-color: var(--accent); text-decoration: none; }
.tag.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.tag button { border: 0; background: none; padding: 0; min-height: 0; width: 16px; height: 16px; color: inherit; }
.tag button svg { width: 12px; height: 12px; }

.empty { text-align: center; padding: 70px 20px; color: var(--muted); border: 2px dashed var(--line); border-radius: 14px; }
.empty svg { width: 40px; height: 40px; margin-bottom: 8px; }
.empty h2 { color: var(--fg); margin-bottom: 6px; }

.drop-overlay {
  position: fixed; inset: 0; z-index: 30; display: none; place-items: center;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  border: 3px dashed var(--accent);
  font-size: 1.2rem; font-weight: 700; color: var(--accent); pointer-events: none;
}
body.dragging .drop-overlay { display: grid; }

/* ---------- video detail ---------- */
.detail { max-width: 1240px; margin: 0 auto; padding: 18px 24px 80px; }
.detail-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.detail-head h1 { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.detail-grid { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 24px; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 18px; }
.panel + .panel { margin-top: 16px; }
.panel h2 { margin-bottom: 14px; }
.player { position: relative; aspect-ratio: 16 / 9; background: #000; border-radius: 12px; overflow: hidden; }
.player iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.player .placeholder { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; color: #d4d4d8; gap: 10px; padding: 20px; }
.spinner { width: 28px; height: 28px; border: 3px solid rgb(255 255 255 / 0.25); border-top-color: #fff; border-radius: 50%; margin: 0 auto; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } .thumb img { transition: none; } }

.thumb-picker { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 16px; align-items: center; }
.thumb-picker .thumb { border-radius: 8px; }
input[type='range'] { width: 100%; accent-color: var(--accent); }

dl.facts { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; font-size: 0.9rem; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.vis-options { display: grid; gap: 8px; }
.vis-option { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-weight: 400; }
.vis-option:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.vis-option input { grid-row: span 2; margin-top: 3px; accent-color: var(--accent); }
.vis-option strong { font-size: 0.92rem; }
.vis-option span { color: var(--muted); font-size: 0.82rem; }

.copy-field { display: flex; gap: 8px; }
.copy-field input, .copy-field textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.8rem; }
.copy-field textarea { min-height: 72px; }

.chip-input { display: flex; flex-wrap: wrap; gap: 6px; padding: 5px 6px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); min-height: 38px; align-items: center; }
.chip-input:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chip-input input { border: 0; min-height: 26px; padding: 0 4px; flex: 1; min-width: 90px; box-shadow: none !important; background: transparent; }
.chip-input.disabled { background: var(--surface-2); }

/* ---------- users ---------- */
.page { max-width: 960px; margin: 0 auto; padding: 22px 24px 80px; }
.page > h1 { margin-bottom: 18px; }
table.list { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
table.list th { text-align: left; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; padding: 0 10px 8px; }
table.list td { border-top: 1px solid var(--line); padding: 10px; vertical-align: middle; }
table.list td select { width: auto; min-height: 32px; padding: 4px 8px; }
.table-wrap { overflow-x: auto; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; align-items: end; }

/* ---------- upload tray ---------- */
.tray {
  position: fixed; right: 16px; bottom: 16px; z-index: 40;
  width: 360px; max-width: calc(100vw - 32px); max-height: 50vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.tray { transition: opacity 0.3s ease, transform 0.3s ease; }
.tray.closing { opacity: 0; transform: translateY(16px); pointer-events: none; }
.tray-countdown { height: 3px; background: var(--surface-2); }
.tray-countdown > div { height: 100%; background: var(--ok); transform-origin: left center; animation: tray-countdown linear forwards; }
.tray:hover .tray-countdown > div, .tray:focus-within .tray-countdown > div { animation-play-state: paused; }
@keyframes tray-countdown { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@media (prefers-reduced-motion: reduce) { .tray.closing { transform: none; } }
.tray-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
.tray-item { padding: 10px 12px; display: grid; gap: 6px; font-size: 0.88rem; }
.tray-item + .tray-item { border-top: 1px solid var(--line); }
.tray-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width 0.2s; }
.tray-item.error .progress > div { background: var(--danger); }
.tray-item.done .progress > div { background: var(--ok); }

/* ---------- dialog & toasts ---------- */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--fg); width: min(440px, calc(100vw - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog form { padding: 22px; display: grid; gap: 14px; }
dialog p { margin: 0; color: var(--muted); }
dialog .row { justify-content: flex-end; }
.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; pointer-events: none; }
.toast { background: var(--fg); color: var(--bg); padding: 9px 16px; border-radius: 8px; font-size: 0.9rem; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .detail-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .library { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); padding: 12px 16px; gap: 12px; }
  .side-section.folders { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
  .side-section.folders h3 { display: none; }
  .side-section.folders .side-link { flex: none; border: 1px solid var(--line); }
  .side-section.folders .side-link .actions { display: none !important; }
  .side-section.folders .side-link .count { display: inline !important; margin-left: 4px; }
  .side-section.tags { display: none; }
  .content, .detail, .page { padding-left: 16px; padding-right: 16px; }
  .search { width: 100%; order: 3; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; }
  .thumb-picker { grid-template-columns: minmax(0, 1fr); }
  .user-chip { display: none; }
  .brand > span:last-child { display: none; }
  .brand { margin-right: 4px; }
  .nav.row { flex-wrap: nowrap; gap: 2px; min-width: 0; flex-shrink: 1; overflow-x: auto; scrollbar-width: none; }
  .topbar > :not(.nav) { flex-shrink: 0; }
  .topbar { gap: 4px; padding: 0 12px; }
  .topbar > .btn, .topbar > button { padding: 0 8px; }
}

/* ---------- PMS units ---------- */
.page.wide { max-width: 1180px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 6px 0 18px; overflow-x: auto; }
.tabs a { padding: 8px 12px; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a:hover { color: var(--fg); text-decoration: none; }
.tabs a.active { color: var(--fg); border-bottom-color: var(--accent); }
.pill.listed { background: var(--accent-soft); color: var(--accent); }
.notice { margin: 0 0 12px; padding: 9px 12px; border-radius: 8px; font-size: 0.9rem; border: 1px solid transparent; }
.notice.info { background: var(--accent-soft); color: var(--fg); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.stack > .notice { margin: 0; }
.mono, .link-preview { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; word-break: break-all; }
.link-preview { display: block; font-size: 0.85rem; padding: 8px 10px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; }

dialog.wide { width: min(680px, calc(100vw - 32px)); max-height: calc(100vh - 48px); }
.dialog-body { padding: 22px; display: grid; gap: 14px; }
.picker-list { display: grid; gap: 6px; max-height: min(52vh, 460px); overflow-y: auto; padding: 2px; }
.picker-item {
  display: flex; align-items: center; justify-content: flex-start; gap: 12px; width: 100%; text-align: left; min-height: 56px;
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--fg); font: inherit;
}
.picker-item:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.picker-item:disabled { opacity: 0.55; cursor: not-allowed; }
.picker-text { display: grid; gap: 2px; min-width: 0; }
.picker-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-thumb {
  flex: none; width: 80px; aspect-ratio: 16 / 9; border-radius: 6px; overflow: hidden; display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--muted);
}
.picker-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.picker-thumb .icon { width: 20px; height: 20px; }
.assign-summary { display: flex; gap: 14px; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.assign-summary .picker-thumb { width: 112px; }
.template-preview { padding: 12px; border: 1px dashed var(--line); border-radius: 10px; }
.template-preview select { width: auto; }
.tag-cloud button.tag { font: inherit; font-size: 0.8rem; }

@media (max-width: 640px) {
  .assign-summary { flex-direction: column; align-items: flex-start; }
  table.list.units th:nth-child(2), table.list.units td:nth-child(2) { display: none; }
}
