/*
 * Haven Finance suite design system — shared token layer over Haven's design system.
 * Tokens: https://github.com/HavenEngineering/module-shared-haven-ui/blob/main/src/tokens/colors.json
 *         https://github.com/HavenEngineering/module-shared-haven-ui/blob/main/src/style/_variables.scss
 * Light/dark semantic tokens: https://github.com/HavenEngineering/design-system
 *         tokens/semantic/color.light.json + color.dark.json
 */
:root {
    /* Haven brand palette */
    --haven-100: #ebf8ff;
    --haven-200: #e1f4ff;
    --haven-300: #c0e8ff;
    --haven-400: #007cc2;
    --haven-500: #0076b8;
    --haven-600: #006299;
    --haven-700: #005180;

    /* Midnight palette */
    --midnight-100: #b0bbce;
    --midnight-200: #8395b2;
    --midnight-300: #566e96;
    --midnight-400: #1d3e73;
    --midnight-500: #193562;
    --midnight-600: #142b51;
    --midnight-700: #10223f;

    /* Pebble (neutrals) */
    --pebble-100: #faf9f7;
    --pebble-200: #f2f1f0;
    --pebble-300: #d9d8d7;
    --pebble-400: #bfbfbd;
    --pebble-500: #8c8c8b;
    --pebble-600: #666665;
    --pebble-700: #4c4c4c;
    --pebble-800: #333332;
    --pebble-900: #262626;

    /* Sand */
    --sand-100: #faf5ed;
    --sand-200: #f5ebdc;

    /* Signal ramps (status tints) */
    --signal-red-100: #fff2f2;
    --signal-red-200: #ffe5e5;
    --signal-red-300: #ffbfbf;
    --signal-red-400: #d13130;
    --signal-red-500: #b22928;
    --signal-green-100: #f2fff9;
    --signal-green-200: #cef2e0;
    --signal-green-300: #a7f0cc;
    --signal-green-400: #248254;
    --signal-green-500: #20734a;
    --signal-orange-200: #fde0c2;
    --signal-orange-400: #f88a30;

    /* Semantic aliases — LIGHT (default). Text = pebble-900/700 per spec §2
       (design-system light semantic tokens), NOT the siblings' legacy
       --dark-blue-100 body text — this is net-new visual work aligned to the
       design system, not the older sibling app.css convention. */
    --color-text: var(--pebble-900);
    --color-text-secondary: var(--pebble-700);
    --color-text-muted: var(--pebble-500);
    --color-primary: var(--haven-500);
    --color-primary-hover: var(--haven-600);
    /* Foreground for text/icons sitting on a primary (haven teal) or dark header
       fill. White reads on the teal and midnight fills in BOTH themes, so unlike
       --color-surface it must NOT track the theme — hence a dedicated token. */
    --color-on-primary: #ffffff;
    --color-surface: #ffffff;
    --color-bg: var(--pebble-100);
    --color-border: var(--pebble-300);
    --color-border-light: var(--pebble-200);
    --color-error: var(--signal-red-400);
    --color-surface-muted: var(--pebble-200);

    /* Extra semantic aliases used by the sibling apps */
    --color-primary-faint: rgba(0, 118, 184, 0.06);
    --color-error-bg: var(--signal-red-100);
    --color-error-border: var(--signal-red-300);
    --color-success: var(--signal-green-400);
    --color-success-bg: var(--signal-green-100);
    --color-success-border: var(--signal-green-300);

    /* Hero gradient stops — LIGHT. */
    --hero-from: var(--midnight-600);
    --hero-mid: var(--midnight-400);
    --hero-to: var(--haven-600);
    --hero-text: #ffffff;
    --hero-text-muted: rgba(255, 255, 255, 0.72);

    /* Spacing (8px base grid) */
    --grid: 8px;

    /* Typography */
    --font: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell,
        "Helvetica Neue", sans-serif;
    --weight-normal: 425;
    --weight-bold: 650;
    --weight-black: 775;
    --font-xs: 0.75rem;

    /* Radii */
    --radius: 4px;
    --radius-input: 16px;
    --radius-badge: 4px;

    /* Elevation */
    --shadow-sm: 0 1px 2px rgba(3, 21, 69, 0.05);
    --shadow: 0 1px 3px rgba(3, 21, 69, 0.08), 0 1px 2px rgba(3, 21, 69, 0.04);
    --shadow-md: 0 4px 12px rgba(3, 21, 69, 0.08), 0 1px 3px rgba(3, 21, 69, 0.04);

    /* Motion */
    --transition: 0.2s ease;

    /* Per-environment top stripe defaults — data-environment baked onto <body> by apply-env-assets. */
    --env-stripe-color: transparent;
    --env-stripe-height: 0px;
}

