/* Single stylesheet, no build step. Colours come from variables so the dark
   scheme only has to override the palette. */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-alt: #fafbfc;
  --border: #dfe3e8;
  --border-strong: #c4cad2;
  --text: #1b1f24;
  --text-muted: #646d78;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --accent-soft: #e8effd;
  --danger: #c0332e;
  --danger-soft: #fdecea;
  --success: #1d7a4c;
  --success-soft: #e6f4ec;
  --warning: #96620a;
  --warning-soft: #fdf1dc;
  --purple: #6b3fb5;
  --purple-soft: #efe8fb;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1c2024;
    --surface-alt: #22272c;
    --border: #30363d;
    --border-strong: #454d56;
    --text: #e6e9ed;
    --text-muted: #9aa4b0;
    --accent: #5b8def;
    --accent-text: #0d1117;
    --accent-soft: #1e2a44;
    --danger: #f08a84;
    --danger-soft: #3a1f1e;
    --success: #6fce9b;
    --success-soft: #16301f;
    --warning: #e5b567;
    --warning-soft: #33280f;
    --purple: #b394ea;
    --purple-soft: #271c3d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

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

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

h1 { font-size: 1.5rem; margin: 0 0 0.25rem; }
h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }
h3 { font-size: 0.95rem; margin: 0 0 0.5rem; }

.muted { color: var(--text-muted); }
.small { font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }

/* ---------- layout ---------- */

.container { max-width: 1280px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0.6rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.brand { font-weight: 650; color: var(--text); letter-spacing: -0.01em; }
.brand:hover { text-decoration: none; }

.mainnav { display: flex; gap: 0.15rem; flex-wrap: wrap; }

.mainnav a {
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.mainnav a:hover { background: var(--surface-alt); color: var(--text); text-decoration: none; }
.mainnav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 550; }

.globalsearch { margin-left: auto; }
.globalsearch input { width: 220px; }

.usermenu { display: flex; align-items: center; gap: 0.6rem; }
.username { font-size: 0.85rem; color: var(--text-muted); display: flex; align-items: center; gap: 0.4rem; }

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.page-actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}

/* Irreversible actions, marked so they read differently from the rest of a page. */
.card-danger { border-color: var(--danger); }
.card-danger .card-header h2 { color: var(--danger); }

.card-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.card-header h2 { margin: 0; }

.grid { display: grid; gap: 1.25rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

/* ---------- stat tiles ---------- */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.75rem; margin-bottom: 1.25rem; }

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow);
}

.stat:hover { text-decoration: none; border-color: var(--border-strong); }
.stat .stat-value { font-size: 1.6rem; font-weight: 600; letter-spacing: -0.02em; color: var(--text); }
.stat .stat-label { font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }

/* ---------- tables ---------- */

.table-wrap { overflow-x: auto; }

table.data { width: 100%; border-collapse: collapse; font-size: 0.9rem; }

table.data th,
table.data td { padding: 0.5rem 0.6rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }

table.data thead th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: var(--surface-alt);
  white-space: nowrap;
  position: sticky;
  top: 0;
}

table.data tbody tr:hover { background: var(--surface-alt); }
table.data th a { color: inherit; }
table.data th.sorted a { color: var(--accent); }
table.data .arrow { font-size: 0.7em; }
table.data tbody tr:last-child td { border-bottom: none; }

.row-muted td { opacity: 0.6; }

/* ---------- badges ---------- */

.badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 550;
  white-space: nowrap;
  border: 1px solid transparent;
}

.badge-grey { background: var(--surface-alt); color: var(--text-muted); border-color: var(--border); }
.badge-blue { background: var(--accent-soft); color: var(--accent); }
.badge-green { background: var(--success-soft); color: var(--success); }
.badge-amber { background: var(--warning-soft); color: var(--warning); }
.badge-red { background: var(--danger-soft); color: var(--danger); }
.badge-purple { background: var(--purple-soft); color: var(--purple); }

/* ---------- forms ---------- */

