:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #17191e;
  --muted: #667085;
  --line: #e4e7ec;
  --accent: #0f62fe;
  --ok: #0d7a43;
  --warn: #a35a00;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
}

* { box-sizing: border-box; }

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

header { display: flex; align-items: center; gap: 10px; max-width: 1180px; margin: 0 auto; padding: 22px 24px 6px; }
header .logo { display: block; height: 24px; width: auto; }
header h1 { margin: 0; font-size: 19px; letter-spacing: -0.01em; }

main { max-width: 1180px; margin: 0 auto; padding: 14px 24px 48px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  display: grid;
  gap: 10px;
}

/* The title sits on its own line so the input, the Clear button and the
   keep-channel toggle share one row and centre against each other. */
.hero { display: grid; gap: 4px; }
.hero-title { font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.hero-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.hero-row #url { flex: 1 1 320px; width: auto; }

.field { display: grid; gap: 4px; min-width: 150px; }
.field > span { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.field.grow { flex: 1 1 320px; }

input[type="text"], select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}
input[type="text"]::placeholder { color: #98a2b3; }
input[type="text"]:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
#url { font-family: var(--mono); font-size: 13px; }
#market { min-width: 170px; }

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 600ms linear infinite;
}
.spinner[hidden] { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }

/* The panel reads as three labelled bands: the page, the campaign naming, the
   UTMs. Every field is visible — nothing hides behind a toggle. */
.group { display: grid; gap: 10px; }
.group + .group { border-top: 1px solid var(--line); padding-top: 12px; }
.group-title {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.check input { accent-color: var(--accent); }

.status {
  margin: 0;
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 13px;
  border: 1px solid var(--line);
  background: #fafbfc;
  transition: opacity 180ms var(--ease-out), transform 180ms var(--ease-out), display 180ms allow-discrete;
  @starting-style { opacity: 0; transform: translateY(-3px); }
}
.status[hidden] { display: none; }
.status.ok { color: var(--ok); border-color: #bfe3cd; background: #f1faf4; }
.status.warn { color: var(--warn); border-color: #f2d9b0; background: #fff8ee; }
.status.muted { color: var(--muted); }

.campaign { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; }
.campaign[hidden] { display: none; }
.campaign-label { color: var(--muted); text-transform: uppercase; font-size: 11px; letter-spacing: 0.04em; }
.campaign-hint { color: var(--muted); font-size: 12px; }
#campaign-name { font-family: var(--mono); background: #f2f4f7; padding: 3px 7px; border-radius: 6px; }

.cards-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin: 14px 2px 0;
  color: var(--muted);
  font-size: 12px;
}
.cards-bar[hidden] { display: none; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 12px; margin-top: 10px; }
.cards[hidden] { display: none; }
/* Stale content dims while a new lookup is in flight. */
.cards, .campaign { transition: opacity 160ms ease; }
body.busy .cards, body.busy .campaign { opacity: 0.5; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px 12px;
  display: grid;
  gap: 8px;
  align-content: start;
}

.card-head { display: flex; align-items: center; gap: 8px; }
.card-title { font-weight: 600; font-size: 14px; }

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: var(--muted);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  flex: none;
}
.badge svg { width: 14px; height: 14px; display: block; }
.badge[data-key="meta"] { background: #0866ff; }
.badge[data-key="tiktok"] { background: #101828; }
.badge[data-key="pinterest"] { background: #e60023; }
.badge[data-key="snapchat"] { background: #fffc00; color: #101828; }
.badge[data-key="google_ads"], .badge[data-key="microsoft_ads"] { background: #fff; border: 1px solid var(--line); }

.rows { display: grid; gap: 8px; }
.row { display: grid; gap: 3px; }
.row-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.row-actions { display: inline-flex; align-items: center; gap: 6px; }
.row-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.scope {
  margin-left: 5px;
  padding: 1px 6px;
  border-radius: 999px;
  background: #eef2ff;
  color: #3b4cca;
  font-size: 10px;
  letter-spacing: 0.02em;
  text-transform: none;
  white-space: nowrap;
}

.value {
  font-family: var(--mono);
  font-size: 12px;
  color: #344054;
  background: #f8f9fb;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  display: block;
  user-select: all;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
/* A field the marketer sets once (account level) does not deserve
   the same visual weight as the per-ad URL. */
.once {
  border-top: 1px dashed var(--line);
  margin-top: 1px;
  padding-top: 1px;
}
.once > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 0;
  color: var(--muted);
  font-size: 12px;
}
.once > summary::-webkit-details-marker { display: none; }
.once > summary::before {
  content: "";
  width: 0;
  height: 0;
  border-left: 4px solid currentColor;
  border-top: 3.5px solid transparent;
  border-bottom: 3.5px solid transparent;
  transition: transform 140ms var(--ease-out);
}
.once[open] > summary::before { transform: rotate(90deg); }
.once > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-radius: 4px; }
.once[open] > .row { margin-top: 4px; }
@media (hover: hover) and (pointer: fine) {
  .once > summary:hover { color: var(--ink); }
}

.copy {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid #98a2b3;
  background: #fff;
  border-radius: 7px;
  padding: 3px 9px;
  font: inherit;
  font-size: 12px;
  color: #1d2939;
  cursor: pointer;
  min-width: 62px;
  text-decoration: none;
  transition: transform 140ms var(--ease-out), border-color 140ms ease, color 140ms ease, background-color 140ms ease;
}
.copy svg { display: block; width: 13px; height: 13px; }
.copy:active { transform: scale(0.97); }
.copy:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.copy.done { border-color: #bfe3cd; background: #f1faf4; color: var(--ok); }
.copy.small { padding: 2px 8px; }

.offscreen { position: fixed; top: -1000px; }

/* Help: a native dialog, so Escape and focus handling come for free. */
.help { margin-left: auto; }
dialog#help-dialog {
  width: min(760px, calc(100vw - 40px));
  max-height: 84vh;
  padding: 16px 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--ink);
  font: inherit;
}
dialog#help-dialog::backdrop { background: rgb(16 24 40 / 45%); }
.help-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.help-head h2 { margin: 0; font-size: 17px; }
.help-body { overflow: auto; max-height: calc(84vh - 78px); margin-top: 8px; padding-right: 4px; }
.help-body p { margin: 0 0 10px; font-size: 13px; }
.help-body h3 {
  margin: 16px 0 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}
.help-body dl { display: grid; grid-template-columns: 160px 1fr; gap: 5px 14px; margin: 0; }
.help-body dt { font-size: 13px; font-weight: 600; }
.help-body dd { margin: 0; font-size: 13px; line-height: 1.5; color: #344054; }
.help-body ul, .help-body ol { margin: 0; padding-left: 18px; }
.help-body li { font-size: 13px; line-height: 1.5; margin-bottom: 4px; }
.help-body code { font-family: var(--mono); font-size: 12px; background: #f2f4f7; padding: 1px 5px; border-radius: 5px; }

@media (hover: hover) and (pointer: fine) {
  .copy:hover { border-color: #1d2939; background: #f2f4f7; color: #101828; }
  .card:hover { border-color: #d0d5dd; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 1ms !important;
    animation-duration: 1ms !important;
  }
  /* Functional motion: the spinner is the only signal that a lookup is running. */
  .spinner { animation: spin 900ms linear infinite !important; }
  .copy:active { transform: none; }
}

@media (max-width: 640px) {
  .cards { grid-template-columns: 1fr; }
}
