/* Shared base for every page: reset, controls, header, footer, account widgets
   and the layouts of the secondary pages. Colour tokens live in theme.css. */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { display: flex; flex-direction: column; min-height: 100dvh; margin: 0; font-size: 15px; line-height: 1.6; }
img { max-width: 100%; }
h1, h2, h3, h4, p { margin: 0; }
a { color: var(--ui-accent); text-underline-offset: 3px; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* Buttons */
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 16px; border: 1px solid var(--ui-border); border-radius: 12px;
  color: var(--ui-text); background: var(--ui-surface); font: inherit; font-size: 14px; font-weight: 600; white-space: nowrap; text-decoration: none;
  cursor: pointer; transition: background-color .16s, border-color .16s, color .16s, transform .12s, box-shadow .16s;
}
.button:hover:not(:disabled) { border-color: color-mix(in srgb, var(--ui-accent) 45%, var(--ui-border)); background: var(--ui-soft); }
.button:active:not(:disabled) { transform: scale(.98); }
.button:disabled { cursor: not-allowed; opacity: .48; }
.button.primary { border-color: transparent; color: var(--ui-action-ink); background: var(--ui-action); }
.button.primary:hover:not(:disabled) { background: color-mix(in srgb, var(--ui-action) 88%, #000); }
.button.ghost { border-color: transparent; color: var(--ui-muted); background: transparent; }
.button.ghost:hover:not(:disabled) { color: var(--ui-text); background: var(--ui-soft); }
.button.small { min-height: 32px; padding: 0 12px; border-radius: 10px; font-size: 13px; }
.button.block { width: 100%; min-height: 46px; }
.button.danger, .text-button.danger { color: var(--ui-danger); }
.text-button { padding: 4px 0; border: 0; color: var(--ui-accent); background: none; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; }
.text-button:hover:not(:disabled) { text-decoration: underline; text-underline-offset: 3px; }
.text-button:disabled { opacity: .45; cursor: not-allowed; }
.icon-button {
  display: inline-grid; place-items: center; flex: none; width: 32px; height: 32px; padding: 0; border: 0; border-radius: 50%;
  color: var(--ui-muted); background: transparent; font-size: 20px; line-height: 1; cursor: pointer;
}
.icon-button:hover { color: var(--ui-text); background: var(--ui-soft); }
:is(.button, .text-button, .icon-button):focus-visible { outline: none; box-shadow: var(--ui-ring); }

/* Form fields */
.field { display: grid; gap: 6px; }
.field > span, .field > label, .field-label { color: var(--ui-text); font-size: 13px; font-weight: 600; }
.field-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.input, .field :is(input, select, textarea) {
  width: 100%; min-width: 0; min-height: 44px; padding: 10px 13px; border: 1px solid var(--ui-border); border-radius: 12px;
  color: var(--ui-text); background: var(--ui-surface); font: inherit; font-size: 15px; transition: border-color .16s, box-shadow .16s;
}
.field textarea { min-height: 140px; resize: vertical; line-height: 1.7; }
.field :is(input, select, textarea):focus, .input:focus { border-color: color-mix(in srgb, var(--ui-action) 70%, var(--ui-border)); outline: none; box-shadow: var(--ui-ring); }
.field :is(input, textarea)::placeholder { color: color-mix(in srgb, var(--ui-muted) 80%, transparent); font-size: 14px; }
.field input[readonly] { color: var(--ui-muted); background: var(--ui-soft); }
.inline-field { display: flex; gap: 8px; }
.inline-field > :first-child { flex: 1; }
.hint { color: var(--ui-muted); font-size: 12px; line-height: 1.6; }
.form-message { min-height: 20px; color: var(--ui-muted); font-size: 13px; overflow-wrap: anywhere; }
.form-message:empty { min-height: 0; }
.form-message.error { color: var(--ui-danger); }
.form-message.ok { color: var(--ui-success); }

/* Header */
.site-nav {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px clamp(16px, 4vw, 40px);
  background: color-mix(in srgb, var(--ui-page) 86%, transparent); backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid color-mix(in srgb, var(--ui-border) 60%, transparent);
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ui-text); font-size: 17px; font-weight: 700; letter-spacing: -.02em; text-decoration: none; }
.brand .brand-mark { width: 28px; height: 28px; background: url('/favicon.svg') center / contain no-repeat; }
.nav-actions { display: flex; align-items: center; gap: 6px; min-width: 0; }
.nav-link { padding: 8px 12px; border: 0; border-radius: 10px; color: var(--ui-muted); background: none; font: inherit; font-size: 14px; font-weight: 500; text-decoration: none; white-space: nowrap; cursor: pointer; }
.nav-link:hover:not(:disabled), .nav-link[aria-current="page"] { color: var(--ui-text); background: var(--ui-soft); }
.nav-actions .theme-toggle { min-width: 0; min-height: 36px; border-color: transparent; background: transparent; color: var(--ui-muted); }
.nav-actions .theme-toggle:hover { color: var(--ui-text); background: var(--ui-soft); }

/* Account controls in the header (filled by account.js) */
[data-account-controls] { display: inline-flex; align-items: center; gap: 10px; min-width: 0; margin-left: 6px; }
.account-login-link { padding: 8px 16px; border-radius: 10px; color: var(--ui-action-ink); background: var(--ui-action); font-size: 14px; font-weight: 600; text-decoration: none; white-space: nowrap; }
.account-login-link:hover { background: color-mix(in srgb, var(--ui-action) 88%, #000); }
.account-name { display: inline-flex; align-items: center; gap: 8px; max-width: 200px; color: var(--ui-text); font-size: 14px; font-weight: 500; text-decoration: none; }
.account-name span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-initial { display: inline-grid; place-items: center; flex: none; width: 30px; height: 30px; border-radius: 50%; color: var(--ui-accent); background: var(--ui-accent-soft); font-size: 13px; font-weight: 700; }
.account-signout, .account-admin-link { padding: 6px 2px; border: 0; color: var(--ui-muted); background: transparent; font: inherit; font-size: 13px; text-decoration: none; white-space: nowrap; cursor: pointer; }
.account-signout:hover:not(:disabled), .account-admin-link:hover { color: var(--ui-accent); }
.account-invite-label { color: var(--ui-muted); font-size: 12px; white-space: nowrap; }
.account-nav-error { color: var(--ui-muted); font-size: 12px; }

/* Service access dialog (account.js) */
.access-dialog {
  width: min(420px, calc(100% - 32px)); padding: 28px; border: 1px solid var(--ui-border); border-radius: 20px;
  color: var(--ui-text); background: var(--ui-surface); box-shadow: var(--ui-shadow-lg);
}
.access-dialog[open] { animation: pop .2s var(--ui-ease); }
.access-dialog::backdrop { background: rgb(8 16 26 / 45%); backdrop-filter: blur(3px); }
.access-dialog h2 { font-size: 20px; font-weight: 650; letter-spacing: -.02em; }
.access-dialog > p { margin: 8px 0 18px; color: var(--ui-muted); font-size: 14px; }
.access-dialog form { display: grid; gap: 10px; }
.access-dialog .dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* Toasts */
.toast-region { position: fixed; bottom: max(24px, env(safe-area-inset-bottom)); left: 50%; z-index: 100; display: grid; justify-items: center; gap: 8px; transform: translateX(-50%); pointer-events: none; }
.toast { max-width: min(420px, calc(100vw - 32px)); padding: 10px 16px; border-radius: 12px; color: var(--ui-surface); background: var(--ui-text); box-shadow: var(--ui-shadow-lg); font-size: 14px; animation: rise .22s var(--ui-ease); transition: opacity .3s, transform .3s; }
.toast.is-leaving { opacity: 0; transform: translateY(6px); }

/* Footer */
.site-footer { display: grid; grid-template-columns: minmax(220px, 1.2fr) minmax(0, 2fr) minmax(220px, 1.2fr); gap: 20px 32px; margin-top: auto; padding: 28px clamp(16px, 4vw, 40px); font-size: 13px; }
.site-footer p { margin-top: 4px; }
.site-footer nav { display: flex; flex-wrap: wrap; align-content: start; gap: 6px 16px; }
.site-footer a { text-decoration: none; }
.site-footer a[aria-current="page"] { color: var(--ui-accent); }
.site-footer-meta { display: grid; align-content: start; gap: 4px; }
.site-registration-links { display: grid; gap: 4px; }
.site-registration-links a { display: inline-flex; align-items: center; gap: 6px; }
.site-registration-links img { width: 16px; height: 16px; }

/* Secondary page layout */
.page-main { flex: 1; width: min(1080px, calc(100% - 32px)); margin: 0 auto; padding: clamp(28px, 6vh, 56px) 0 64px; animation: rise .32s var(--ui-ease); }
.page-head { display: grid; gap: 8px; margin-bottom: 24px; }
.page-head h1 { font-size: clamp(26px, 3.4vw, 34px); font-weight: 700; letter-spacing: -.03em; line-height: 1.25; }
.page-head p { max-width: 640px; color: var(--ui-muted); }
.card { min-width: 0; padding: 24px; border: 1px solid var(--ui-border); border-radius: var(--ui-radius); background: var(--ui-surface); }
.card h2 { font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
.card h2 + p { margin-top: 4px; color: var(--ui-muted); font-size: 14px; }

/* Sign-in and invite pages */
.auth-main { display: grid; flex: 1; place-items: center; width: 100%; padding: clamp(24px, 6vh, 64px) 16px; }
.auth-card { display: grid; gap: 18px; width: min(420px, 100%); padding: 32px; border: 1px solid var(--ui-border); border-radius: 22px; background: var(--ui-surface); box-shadow: var(--ui-shadow-lg); animation: rise .32s var(--ui-ease); }
.auth-card h1 { font-size: 26px; font-weight: 700; letter-spacing: -.03em; }
.auth-card h1 + p { margin-top: -10px; color: var(--ui-muted); font-size: 14px; }
.auth-card form { display: grid; gap: 14px; }
.auth-switch { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: var(--ui-soft); }
.auth-switch button { flex: 1; min-height: 34px; border: 0; border-radius: 9px; color: var(--ui-muted); background: none; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; }
.auth-switch button[aria-pressed="true"] { color: var(--ui-text); background: var(--ui-surface); box-shadow: 0 1px 2px rgb(16 32 52 / 10%); }
.auth-links { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; padding-top: 16px; border-top: 1px solid var(--ui-border); font-size: 13px; }
.auth-links a { text-decoration: none; }
.auth-terms { color: var(--ui-muted); font-size: 12px; text-align: center; }
.auth-terms a { color: var(--ui-text); }

/* Documentation and legal pages */
.doc-layout { display: grid; grid-template-columns: 200px minmax(0, 1fr); gap: 40px; align-items: start; }
.doc-nav { position: sticky; top: 84px; display: grid; gap: 2px; }
.doc-nav a { padding: 8px 12px; border-radius: 10px; color: var(--ui-muted); font-size: 14px; text-decoration: none; }
.doc-nav a:hover { color: var(--ui-text); background: var(--ui-soft); }
.doc-nav a[aria-current="page"] { color: var(--ui-text); background: var(--ui-surface); box-shadow: inset 0 0 0 1px var(--ui-border); font-weight: 600; }
.doc { display: grid; gap: 28px; min-width: 0; padding: clamp(24px, 4vw, 40px); border: 1px solid var(--ui-border); border-radius: var(--ui-radius); background: var(--ui-surface); }
.doc section { display: grid; gap: 10px; }
.doc section + section { padding-top: 28px; border-top: 1px solid var(--ui-border); }
.doc h2 { font-size: 18px; font-weight: 650; letter-spacing: -.01em; }
.doc h3 { margin-top: 6px; font-size: 15px; font-weight: 650; }
.doc :is(p, li) { color: color-mix(in srgb, var(--ui-text) 82%, var(--ui-muted)); line-height: 1.85; }
.doc :is(ul, ol) { display: grid; gap: 6px; margin: 0; padding-left: 1.3em; }
.doc code { padding: 1px 6px; border-radius: 6px; background: var(--ui-soft); font-size: .92em; overflow-wrap: anywhere; }
.doc pre { margin: 0; padding: 16px; overflow: auto; border-radius: 12px; background: var(--ui-soft); font-size: 13px; line-height: 1.6; }
.doc pre code { padding: 0; background: none; }
.doc table { width: 100%; border-collapse: collapse; font-size: 14px; }
.doc :is(th, td) { padding: 10px 12px; border-bottom: 1px solid var(--ui-border); text-align: left; vertical-align: top; }
.doc th { color: var(--ui-muted); font-size: 12px; font-weight: 600; }
.doc-meta { color: var(--ui-muted); font-size: 13px; }
.doc .form-grid { display: grid; gap: 14px; max-width: 560px; }

/* Account page */
.profile-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.profile-grid .card { display: grid; gap: 16px; }
.profile-grid .card.wide { grid-column: 1 / -1; }
.profile-identity { display: flex; align-items: center; gap: 16px; }
.profile-identity .account-initial { width: 52px; height: 52px; font-size: 20px; }
.profile-identity strong { display: block; font-size: 18px; }
.profile-identity span { color: var(--ui-muted); font-size: 14px; overflow-wrap: anywhere; }
.status-pill { display: inline-flex; align-items: center; gap: 6px; width: fit-content; padding: 4px 10px; border-radius: 999px; background: var(--ui-soft); font-size: 13px; font-weight: 600; }
.status-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--ui-muted); }
.status-pill[data-state="ok"]::before { background: var(--ui-success); }
.status-pill[data-state="warn"]::before { background: var(--ui-warn); }
.status-pill[data-state="error"]::before { background: var(--ui-danger); }
.link-list { display: grid; gap: 4px; margin: 0; padding: 0; list-style: none; }
.link-list a { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--ui-border); color: var(--ui-text); font-size: 14px; text-decoration: none; }
.link-list li:last-child a { border-bottom: 0; }
.link-list a:hover { color: var(--ui-accent); }
.key-list { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.key-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; border: 1px solid var(--ui-border); border-radius: 12px; }
.key-list li[data-status="revoked"], .key-list li[data-status="expired"] { opacity: .6; }
.key-list code { font-size: 13px; }
.key-list small { display: block; color: var(--ui-muted); font-size: 12px; }
.new-key { display: grid; gap: 8px; padding: 14px; border: 1px solid color-mix(in srgb, var(--ui-success) 40%, var(--ui-border)); border-radius: 12px; background: color-mix(in srgb, var(--ui-success) 8%, var(--ui-surface)); }
.new-key code { padding: 8px 10px; border-radius: 8px; background: var(--ui-surface); font-size: 13px; overflow-wrap: anywhere; user-select: all; }
.key-form { display: grid; grid-template-columns: minmax(0, 1fr) 140px auto; gap: 8px; align-items: end; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } }

@media (max-width: 860px) {
  .site-footer { grid-template-columns: 1fr; }
  .doc-layout, .profile-grid { grid-template-columns: 1fr; }
  .doc-nav { position: static; display: flex; flex-wrap: wrap; }
  .key-form { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  body { font-size: 14px; }
  .site-nav { padding: 10px 14px; }
  .nav-actions { gap: 2px; }
  .nav-actions .theme-toggle [data-theme-label], .nav-actions :is(.account-invite-label, .account-admin-link), .account-name span:last-child { display: none; }
  .nav-link { padding: 8px; }
  .nav-link[href="/monitor"] { display: none; }
  .brand { gap: 0; font-size: 0; }
  .auth-card { padding: 24px 20px; }
  .card { padding: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
.stack { display: grid; gap: 14px; }
