/*
 * Affiliatant. One stylesheet, no build step, no framework.
 *
 * Colour is defined once as tokens on :root and redefined for dark mode. A
 * screen that reports money has to be readable in both, and a merchant
 * checking a payout run at night should not be flashbanged.
 */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #d8dce3;
  --border-strong: #b6bcc7;
  --text: #15181d;
  --text-muted: #5b6573;
  --text-faint: #828c9b;

  --accent: #1f5f8b;
  --accent-text: #ffffff;
  --accent-soft: #e4eef6;

  --good: #1d7a4c;
  --good-soft: #e3f3ea;
  --warn: #8a5a00;
  --warn-soft: #fbf0d9;
  --bad: #a3282c;
  --bad-soft: #fbe6e7;
  --info-soft: #e8eefb;

  --radius: 6px;
  --gutter: 20px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181d;
    --surface: #1c2027;
    --surface-2: #242933;
    --border: #333a46;
    --border-strong: #4a5464;
    --text: #e8eaee;
    --text-muted: #a3acba;
    --text-faint: #7d8796;

    --accent: #6ba9d4;
    --accent-text: #0d1116;
    --accent-soft: #1d2d3b;

    --good: #5bc48c;
    --good-soft: #15291f;
    --warn: #e0b057;
    --warn-soft: #2b2415;
    --bad: #e8888c;
    --bad-soft: #2e1a1c;
    --info-soft: #1a2133;
  }
}

:root[data-theme="dark"] {
  --bg: #15181d;
  --surface: #1c2027;
  --surface-2: #242933;
  --border: #333a46;
  --border-strong: #4a5464;
  --text: #e8eaee;
  --text-muted: #a3acba;
  --text-faint: #7d8796;
  --accent: #6ba9d4;
  --accent-text: #0d1116;
  --accent-soft: #1d2d3b;
  --good: #5bc48c;
  --good-soft: #15291f;
  --warn: #e0b057;
  --warn-soft: #2b2415;
  --bad: #e8888c;
  --bad-soft: #2e1a1c;
  --info-soft: #1a2133;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--sans);
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; font-weight: 600; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.175rem; }
h3 { font-size: 1rem; }

code, .mono, .token { font-family: var(--mono); font-size: .875em; }

/* ---------- Shell ---------- */

.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 220px;
  flex: 0 0 220px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 0;
  display: flex;
  flex-direction: column;
}

.brand {
  padding: 0 var(--gutter) 16px;
  font-weight: 650;
  font-size: 1.05rem;
  letter-spacing: -.01em;
}
.brand small { display: block; font-weight: 400; font-size: .75rem; color: var(--text-faint); }

