:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-alt: #f0efec;
  --border: #e4e2dd;
  --border-strong: #cfccc5;
  --text: #1d1d1b;
  --text-2: #5f5e5a;
  --text-3: #8b8a85;
  --accent: #2f5fd0;
  --accent-hover: #2752b8;
  --accent-bg: #e8eefb;
  --danger: #a32d2d;
  --radius: 8px;

  --st-prod-bg: #fbf1dc; --st-prod-fg: #7a4d06;
  --st-ready-bg: #e5f2df; --st-ready-fg: #2f5a12;
  --st-ua-bg: #e3edfb;   --st-ua-fg: #1d4a8c;
  --st-arch-bg: #ecebe7; --st-arch-fg: #55544f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-width: 1100px;
}

h1 { font-size: 22px; font-weight: 600; margin: 0 0 8px; }
a { color: inherit; }
.muted { color: var(--text-2); }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  padding: 7px 14px; border-radius: var(--radius); font: inherit; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--surface-alt); }
.btn:disabled { color: var(--text-3); cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { background: var(--accent); opacity: .5; color: #fff; }
.btn-ghost { border-color: transparent; background: none; color: var(--text-2); }
.btn-block { width: 100%; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 28px;
  height: 56px; padding: 0 32px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.logo { font-weight: 600; letter-spacing: -0.01em; text-decoration: none; }
.nav { display: flex; gap: 4px; }
.nav a { color: var(--text-2); padding: 6px 12px; border-radius: var(--radius); text-decoration: none; }
.nav a.on { color: var(--text); background: var(--surface-alt); font-weight: 500; }
.spacer { flex: 1; }
.topbar form { margin: 0; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent-bg); color: var(--accent);
  display: grid; place-items: center; font-size: 12px; font-weight: 600;
}

/* Layout */
.page { max-width: 1280px; margin: 0 auto; padding: 24px 32px 64px; }
.empty-state { text-align: center; padding: 96px 0; }

/* Sign-in */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 32px; }
.login-card {
  width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 32px; display: flex; flex-direction: column; gap: 16px;
}
.login-card p { margin: 0; }
.dev-login {
  display: flex; flex-direction: column; gap: 8px;
  border-top: 1px dashed var(--border-strong); padding-top: 16px;
}
.dev-login label { font-size: 12px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; }
.dev-login input {
  height: 36px; border: 1px solid var(--border-strong); border-radius: var(--radius);
  padding: 0 10px; font: inherit;
}
.error { color: var(--danger); font-size: 13px; }
.hint { color: var(--text-3); font-size: 12px; }

/* Showcase filters */
.filters { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; }
.filters input[type=search], .filters select, .filters input[type=date] {
  height: 36px; border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); padding: 0 10px; font: inherit; color: var(--text);
}
.filters input[type=search] { flex: 1; min-width: 240px; }
.filters .date-field { display: inline-flex; gap: 6px; align-items: center; color: var(--text-3); font-size: 13px; }
.filters .check { display: inline-flex; gap: 6px; align-items: center; color: var(--text-2); margin-left: 4px; white-space: nowrap; }
.results-head { display: flex; gap: 16px; align-items: baseline; margin-bottom: 12px; font-size: 13px; }
.results-head .count { color: var(--text-3); }
.results-head .reset { color: var(--accent); text-decoration: none; }
.results-head .reset:hover { text-decoration: underline; }

