:root {
  --bg:        #f5f6f8;
  --surface:   #ffffff;
  --border:    #d8dde6;
  --text:      #1a1f2e;
  --muted:     #6c7791;
  --accent:    #0a55a8;
  --accent-fg: #ffffff;
  --ok:        #1a8048;
  --warn:      #b56400;
  --err:       #b1261d;
  --radius:    8px;

  /* Surface variants used in hovers, locked panels, banners. Default is the
     light-mode palette; the dark-mode block below overrides them. Keeping
     these as variables (rather than hardcoded hexes elsewhere) means dark
     mode is one block of overrides instead of a hundred little ones. */
  --surface-hover:    #eef0f4;
  --surface-subtle:   #f7f8fb;
  --row-hover-bg:     #f7faff;
  --row-hover-border: #cde0f5;
  --emphasis-bg:      #f7faff;
  --emphasis-border:  #cde0f5;
  --tab-active-bg:    #e7eef7;
  --avatar-default-bg: #cdd5e3;

  --pill-valpak-bg:  #e7eef7;
  --pill-clipp-bg:   #fde7e5;
  --pill-other-bg:   #eef0f4;

  --banner-warning-bg:     #fff5e0;
  --banner-warning-border: #f0c97e;
  --banner-error-bg:       #fde7e5;
  --banner-error-border:   #f0a39d;
  --banner-success-bg:     #e3f6e9;
  --banner-success-border: #a4dab4;

  --modal-overlay: rgba(26, 31, 46, 0.5);
  --shadow:        0 8px 32px rgba(0, 0, 0, 0.2);
}

/* Auto dark theme — follows the OS "appearance" setting via
   prefers-color-scheme. The QR-preview image keeps a white background
   even in dark mode because the printed QR codes themselves are
   black-on-white; matching that on screen makes the on-screen preview
   look identical to the file the user prints/exports. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #0e1219;
    --surface:   #181d2a;
    --border:    #2c3447;
    --text:      #e6e9f0;
    --muted:     #98a1b8;
    --accent:    #5a9def;
    --accent-fg: #0e1219;
    --ok:        #4cc488;
    --warn:      #e6a64a;
    --err:       #ed5b50;

    --surface-hover:    #232a3d;
    --surface-subtle:   #141924;
    --row-hover-bg:     #1e2740;
    --row-hover-border: #3c5b8a;
    --emphasis-bg:      #14213a;
    --emphasis-border:  #3c5b8a;
    --tab-active-bg:    #1f3458;
    --avatar-default-bg: #3a4259;

    --pill-valpak-bg:  #1f3458;
    --pill-clipp-bg:   #4a1f1c;
    --pill-other-bg:   #2a3148;

    --banner-warning-bg:     #3a2a0e;
    --banner-warning-border: #7a5a1c;
    --banner-error-bg:       #3a1816;
    --banner-error-border:   #7a302c;
    --banner-success-bg:     #143625;
    --banner-success-border: #2c6a48;

    --modal-overlay: rgba(0, 0, 0, 0.65);
    --shadow:        0 8px 32px rgba(0, 0, 0, 0.55);
  }
  /* QR PNG preview stays on a white card regardless of theme so the
     on-screen preview matches the actual printed asset. */
  .qr-preview img { background: #ffffff; }
  /* Inputs autofilled by Chrome look bright-yellow on dark backgrounds;
     this hack keeps the inset color matching our theme. */
  input:-webkit-autofill {
    -webkit-text-fill-color: var(--text);
    -webkit-box-shadow: 0 0 0px 1000px var(--surface) inset;
  }
}

* { box-sizing: border-box; }

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