.nav { list-style: none; margin: 0; padding: 0; flex: 1; }
.nav a {
  display: block;
  padding: 7px var(--gutter);
  color: var(--text-muted);
  border-left: 3px solid transparent;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a[aria-current="page"] {
  color: var(--text);
  background: var(--accent-soft);
  border-left-color: var(--accent);
  font-weight: 550;
}
.nav .nav-group {
  padding: 14px var(--gutter) 4px;
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
}
.nav .count {
  float: right;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 0 7px;
  font-size: .75rem;
  color: var(--text-muted);
}
.nav a[aria-current="page"] .count { background: var(--surface); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px var(--gutter);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .spacer { flex: 1; }
.topbar .who { color: var(--text-muted); font-size: .875rem; }

.content { padding: var(--gutter); max-width: 1400px; width: 100%; }

.page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }
.page-head p { margin: 2px 0 0; color: var(--text-muted); }

/* ---------- Impersonation and other standing warnings ---------- */

.banner {
  padding: 8px var(--gutter);
  font-size: .875rem;
  border-bottom: 1px solid var(--border);
}
.banner--impersonating { background: var(--warn-soft); color: var(--warn); }
.banner--over-quota { background: var(--bad-soft); color: var(--bad); }
.banner--test { background: var(--info-soft); }
.banner a { font-weight: 600; }

/* ---------- Cards and panels ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }

.grid { display: grid; gap: 16px; }
.grid--2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid--3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid--4 { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }

/* ---------- Stat tiles ---------- */

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat .label { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); }
.stat .value { font-size: 1.6rem; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .sub { font-size: .8rem; color: var(--text-muted); margin-top: 2px; }

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th {
  background: var(--surface-2);
  font-weight: 600;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.money { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-family: var(--mono); }
td.money.negative { color: var(--bad); }

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

/* ---------- Badges ---------- */

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: .75rem;
  font-weight: 550;
  white-space: nowrap;
  background: var(--surface-2);
  color: var(--text-muted);
}
.badge--good, .badge--approved, .badge--active, .badge--settled, .badge--delivered { background: var(--good-soft); color: var(--good); }
.badge--warn, .badge--pending, .badge--held, .badge--draft, .badge--queued { background: var(--warn-soft); color: var(--warn); }
.badge--bad, .badge--rejected, .badge--failed, .badge--suspended, .badge--reversed { background: var(--bad-soft); color: var(--bad); }
.badge--info, .badge--unmatched, .badge--instructed { background: var(--info-soft); color: var(--accent); }

/* ---------- Forms ---------- */

label { display: block; font-weight: 550; margin-bottom: 4px; font-size: .875rem; }
.field { margin-bottom: 14px; }
.field .hint { font-size: .8rem; color: var(--text-muted); margin-top: 3px; }
.field .error { font-size: .8rem; color: var(--bad); margin-top: 3px; }

input[type=text], input[type=email], input[type=password], input[type=url],
input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
textarea { min-height: 110px; resize: vertical; }
input[type=checkbox], input[type=radio] { width: auto; margin-right: 6px; }

.inline-form { display: inline; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 14px; }
.filters .field { margin-bottom: 0; min-width: 150px; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-block;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn--primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn--danger { color: var(--bad); border-color: var(--bad); }
.btn--small { padding: 3px 9px; font-size: .8rem; }
.btn[disabled], .btn--disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- Notices ---------- */

.notice { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid var(--border); }
.notice--good { background: var(--good-soft); border-color: var(--good); color: var(--good); }
.notice--warn { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.notice--bad  { background: var(--bad-soft);  border-color: var(--bad);  color: var(--bad); }
.notice--info { background: var(--info-soft); border-color: var(--accent); }

/*
 * Freshness. Every report says when it was built and when it next will be.
 * No competitor publishes a reporting latency, and this one line removes the
 * commonest support question in the category.
 */
.freshness { font-size: .8rem; color: var(--text-faint); }
.freshness--pending { color: var(--warn); }

/* Tracking status, which is a screen here rather than a help article. */
.tracking-status { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: var(--radius); border: 1px solid var(--border); }
.tracking-status .dot { width: 11px; height: 11px; border-radius: 50%; flex: 0 0 11px; background: var(--text-faint); }
.tracking-status--live { background: var(--good-soft); border-color: var(--good); }
.tracking-status--live .dot { background: var(--good); }
.tracking-status--waiting { background: var(--warn-soft); border-color: var(--warn); }
.tracking-status--waiting .dot { background: var(--warn); }

/* ---------- Attribution explanation ---------- */

.attribution { border-left: 3px solid var(--accent); padding-left: 12px; }
.attribution dl { display: grid; grid-template-columns: max-content 1fr; gap: 3px 12px; margin: 0; }
.attribution dt { color: var(--text-muted); font-size: .85rem; }
.attribution dd { margin: 0; font-size: .85rem; }
.attribution .losers { margin-top: 8px; font-size: .82rem; color: var(--text-muted); }

/* ---------- Bars, used instead of a chart library ---------- */

.bars { display: flex; align-items: flex-end; gap: 2px; height: 70px; }
.bars .bar { flex: 1; background: var(--accent); border-radius: 2px 2px 0 0; min-height: 2px; opacity: .85; }
.bars .bar:hover { opacity: 1; }

.sparkrow { display: flex; align-items: center; gap: 10px; }

/* ---------- Setup checklist ---------- */

.checklist { list-style: none; margin: 0; padding: 0; }
.checklist li { padding: 7px 0; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: baseline; }
.checklist li:last-child { border-bottom: 0; }
.checklist .tick { color: var(--good); font-weight: 700; }
.checklist .todo { color: var(--text-faint); }
.checklist li.done { color: var(--text-muted); }

/* ---------- Auth pages ---------- */

.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--gutter); }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.auth-card h1 { font-size: 1.25rem; }

/* ---------- Utilities ---------- */

.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.small { font-size: .85rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.mt0 { margin-top: 0; }
.mb0 { margin-bottom: 0; }
.mt16 { margin-top: 16px; }
.pill { background: var(--surface-2); border-radius: var(--radius); padding: 2px 7px; font-family: var(--mono); font-size: .8rem; }
.truncate { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; vertical-align: bottom; }

.pagination { display: flex; gap: 8px; align-items: center; margin-top: 14px; }

/* ---------- Phone ---------- */

@media (max-width: 860px) {
  .shell { flex-direction: column; }
  .sidebar { width: 100%; flex: none; border-right: 0; border-bottom: 1px solid var(--border); }
  .nav { display: flex; flex-wrap: wrap; }
  .nav a { border-left: 0; border-bottom: 3px solid transparent; }
  .nav a[aria-current="page"] { border-left: 0; border-bottom-color: var(--accent); }
  .nav .nav-group { display: none; }
  .content { padding: 16px; }
}

@media print {
  .sidebar, .topbar, .btn, .filters, .pagination { display: none !important; }
  body { background: #fff; color: #000; }
  .card, .table-wrap { border-color: #999; }
}

/* ---------- Operator console additions ---------- */

/*
 * The outbox message page. Mail delivery is off, so the only way to see what
 * a message would have looked like is to render it here. The HTML part is
 * assembled from tenant-supplied values, so it goes inside a sandboxed frame
 * rather than into this page: the frame is what needs a size.
 */
.msg-frame {
  width: 100%;
  height: 420px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  /* White, not var(--surface): an email body expects a light background and
     is not written against our tokens. */
  background: #fff;
}

/* The text part and the HTML source, both shown verbatim. */
.wrap-pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}

/* The twelve month usage table on the operator billing screen: one bar per
   row, in a column, rather than a chart library. */
.bar-cell { width: 40%; }
.bars--inline { height: 14px; }
.bars--inline .bar { flex: 0 0 auto; }
