/*
 * PostMQ dashboard design system (TASK-225 PR-A).
 * Clean enterprise-neutral, light + dark, WCAG 2.2 AA contrast. No external fonts or CDNs (CSP: everything
 * is same-origin). Tokens are theme-aware: :root is light, the prefers-color-scheme media query supplies the
 * OS default, and an explicit [data-theme] (set by theme.js when the user toggles) overrides both.
 */

/* ----- Design tokens ----- */
:root {
    color-scheme: light;
    --bg: #ffffff;
    --surface: #f7f8fa;
    --surface-2: #eef1f5;
    --border: #d6dae1;
    --text: #1a1e26;
    --text-muted: #55606f;
    --brand: #2563eb;
    --brand-hover: #1d4ed8;
    --brand-contrast: #ffffff;
    --link: #1d4ed8;
    --danger: #c62828;
    --danger-surface: #fbeaea;
    --success: #1b7f4d;
    --success-surface: #e7f4ec;
    --warn: #8a5a00;
    --warn-surface: #fbf1df;
    --focus: #1d4ed8;

    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;
    --space-6: 2rem;
    --space-8: 3rem;

    --radius: 0.5rem;
    --radius-sm: 0.3rem;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
    --container: 72rem;
    --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
        --bg: #0e1116;
        --surface: #161b22;
        --surface-2: #1f262f;
        --border: #2c333d;
        --text: #e7eaef;
        --text-muted: #9aa4b2;
        --brand: #2563eb;
        --brand-hover: #1d4ed8;
        --brand-contrast: #ffffff;
        --link: #7aa7ff;
        --danger: #f2a5a5;
        --danger-surface: #2a1618;
        --success: #7fd6a4;
        --success-surface: #14251c;
        --warn: #e6c07a;
        --warn-surface: #2a2313;
        --focus: #7aa7ff;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.5);
    }
}

/* Explicit user choice wins over the OS default (higher specificity than :root). */
:root[data-theme="light"] {
    color-scheme: light;
    --bg: #ffffff;
    --surface: #f7f8fa;
    --surface-2: #eef1f5;
    --border: #d6dae1;
    --text: #1a1e26;
    --text-muted: #55606f;
    --brand: #2563eb;
    --brand-hover: #1d4ed8;
    --brand-contrast: #ffffff;
    --link: #1d4ed8;
    --danger: #c62828;
    --danger-surface: #fbeaea;
    --success: #1b7f4d;
    --success-surface: #e7f4ec;
    --warn: #8a5a00;
    --warn-surface: #fbf1df;
    --focus: #1d4ed8;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0e1116;
    --surface: #161b22;
    --surface-2: #1f262f;
    --border: #2c333d;
    --text: #e7eaef;
    --text-muted: #9aa4b2;
    --brand: #2563eb;
    --brand-hover: #3b82f6;
    --brand-contrast: #ffffff;
    --link: #7aa7ff;
    --danger: #f2a5a5;
    --danger-surface: #2a1618;
    --success: #7fd6a4;
    --success-surface: #14251c;
    --warn: #e6c07a;
    --warn-surface: #2a2313;
    --focus: #7aa7ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.5);
}

/* ----- Reset + base ----- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

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

h1, h2, h3, h4 {
    line-height: 1.25;
    margin: 0 0 var(--space-3);
    color: var(--text);
}

h1 { font-size: 1.75rem; letter-spacing: -0.02em; }
h2 { font-size: 1.25rem; letter-spacing: -0.01em; }
h3 { font-size: 1.05rem; }

p { margin: 0 0 var(--space-3); }

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

a:hover { text-decoration: none; }

code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    background: var(--surface-2);
    padding: 0.1em 0.35em;
    border-radius: var(--radius-sm);
}

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

/* ----- Layout utilities ----- */
.container {
    width: 100%;
    max-width: var(--container);
    margin-inline: auto;
    padding-inline: var(--space-4);
}