header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  font-weight: 600;
  font-size: 17px;
  min-width: 0;
}
.brand .logo {
  display: inline-flex;       /* picture default is inline; flex centers img */
  align-items: center;
  height: 28px;
  flex-shrink: 0;
}
.brand .logo img {
  height: 100%;
  width: auto;
  display: block;
}
.brand-suffix {
  color: var(--muted);
  font-weight: 400;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#primary-nav {
  display: flex;
  gap: 4px;
  margin-left: 16px;
}
.nav-tab {
  text-decoration: none;
  padding: 6px 12px;
  border-radius: 6px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
}
.nav-tab:hover { color: var(--text); background: var(--surface-hover); }
.nav-tab.active {
  color: var(--accent);
  background: var(--tab-active-bg);
}

#user-badge {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
  margin-left: auto;
}
#user-badge .avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 13px;
  flex-shrink: 0;
  overflow: hidden;
}
#user-badge .avatar img {
  width: 100%; height: 100%; object-fit: cover;
}
#user-badge .user-meta { line-height: 1.2; }
#user-badge .user-name  { color: var(--text); font-weight: 500; font-size: 14px; }
#user-badge .user-email { color: var(--muted); font-size: 12px; }
#user-badge button {
  background: none; border: none; color: var(--accent);
  cursor: pointer; font-size: 13px; padding: 0 0 0 8px;
  border-left: 1px solid var(--border);
  margin-left: 4px;
  height: 28px;
}

main {
  max-width: 880px;
  margin: 32px auto;
  padding: 0 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 16px;
}
.card h1 { margin: 0 0 16px; font-size: 20px; }