/* Semantic aliases — DARK. Background pebble-800/900, text pebble-100/300,
   brand shifts to haven-400; the hero deepens rather than going near-black. */
[data-theme="dark"] {
    --color-text: var(--pebble-100);
    --color-text-secondary: var(--pebble-300);
    --color-text-muted: var(--pebble-400);
    --color-primary: var(--haven-400);
    --color-primary-hover: var(--haven-300);
    --color-surface: var(--pebble-800);
    --color-bg: var(--pebble-900);
    --color-border: var(--pebble-700);
    --color-border-light: var(--pebble-700);
    --color-surface-muted: var(--pebble-700);

    --hero-from: var(--midnight-700);
    --hero-mid: var(--midnight-500);
    --hero-to: var(--midnight-400);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.55), 0 1px 3px rgba(0, 0, 0, 0.4);

    /* Status tints — darkened so pale rows read on dark surfaces (spec 3.3). */
    --signal-red-100: #3a1f1f;
    --signal-red-200: #4a2626;
    --signal-red-300: #6b3232;
    --signal-green-100: #16301f;
    --signal-green-200: #1c4029;
    --signal-green-300: #245234;
    --signal-orange-200: #4a3316;
    --haven-100: #12303f;
    --haven-200: #163a4a;

    /* Extra semantic aliases re-derived for dark. */
    --color-primary-faint: rgba(0, 124, 194, 0.14);
    --color-error-bg: var(--signal-red-100);
    --color-error-border: var(--signal-red-300);
    --color-success-bg: var(--signal-green-100);
    --color-success-border: var(--signal-green-300);
}

body[data-environment="dev"] {
    --env-stripe-color: var(--signal-red-400);
    --env-stripe-height: 5px;
}

body[data-environment="staging"] {
    --env-stripe-color: var(--signal-orange-400);
    --env-stripe-height: 5px;
}

body::before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--env-stripe-height);
    background: var(--env-stripe-color);
    z-index: 1000;
    pointer-events: none;
}

/* Focus-visible for keyboard accessibility — orange ring per the design system. */
:focus-visible {
    outline: 2px solid var(--signal-orange-400);
    outline-offset: 2px;
}

/* --- Shared suite header --- */
.suite-header {
    background: var(--midnight-400);
    padding: 0 calc(var(--grid) * 4);
    height: 56px;
    display: flex;
    align-items: center;
    gap: calc(var(--grid) * 1.5);
    border-bottom: 3px solid var(--haven-500);
}

.suite-header .suite-ribbon {
    height: 28px;
    width: auto;
}

.suite-header .suite-mark {
    height: 36px;
    width: auto;
}

.suite-header .suite-divider {
    width: 1px;
    height: 24px;
    background: rgba(255, 255, 255, 0.2);
}

.suite-header .suite-title {
    margin: 0;
    font-size: 1rem;
    font-weight: var(--weight-black);
    color: #ffffff;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.suite-header .suite-spacer {
    flex: 1 1 auto;
}

.suite-header .suite-env {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: rgba(255, 255, 255, 0.6);
}

.suite-header .suite-back {
    color: rgba(255, 255, 255, 0.85);
    text-decoration: none;
    font-size: 0.8125rem;
    font-weight: var(--weight-bold);
}

.suite-header .suite-back:hover {
    color: #ffffff;
}

.suite-header a:focus-visible {
    outline: 2px solid var(--signal-orange-400);
    outline-offset: 3px;
}

/* Visually-hidden skip link that becomes visible on keyboard focus (WCAG 2.4.1). */
.skip-link {
    position: absolute;
    left: calc(var(--grid) * 1);
    top: calc(var(--grid) * -8);
    z-index: 1100;
    padding: calc(var(--grid) * 1) calc(var(--grid) * 2);
    background: var(--color-surface);
    color: var(--color-primary);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    text-decoration: none;
    font-weight: var(--weight-bold);
    transition: top var(--transition);
}

.skip-link:focus-visible {
    top: calc(var(--grid) * 1);
}