.stack-sm > * + * { margin-top: var(--space-2); }
.stack-md > * + * { margin-top: var(--space-4); }
.stack-lg > * + * { margin-top: var(--space-6); }

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

.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
    z-index: 100;
    background: var(--brand);
    color: var(--brand-contrast);
    padding: var(--space-2) var(--space-4);
    border-radius: 0 0 var(--radius) 0;
}

.skip-link:focus {
    left: 0;
}

/* ----- Navigation ----- */
.nav-list {
    display: flex;
    gap: var(--space-2);
    list-style: none;
    margin: 0;
    padding: 0;
}

.nav-link {
    display: inline-block;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    text-decoration: none;
    font-weight: 500;
}

.nav-link:hover {
    background: var(--surface-2);
    color: var(--text);
}

.nav-link.active,
.nav-link[aria-current="page"] {
    /* --link (not --brand) so the active label clears AA on --surface-2 in dark mode. */
    color: var(--link);
    background: var(--surface-2);
}

/* ----- Buttons ----- */
.btn,
.btn-primary,
.btn-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    font: inherit;
    font-weight: 600;
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
}

.btn-primary {
    background: var(--brand);
    color: var(--brand-contrast);
}

.btn-primary:hover { background: var(--brand-hover); }

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

.btn-secondary:hover { background: var(--surface-2); }

.btn:disabled,
.btn-primary:disabled,
.btn-secondary:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    font-size: 1.1rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
}

.btn-icon:hover { background: var(--surface-2); }

.btn-sm {
    padding: var(--space-1) var(--space-3);
    font-size: 0.9rem;
}

.inline-form {
    display: inline;
    margin: 0;
}

/* ----- Auth (login + signup) ----- */
.auth-card {
    max-width: 28rem;
    margin-inline: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-6);
    box-shadow: var(--shadow);
}

.field__hint {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.field-check {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
}

.field-check input {
    margin-top: 0.2rem;
    flex: none;
}

.field-check label {
    font-size: 0.95rem;
}

.auth-alt {
    margin-top: var(--space-5);
    padding-top: var(--space-4);
    border-top: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* The manually-entered MFA setup key / otpauth URI (no QR — CSP-clean). */
.mfa-code {
    display: block;
    margin-top: var(--space-1);
    padding: var(--space-2) var(--space-3);
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    word-break: break-all;
    font-size: 0.85rem;
}

/* A once-disclosed secret (issued/rotated credential plaintext): a read-only monospace field the user copies. */
.secret-value {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    word-break: break-all;
}

.secret-reveal {
    display: grid;
    gap: var(--space-3);
}

/* A read-only, horizontally-scrollable code block (e.g. the MCP client config JSON). */
.code-block {
    margin: 0;
    padding: var(--space-3);
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    overflow-x: auto;
    font-size: 0.85rem;
    line-height: 1.4;
}

/* A grouped checkbox set (credential scopes) — reset the browser fieldset chrome to match the design system. */
fieldset.field {
    border: 0;
    padding: 0;
    margin: 0;
    min-width: 0;
}

fieldset.field > legend {
    padding: 0;
    color: var(--text-muted);
    font-weight: 600;
}

/* ----- Cards ----- */
.hero__lead {
    font-size: 1.1rem;
    color: var(--text-muted);
    max-width: 46rem;
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
    gap: var(--space-4);
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-5);
    box-shadow: var(--shadow);
}

.card__title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: 1.05rem;
}

/* A card variant for a destructive "danger zone" (e.g. revoke). */
.card--danger {
    border-color: var(--danger);
}

/* A term/description grid for a metadata panel (dt in the muted first column, dd in the second). */
.detail-list {
    display: grid;
    gap: var(--space-2);
}

.detail-list > div {
    display: grid;
    grid-template-columns: minmax(9rem, max-content) 1fr;
    gap: var(--space-3);
    align-items: baseline;
}

.detail-list dt {
    color: var(--text-muted);
    font-weight: 600;
}

.detail-list dd {
    margin: 0;
}