label {
  display: block;
  margin-bottom: 16px;
  font-weight: 500;
  font-size: 14px;
}
label small {
  display: block;
  font-weight: 400;
  color: var(--muted);
  margin-top: 4px;
}
label.emphasis {
  background: var(--emphasis-bg);
  border: 1px solid var(--emphasis-border);
  border-radius: 6px;
  padding: 12px;
}
input[type="text"], input[type="url"], input[type="search"] {
  display: block;
  width: 100%;
  padding: 8px 10px;
  margin-top: 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input::placeholder {
  color: var(--muted);
  opacity: 1;  /* Firefox lowers placeholder opacity by default; reset */
}

.field-status {
  display: block;
  margin-top: 6px;
  font-size: 13px;
  font-weight: 400;
}
.field-status.ok   { color: var(--ok); }
.field-status.warn { color: var(--warn); }
.field-status.err  { color: var(--err); }

/* Inline call-out box for field-level validation messages. Same colour
   treatment as .banner.{warning,error} but tighter margins so it sits
   directly under the input it describes. */
.field-banner {
  margin-top: 8px;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 400;
}
.field-banner.warning {
  background: var(--banner-warning-bg);
  color: var(--warn);
  border: 1px solid var(--banner-warning-border);
}
.field-banner.error {
  background: var(--banner-error-bg);
  color: var(--err);
  border: 1px solid var(--banner-error-border);
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 16px;
  margin: 0 0 16px;
}
fieldset legend { font-weight: 500; font-size: 14px; padding: 0 6px; }
.radio {
  display: flex; align-items: flex-start; gap: 10px;
  margin: 6px 0; font-weight: 400;
}
.radio input { margin-top: 4px; }

details summary {
  cursor: pointer;
  font-size: 14px;
  color: var(--muted);
  margin: 8px 0 12px;
}

.actions {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 8px;
  flex-wrap: wrap;
}
button.primary, a.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  padding: 9px 16px;
  font: inherit;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
button.primary:disabled { background: var(--muted); cursor: not-allowed; }
button.link {
  background: none; border: none; color: var(--accent);
  cursor: pointer; font: inherit; padding: 0;
}
button.danger {
  background: var(--err);
  color: white;
  border: none;
  padding: 9px 16px;
  font: inherit;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
}
.primary.block { display: block; text-align: center; margin-top: 8px; }

.banner {
  border-radius: 6px;
  padding: 10px 12px;
  margin: 12px 0;
  font-size: 14px;
}
.banner.warning { background: var(--banner-warning-bg); color: var(--warn); border: 1px solid var(--banner-warning-border); }
.banner.error   { background: var(--banner-error-bg);   color: var(--err);  border: 1px solid var(--banner-error-border); }
.banner ul { margin: 4px 0 0; padding-left: 20px; }

.form-banner.warning { background: var(--banner-warning-bg); color: var(--warn); border: 1px solid var(--banner-warning-border); padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; }
.form-banner.error   { background: var(--banner-error-bg);   color: var(--err);  border: 1px solid var(--banner-error-border);   padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; }
.form-banner.success { background: var(--banner-success-bg); color: var(--ok);   border: 1px solid var(--banner-success-border); padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; }

.result-grid {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 24px;
}
.qr-preview img {
  width: 100%;
  height: auto;
  aspect-ratio: 1;     /* reserve a square even before the src loads */
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  display: block;
}
/* Loading state: shimmer behind a transparent placeholder src so we never
   show the broken-image icon while the presigned URL is being fetched. */
.qr-preview img.loading {
  background:
    linear-gradient(90deg,
      var(--surface-subtle) 0%,
      var(--surface-hover) 50%,
      var(--surface-subtle) 100%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.qr-preview .qr-meta {
  margin-top: 10px;
  display: flex;
  align-items: flex-start;  /* icon hugs the top so it stays put as URL wraps */
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}
.qr-preview .qr-meta code {
  flex: 1 1 auto;
  min-width: 0;             /* lets the URL shrink + wrap inside its own box */
  word-break: break-all;
  /* Line-height matches .copy-icon-btn height (22px) so the URL's first-line
     center aligns with the icon center under align-items: flex-start. */
  line-height: 22px;
}
.copy-icon-btn {
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--border);
  padding: 0;
  width: 26px;
  height: 22px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.1s, border-color 0.1s, background 0.1s;
}
.copy-icon-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}
.copy-icon-btn.copied {
  color: var(--ok);
  border-color: var(--ok);
}
.copy-icon-btn svg { display: block; }
.result-meta dl { margin: 0 0 16px; }
.result-meta dt {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--muted); margin-top: 10px;
  display: flex; align-items: baseline; gap: 8px;
}
.result-meta dt .link.inline {
  font-size: 11px;
  text-transform: none;     /* override the dt's uppercase */
  letter-spacing: 0;
  font-weight: 500;
  cursor: pointer;
  color: var(--accent);
  text-decoration: none;
}
.result-meta dt .link.inline:hover { text-decoration: underline; }
.result-meta dd {
  margin: 2px 0 0; font-size: 14px; word-break: break-all;
}
/* Definition-list rows that pair a value (typically a <code> URL) with a
   small icon button — keeps the icon parked at the top-right while the
   value wraps in its own column. */
.result-meta dd.copy-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.result-meta dd.copy-row code {
  flex: 1 1 auto;
  min-width: 0;
  word-break: break-all;
  /* Match the .copy-icon-btn height (22px) so the URL first-line center
     lines up with the icon center. */
  line-height: 22px;
}
code { font-family: SFMono-Regular, Menlo, monospace; font-size: 13px; }

footer {
  text-align: center;
  padding: 24px;
  color: var(--muted);
}
footer a { color: var(--muted); }

.hidden { display: none !important; }

/* ==================== List view ==================== */
.omnibar {
  position: relative;
  margin-bottom: 12px;
}
.omnibar input {
  font-size: 16px;
  padding: 12px 36px 12px 14px;
}
.spinner {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: translateY(-50%) rotate(360deg); } }

.filter-row {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.toggle-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface);
}
.toggle {
  background: transparent;
  border: none;
  padding: 6px 14px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}
.toggle.on {
  background: var(--accent);
  color: var(--accent-fg);
}
.list-meta {
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
}