/* Theme toggle — a small square showing the INVERSE colour of the active theme. */
.theme-toggle {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    width: 28px;
    height: 28px;
    background: transparent;
    border: none;
    cursor: pointer;
}

/* Expand the tappable area to the WCAG 2.5.5 44px target without growing the
   visual swatch. */
.theme-toggle::before {
    content: "";
    position: absolute;
    inset: -8px;
}

.theme-toggle-swatch {
    width: 16px;
    height: 16px;
    border-radius: 3px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    /* In light mode show a dark square (switch-to-dark); dark mode shows light. */
    background: var(--pebble-900);
}

[data-theme="dark"] .theme-toggle-swatch {
    background: var(--pebble-100);
    border-color: rgba(0, 0, 0, 0.3);
}

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

html, body {
    margin: 0;
    padding: 0;
    font-family: var(--font);
    font-size: 16px;
    font-weight: var(--weight-normal);
    line-height: 1.75;
    color: var(--color-text);
    background-color: var(--color-bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

::selection {
    background: var(--haven-200);
    color: var(--color-text);
}

/* --- Scrollbar (Webkit) --- */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--pebble-100);
}

::-webkit-scrollbar-thumb {
    background: var(--pebble-400);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--pebble-500);
}

/* --- Cards --- */
.card {
    background: var(--color-surface);
    border-radius: var(--radius);
    border: 1px solid var(--color-border-light);
    padding: calc(var(--grid) * 3) calc(var(--grid) * 4);
    margin-bottom: calc(var(--grid) * 3);
    box-shadow: var(--shadow-sm);
    animation: card-enter 0.3s ease both;
}

@keyframes card-enter {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card h2 {
    margin: 0 0 calc(var(--grid) * 0.5) 0;
    font-size: 1.125rem;
    font-weight: var(--weight-black);
    color: var(--color-text);
}

.card-description {
    margin: 0 0 calc(var(--grid) * 2.5) 0;
    font-size: 0.875rem;
    color: var(--color-text-secondary);
    line-height: 1.5;
}

/* --- Form controls --- */
.form-group {
    margin-bottom: calc(var(--grid) * 2.5);
}

.form-group label {
    display: block;
    font-size: 0.8125rem;
    font-weight: var(--weight-bold);
    margin-bottom: calc(var(--grid) * 0.5);
    color: var(--color-text);
    letter-spacing: 0.01em;
}

.form-group select,
.form-group input[type="text"] {
    font-family: var(--font);
    font-size: 0.875rem;
    font-weight: var(--weight-normal);
    padding: calc(var(--grid) * 1) calc(var(--grid) * 1.5);
    border: 1px solid var(--pebble-700);
    border-radius: var(--radius-input);
    background: var(--color-surface);
    color: var(--color-text);
    min-width: 220px;
    height: 40px;
    transition: border-color var(--transition), box-shadow var(--transition);
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%234c4c4c' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
    background-position: right calc(var(--grid) * 1) center;
    background-repeat: no-repeat;
    background-size: 1.25em 1.25em;
    padding-right: calc(var(--grid) * 4);
}

/* On dark inputs the pebble-700 (#4c4c4c) chevron is too dark to read —
   override with pebble-400 (#bfbfbd) so the arrow remains visible. */
[data-theme="dark"] .form-group select,
[data-theme="dark"] .form-group input[type="text"] {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%23bfbfbd' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
}

.form-group select:focus,
.form-group input[type="text"]:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-faint);
}

.form-group select:focus-visible,
.form-group input[type="text"]:focus-visible {
    outline: none;
}

.form-hint {
    display: block;
    font-size: var(--font-xs);
    color: var(--color-text-secondary);
    margin-top: calc(var(--grid) * 0.5);
}

/* --- Buttons --- */
.btn {
    font-family: var(--font);
    font-size: 0.875rem;
    font-weight: var(--weight-bold);
    padding: calc(var(--grid) * 1) calc(var(--grid) * 3);
    border: none;
    border-radius: var(--radius-input);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: calc(var(--grid) * 1);
    transition: all var(--transition);
    letter-spacing: 0.01em;
    min-height: 40px;
}

/* text-decoration only matters for anchor buttons — a <button> has none by default. */
a.btn {
    text-decoration: none;
}

.btn-primary {
    background-color: var(--color-primary);
    color: var(--color-on-primary);
    box-shadow: var(--shadow-sm);
}

.btn-primary:hover:not(:disabled) {
    background-color: var(--color-primary-hover);
    box-shadow: var(--shadow);
    transform: translateY(-1px);
}