/* A horizontal group of buttons (confirm / cancel). */
.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
}

.step-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.6rem;
    height: 1.6rem;
    border-radius: 50%;
    background: var(--brand);
    color: var(--brand-contrast);
    font-size: 0.85rem;
    font-weight: 700;
    flex: none;
}

/* ----- Badges + notices + alerts ----- */
.badge {
    display: inline-block;
    padding: 0.1em 0.5em;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--surface-2);
    color: var(--text-muted);
}

.badge--soon {
    background: var(--warn-surface);
    color: var(--warn);
}

.badge--ok {
    background: var(--success-surface);
    color: var(--success);
}

.notice {
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--brand);
    border-radius: var(--radius);
    padding: var(--space-4);
    color: var(--text-muted);
}

.alert {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-3) var(--space-4);
}

.alert--danger { background: var(--danger-surface); border-color: var(--danger); color: var(--text); }
.alert--success { background: var(--success-surface); border-color: var(--success); color: var(--text); }
.alert--warn { background: var(--warn-surface); border-color: var(--warn); color: var(--text); }

/* ----- Forms (used from PR-B) ----- */
.field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    margin-bottom: var(--space-4);
}

.field > label {
    font-weight: 600;
    font-size: 0.9rem;
}

.field input,
.field select,
.field textarea {
    font: inherit;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--text);
}

.field__error {
    color: var(--danger);
    font-size: 0.85rem;
}

/* ----- Tables (used from PR-C/PR-D) ----- */
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

.table th,
.table td {
    text-align: left;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--border);
}

.table th {
    color: var(--text-muted);
    font-weight: 600;
}

/* Wraps a wide table so it scrolls horizontally inside its own box instead of overflowing the page. */
.table-scroll {
    overflow-x: auto;
}

/* Visually hidden but exposed to assistive tech (accessible column-action labels, etc.). */
.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;
}

/* ----- Blazor circuit reconnect modal (Doc 04 §4) ----- */
.reconnect-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(8, 11, 16, 0.6);
}

/* blazor.web.js toggles these classes on the modal element. */
.components-reconnect-show,
.components-reconnect-failed,
.components-reconnect-rejected {
    display: flex;
}

.components-reconnect-hide {
    display: none;
}

.reconnect-dialog {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-5);
    box-shadow: var(--shadow);
    max-width: 24rem;
}

.reconnect-message { margin: 0; }
.reconnect-message--failed { display: none; }

.components-reconnect-failed .reconnect-message--active,
.components-reconnect-rejected .reconnect-message--active {
    display: none;
}

.components-reconnect-failed .reconnect-message--failed,
.components-reconnect-rejected .reconnect-message--failed {
    display: block;
}

/* ----- Blazor error UI (framework toggles display:block on #blazor-error-ui) ----- */
#blazor-error-ui {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: var(--warn-surface);
    color: var(--text);
    border-top: 1px solid var(--warn);
    padding: var(--space-3) var(--space-4);
}

#blazor-error-ui .reload { margin-inline-start: var(--space-3); }

#blazor-error-ui .dismiss {
    cursor: pointer;
    float: right;
}

/* ----- Message browser (TASK-225 PR-D) ----- */

/* A red badge for the dead-letter terminal state; pairs with .badge--ok / .badge--soon. */
.badge--danger {
    background: var(--danger-surface);
    color: var(--danger);
}

/* Filter row: the message-browser selects flow inline and wrap on narrow viewports. Each .field keeps a sane
   min-width so a long option label doesn't stretch one control across the row. */
.filters {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-4);
}

.filters .field {
    flex: 1 1 12rem;
    min-width: 12rem;
    margin: 0;
}

/* A scrollable monospace block for rendered JSON (payload + labels). Wide content scrolls inside its own box so the
   page body never scrolls horizontally. */
.code-block {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    line-height: 1.5;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: var(--space-3);
    margin: 0;
    overflow-x: auto;
    white-space: pre;
    max-height: 32rem;
}