label { display: block; font-size: 0.85rem; font-weight: 550; margin-bottom: 0.25rem; }

input, select, textarea {
  width: 100%;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

textarea { min-height: 5rem; resize: vertical; }

input[type="checkbox"] { width: auto; }

.field { margin-bottom: 0.9rem; }
.field .hint { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.2rem; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 1rem; }
.form-actions { display: flex; gap: 0.6rem; align-items: center; margin-top: 1rem; flex-wrap: wrap; }

fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin: 0 0 1.25rem; }
legend { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); padding: 0 0.4rem; }

/* ---------- filter bar ---------- */

.filters { display: flex; gap: 0.6rem; align-items: flex-end; flex-wrap: wrap; }
.filters .field { margin-bottom: 0; }
.filters label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); }
.filters input, .filters select { min-width: 150px; }
.filters .grow { flex: 1 1 220px; }

/* ---------- buttons ---------- */

.btn {
  display: inline-block;
  padding: 0.42rem 0.8rem;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 550;
  cursor: pointer;
  text-align: center;
}

.btn:hover { background: var(--surface-alt); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-small { padding: 0.22rem 0.5rem; font-size: 0.8rem; }
.btn-block { width: 100%; margin-top: 0.5rem; }
.btn-link { border: none; background: none; color: var(--accent); padding: 0.2rem; font-size: 0.85rem; }
.btn-link:hover { background: none; text-decoration: underline; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.inline-form { display: inline; }

/* ---------- flash & empty states ---------- */

.flash {
  padding: 0.6rem 0.85rem;
  border-radius: 6px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  border: 1px solid transparent;
}

.flash-success { background: var(--success-soft); color: var(--success); }
.flash-error { background: var(--danger-soft); color: var(--danger); }
.flash-info { background: var(--accent-soft); color: var(--accent); }
.flash-warning { background: var(--warning-soft); color: var(--warning); }

.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-muted); }
.empty-state .error-code { font-size: 3rem; font-weight: 650; margin: 0; color: var(--border-strong); }

/* ---------- detail pages ---------- */

.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0.9rem 1.5rem; }
.detail-item .detail-label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.detail-item .detail-value { margin-top: 0.1rem; word-break: break-word; }

.change-list { margin: 0.25rem 0 0; padding-left: 1rem; font-size: 0.85rem; }
.change-list li { margin-bottom: 0.1rem; }
.change-from { color: var(--text-muted); text-decoration: line-through; }

/* ---------- pagination ---------- */

.pager { display: flex; gap: 0.4rem; align-items: center; justify-content: space-between; margin-top: 1rem; flex-wrap: wrap; }
.pager .pager-links { display: flex; gap: 0.3rem; }

/* ---------- login ---------- */

.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; flex-direction: column; }
.login-card { width: 100%; max-width: 360px; margin: 0; }
.login-card h1 { margin-bottom: 0.1rem; }
.login-card label { margin-top: 0.8rem; }

.login-alt { width: 100%; max-width: 360px; margin-top: 1rem; }

/* "or" rule between the password form and the SSO button. */
.login-divider {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-bottom: 0.75rem;
}

.login-divider::before,
.login-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.login-note { margin: 0.6rem 0 0; text-align: center; }

.btn-microsoft { display: flex; align-items: center; justify-content: center; gap: 0.5rem; margin-top: 0; }

