/* The design system's tokens and the pages' styles (docs/identity-design-system.md, ADR-0007). No framework. */

/* Tokens. light-dark() picks by color-scheme: the OS by default, or the person's choice in data-theme (Theme.cs). */
:root {
  color-scheme: light dark;
  --bg: light-dark(#f8fafc, #0b1220);
  --surface: light-dark(#fff, #162033);
  --surface-muted: light-dark(#f1f5f9, #1e2b40);
  --surface-hover: light-dark(#eaf0f7, #27364d);
  --text: light-dark(#0f172a, #f1f5f9);
  --text-secondary: light-dark(#475569, #b5c3d7);
  --text-tertiary: light-dark(#5b6a80, #94a3b8);
  /* Panels and dividers. Controls get their own border, which keeps 3:1 against every surface. */
  --border: light-dark(#d9e2ed, #334155);
  --border-control: light-dark(#7c8ba1, #718096);
  --divider: light-dark(#e6edf5, #2b3a50);
  --primary: light-dark(#1d4ed8, #60a5fa);
  --primary-hover: light-dark(#1e40af, #93c5fd);
  --primary-subtle: light-dark(#dbeafe, #1b3557);
  --on-primary: light-dark(#fff, #0b1220);
  /* Links and the focus ring; a Client's accent sets them apart from --primary (PageBranding). */
  --link: var(--primary);
  --focus: light-dark(#2563eb, #93c5fd);
  --success: light-dark(#166534, #86efac);
  --success-bg: light-dark(#dcfce7, #163c2a);
  --warning: light-dark(#92400e, #fcd34d);
  --warning-bg: light-dark(#fef3c7, #493718);
  --danger: light-dark(#b91c1c, #fca5a5);
  --danger-bg: light-dark(#fee2e2, #482328);
  --shadow-popup: 0 8px 24px rgb(15 23 42 / 12%);
  --radius-control: 8px;
  --radius-panel: 12px;
  --gap-section: 24px;
}

:root[data-theme=light] { color-scheme: light; }

:root[data-theme=dark] { color-scheme: dark; }

/* Base. */

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

html {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

h1, h2, h3 { margin: 0 0 12px; overflow-wrap: anywhere; }

h1 { font-size: 26px; line-height: 34px; font-weight: 700; letter-spacing: -0.015em; }

h2 { font-size: 18px; line-height: 26px; font-weight: 650; }

h3 { font-size: 15px; line-height: 22px; font-weight: 600; }

p, ul, ol, dl, table, pre, figure, fieldset { margin: 0 0 12px; }

:is(p, ul, ol, dl, table, pre, figure, fieldset, form):last-child { margin-bottom: 0; }

ul, ol { padding-inline-start: 20px; }

a { color: var(--link); text-underline-offset: 2px; }

a:hover { color: var(--primary-hover); }

small { font-size: 12px; line-height: 17px; }

code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }

/* Emails, keys and codes are long and unbroken; let them wrap rather than widen the page. */
code { word-break: break-all; }

pre {
  padding: 12px;
  overflow-x: auto;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
}

pre code { word-break: normal; }

hr { margin: 8px 0; border: 0; border-top: 1px solid var(--divider); }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.muted { color: var(--text-secondary); }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Page heading with its one-line description. */
hgroup { margin-bottom: var(--gap-section); }

hgroup > * { margin: 0; }

hgroup > p { margin-top: 4px; color: var(--text-secondary); }

/* Panels: one bordered group per section. */
article {
  margin: 0 0 var(--gap-section);
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-panel);
}

article > h2:first-child { font-size: 15px; line-height: 22px; font-weight: 600; }

.danger-zone { border-color: var(--danger); }

.danger-zone h2 { color: var(--danger); }

/* Forms. Labels wrap their control: the text, then the input. */

label {
  display: grid;
  gap: 6px;
  margin-bottom: 16px;
  font-size: 13px;
  line-height: 19px;
  font-weight: 500;
}

input:not([type=checkbox], [type=radio], [type=hidden]), select, textarea {
  display: block;
  width: 100%;
  max-width: 600px;
  min-height: 40px;
  padding: 8px 12px;
  font: inherit;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-control);
  border-radius: var(--radius-control);
}

input:not([type=checkbox], [type=radio], [type=hidden]):focus-visible, select:focus-visible, textarea:focus-visible {
  border-color: var(--focus);
  outline-offset: 0;
}

:is(input, select, textarea):is([aria-invalid=true], .invalid) { border-color: var(--danger); }

input[type=checkbox], input[type=radio] {
  flex: none;
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--primary);
}

/* A checkbox and its text on one line. */
label.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 400;
}

fieldset { min-width: 0; padding: 0; border: 0; }

legend { padding: 0; margin-bottom: 6px; font-size: 13px; font-weight: 500; }

.error, .validation-message { color: var(--danger); }

.validation-errors { padding-inline-start: 20px; color: var(--danger); }

/* Buttons: primary by default, .outline for secondary, .danger for destructive. */

button, [role=button] {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 8px 16px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  line-height: 20px;
  text-align: center;
  text-decoration: none;
  color: var(--on-primary);
  background: var(--primary);
  border: 1px solid transparent;
  border-radius: var(--radius-control);
  cursor: pointer;
  transition: background-color 150ms ease-out;
}

:is(button, [role=button]):hover { color: var(--on-primary); background: var(--primary-hover); }

:is(button, [role=button]).outline {
  color: var(--text);
  background: var(--surface);
  border-color: var(--border-control);
}

:is(button, [role=button]).outline:hover { color: var(--text); background: var(--surface-hover); }

:is(button, [role=button]).danger { color: var(--on-primary); background: var(--danger); }

:is(button, [role=button]).danger:hover { background: color-mix(in srgb, var(--danger) 85%, var(--text)); }

button:disabled { cursor: not-allowed; opacity: 0.6; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }

.actions form { margin: 0; }

/* Messages after an action, and errors. Text says what happened; color only supports it. */

p[role=status], p.error[role=alert], article > .error:first-child {
  padding: 8px 12px;
  border: 1px solid;
  border-radius: var(--radius-control);
}

p[role=status] { color: var(--text); background: var(--primary-subtle); border-color: color-mix(in srgb, var(--primary) 35%, transparent); }

p.error[role=alert] { background: var(--danger-bg); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }

.badge {
  display: inline-block;
  margin-inline-start: 8px;
  padding: 0 8px;
  font-size: 12px;
  font-weight: 500;
  line-height: 20px;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 999px;
  vertical-align: middle;
}

.badge.success { color: var(--success); background: var(--success-bg); border-color: transparent; }

/* Read-only details: label and value side by side. */
dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 24px; }

dt { color: var(--text-secondary); }

dd { margin: 0; overflow-wrap: anywhere; }

dd > :is(ul, p) { margin: 0; }

/* A setting's one-line hint, under its value (admin Setting). */
dd > .hint { display: block; margin-top: 2px; }

/* Admin settings: a little air between one setting and the next. */
dl:has(> dd > .hint) { row-gap: 12px; }

/* Rows of "thing, then its action", such as signed-in applications. */
ul.items { padding: 0; }

ul.items li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  min-height: 60px;
  padding: 12px 0;
  list-style: none;
  border-bottom: 1px solid var(--divider);
}

ul.items li:last-child { border-bottom: 0; padding-bottom: 0; }

ul.items li:first-child { padding-top: 0; }

:is(ul.items, td) form { margin: 0; }

:is(ul.items, td) :is(button, [role=button]) { min-height: 36px; padding: 6px 12px; font-size: 13px; white-space: nowrap; }

/* Tables. */

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }

/* 11px + a 21px line + the 1px divider: 44px rows. */
th, td { padding: 11px 12px; text-align: start; vertical-align: middle; border-bottom: 1px solid var(--divider); }

:is(th, td):first-child { padding-inline-start: 0; }

:is(th, td):last-child { padding-inline-end: 0; }

tbody tr:last-child td { border-bottom: 0; }

th { font-size: 13px; font-weight: 600; color: var(--text-secondary); }

td { overflow-wrap: break-word; }

/* The email column takes the long, unbroken words; the others keep theirs whole. */
td:first-child { min-width: 10rem; overflow-wrap: anywhere; }

/* Disclosures. */

summary { cursor: pointer; }

details > summary { color: var(--link); }

details[open] > summary { margin-bottom: 8px; }

/* The Authenticator's QR code: dark on light whatever the theme, with a quiet zone. */
.qr svg { display: block; width: 200px; max-width: 100%; height: auto; padding: 8px; background: #fff; border-radius: var(--radius-control); }

p.warning { padding: 8px 12px; color: var(--text); background: var(--warning-bg); border: 1px solid color-mix(in srgb, var(--warning) 35%, transparent); border-radius: var(--radius-control); }

/* Settings rows in one bordered group: icon, what it is and its state, then its action. */
.settings {
  margin-bottom: var(--gap-section);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-panel);
}

.setting {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  min-height: 60px;
  padding: 12px 16px;
}

.setting + .setting { border-top: 1px solid var(--divider); }

.setting > .icon { margin-top: 2px; color: var(--text-secondary); }

.setting-body { flex: 1; min-width: 0; }

.setting-body h2 { margin: 0 0 2px; font-size: 14px; line-height: 21px; font-weight: 600; }

.setting-body > :not(h2) { margin-bottom: 4px; }

.setting-body > :last-child { margin-bottom: 0; }

.setting > .account-link { flex: none; min-height: 21px; }

ul.plain { padding: 0; list-style: none; }

/* Overview: profile and security side by side on wide screens. */
@media (min-width: 1024px) {
  .overview { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--gap-section); }
}

/* Links to an edit page; a padlock when opening it asks the User to prove it's them. */
.account-link { display: inline-flex; align-items: center; gap: 6px; min-height: 40px; }

.padlock, .icon { flex: none; }

/* Admin: a form beside its lists on wide screens, above them on narrow ones. */
@media (min-width: 960px) {
  .split { display: grid; grid-template-columns: minmax(18rem, 22rem) minmax(0, 1fr); gap: var(--gap-section); align-items: start; }
}

/* One Client per row: its checkboxes, then Save. */
.client-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 12px 0;
  margin: 0;
  border-bottom: 1px solid var(--divider);
}

.client-row:last-child { border-bottom: 0; }

.client-row fieldset { display: flex; flex: 1 1 0; flex-wrap: wrap; gap: 4px 20px; min-width: 0; margin: 0; }

.client-row :is(label, button) { margin: 0; }

/* A Client's checkbox, then its roles indented under it. */
.client-access { margin-bottom: 8px; }

.client-access > label { margin-bottom: 8px; }

.client-access > label:not(:first-child) { margin-inline-start: 26px; }

.client-row .client-access > label:not(:first-child) { margin-inline-start: 0; }

.client-row .client-access > label:first-child { font-weight: 600; }

/* Auth shell: one centred card (AuthLayout). */

body > main.auth {
  min-height: 100dvh;
  display: grid;
  justify-items: center;
  align-content: start;
  padding: 10vh 16px 24px;
  background: var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  margin: 0;
  padding: 24px;
}

.auth-card h1 { font-size: 24px; line-height: 32px; margin-bottom: 8px; }

/* Forms in a card are often one button each, such as Allow and Deny: they stack 8px apart like the buttons in one form. */
.auth-card form { margin-bottom: 8px; }

.auth-card form ~ :not(form, script) { margin-top: 16px; }

.auth-card :is(button, [role=button]) { width: 100%; }

.auth-card :is(button, [role=button]) ~ :is(button, [role=button]) { margin-top: 8px; }

/* Consent and device approval list what a Client asks for: a little wider. */
.auth-card:has(.technical-details) { max-width: 480px; }

.technical-details { margin-bottom: 12px; color: var(--text-secondary); }

.technical-details code { margin-inline-end: 6px; }

/* Codes are short: a narrow field says so. */
input[autocomplete=one-time-code], input#user_code { max-width: 220px; letter-spacing: 0.08em; }

/* The card's top: a logo or the wordmark (BrandHeader). */
.brand { margin-bottom: 20px; }

.brand img { display: block; width: auto; height: auto; max-width: 200px; max-height: 48px; }

/* A logo without a dark variant sits on a light tile, so a dark logo stays visible in dark mode; on a light card the tile doesn't show. */
.brand img.tile { padding: 6px 8px; background: #fff; border-radius: 6px; }

.wordmark {
  display: inline-block;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
  text-decoration: none;
}

/* App shell: top bar, sidebar and content (AppLayout). */

.shell {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 216px minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top" "side main";
}

.topbar {
  grid-area: top;
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar .wordmark { margin-inline-end: auto; }

.sidebar {
  grid-area: side;
  padding: 8px;
  background: var(--surface-muted);
  border-inline-end: 1px solid var(--border);
}

.sidebar nav { position: sticky; top: 64px; }

.app-main { grid-area: main; width: 100%; max-width: 1008px; padding: 24px; }

ul.nav { padding: 0; margin: 0; list-style: none; }

ul.nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 8px 12px;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius-control);
}

ul.nav a:hover { color: var(--text); background: var(--surface-hover); }

ul.nav a[aria-current=page] { color: var(--primary); background: var(--primary-subtle); }

.nav-heading { padding: 16px 12px 4px; font-size: 12px; font-weight: 600; color: var(--text-tertiary); }

/* The menus in the top bar: details elements, so they open without script. */

:is(.nav-menu, .user-menu) > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 4px 8px;
  font-weight: 500;
  list-style: none;
  border-radius: var(--radius-control);
}

:is(.nav-menu, .user-menu) > summary::-webkit-details-marker { display: none; }

:is(.nav-menu, .user-menu) > summary:hover { background: var(--surface-hover); }

.nav-menu { display: none; }

.nav-menu > nav {
  position: absolute;
  inset-inline: 0;
  top: 100%;
  padding: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-popup);
}

.user-menu { position: relative; }

.user-name { max-width: 16rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.avatar {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  font-size: 13px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-subtle);
  border-radius: 50%;
}

.menu {
  position: absolute;
  inset-inline-end: 0;
  top: calc(100% + 4px);
  z-index: 20;
  width: 260px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  box-shadow: var(--shadow-popup);
}

.menu > a {
  display: flex;
  align-items: center;
  min-height: 40px;
  padding: 8px 12px;
  color: var(--text);
  text-decoration: none;
  border-radius: 6px;
}

.menu > a:hover { background: var(--surface-hover); }

.menu-label { margin: 4px 12px 6px; font-size: 12px; font-weight: 600; color: var(--text-tertiary); }

/* System, Light, Dark: three buttons, the chosen one pressed. */
.theme-choice { display: flex; gap: 4px; padding: 0 4px 4px; }

.theme-choice button {
  flex: 1;
  min-height: 36px;
  padding: 6px 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border-color: var(--border-control);
}

.theme-choice button:hover { color: var(--text); background: var(--surface-hover); }

.theme-choice button[aria-pressed=true] { color: var(--primary); background: var(--primary-subtle); border-color: var(--primary); }

/* Tablet and narrow: the sidebar's links move into the top bar's menu. */
@media (max-width: 1023px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "main"; }
  .sidebar { display: none; }
  .nav-menu { display: block; }
  .app-main { padding: 20px; }
}

/* Mobile. */
@media (max-width: 639px) {
  :root { --gap-section: 20px; }
  h1 { font-size: 24px; line-height: 32px; }
  /* Below 16px, iOS Safari zooms in when a field takes focus. */
  input:not([type=checkbox], [type=radio], [type=hidden]), select, textarea { font-size: 16px; }
  .topbar { height: 52px; padding: 0 16px; }
  .user-name { display: none; }
  /* The action moves under the text rather than squeezing it. */
  .setting { flex-wrap: wrap; padding-inline: 12px; }
  .setting > .account-link { flex-basis: 100%; padding-inline-start: 30px; }
  .app-main { padding: 16px; }
  body > main.auth { padding: 16px; }
  .auth-card { padding: 20px 16px; }

  /* Tables become one card per row, each cell labelled. */
  table, thead, tbody, tr, th, td { display: block; }
  thead { display: none; }
  tr { padding: 8px 0; border-bottom: 1px solid var(--divider); }
  tbody tr:last-child { border-bottom: 0; }
  td { padding: 2px 0; border: 0; overflow-wrap: anywhere; }
  td[data-label]::before { content: attr(data-label) ": "; color: var(--text-secondary); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