/* Showcase grid */
.grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 20px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.thumb {
  position: relative; aspect-ratio: 9 / 16; overflow: hidden;
  background: linear-gradient(160deg, #c9ccd1, #80868f);
  display: grid; place-items: center; cursor: pointer;
}
.thumb:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.thumb .play {
  width: 52px; height: 52px; border-radius: 50%; background: rgba(255, 255, 255, .92);
  display: grid; place-items: center; transition: transform .12s;
}
.thumb:hover .play { transform: scale(1.06); }
.thumb .play svg { width: 20px; height: 20px; fill: var(--text); }
.thumb video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.thumb.playing { cursor: default; }
.thumb.playing .play, .thumb.playing .badge { display: none; }
.thumb.failed::after {
  content: "Can't play this video"; position: absolute; left: 0; right: 0; bottom: 48px;
  text-align: center; color: #fff; font-size: 12px;
}
.thumb.empty { cursor: default; background: var(--surface-alt); color: var(--text-3); font-size: 13px; text-align: center; padding: 16px; }
.thumb .badge { position: absolute; top: 10px; left: 10px; z-index: 1; }
.meta { padding: 10px 12px 12px; }
.cid { font-size: 13px; font-weight: 600; }
.desc { color: var(--text-2); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.date { color: var(--text-3); font-size: 12px; margin-top: 2px; }
.load-more { grid-column: 1 / -1; text-align: center; color: var(--text-3); padding: 16px; }

/* Status badges */
.badge { display: inline-block; font-size: 12px; font-weight: 500; padding: 2px 8px; border-radius: 6px; white-space: nowrap; }
.status-in_production { background: var(--st-prod-bg); color: var(--st-prod-fg); }
.status-ready { background: var(--st-ready-bg); color: var(--st-ready-fg); }
.status-in_ua { background: var(--st-ua-bg); color: var(--st-ua-fg); }
.status-archived { background: var(--st-arch-bg); color: var(--st-arch-fg); }

/* Showcase card link */
a.meta { display: block; text-decoration: none; color: inherit; }
a.meta > span { display: block; }
a.meta:hover .cid { color: var(--accent); }

/* Creative card modal */
body:has(.overlay) { overflow: hidden; }
.overlay {
  position: fixed; inset: 0; z-index: 10; background: rgba(20, 20, 18, .45);
  display: flex; align-items: center; justify-content: center; padding: 32px;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 980px; max-height: calc(100vh - 64px);
  display: grid; grid-template-columns: 360px 1fr; overflow: hidden;
}
.player { background: #111; aspect-ratio: 9 / 16; max-height: calc(100vh - 64px); display: grid; place-items: center; }
.player video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.player-empty { color: #aaa; font-size: 13px; }
.card-body { padding: 24px 28px; overflow-y: auto; display: flex; flex-direction: column; gap: 20px; }
.card-head { display: flex; align-items: flex-start; gap: 12px; }
.card-heading { flex: 1; min-width: 0; }
.card-head h2 { margin: 0; font-size: 22px; font-weight: 600; }
.card-description { color: var(--text-2); margin-top: 4px; overflow-wrap: anywhere; }
.icon-btn {
  border: 0; background: none; color: var(--text-2); width: 32px; height: 32px;
  border-radius: var(--radius); font-size: 22px; line-height: 1; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-alt); color: var(--text); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.section-title { font-size: 12px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px; }
.card-body .hint { margin-left: 8px; }
.fields { display: grid; grid-template-columns: 140px 1fr; gap: 8px 16px; margin: 0; }
.fields dt { color: var(--text-2); }
.fields dd { margin: 0; overflow-wrap: anywhere; }
.muted-3 { color: var(--text-3); }
.history { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.history li { display: flex; gap: 12px; padding: 6px 0; border-top: 1px solid var(--border); }
.history li:first-child { border-top: 0; }
.history time { color: var(--text-3); min-width: 130px; }
.history .who { color: var(--text); }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 20;
  background: var(--text); color: #fff; padding: 8px 16px; border-radius: var(--radius);
  font-size: 13px; opacity: 0; transition: opacity .15s; pointer-events: none;
}
.toast.show { opacity: 1; }

/* Creative form */
.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: #fcebeb; }
.form-page { max-width: 640px; margin: 0 auto; }
.form-page .back { color: var(--text-2); text-decoration: none; font-size: 13px; }
.form-page h1 { margin-top: 12px; }
.creative-form, .danger-zone {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 24px; margin-top: 16px;
}
.creative-form { display: flex; flex-direction: column; gap: 18px; }
.form-row { display: flex; flex-direction: column; gap: 6px; }
.form-row label { font-weight: 500; }
.form-row input[type=file], .form-row select, .form-row textarea {
  font: inherit; color: var(--text); border: 1px solid var(--border-strong);
  border-radius: var(--radius); background: var(--surface); padding: 8px 10px;
}
.form-row textarea { resize: vertical; }
.form-row.has-error textarea, .form-row.has-error select, .form-row.has-error input { border-color: var(--danger); }
.form-row .hint, .form-row .error, .current-file { margin: 0; }
.current-file { font-size: 13px; color: var(--text-2); }
.current-file a { color: var(--accent); }
.form-error { color: var(--danger); background: #fcebeb; border-radius: var(--radius); padding: 8px 12px; }
.form-actions { display: flex; gap: 8px; }
.upload-progress[hidden] { display: none; }
.upload-progress { display: flex; gap: 12px; align-items: center; font-size: 13px; color: var(--text-2); }
.upload-progress .bar { flex: 1; height: 6px; background: var(--surface-alt); border-radius: 3px; overflow: hidden; }
.upload-progress .bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.danger-zone { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.danger-zone p { margin: 2px 0 0; }
.topbar .btn-primary { padding: 6px 12px; }

/* Status controls */
.status-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.status-row form { margin: 0; }
.status-row .hint { margin-left: 0; }
.btn-small { padding: 4px 10px; font-size: 13px; }

/* Users */
.users-page h1 { margin-bottom: 4px; }
.add-user { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin: 20px 0 16px; }
.add-user-row { display: flex; gap: 16px; align-items: center; }
.add-user input[type=email] {
  flex: 1; height: 36px; border: 1px solid var(--border-strong); border-radius: var(--radius);
  padding: 0 10px; font: inherit;
}
.add-user input[aria-invalid=true] { border-color: var(--danger); }
.add-user .check { display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; color: var(--text-2); }
.add-user .error, .add-user .success { margin: 8px 0 0; font-size: 13px; }
.success { color: var(--st-ready-fg); }
table.users { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
table.users th, table.users td { text-align: left; padding: 10px 16px; border-top: 1px solid var(--border); }
table.users th { border-top: 0; font-size: 12px; color: var(--text-3); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; background: var(--surface-alt); }
table.users .c { text-align: center; width: 130px; }
table.users .r { text-align: right; width: 120px; }
table.users input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }
table.users input[type=checkbox]:disabled { cursor: not-allowed; }
table.users tr.inactive td:first-child { color: var(--text-3); }
table.users .badge { margin-left: 6px; }
table.users tr.htmx-request { opacity: .6; }

/* Posters and processing */
.thumb .poster { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.thumb .play { position: relative; }
.thumb .processing-note {
  position: relative; background: rgba(0, 0, 0, .55); color: #fff; font-size: 12px;
  padding: 4px 10px; border-radius: 6px;
}
.thumb .duration {
  position: absolute; right: 8px; bottom: 8px; background: rgba(0, 0, 0, .6); color: #fff;
  font-size: 11px; padding: 1px 6px; border-radius: 4px;
}
.thumb.playing .duration, .thumb.playing .poster { display: none; }
.fields .error { margin: 0; }
.login-error { background: #fcebeb; border-radius: var(--radius); padding: 8px 12px; }

/* Generic dialog over the page */
.dialog {
  background: var(--surface); border-radius: 14px; width: 640px; max-height: calc(100vh - 64px);
  display: flex; flex-direction: column; gap: 16px; padding: 24px 28px; overflow: hidden;
}
.dialog-head { display: flex; align-items: flex-start; gap: 12px; }
.dialog-head > div { flex: 1; }
.dialog-head h2 { margin: 0 0 4px; font-size: 20px; font-weight: 600; }
.dialog-head p { margin: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Creative picker ("Filter") */
.picker-search { position: relative; }
.picker-search input[type=search] {
  width: 100%; height: 40px; border: 1px solid var(--border-strong); border-radius: var(--radius);
  padding: 0 12px; font: inherit;
}
.picker-suggestions {
  list-style: none; margin: 0; padding: 0; position: absolute; left: 0; right: 0; top: 44px; z-index: 2;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  max-height: 320px; overflow-y: auto; box-shadow: 0 8px 24px rgba(0, 0, 0, .12);
}
.picker-suggestions:empty { display: none; }
.picker-suggestions li { border-top: 1px solid var(--border); }
.picker-suggestions li:first-child { border-top: 0; }
.picker-suggestion {
  width: 100%; border: 0; background: none; font: inherit; text-align: left; cursor: pointer;
  display: flex; align-items: center; gap: 10px; padding: 6px 10px;
}
.picker-suggestion:hover, .picker-suggestion:focus-visible { background: var(--surface-alt); outline: none; }
.picker-none { padding: 10px 12px; font-size: 13px; }
.picker-add-all { margin: 8px 10px; }
.picker-selected-head { display: flex; align-items: center; justify-content: space-between; }
.picker-selected-head .section-title { margin: 0; }
.picker-selected { list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 48px; max-height: 40vh; }
.picker-item { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-top: 1px solid var(--border); }
.picker-item:first-child { border-top: 0; }
.picker-thumb {
  width: 27px; height: 48px; border-radius: 4px; overflow: hidden; flex: none;
  background: linear-gradient(160deg, #c9ccd1, #80868f);
}
.picker-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.picker-text { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 13px; }
.picker-text .muted { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.picker-empty { margin: 0; font-size: 13px; }

/* Shared selection on the showcase */
.selection-banner {
  display: flex; align-items: center; gap: 12px; margin-bottom: 12px; padding: 10px 14px;
  background: var(--accent-bg); color: var(--accent); border-radius: var(--radius); font-size: 13px;
}
.selection-banner span { flex: 1; }

/* Users: permission descriptions */
.roles { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; margin: 16px 0 0; }
.roles summary { cursor: pointer; font-weight: 500; }
.roles-list { display: grid; grid-template-columns: 170px 1fr; gap: 8px 16px; margin: 12px 0 4px; font-size: 13px; }
.roles-list dt { font-weight: 600; }
.roles-list dd { margin: 0; color: var(--text-2); }
table.users th[title] { cursor: help; }

/* Shared page pieces */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.back { color: var(--text-2); text-decoration: none; font-size: 13px; }
.chip { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--surface-alt); color: var(--text-2); margin-right: 4px; white-space: nowrap; }
.small { font-size: 12px; }
.strong { font-weight: 600; }
.data-table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.data-table th, .data-table td { text-align: left; padding: 10px 12px; border-top: 1px solid var(--border); font-size: 13px; white-space: nowrap; }
.data-table th { border-top: 0; background: var(--surface-alt); color: var(--text-3); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table a { color: inherit; }
.link-row { cursor: pointer; }
.link-row:hover { background: #fafaf8; }
.hyp { max-width: 320px; overflow: hidden; text-overflow: ellipsis; color: var(--text-2); }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 16px; }

/* UA test statuses */
.ua-created { background: var(--st-arch-bg); color: var(--st-arch-fg); }
.ua-in_ua { background: var(--st-ua-bg); color: var(--st-ua-fg); }
.ua-results { background: #e6f4ef; color: #0f6e56; }

/* UA test card */
.test-head { display: flex; align-items: flex-start; gap: 16px; margin: 8px 0 20px; }
.test-heading { flex: 1; }
.test-head h1 { font-size: 26px; }
.test-meta { color: var(--text-2); margin-top: 4px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.test-actions { display: flex; gap: 8px; }
.test-actions form { margin: 0; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; margin-bottom: 16px; }
.panel-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.panel-head .section-title { flex: 1; margin: 0; }
.hyp-text { overflow-wrap: anywhere; }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--radius); overflow: hidden; }
.seg a, .seg span { padding: 5px 12px; font-size: 13px; color: var(--text-2); text-decoration: none; background: var(--surface); }
.seg a + a, .seg a + span, .seg span + a, .seg span + span { border-left: 1px solid var(--border); }
.seg .on { background: var(--accent-bg); color: var(--accent); font-weight: 500; }
.seg .off { color: var(--text-3); cursor: not-allowed; }
.import-note { font-size: 12px; color: var(--text-3); margin: 0 0 10px; }
.table-scroll { overflow-x: auto; }
.results-table th.sortable { cursor: pointer; }
.results-table th.sortable:hover { color: var(--text); }
.results-table th.sorted::after { content: " \25BC"; }
.results-table th.sorted.asc::after { content: " \25B2"; }
.results-table .ci { color: var(--text-3); }

/* UA test form */
.form-page:has(.ua-form) { max-width: 760px; }
.form-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.checks { display: flex; gap: 16px; flex-wrap: wrap; }
.checks label { display: inline-flex; gap: 6px; align-items: center; font-weight: 400; }
.ua-form .picker-selected { border: 1px solid var(--border); border-radius: var(--radius); padding: 0 10px; max-height: 360px; }
.ua-form .picker-selected:empty { display: none; }
.ua-pick-error { color: var(--danger); background: var(--danger-bg, #fcebeb); border-radius: var(--radius); padding: 8px 12px; font-size: 13px; }
.ua-pick-error[hidden] { display: none; }

/* UA test: creatives with results */
.results-table .thumb-col { width: 44px; padding: 6px 4px 6px 12px; }
.row-thumb {
  display: block; width: 32px; aspect-ratio: 9 / 16; padding: 0; border: 0; border-radius: 4px; overflow: hidden;
  background: linear-gradient(160deg, #c9ccd1, #80868f); cursor: pointer;
}
.row-thumb:disabled { cursor: default; opacity: .5; }
.row-thumb:hover:not(:disabled) { outline: 2px solid var(--accent); outline-offset: 1px; }
.row-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.creative-link { color: inherit; text-decoration: none; }
.creative-link .mono { text-decoration: underline; text-underline-offset: 2px; }
.creative-link .title { color: var(--text-2); }
.creative-link:hover .mono, .creative-link:hover .title { color: var(--accent); }
.video-dialog { background: #111; border-radius: 14px; overflow: hidden; display: flex; flex-direction: column; max-height: calc(100vh - 64px); }
.video-dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 8px 8px 16px; color: #fff; font-size: 13px; }
.video-dialog-head .icon-btn { color: #ddd; }
.video-dialog-head .icon-btn:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.video-dialog video { display: block; height: min(80vh, 760px); aspect-ratio: 9 / 16; background: #000; }

/* UA test form: platforms with buying dates */
.platform-rows { display: flex; flex-direction: column; gap: 8px; }
.platform-row { display: flex; align-items: center; gap: 16px; }
.platform-row .platform-name { display: inline-flex; gap: 6px; align-items: center; width: 120px; font-weight: 400; }
.platform-row .date-field { display: inline-flex; gap: 6px; align-items: center; color: var(--text-3); font-size: 13px; font-weight: 400; }
.platform-row input[type=date] { height: 32px; border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 0 8px; font: inherit; }

.platform-row .date-col { width: 160px; }
.platform-head { color: var(--text-3); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.platform-head .platform-name { font-weight: 600; }

/* Import dialog rules */
.import-rules { margin: 4px 0 0; padding-left: 18px; color: var(--text-2); font-size: 13px; display: flex; flex-direction: column; gap: 4px; }
.import-format { display: inline-flex; flex-wrap: wrap; gap: 0; vertical-align: middle; }
.import-format code {
  font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 12px; padding: 1px 8px;
  border: 1px solid var(--border-strong); background: var(--surface-alt); color: var(--text);
}
.import-format code + code { border-left: 0; }
.import-format code:first-child { border-radius: 4px 0 0 4px; }
.import-format code:last-child { border-radius: 0 4px 4px 0; }