/* Microsoft's four-square mark, built from spans so no external asset is needed. */
.ms-logo { display: grid; grid-template-columns: repeat(2, 8px); gap: 2px; }
.ms-square { width: 8px; height: 8px; }
.ms-r { background: #f25022; }
.ms-g { background: #7fba00; }
.ms-b { background: #00a4ef; }
.ms-y { background: #ffb900; }

/* ---------- inline notices ---------- */

/* A short status message inside a card, e.g. "mail is not configured yet". */
.notice {
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  padding: 0.7rem 0.85rem;
  margin: 0 0 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-alt);
  font-size: 0.85rem;
  line-height: 1.5;
}

.notice-warning {
  border-color: var(--warning);
  background: var(--warning-soft);
  color: var(--warning);
}

/* Keeps strong/mono text inside a notice readable against the tinted background. */
.notice-warning strong,
.notice-warning .mono { color: inherit; }

/* ---------- expiry notification settings ---------- */

/* Explanatory line under a card heading, before its controls. */
.section-intro { margin: 0 0 1rem; max-width: 62ch; }

/*
 * The two schedule selects are narrow and belong side by side; the recipients box
 * wants the rest of the row. A wrapping flex row does that at any width, where the
 * shared .filters bar left them on a ragged baseline with the button adrift.
 */
.notify-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 1.25rem;
  align-items: flex-start;
}

/* Tighter than a stacked form: the row already separates these from the actions. */
.notify-fields .field { margin-bottom: 0.5rem; }
.notify-fields .field-narrow { flex: 0 0 auto; }
.notify-fields .field-narrow select { min-width: 8.5rem; }
/* Grows to fill the row but wraps to its own line before getting cramped. */
.notify-fields .field-wide { flex: 1 1 20rem; min-width: 0; }

.notify-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: flex-end; }

/* Status footer: what it sends as, when it last went, and the manual trigger. */
.notify-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 2rem;
  margin-top: 1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
}

.notify-meta > div { display: flex; align-items: baseline; gap: 0.5rem; min-width: 0; }
.notify-meta .mono { word-break: break-all; }

.meta-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

/* Pushes the manual send button to the far end of the footer. */
.notify-send { margin-left: auto; }

/* A list of addresses long enough to be worth folding away. */
.recipient-list summary { cursor: pointer; color: var(--accent); }
.recipient-list summary::marker { color: var(--text-muted); }
.recipient-list ul { margin: 0.35rem 0 0; padding-left: 1.1rem; }
.recipient-list li { margin: 0.1rem 0; }

/* ---------- managed lists (brands, models, vendors, suppliers) ---------- */

/* Add-a-value row at the foot of each list. */
.add-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: flex-end;
  margin-top: 0.9rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--border);
}

/* Per-row edit controls, disclosed with <details> so no JavaScript is needed. */
.row-edit summary { cursor: pointer; display: inline-flex; list-style: none; }
.row-edit summary::-webkit-details-marker { display: none; }
.row-edit-body {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.4rem;
  margin-top: 0.5rem;
}
.inline-form { display: flex; gap: 0.4rem; align-items: center; }
.inline-form input { width: auto; min-width: 9rem; }

/* Visible to screen readers only — labels for the inline rename boxes. */
.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;
}

/* ---------- misc ---------- */

.key-reveal { display: inline-flex; align-items: center; gap: 0.4rem; }

.import-errors { max-height: 320px; overflow-y: auto; }

/* ---------- phones and small tablets ---------- */