.results {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.row {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}
.row:hover { background: var(--row-hover-bg); border-color: var(--row-hover-border); }
.row.deactivated { opacity: 0.65; }
.row .row-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--avatar-default-bg); color: var(--text);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 13px; overflow: hidden;
}
.row .row-avatar img { width: 100%; height: 100%; object-fit: cover; }
.row .row-main {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.row .row-title {
  display: flex; align-items: center; gap: 8px;
  font-weight: 500;
}
.row .row-title .campaign {
  flex: 1;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  min-width: 0;
}
.brand-pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
}
.brand-pill.valpak { background: var(--pill-valpak-bg); color: var(--accent); }
.brand-pill.clipp  { background: var(--pill-clipp-bg);  color: var(--err); }
.brand-pill.other  { background: var(--pill-other-bg);  color: var(--muted); }
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.status-dot.active   { background: var(--ok); }
.status-dot.inactive { background: var(--muted); border: 1px solid var(--border); }
.row .row-sub {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.row .row-sub code { color: var(--text); }
.row .row-sub .arrow { color: var(--muted); margin: 0 6px; }
.row .row-foot {
  font-size: 12px;
  color: var(--muted);
}
.row .row-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}
.row .row-actions .row-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  transition: background 0.1s, border-color 0.1s, color 0.1s;
}
.row .row-actions .row-btn:hover {
  background: var(--surface-hover);
  border-color: var(--muted);
}
.row .row-actions .row-btn.primary-btn {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.row .row-actions .row-btn.primary-btn:hover {
  filter: brightness(0.9);
}

.empty {
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

/* ==================== Edit view ==================== */
.edit-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
  border: none;
  padding: 0;
  background: transparent;
}
.edit-header h1 { margin: 0; }

.locked-fields {
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 16px;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.locked-field {
  display: grid;
  grid-template-columns: 24px 110px 1fr auto;
  align-items: baseline;
  gap: 8px;
  font-size: 13px;
}
.locked-field .lock-icon { font-size: 12px; opacity: 0.5; }
.locked-field .locked-label { color: var(--muted); }
.locked-field .locked-value { font-weight: 500; }
.locked-field .locked-why {
  color: var(--muted); font-size: 11px; font-style: italic;
}

.mod-info {
  /* Inherit font-size from body (15px) so the version/created text shares
     line metrics + baseline with the adjacent Cancel link, letting the
     parent .actions { align-items: center } produce a clean shared
     baseline. Spacing also comes from the parent .actions { gap: 12px }. */
  color: var(--muted);
}

.audit-history {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.audit-history summary { font-size: 13px; color: var(--muted); }
.history-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
}
.history-list li {
  display: grid;
  /* Action column needs to fit the longest action token ("update_url_and_name")
     without wrapping; spans without breakable spaces overflow a fixed-width
     grid cell, which is what bled the action text into the date column. */
  grid-template-columns: 160px 110px 1fr;
  gap: 12px;
  padding: 6px 0;
  font-size: 12px;
  border-bottom: 1px dashed var(--border);
}
.history-list li:last-child { border-bottom: none; }
.history-list .h-action {
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
}
.history-list .h-action.create     { color: var(--accent); }
.history-list .h-action.deactivate { color: var(--err); }
.history-list .h-action.reactivate { color: var(--ok); }
.history-list .h-action.update_url,
.history-list .h-action.update_name,
.history-list .h-action.update_url_and_name { color: var(--warn); }

.danger-zone {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* ==================== Modal ==================== */
.modal-overlay {
  position: fixed; inset: 0;
  background: var(--modal-overlay);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal-card {
  background: var(--surface);
  padding: 24px;
  border-radius: var(--radius);
  max-width: 460px;
  width: 90%;
  box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 12px; font-size: 18px; }
.modal-card p { margin: 0 0 16px; }

@media (max-width: 720px) {
  .result-grid { grid-template-columns: 1fr; }
  .row { grid-template-columns: 36px 1fr; }
  .row .row-actions { grid-column: 2; margin-top: 8px; }
  .locked-field { grid-template-columns: 24px 1fr; }
  .locked-field .locked-why { grid-column: 2; font-size: 11px; }
}
