/* CrRTB web UI. One accent colour, system fonts, light + dark. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #16191d;
  --muted: #646b75;
  --accent: #2f6df6;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --danger-bg: #fdecea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 20, 30, .06), 0 4px 16px rgba(16, 20, 30, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #232830;
    --border: #313843;
    --text: #e7eaee;
    --muted: #9aa3b0;
    --accent: #5b8dff;
    --accent-text: #0f1216;
    --danger: #ff8071;
    --danger-bg: #3a2320;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }

/* Any rule that sets `display` on an element we toggle with the `hidden`
   attribute -- the card menus, the modal's error line -- outranks the
   browser's own [hidden] rule, so state it once, loudly, here. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- buttons and fields ---------- */

.btn {
  font: inherit;
  padding: .5rem .9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease, opacity .12s ease;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }

input[type=text], input[type=password], input[type=email], input[type=url], input[type=number],
select, textarea {
  font: inherit;
  width: 100%;
  padding: .5rem .65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

/* ---------- auth pages ---------- */

body.auth {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1.5rem;
}
.card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
}
.card .brand { margin: 0; font-size: 1.4rem; letter-spacing: -.02em; }
.tagline { margin: .25rem 0 1.5rem; color: var(--muted); }
.field { display: block; margin-bottom: 1rem; }
.field .label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .3rem; }
.field textarea { resize: vertical; min-height: 4.5rem; }
.field .help { display: block; font-size: .78rem; color: var(--muted); margin-top: .35rem; }
.field .help ul { margin: .3rem 0; padding-left: 1.1rem; }
.field .error, .modal-error { display: block; font-size: .82rem; color: var(--danger); margin-top: .35rem; }
.alert {
  background: var(--danger-bg);
  color: var(--danger);
  border-radius: 8px;
  padding: .6rem .8rem;
  margin-bottom: 1rem;
  font-size: .88rem;
}
.alert p { margin: 0; }
.switch { margin: 1.25rem 0 0; text-align: center; font-size: .88rem; color: var(--muted); }

/* ---------- app chrome ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .7rem 1.1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
  flex-wrap: wrap;
}
.topbar .brand { font-weight: 700; letter-spacing: -.02em; margin-right: .5rem; }
.topbar .spacer { flex: 1; }
.topbar .user { color: var(--muted); font-size: .88rem; }
.org-picker select { min-width: 190px; }
.inline { display: inline; margin: 0; }

.layout {
  display: grid;
  grid-template-columns: minmax(220px, 300px) 1fr;
  gap: 1rem;
  padding: 1rem;
  align-items: start;
}
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
}

.pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.pane-head h2 { margin: 0; font-size: .95rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

/* ---------- inventory list ---------- */

.list { list-style: none; margin: 0; padding: .4rem; }
.list li { display: flex; align-items: center; gap: .4rem; }
.list .item {
  flex: 1;
  text-align: left;
  font: inherit;
  background: none;
  border: 0;
  border-radius: 8px;
  padding: .55rem .65rem;
  cursor: pointer;
  color: var(--text);
  min-width: 0;
}
.list .item:hover { background: var(--surface-2); }
.list li.selected .item { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.list .item .name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .item .meta { display: block; font-size: .8rem; color: var(--muted); }
.row-actions { display: flex; gap: .15rem; padding-right: .3rem; }

/* ---------- ads ---------- */

.ad-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
  padding: 1rem;
}
.ad {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  /* The kebab menu is positioned against the card, and its dropdown is
     allowed to spill past the bottom edge -- so no overflow clipping here;
     the thumbnail rounds its own corners instead. */
  position: relative;
}
.ad .thumb {
  aspect-ratio: 16 / 9;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  overflow: hidden;
  position: relative;
  border-radius: calc(var(--radius) - 1px) calc(var(--radius) - 1px) 0 0;
}
.ad .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ad .thumb .empty { color: var(--muted); font-size: .82rem; text-align: center; padding: .5rem; }
.ad .thumb.dragover { outline: 2px dashed var(--accent); outline-offset: -4px; }
.ad .body { padding: .6rem .7rem; flex: 1; min-width: 0; }
.ad .body h3 { margin: 0 0 .1rem; font-size: .95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad .body a {
  font-size: .8rem;
  color: var(--muted);
  text-decoration: none;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ad .body a:hover { color: var(--accent); text-decoration: underline; }
.ad .stats { margin: .3rem 0 0; font-size: .75rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.ad.busy { opacity: .6; pointer-events: none; }

/* ---------- card kebab menu ---------- */

.ad-menu { position: absolute; top: .45rem; right: .45rem; z-index: 2; }
.ad-menu .kebab {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  color: #fff;
  background: rgba(12, 15, 20, .5);
  backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity .12s ease, background .12s ease;
}
.ad-menu .kebab svg { width: 16px; height: 16px; }
/* Out of the way until wanted -- but never unreachable: touch devices and
   keyboards get it unconditionally. */
.ad:hover .kebab,
.ad:focus-within .kebab,
.ad-menu.open .kebab { opacity: 1; }
@media (hover: none) {
  .ad-menu .kebab { opacity: 1; }
}
.ad-menu .kebab:hover, .ad-menu.open .kebab { background: rgba(12, 15, 20, .75); }
.ad-menu .kebab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; opacity: 1; }

.ad-menu .menu-items {
  position: absolute;
  top: calc(100% + .3rem);
  right: 0;
  min-width: 160px;
  padding: .25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.ad-menu .menu-item {
  font: inherit;
  font-size: .86rem;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 6px;
  padding: .45rem .55rem;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.ad-menu .menu-item:hover { background: var(--surface-2); }
.ad-menu .menu-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.ad-menu .menu-item.danger { color: var(--danger); }
.ad-menu .menu-item.danger:hover { background: var(--danger-bg); }

.empty-state { padding: 2.5rem 1rem; text-align: center; color: var(--muted); }

/* Footer under a list that has more pages waiting on the server. */
.load-more { display: flex; justify-content: center; padding: 0 1rem 1rem; }
.load-more .btn { min-width: 160px; }
.list .load-more { flex: 1; padding: .3rem .35rem .45rem; }
.list .load-more .btn { width: 100%; }

/* ---------- modal ---------- */

.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.25rem;
  width: min(420px, calc(100vw - 2rem));
  box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(8, 10, 14, .45); }
.modal h3 { margin: 0 0 1rem; font-size: 1.05rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin: 1.25rem 0 0; padding: 0; }

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: .6rem .9rem;
  max-width: 340px;
  font-size: .88rem;
}
.toast.error { border-left-color: var(--danger); color: var(--danger); }