@media (max-width: 720px) {
  .container { padding-left: 0.85rem; padding-right: 0.85rem; }

  /* iOS Safari zooms the whole page when a focused field is under 16px, which
     then leaves the layout scrolled sideways. 16px is the floor, not a choice. */
  input,
  select,
  textarea { font-size: 16px; }

  /* Topbar: brand and account on one line, the sections on a single strip that
     scrolls sideways rather than wrapping onto three rows. */
  .topbar-inner { flex-wrap: wrap; gap: 0.5rem 0.75rem; padding-top: 0.5rem; padding-bottom: 0.5rem; }
  .brand { flex: 1 1 auto; }
  .usermenu { order: 1; margin-left: auto; flex: 0 0 auto; }
  .usermenu .username { font-size: 0.8rem; }

  .mainnav {
    order: 2;
    flex: 1 0 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    /* Momentum scrolling without a visible scrollbar eating vertical space. */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    margin: 0 -0.85rem;
    padding: 0 0.85rem;
  }
  .mainnav::-webkit-scrollbar { display: none; }
  .mainnav a {
    white-space: nowrap;
    /* 44px is the smallest comfortable touch target. */
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }

  .globalsearch { order: 3; flex: 1 0 100%; margin-left: 0; width: 100%; }
  .globalsearch input { width: 100%; }

  /* Actions stack and fill the width, so they are easy to hit and never crowd. */
  .page-header { flex-direction: column; align-items: stretch; gap: 0.75rem; }
  .page-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
  .page-actions .btn { flex: 1 1 auto; text-align: center; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; }

  .filters { flex-direction: column; align-items: stretch; }
  .filters > * { width: 100%; }
  /* The search field carries `grow`, which is `flex: 1 1 220px` for the desktop
     row layout. Once the container is a column that 220px basis becomes a height,
     leaving a tall empty gap above the other filters. Same specificity as the rule
     it overrides, so it has to name both classes. */
  .filters .grow { flex: 0 0 auto; }

  .btn { min-height: 44px; }
  .form-actions { flex-direction: column-reverse; align-items: stretch; }
  .form-actions .btn { width: 100%; justify-content: center; }

  /* Detail and summary grids collapse to one column. */
  .detail-grid,
  .grid,
  .summary-grid { grid-template-columns: 1fr !important; }

  /*
   * Data tables become stacked cards. An eight-column table cannot be read on a
   * 375px screen, and a sideways-scrolling table is little better — you lose the
   * row you were reading. Each row turns into a card, each cell into a
   * label/value line, with the label coming from the td's data-label attribute
   * (the thead is hidden, so the column headings would otherwise be lost).
   */
  .table-wrap { overflow-x: visible; }

  table.data { font-size: 0.95rem; }
  table.data thead { display: none; }

  table.data,
  table.data tbody,
  table.data tr,
  table.data td { display: block; width: 100%; }

  table.data tbody tr {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.7rem 0.85rem;
    margin-bottom: 0.7rem;
    background: var(--surface);
  }
  table.data tbody tr:hover { background: var(--surface); }
  table.data tbody tr:last-child { margin-bottom: 0; }

  table.data td {
    display: flex;
    /* Wrapping is a safety net: a cell holding several inline elements turns each
       one into a flex item, and without this they would run off the card. */
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.15rem 0.75rem;
    padding: 0.25rem 0;
    border-bottom: none;
    text-align: right;
  }

  table.data td::before {
    content: attr(data-label);
    flex: 0 0 40%;
    text-align: left;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
  }

  /* Cells with nothing to say are noise on a phone. */
  table.data td.cell-empty { display: none; }

  /* The first cell is the card's heading, not a label/value pair. */
  table.data td.cell-title {
    text-align: left;
    font-size: 1.05rem;
    padding: 0 0 0.5rem;
    margin-bottom: 0.4rem;
    border-bottom: 1px solid var(--border);
  }
  table.data td.cell-title::before { content: none; }

  /* Cells holding prose, a change list, or a long run of inline items need the
     full card width with the label above the value. Deliberately `block` and not
     a flex column: inside a flex container every child becomes a flex item, which
     breaks up running text and stops it wrapping. As a block, the content flows
     and wraps normally. */
  table.data td.cell-details {
    display: block;
    text-align: left;
  }

  /*
   * Audit details can contain a token, URL or error body with no spaces in it. On
   * a phone that is wide enough to push the whole page sideways, so break inside
   * the word rather than let it set the page width.
   */
  table.data td { overflow-wrap: anywhere; }
  table.data td.cell-details::before { display: block; flex: none; }

  /* A row-actions cell keeps its buttons on one line, right-aligned. */
  table.data td.cell-actions { justify-content: flex-end; padding-top: 0.5rem; }
  table.data td.cell-actions::before { content: none; }

  /* The "nothing matched" row is a single full-width message, not a card. */
  table.data td[colspan] {
    text-align: left;
    border: none;
  }
  table.data td[colspan]::before { content: none; }
  table.data tbody tr.row-empty {
    border: none;
    padding: 0;
    background: none;
  }

  /* nowrap is a desktop concern; on a phone let long values wrap instead of
     forcing the card wider than the screen. */
  table.data td.nowrap { white-space: normal; }
}