.btn-primary:active:not(:disabled) {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
}

.btn-primary:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn:focus-visible {
    outline: 3px solid var(--signal-orange-400);
    outline-offset: 2px;
}

.btn-secondary {
    background-color: transparent;
    color: var(--color-primary);
    border: 1.5px solid var(--color-primary);
}

.btn-secondary:hover:not(:disabled) {
    background-color: var(--color-primary-faint);
}

.btn-secondary:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn-group {
    display: flex;
    gap: calc(var(--grid) * 1.5);
    margin-top: calc(var(--grid) * 1);
}

/* --- Loading spinner --- */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: currentColor;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

.btn-secondary .spinner {
    border-color: rgba(0, 118, 184, 0.2);
    border-top-color: var(--color-primary);
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --- Alerts --- */
.alert {
    padding: calc(var(--grid) * 1.5) calc(var(--grid) * 2);
    border-radius: var(--radius);
    font-size: 0.8125rem;
    margin-bottom: calc(var(--grid) * 2);
    display: flex;
    align-items: flex-start;
    gap: calc(var(--grid) * 1);
    line-height: 1.5;
}

.alert-icon {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    line-height: 1;
}

.alert-icon svg {
    display: block;
}

.alert-error {
    background-color: var(--color-error-bg);
    border: 1px solid var(--color-error-border);
    color: var(--color-error);
}

.alert-success {
    background-color: var(--color-success-bg);
    border: 1px solid var(--color-success-border);
    color: var(--color-success);
}

.alert-warning {
    background-color: color-mix(in srgb, var(--signal-orange-400) 10%, transparent);
    border: 1px solid color-mix(in srgb, var(--signal-orange-400) 40%, transparent);
    color: color-mix(in srgb, var(--signal-orange-400) 60%, var(--midnight-600));
    flex-wrap: wrap;
    gap: calc(var(--grid) * 1.5);
}

/* --- Data table (shared) --- */
.table-wrapper {
    overflow-x: auto;
    border-radius: var(--radius);
    border: 1px solid var(--color-border-light);
    background: var(--color-surface);
}

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

.data-table thead th {
    background-color: var(--midnight-400);
    color: var(--color-on-primary);
    font-weight: var(--weight-bold);
    text-align: left;
    padding: calc(var(--grid) * 1) calc(var(--grid) * 1.5);
    white-space: nowrap;
    letter-spacing: 0.01em;
    position: sticky;
    top: var(--env-stripe-height);
    z-index: 10;
}

.data-table thead th:first-child {
    border-radius: var(--radius) 0 0 0;
}

.data-table thead th:last-child {
    border-radius: 0 var(--radius) 0 0;
}

.data-table tbody td {
    padding: calc(var(--grid) * 1) calc(var(--grid) * 1.5);
    border-bottom: 1px solid var(--color-border-light);
    vertical-align: top;
}

.data-table tbody tr:last-child td {
    border-bottom: none;
}

.data-table tbody tr:nth-child(even) {
    background-color: var(--color-bg);
}

.data-table tbody tr {
    transition: background-color 0.1s ease;
}

.data-table tbody tr:hover {
    background-color: var(--haven-100);
}

.data-table .col-narrow {
    width: 80px;
}

.data-table .col-message {
    min-width: 200px;
}

/* Sticky first column on narrow viewports */
@media (max-width: 1100px) {
    .data-table td:first-child,
    .data-table th:first-child {
        position: sticky;
        left: 0;
        z-index: 5;
        background-color: inherit;
    }

    .data-table thead th:first-child {
        z-index: 15;
        background-color: var(--midnight-400);
    }
}

/* --- Version footer --- */
.version-footer {
    position: fixed;
    bottom: 0;
    right: 0;
    padding: calc(var(--grid) * 1) calc(var(--grid) * 1.5);
    font-size: 0.6875rem;
    color: var(--pebble-500);
    letter-spacing: 0.01em;
    background-color: var(--color-surface);
}

.version-separator {
    margin: 0 calc(var(--grid) * 0.5);
    color: var(--pebble-300);
}

/* Respect a user's reduced-motion preference: suppress the card entrance, the
   spinner sweep, and crush all transition/animation durations suite-wide (WCAG
   2.3.3). Restores behaviour the sibling app.css consolidation dropped. */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .card {
        animation: none;
    }

    .spinner {
        animation: none;
    }
}
