/**
 * ResponseReady - UI Components
 * 
 * A comprehensive component library following Material Design principles
 * with WCAG 2.1 AA compliance and 44px minimum touch targets.
 * 
 * This file uses CSS custom properties defined in tokens.css,
 * which is imported first by main.css.
 * 
 * Table of Contents:
 * 1. Design Tokens (from tokens.css)
 * 2. Buttons
 * 3. Form Controls
 * 4. Cards
 * 5. Tables
 * 6. Badges & Tags
 * 7. Alerts
 * 8. Modals
 * 9. Tabs
 * 10. Loading States
 * 11. Utility Classes
 * 12. Accessibility
 */

/* ==========================================================================
   1. DESIGN TOKENS
   All design tokens are defined in tokens.css (imported first by main.css).
   No local token overrides — single source of truth.
   ========================================================================== */


/* ==========================================================================
   2. BUTTONS
   Touch-friendly buttons with 44px minimum height for accessibility.
   ========================================================================== */

/**
 * Base Button
 * All buttons inherit from this base class.
 * - 44px min-height for WCAG touch targets
 * - Flexbox for icon + text alignment
 * - Smooth transitions on all interactive states
 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--touch-target-min);
    padding: var(--space-3) var(--space-6);
    border: 2px solid transparent;
    border-radius: var(--radius-md);
    font-family: var(--font-family-base);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-medium);
    line-height: var(--line-height-tight);
    text-align: center;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    touch-action: manipulation;
    transition: 
        background-color var(--transition-fast),
        border-color var(--transition-fast),
        color var(--transition-fast),
        box-shadow var(--transition-fast),
        transform var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

/* Button icon sizing */
.btn svg,
.btn [class*="icon"] {
    width: 1.25em;
    height: 1.25em;
    flex-shrink: 0;
}

/* Focus state - visible focus ring for keyboard navigation */
.btn:focus {
    outline: none;
}

.btn:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

/* Active state - subtle press feedback */
.btn:active:not(:disabled) {
    transform: scale(0.98);
}

/* Disabled state */
.btn:disabled,
.btn[aria-disabled="true"] {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/**
 * Button Variants
 */

/* Primary - Filled red, white text */
.btn-primary {
    background-color: var(--color-primary);
    border-color: var(--color-primary);
    color: var(--text-on-primary);
}

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

.btn-primary:active:not(:disabled) {
    background-color: var(--color-primary-active);
    border-color: var(--color-primary-active);
}

/* Secondary - Outlined, neutral border */
.btn-secondary {
    background-color: transparent;
    border-color: var(--border-strong);
    color: var(--text-primary);
}

.btn-secondary:hover:not(:disabled) {
    background-color: var(--color-neutral-100);
    border-color: var(--color-neutral-400);
}

.btn-secondary:active:not(:disabled) {
    background-color: var(--color-neutral-200);
}

/* Ghost - Text only, transparent background */
.btn-ghost {
    background-color: transparent;
    border-color: transparent;
    color: var(--text-primary);
}

.btn-ghost:hover:not(:disabled) {
    background-color: var(--color-neutral-100);
}

.btn-ghost:active:not(:disabled) {
    background-color: var(--color-neutral-200);
}

/* Danger - Filled error color */
.btn-danger {
    background-color: var(--color-error);
    border-color: var(--color-error);
    color: var(--text-on-primary);
}

.btn-danger:hover:not(:disabled) {
    background-color: var(--color-error-dark);
    border-color: var(--color-error-dark);
}

.btn-danger:active:not(:disabled) {
    background-color: var(--color-primary-active);
    border-color: var(--color-primary-active);
}

/* Success - Filled success color */
.btn-success {
    background-color: var(--color-success);
    border-color: var(--color-success);
    color: var(--text-on-primary);
}

.btn-success:hover:not(:disabled) {
    background-color: var(--color-success-dark);
    border-color: var(--color-success-dark);
}

.btn-success:active:not(:disabled) {
    background-color: var(--color-success-active);
    border-color: var(--color-success-active);
}

/**
 * Button Sizes
 */

/* Small - 36px height */
.btn-sm {
    min-height: 36px;
    padding: var(--space-2) var(--space-4);
    font-size: var(--font-size-sm);
    border-radius: var(--radius-sm);
}

/* Large - 52px height */
.btn-lg {
    min-height: 52px;
    padding: var(--space-4) var(--space-8);
    font-size: var(--font-size-lg);
    border-radius: var(--radius-lg);
}

/* Full width button */
.btn-block {
    display: flex;
    width: 100%;
}

/* Button group */
.btn-group {
    display: inline-flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.btn-group-vertical {
    display: inline-flex;
    flex-direction: column;
    gap: var(--space-2);
}

@media (max-width: 640px) {
    .btn-group {
        width: 100%;
    }
    
    .btn-group .btn {
        flex: 1 1 auto;
    }
    
    .btn-group-stack-mobile {
        flex-direction: column;
    }
    
    .btn-group-stack-mobile .btn {
        width: 100%;
    }
}

/* Icon-only button modifier - use with .btn: <button class="btn btn-icon"> */
.btn-icon {
    padding: var(--space-3);
    aspect-ratio: 1;
}

.btn-icon.btn-sm {
    padding: var(--space-2);
}

.btn-icon.btn-lg {
    padding: var(--space-4);
}


/* ==========================================================================
   3. FORM CONTROLS
   Accessible form inputs with proper focus states and error handling.
   ========================================================================== */

/**
 * Form Group Container
 * Wraps label, input, and helper/error text with proper spacing.
 */
.form-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-bottom: var(--space-6);
}

/**
 * Form Labels
 */
.label {
    display: block;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--text-primary);
    line-height: var(--line-height-normal);
}

/* Required field indicator */
.label-required::after {
    content: " *";
    color: var(--color-error);
}

/**
 * Base Input
 * Text inputs, email, password, tel, url, search, number
 */
.input {
    display: block;
    width: 100%;
    min-height: var(--touch-target-comfortable, 48px);
    padding: var(--space-3) var(--space-4);
    border: 2px solid var(--border-default);
    border-radius: var(--radius-md);
    background-color: var(--surface-primary);
    font-family: var(--font-family-base);
    font-size: 1rem;
    color: var(--text-primary);
    line-height: var(--line-height-normal);
    transition: 
        border-color var(--transition-fast),
        box-shadow var(--transition-fast),
        background-color var(--transition-fast);
    -webkit-appearance: none;
    appearance: none;
}

.input::placeholder {
    color: var(--text-tertiary);
}

/* Focus state with primary color ring */
.input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--focus-ring-color);
}

/* Hover state (only when not focused or disabled) */
.input:hover:not(:focus):not(:disabled) {
    border-color: var(--border-strong);
}

/* Disabled state */
.input:disabled {
    background-color: var(--surface-tertiary);
    color: var(--text-disabled);
    cursor: not-allowed;
}

/* Read-only state */
.input:read-only {
    background-color: var(--surface-secondary);
}

/* Error state */
.input-error,
.input.is-invalid {
    border-color: var(--color-error);
}

.input-error:focus,
.input.is-invalid:focus {
    border-color: var(--color-error);
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.2);
}

/* Success state */
.input-success,
.input.is-valid {
    border-color: var(--color-success);
}

.input-success:focus,
.input.is-valid:focus {
    border-color: var(--color-success);
    box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.2);
}

/**
 * Helper and Error Text
 */
.help-text {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    line-height: var(--line-height-normal);
}

.error-text {
    font-size: var(--font-size-sm);
    color: var(--color-error);
    line-height: var(--line-height-normal);
}

/**
 * Select Dropdown
 * Custom styled select with arrow indicator
 */
.select {
    display: block;
    width: 100%;
    min-height: var(--touch-target-comfortable, 48px);
    padding: var(--space-3) var(--space-10) var(--space-3) var(--space-4);
    border: 2px solid var(--border-default);
    border-radius: var(--radius-md);
    background-color: var(--surface-primary);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23525252' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-4) center;
    background-size: 16px;
    font-family: var(--font-family-base);
    font-size: 1rem;
    color: var(--text-primary);
    line-height: var(--line-height-normal);
    cursor: pointer;
    transition: 
        border-color var(--transition-fast),
        box-shadow var(--transition-fast);
    -webkit-appearance: none;
    appearance: none;
}

.select:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--focus-ring-color);
}

.select:disabled {
    background-color: var(--surface-tertiary);
    color: var(--text-disabled);
    cursor: not-allowed;
}

/**
 * File picker — wraps a native file input so the browser's raw "Choose file"
 * control never shows. The input is an invisible overlay (clicking or
 * keyboard-activating anywhere on the picker opens the dialog, keeping it
 * screen-reader and keyboard reachable). main.js keeps the .file-picker-name
 * span in sync; call ResponseReady.syncFilePicker(input) after
 * programmatically clearing the input.
 *
 *   <div class="file-picker">
 *       <input type="file" id="x" accept="...">
 *       <span class="btn btn-secondary btn-sm">Choose file</span>
 *       <span class="file-picker-name">No file chosen</span>
 *   </div>
 */
.file-picker {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--touch-target-comfortable, 48px);
}

.file-picker input[type="file"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.file-picker .btn {
    pointer-events: none;
}

.file-picker:focus-within .btn {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--focus-ring-color);
}

.file-picker-name {
    font-size: var(--text-sm);
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/**
 * Textarea
 */
.textarea {
    display: block;
    width: 100%;
    min-height: calc(var(--touch-target-comfortable, 48px) * 2);
    padding: var(--space-3) var(--space-4);
    border: 2px solid var(--border-default);
    border-radius: var(--radius-md);
    background-color: var(--surface-primary);
    font-family: var(--font-family-base);
    font-size: 1rem;
    color: var(--text-primary);
    line-height: var(--line-height-relaxed);
    resize: vertical;
    transition: 
        border-color var(--transition-fast),
        box-shadow var(--transition-fast);
    -webkit-appearance: none;
    appearance: none;
}

.textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--focus-ring-color);
}

.textarea::placeholder {
    color: var(--text-tertiary);
}

.textarea:disabled {
    background-color: var(--surface-tertiary);
    color: var(--text-disabled);
    cursor: not-allowed;
    resize: none;
}

/**
 * Checkbox and Radio
 * Custom styled with proper touch targets
 */
.checkbox-wrapper,
.radio-wrapper {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    min-height: var(--touch-target-min);
    padding: var(--space-2) 0;
    cursor: pointer;
}

.checkbox,
.radio {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    margin-top: 2px;
    border: 2px solid var(--border-strong);
    background-color: var(--surface-primary);
    cursor: pointer;
    transition: 
        border-color var(--transition-fast),
        background-color var(--transition-fast),
        box-shadow var(--transition-fast);
    -webkit-appearance: none;
    appearance: none;
}

.checkbox {
    border-radius: var(--radius-sm);
}

.radio {
    border-radius: var(--radius-full);
}

.checkbox:focus,
.radio:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--focus-ring-color);
}

.checkbox:checked,
.radio:checked {
    border-color: var(--color-primary);
    background-color: var(--color-primary);
}

/* Checkbox checkmark */
.checkbox:checked {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 12px;
}

/* Radio dot */
.radio:checked {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 8 8'%3E%3Ccircle cx='4' cy='4' r='4' fill='white'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 8px;
}

.checkbox:disabled,
.radio:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.checkbox-label,
.radio-label {
    font-size: var(--font-size-base);
    color: var(--text-primary);
    line-height: var(--line-height-normal);
    cursor: pointer;
}

/* Input with icon */
.input-group {
    position: relative;
    display: flex;
    align-items: center;
}

.input-group .input {
    padding-left: var(--space-10);
}

.input-group-icon {
    position: absolute;
    left: var(--space-4);
    color: var(--text-tertiary);
    pointer-events: none;
}

.input-group-icon svg {
    width: 18px;
    height: 18px;
}


/* ==========================================================================
   4. CARDS
   Content containers with subtle styling and status variants.
   ========================================================================== */

/**
 * Base Card
 * White background with subtle border, no heavy shadows by default.
 */
.card {
    position: relative;
    display: flex;
    flex-direction: column;
    background-color: var(--surface-primary);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

/**
 * Card Sections
 */
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    padding: var(--space-6);
    border-bottom: 1px solid var(--border-default);
}

.card-body {
    flex: 1;
    padding: var(--space-6);
}

.card-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-6);
    border-top: 1px solid var(--border-default);
    background-color: var(--surface-secondary);
}

.card-title {
    margin: 0;
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    line-height: var(--line-height-tight);
}

.card-subtitle {
    margin: var(--space-1) 0 0 0;
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

/**
 * Collapsible card — a .card whose body hides when collapsed. The header is the
 * toggle (.collapse-toggle); action buttons inside it must stopPropagation.
 * Independent (not a group): several can be open at once. The chevron rotates
 * and a .section-summary on the header surfaces the section's current state so
 * it's assessable while collapsed. Pair with data-collapse-key for persistence
 * (main.js restores/saves the open state and wires the toggle).
 */
.collapsible-card.is-collapsed > .collapse-body { display: none; }

.collapse-toggle {
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
}

/* On phones, let a section header wrap so its action button drops below the
   title+summary instead of squeezing the title into a mid-word wrap. */
@media (max-width: 640px) {
    .collapsible-card > .collapse-toggle { flex-wrap: wrap; }
}

.collapse-toggle:hover { background: var(--surface-secondary); }

.collapse-toggle:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: -2px;
}

.collapse-chevron {
    flex-shrink: 0;
    color: var(--text-tertiary);
    transition: transform var(--transition-fast);
}

.collapsible-card:not(.is-collapsed) > .collapse-toggle .collapse-chevron {
    transform: rotate(90deg);
}

.section-summary {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-normal);
    color: var(--text-tertiary);
}

/* A ⚠ marker on a collapsible section's header flagging a cross-reference
   problem (e.g. a setting that can never take effect), visible while collapsed.
   The full message is in the title tooltip and the page's warnings banner. */
.section-warning {
    color: #b45309;
    cursor: help;
    font-size: var(--font-size-sm);
    margin-left: var(--space-1);
}

/**
 * Section jump-nav — a sticky row of chips that scroll to (and expand) the
 * collapsible sections of a long config page.
 */
.section-nav {
    position: sticky;
    /* Stick below the fixed app topbar, not behind it. */
    top: var(--topbar-height, 56px);
    z-index: 20;
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    padding: var(--space-3) 0;
    margin-bottom: var(--space-4);
    background: var(--surface-page, var(--surface-secondary));
    border-bottom: 1px solid var(--border-default);
}

.section-nav a {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    text-decoration: none;
    padding: var(--space-1) var(--space-3);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-full);
    background: var(--surface-primary);
    white-space: nowrap;
}

.section-nav a:hover {
    border-color: var(--color-primary);
    color: var(--color-primary-700);
}

/* On phones the chips wrap (rather than scroll off-screen); drop the sticky so
   the wrapped multi-row band doesn't eat the small viewport. */
@media (max-width: 640px) {
    .section-nav { position: static; }
}

/**
 * Card Variants
 */

/* Elevated - With shadow */
.card-elevated {
    border-color: transparent;
    box-shadow: var(--shadow-md);
}

.card-elevated:hover {
    box-shadow: var(--shadow-lg);
}

/* Interactive - Clickable card with hover effects */
.card-interactive {
    cursor: pointer;
    transition: 
        border-color var(--transition-fast),
        box-shadow var(--transition-fast),
        transform var(--transition-fast);
}

.card-interactive:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-md);
}

.card-interactive:active {
    transform: scale(0.995);
}

.card-interactive:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

/**
 * Card Status Variants
 * Left border indicator for operational status
 */
.card-status-operational {
    border-left: 4px solid var(--color-success);
}

.card-status-warning {
    border-left: 4px solid var(--color-warning);
}

.card-status-critical {
    border-left: 4px solid var(--color-error);
}

.card-status-info {
    border-left: 4px solid var(--color-info);
}

/* Card image */
.card-image {
    width: 100%;
    aspect-ratio: 16 / 9;
    object-fit: cover;
}

/* Card actions row */
.card-actions {
    display: flex;
    gap: var(--space-2);
    padding: var(--space-4) var(--space-6);
    border-top: 1px solid var(--border-default);
}


/* ==========================================================================
   5. TABLES
   Responsive data tables with proper styling.
   ========================================================================== */

/**
 * Table Container
 * Provides horizontal scroll on small screens
 */
.table-container {
    position: relative;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
}

.table-container::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 40px;
    background: linear-gradient(to right, transparent, var(--surface-primary));
    pointer-events: none;
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.table-container.has-scroll::after {
    opacity: 1;
}

.table-container.scrolled-end::after {
    opacity: 0;
}

/**
 * Base Table
 * Collapses to labelled cards ≤640px (see media block below). Tables that
 * genuinely need a dense grid on mobile (matrices, reports) opt out with
 * .table-wide ({ wide: true } on the table-open partial) and keep the
 * horizontal-scroll behaviour instead.
 */
.table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    font-size: var(--font-size-sm);
}

.table-wide {
    min-width: 800px;
}

/* Table Header */
.table th {
    padding: var(--space-4);
    text-align: left;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    background-color: var(--surface-secondary);
    border-bottom: 1px solid var(--border-default);
    white-space: nowrap;
}

/* Table Cells */
.table td {
    padding: var(--space-4);
    text-align: left;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-default);
    vertical-align: middle;
}

/* Remove bottom border from last row */
.table tbody tr:last-child td {
    border-bottom: none;
}

/* Row hover effect */
.table tbody tr:hover {
    background-color: var(--surface-secondary);
}

/**
 * Table Variants
 */

/* Striped rows */
.table-striped tbody tr:nth-child(even) {
    background-color: var(--surface-secondary);
}

.table-striped tbody tr:nth-child(even):hover {
    background-color: var(--color-neutral-100);
}

/* Compact table */
.table-compact th,
.table-compact td {
    padding: var(--space-2) var(--space-3);
}

/* Bordered table */
.table-bordered {
    border: 1px solid var(--border-default);
}

.table-bordered th,
.table-bordered td {
    border: 1px solid var(--border-default);
}

.table-bordered tbody tr:last-child td {
    border-bottom: 1px solid var(--border-default);
}

/* Sortable column header */
.table th[data-sortable] {
    cursor: pointer;
    user-select: none;
}

.table th[data-sortable]:hover {
    background-color: var(--color-neutral-200);
}

/* Table cell alignment */
.table .text-right {
    text-align: right;
}

.table .text-center {
    text-align: center;
}

/* Empty state */
.table-empty {
    padding: var(--space-12);
    text-align: center;
    color: var(--text-tertiary);
}

/**
 * "Load More" pagination block
 * Paired with components/pagination.ejs + static/js/pagination.js
 */
.pagination-container {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: var(--space-4);
    margin-top: var(--space-4);
    padding-top: var(--space-4);
    border-top: 1px solid var(--border-default);
}

/**
 * Mobile: tables collapse to labelled cards.
 * Each row becomes a card; each cell shows its column name via the
 * data-label attribute (stamped from the thead by main.js labelTable()).
 * .table-wide tables opt out and keep the horizontal-scroll behaviour.
 */
@media (max-width: 640px) {
    /* Container chrome belongs to the row-cards, not the wrapper.
       :has() keeps scroll styling for .table-wide wrappers. */
    .table-container:not(:has(> .table-wide)) {
        border: none;
        border-radius: 0;
        overflow-x: visible;
    }

    .table-container:not(:has(> .table-wide))::after {
        display: none;
    }

    .table:not(.table-wide) {
        display: block;
    }

    .table:not(.table-wide) thead {
        display: none;
    }

    .table:not(.table-wide) tbody {
        display: block;
    }

    .table:not(.table-wide) tbody tr {
        display: block;
        margin-bottom: var(--space-3);
        padding: var(--space-2) 0;
        background: var(--surface-primary);
        border: 1px solid var(--border-default);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-xs);
    }

    .table:not(.table-wide) tbody tr:hover {
        background-color: var(--surface-primary);
    }

    .table:not(.table-wide) tbody td {
        display: block;
        padding: var(--space-2) var(--space-4);
        border-bottom: none;
    }

    .table:not(.table-wide) tbody td[data-label]::before {
        content: attr(data-label);
        display: block;
        margin-bottom: var(--space-0-5);
        font-size: var(--font-size-xs);
        font-weight: var(--font-weight-semibold);
        text-transform: uppercase;
        letter-spacing: var(--tracking-wider);
        color: var(--text-tertiary);
    }

    /* Cells with no content add noise in card mode */
    .table:not(.table-wide) tbody td:empty {
        display: none;
    }

    /* Action cells read as a card footer; buttons get full thumb width */
    .table:not(.table-wide) tbody td.table-actions {
        margin-top: var(--space-2);
        padding-top: var(--space-3);
        border-top: 1px solid var(--border-default);
    }

    .table:not(.table-wide) tbody td.table-actions .btn {
        width: 100%;
    }

    .table:not(.table-wide) tbody td.table-actions .btn + .btn {
        margin-top: var(--space-2);
    }
}


/* ==========================================================================
   6. BADGES & TAGS
   Status indicators and labels.
   ========================================================================== */

/**
 * Base Badge
 * Pill-shaped inline element for status/labels
 */
.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-full);
    border: 1px solid transparent;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    line-height: var(--line-height-tight);
    white-space: nowrap;
}

.badge-success {
    background-color: var(--color-success-light);
    color: #166534;
    border-color: rgba(22, 163, 74, 0.2);
}

.badge-warning {
    background-color: var(--color-warning-light);
    color: #92400E;
    border-color: rgba(217, 119, 6, 0.2);
}

.badge-error {
    background-color: var(--color-error-light);
    color: #991B1B;
    border-color: rgba(220, 38, 38, 0.2);
}

.badge-info {
    background-color: var(--color-info-light);
    color: #1E40AF;
    border-color: rgba(59, 130, 246, 0.2);
}

.badge-neutral {
    background-color: var(--color-neutral-200);
    color: var(--color-neutral-700);
    border-color: rgba(100, 116, 139, 0.2);
}

.badge-primary {
    background-color: var(--color-primary-light);
    color: var(--color-primary-dark, var(--color-primary));
    border-color: rgba(var(--color-primary-rgb, 220, 38, 38), 0.2);
}

/* Solid badge variants */
.badge-success-solid {
    background-color: var(--color-success);
    color: var(--text-on-primary);
}

.badge-warning-solid {
    background-color: var(--color-warning);
    color: var(--text-on-primary);
}

.badge-error-solid {
    background-color: var(--color-error);
    color: var(--text-on-primary);
}

/**
 * Status Dots
 * Small circular indicators
 */
.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: var(--radius-full);
    background-color: var(--color-neutral-400);
}

.status-dot-success {
    background-color: var(--color-success);
}

.status-dot-warning {
    background-color: var(--color-warning);
}

.status-dot-error {
    background-color: var(--color-error);
}

.status-dot-info {
    background-color: var(--color-info);
}

/* Pulsing animation for active status */
.status-dot-pulse {
    position: relative;
}

.status-dot-pulse::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: var(--radius-full);
    background-color: inherit;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(2);
        opacity: 0;
    }
}

/* Status dot with label */
.status-indicator {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

/**
 * Removable chips — a wrapping list of small pills, each with a remove (✕) button.
 * Used wherever a field holds a small ordered set of selections (e.g. the per-task
 * file attachments in the duty roster editor). `.is-missing` styles a chip whose
 * referent no longer exists (kept so saving doesn't silently drop the link).
 */
.chip-list { display: flex; gap: var(--space-1); flex-wrap: wrap; }
.att-chip { display: inline-flex; align-items: center; gap: var(--space-1); padding: 2px var(--space-2); background: var(--color-neutral-100); border: 1px solid var(--color-neutral-200); border-radius: var(--radius-full); font-size: var(--font-size-xs); }
.att-chip button { border: 0; background: none; cursor: pointer; line-height: 1; padding: 0; color: var(--color-neutral-600); font-size: var(--font-size-sm); }
.att-chip.is-missing { color: var(--color-neutral-500); font-style: italic; }


/* ==========================================================================
   7. ALERTS
   Contextual feedback messages.
   ========================================================================== */

/**
 * Base Alert
 */
.alert {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-radius: var(--radius-md);
    border-left: 4px solid;
    font-size: var(--font-size-sm);
    line-height: var(--line-height-normal);
}

.alert-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    margin-top: 1px;
}

.alert-content {
    flex: 1;
    min-width: 0;
}

.alert-title {
    margin: 0 0 var(--space-1) 0;
    font-weight: var(--font-weight-semibold);
}

.alert-description {
    margin: 0;
}

/**
 * Alert Variants
 */
.alert-success {
    background-color: var(--color-success-light);
    border-left-color: var(--color-success);
    color: var(--color-success-dark);
}

.alert-warning {
    background-color: var(--color-warning-light);
    border-left-color: var(--color-warning);
    color: var(--color-warning-dark);
}

.alert-error,
.alert-danger {
    background-color: var(--color-error-light);
    border-left-color: var(--color-error);
    color: var(--color-error-dark);
}

.alert-info {
    background-color: var(--color-info-light);
    border-left-color: var(--color-info);
    color: var(--color-info-dark);
}

/**
 * Dismissible Alert
 */
.alert-dismissible {
    padding-right: var(--space-12);
}

.alert-close {
    position: absolute;
    top: var(--space-3);
    right: var(--space-3);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: var(--radius-sm);
    background-color: transparent;
    color: currentColor;
    opacity: 0.7;
    cursor: pointer;
    transition: 
        opacity var(--transition-fast),
        background-color var(--transition-fast);
}

.alert-close:hover {
    opacity: 1;
    background-color: rgba(0, 0, 0, 0.1);
}

.alert-close:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

.alert-close svg {
    width: 16px;
    height: 16px;
}


/* ==========================================================================
   8. MODALS
   Dialog overlays for focused interactions.
   ========================================================================== */

/**
 * Modal Overlay
 * Semi-transparent backdrop
 */
/* Scroll lock while a modal is open (toggled by main.js) */
body.modal-is-open {
    overflow: hidden;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--z-modal-backdrop);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transition: 
        opacity var(--transition-normal),
        visibility var(--transition-normal);
}

.modal-overlay.is-open {
    opacity: 1;
    visibility: visible;
}

/**
 * Modal Content Box
 */
.modal {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 500px;
    max-height: calc(100vh - var(--space-8));
    background-color: var(--surface-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    transform: scale(0.95) translateY(10px);
    transition: transform var(--transition-normal);
}

.modal-overlay.is-open .modal {
    transform: scale(1) translateY(0);
}

/* A form may wrap .modal-body + .modal-footer (submit semantics). Pass the
   flex sizing through so the body still gets a bounded height to scroll in. */
.modal > form {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
}

/* Modal size variants */
.modal-sm {
    max-width: 400px;
}

.modal-lg {
    max-width: 700px;
}

.modal-xl {
    max-width: 900px;
}

.modal-fullscreen {
    max-width: none;
    width: calc(100% - var(--space-8));
    height: calc(100% - var(--space-8));
    max-height: none;
    border-radius: var(--radius-md);
}

/**
 * Modal Sections
 */
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    flex-shrink: 0;
    padding: var(--space-5) var(--space-6);
    border-bottom: 1px solid var(--border-default);
}

.modal-title {
    margin: 0;
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    line-height: var(--line-height-tight);
}

.modal-body {
    flex: 1;
    padding: var(--space-6);
    overflow-y: auto;
}

.modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: var(--space-3);
    flex-shrink: 0;
    padding: var(--space-4) var(--space-6);
    border-top: 1px solid var(--border-default);
    background-color: var(--surface-secondary);
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

/**
 * Modal Close Button
 */
.modal-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--touch-target-min);
    height: var(--touch-target-min);
    margin: calc(-1 * var(--space-2));
    padding: 0;
    border: none;
    border-radius: var(--radius-md);
    background-color: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: 
        background-color var(--transition-fast),
        color var(--transition-fast);
}

.modal-close:hover {
    background-color: var(--color-neutral-100);
    color: var(--text-primary);
}

.modal-close:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: calc(-1 * var(--focus-ring-offset));
}

.modal-close svg {
    width: 20px;
    height: 20px;
}

/**
 * Mobile: modals render as bottom sheets.
 * Same markup — full width, pinned to the bottom edge, grab-bar affordance,
 * stacked full-width actions in thumb reach.
 */
@media (max-width: 640px) {
    .modal-overlay {
        align-items: flex-end;
        padding: 0;
    }

    .modal,
    .modal-sm,
    .modal-lg,
    .modal-xl {
        max-width: none;
        max-height: 90dvh;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        padding-bottom: env(safe-area-inset-bottom, 0px);
        transform: translateY(100%);
    }

    .modal-overlay.is-open .modal {
        transform: translateY(0);
    }

    /* Grab bar affordance */
    .modal::before {
        content: '';
        display: block;
        flex-shrink: 0;
        width: 36px;
        height: 4px;
        margin: var(--space-2) auto calc(-1 * var(--space-2));
        border-radius: var(--radius-full);
        background: var(--border-strong);
    }

    .modal-footer {
        flex-direction: column-reverse;
        align-items: stretch;
        border-radius: 0;
    }

    .modal-footer .btn {
        width: 100%;
    }
}


/* ==========================================================================
   9. TABS
   Tab navigation for content panels.
   ========================================================================== */

/**
 * Tab Group Container
 * Horizontal container for tab buttons with bottom border.
 */
.tab-group {
    display: flex;
    gap: var(--space-2);
    border-bottom: 1px solid var(--border-default);
}

/**
 * Individual Tab Button
 * Touch-friendly tab with proper focus and active states.
 */
.tab {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--touch-target-min);
    padding: var(--space-3) var(--space-4);
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    background-color: transparent;
    font-family: var(--font-family-base);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--text-secondary);
    cursor: pointer;
    transition: 
        color var(--transition-fast),
        border-color var(--transition-fast),
        background-color var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

/* Tab hover state */
.tab:hover:not(:disabled):not(.active) {
    color: var(--text-primary);
    background-color: var(--color-neutral-100);
}

/* Tab focus state with proper focus ring */
.tab:focus {
    outline: none;
}

.tab:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
    border-radius: var(--radius-md);
}

/* Active tab state with primary color indicator */
.tab.active {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
}

/* Disabled tab state */
.tab:disabled {
    color: var(--text-disabled);
    cursor: not-allowed;
}

/**
 * Tab Content Panel
 * Content container for each tab, hidden by default.
 */
.tab-content {
    display: none;
    padding: var(--space-6);
}

/* Visible tab content when active */
.tab-content.active {
    display: block;
}


/* ==========================================================================
   10. LOADING STATES
   Visual indicators for async operations.
   ========================================================================== */

/**
 * Spinner
 * CSS-only animated loading indicator
 */
.spinner {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid var(--color-neutral-200);
    border-top-color: var(--color-primary);
    border-radius: var(--radius-full);
    animation: spin 0.8s linear infinite;
}

.spinner-sm {
    width: 16px;
    height: 16px;
    border-width: 2px;
}

.spinner-lg {
    width: 40px;
    height: 40px;
    border-width: 4px;
}

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

/* Spinner on button */
.btn .spinner {
    width: 1em;
    height: 1em;
    border-width: 2px;
}

/**
 * Skeleton Loaders
 * Placeholder content while loading
 */
.skeleton {
    position: relative;
    overflow: hidden;
    background-color: var(--color-neutral-200);
    border-radius: var(--radius-sm);
}

.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 255, 255, 0.5),
        transparent
    );
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    100% {
        transform: translateX(100%);
    }
}

/* Skeleton variants */
.skeleton-text {
    height: 1em;
    margin-bottom: var(--space-2);
    border-radius: var(--radius-sm);
}

.skeleton-text:last-child {
    width: 80%;
    margin-bottom: 0;
}

.skeleton-title {
    height: 1.5em;
    width: 60%;
    margin-bottom: var(--space-3);
}

.skeleton-avatar {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-full);
}

.skeleton-avatar-lg {
    width: 64px;
    height: 64px;
}

.skeleton-card {
    height: 200px;
    border-radius: var(--radius-lg);
}

.skeleton-button {
    height: var(--touch-target-min);
    width: 120px;
    border-radius: var(--radius-md);
}

/* Loading overlay for containers */
.loading-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: inherit;
    z-index: 1;
}

.page-loading-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: linear-gradient(90deg, var(--color-primary), var(--color-primary-400));
    z-index: 9999;
    transform-origin: left;
    transform: scaleX(0);
    opacity: 0;
    transition: transform 0.2s ease, opacity 0.2s ease;
    pointer-events: none;
}

.page-loading-bar.is-loading {
    opacity: 1;
    transform: scaleX(0.3);
    animation: loadingProgress 1.5s ease-in-out infinite;
}

.page-loading-bar.is-complete {
    transform: scaleX(1);
    opacity: 0;
    transition: transform 0.2s ease, opacity 0.3s ease 0.2s;
}

@keyframes loadingProgress {
    0% {
        transform: scaleX(0.3);
    }
    50% {
        transform: scaleX(0.7);
    }
    100% {
        transform: scaleX(0.3);
    }
}


/* ==========================================================================
   10b. PAGE PATTERNS
   Canonical versions of patterns previously redefined per-page.
   Do NOT redefine these in templates — extend here instead.
   ========================================================================== */

/**
 * Page Container
 * Standard max-width content container for admin/brigade pages.
 * Replaces per-page .brigade-container copies.
 */
.page-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: var(--space-6);
}

@media (max-width: 640px) {
    .page-container {
        padding: var(--space-4);
    }
}

/**
 * Form Row
 * Side-by-side form fields; collapses to a single column on mobile.
 * Previously redefined per-page in 14 templates.
 */
.form-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-4);
    margin-bottom: var(--space-5);
}

.form-row--cols-3 {
    grid-template-columns: repeat(3, 1fr);
}

.form-row .form-group {
    margin-bottom: 0;
}

@media (max-width: 640px) {
    .form-row,
    .form-row--cols-3 {
        grid-template-columns: 1fr;
    }
}

/**
 * Checkbox Group
 * Vertical list of labelled checkboxes (modal forms, settings pages).
 */
.form-checkbox-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.form-checkbox-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.form-checkbox-item input[type="checkbox"] {
    width: 16px;
    height: 16px;
    cursor: pointer;
}

.form-checkbox-item label {
    cursor: pointer;
    font-size: var(--font-size-sm);
}

/**
 * Empty State
 * Placeholder for empty lists/tables. Canonical version of the
 * pattern previously copied into 25+ templates.
 */
.empty-state,
td.empty-state {
    /* td variant outranks `.table td { text-align: left }` */
    text-align: center;
    padding: var(--space-12) var(--space-6);
    color: var(--text-secondary);
}

.empty-state p {
    margin: 0;
}

.empty-state-icon {
    font-size: var(--text-5xl);
    margin-bottom: var(--space-4);
    color: var(--color-neutral-300);
}

.empty-state .empty-state-hint {
    margin-top: var(--space-2);
    font-size: var(--font-size-sm);
    color: var(--text-tertiary);
}

/**
 * Hover Tooltip
 * Add data-tooltip="..." to any element (pairs well with .btn-icon).
 * Previously duplicated across admin pages.
 */
[data-tooltip] {
    position: relative;
}

[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: var(--space-1) var(--space-2);
    background: var(--color-neutral-800);
    color: white;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    white-space: nowrap;
    border-radius: var(--radius-sm);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--duration-fast) var(--ease-default), visibility var(--duration-fast) var(--ease-default);
    pointer-events: none;
    margin-bottom: var(--space-1);
    z-index: var(--z-tooltip);
}

[data-tooltip]:hover::after {
    opacity: 1;
    visibility: visible;
}

/**
 * Kicker
 * Small uppercase label for section/meta headings. Replaces the
 * repeated inline "font-size:xs; uppercase; muted" pattern.
 */
.kicker {
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: var(--tracking-wider);
    color: var(--text-tertiary);
}

/**
 * List-editor row
 * A compact bordered card for one entry in an inline list editor: an identity line
 * (.row-main, e.g. name + phone inputs) with a number (.slot-pos) and remove button
 * (.row-rm), and a second labelled line (.row-attach) for attachments/extras. Used
 * by the duty roster slot + task editors; extend here rather than per-page. Page-
 * specific input sizing inside .row-main stays in the page.
 */
.editor-row { display: grid; grid-template-columns: 1.5rem minmax(0,1fr) auto; grid-template-areas: "pos main rm" ". attach attach"; gap: var(--space-2); align-items: center; padding: var(--space-2-5) var(--space-3); margin-bottom: var(--space-2); border: 1px solid var(--color-neutral-200); border-radius: var(--radius-md); }
.editor-row .slot-pos { grid-area: pos; }
.editor-row .row-main { grid-area: main; display: flex; gap: var(--space-2); flex-wrap: wrap; }
.editor-row .row-rm { grid-area: rm; }
.row-attach { grid-area: attach; display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.row-attach .label-tag { font-size: var(--font-size-xs); color: var(--color-neutral-500); white-space: nowrap; }
.row-attach select { width: auto; flex: 1 1 180px; max-width: 300px; }
.row-attach .att-add { flex: 0 1 150px; }

/* ==========================================================================
   11. UTILITY CLASSES
   Helper classes for common styling needs.
   ========================================================================== */

/**
 * Text Utilities
 */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

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

.text-success { color: var(--color-success); }
.text-warning { color: var(--color-warning); }
.text-error { color: var(--color-error); }
.text-info { color: var(--color-info); }

.text-xs { font-size: var(--font-size-xs); }
.text-sm { font-size: var(--font-size-sm); }
.text-base { font-size: var(--font-size-base); }
.text-lg { font-size: var(--font-size-lg); }
.text-xl { font-size: var(--font-size-xl); }

.font-normal { font-weight: var(--font-weight-normal); }
.font-medium { font-weight: var(--font-weight-medium); }
.font-semibold { font-weight: var(--font-weight-semibold); }
.font-bold { font-weight: var(--font-weight-bold); }

.uppercase { text-transform: uppercase; }
.lowercase { text-transform: lowercase; }
.capitalize { text-transform: capitalize; }

.truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.text-nowrap { white-space: nowrap; }

/* Grid/flex children default to min-width:auto, letting long content blow the
   track out sideways — pin the minimum so .truncate / wrapping can bite. */
.min-w-0 { min-width: 0; }

.break-word { overflow-wrap: break-word; }

.cursor-pointer { cursor: pointer; }

.line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/**
 * Spacing Utilities
 * Margin and padding using the spacing scale
 */

/* Margin */
.m-0 { margin: 0; }
.m-1 { margin: var(--space-1); }
.m-2 { margin: var(--space-2); }
.m-3 { margin: var(--space-3); }
.m-4 { margin: var(--space-4); }
.m-5 { margin: var(--space-5); }
.m-6 { margin: var(--space-6); }
.m-8 { margin: var(--space-8); }

.mt-0 { margin-top: 0; }
.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-5 { margin-top: var(--space-5); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }

.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-5 { margin-bottom: var(--space-5); }
.mb-6 { margin-bottom: var(--space-6); }
.mb-8 { margin-bottom: var(--space-8); }

.ml-0 { margin-left: 0; }
.ml-1 { margin-left: var(--space-1); }
.ml-2 { margin-left: var(--space-2); }
.ml-3 { margin-left: var(--space-3); }
.ml-4 { margin-left: var(--space-4); }
.ml-auto { margin-left: auto; }

.mr-0 { margin-right: 0; }
.mr-1 { margin-right: var(--space-1); }
.mr-2 { margin-right: var(--space-2); }
.mr-3 { margin-right: var(--space-3); }
.mr-4 { margin-right: var(--space-4); }
.mr-auto { margin-right: auto; }

.mx-auto { margin-left: auto; margin-right: auto; }

/* Padding */
.p-0 { padding: 0; }
.p-1 { padding: var(--space-1); }
.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }
.p-5 { padding: var(--space-5); }
.p-6 { padding: var(--space-6); }
.p-8 { padding: var(--space-8); }

.pt-0 { padding-top: 0; }
.pt-1 { padding-top: var(--space-1); }
.pt-2 { padding-top: var(--space-2); }
.pt-3 { padding-top: var(--space-3); }
.pt-4 { padding-top: var(--space-4); }
.pt-5 { padding-top: var(--space-5); }
.pt-6 { padding-top: var(--space-6); }
.pt-8 { padding-top: var(--space-8); }

.pb-0 { padding-bottom: 0; }
.pb-1 { padding-bottom: var(--space-1); }
.pb-2 { padding-bottom: var(--space-2); }
.pb-3 { padding-bottom: var(--space-3); }
.pb-4 { padding-bottom: var(--space-4); }
.pb-5 { padding-bottom: var(--space-5); }
.pb-6 { padding-bottom: var(--space-6); }
.pb-8 { padding-bottom: var(--space-8); }

.pl-0 { padding-left: 0; }
.pl-1 { padding-left: var(--space-1); }
.pl-2 { padding-left: var(--space-2); }
.pl-3 { padding-left: var(--space-3); }
.pl-4 { padding-left: var(--space-4); }

.pr-0 { padding-right: 0; }
.pr-1 { padding-right: var(--space-1); }
.pr-2 { padding-right: var(--space-2); }
.pr-3 { padding-right: var(--space-3); }
.pr-4 { padding-right: var(--space-4); }

/**
 * Display Utilities
 */
.d-none { display: none; }

/* JS visibility toggle. !important so it wins over component display
   rules; matches the public.css definition. Toggle via classList. */
.hidden { display: none !important; }
.d-block { display: block; }
.d-inline { display: inline; }
.d-inline-block { display: inline-block; }
.d-flex { display: flex; }
.d-inline-flex { display: inline-flex; }
.d-grid { display: grid; }

/**
 * Flexbox Utilities
 */
.flex-row { flex-direction: row; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.flex-nowrap { flex-wrap: nowrap; }

.justify-start { justify-content: flex-start; }
.justify-end { justify-content: flex-end; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-around { justify-content: space-around; }
.justify-evenly { justify-content: space-evenly; }

.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.items-center { align-items: center; }
.items-baseline { align-items: baseline; }
.items-stretch { align-items: stretch; }

.flex-1 { flex: 1 1 0%; }
.flex-2 { flex: 2 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-none { flex: none; }
.flex-shrink-0 { flex-shrink: 0; }
.flex-grow { flex-grow: 1; }

/* Full-width cell inside a grid form (.form-row / .form-grid) */
.col-span-full { grid-column: 1 / -1; }

.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-5 { gap: var(--space-5); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }

/**
 * Card Grid — responsive auto-fill grid for cards/tiles. Columns size
 * themselves to fit the container and collapse to a single column on narrow
 * screens (the min(...) guard prevents overflow below the track width).
 * Carries its own gap; no extra .gap-* needed.
 */
.card-grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: repeat(auto-fill, minmax(min(320px, 100%), 1fr));
}

/**
 * Width & Height
 */
.w-full { width: 100%; }
.w-auto { width: auto; }
.h-full { height: 100%; }
.h-auto { height: auto; }
.min-h-screen { min-height: 100vh; }
.max-w-xs { max-width: 16rem; }
.max-w-sm { max-width: 24rem; }
/* Clip overflow WITHOUT becoming a scroll container, so a position:sticky child
   (e.g. .form-actions--sticky) can stick to the viewport rather than to the
   card. Opt-in: .card defaults to overflow:hidden, which would trap the sticky. */
.overflow-clip { overflow: clip; }

/**
 * Border Utilities
 */
.rounded { border-radius: var(--radius-md); }
.rounded-sm { border-radius: var(--radius-sm); }
.rounded-lg { border-radius: var(--radius-lg); }
.rounded-xl { border-radius: var(--radius-xl); }
.rounded-full { border-radius: var(--radius-full); }
.rounded-none { border-radius: 0; }

.border { border: 1px solid var(--border-default); }
.border-0 { border: none; }
.border-t { border-top: 1px solid var(--border-default); }
.border-b { border-bottom: 1px solid var(--border-default); }
.border-l { border-left: 1px solid var(--border-default); }
.border-r { border-right: 1px solid var(--border-default); }

/**
 * Background Utilities
 */
.bg-white { background-color: var(--surface-primary); }
.bg-surface { background-color: var(--surface-secondary); }
.bg-muted { background-color: var(--surface-tertiary); }
.bg-transparent { background-color: transparent; }

/**
 * Shadow Utilities
 */
.shadow-none { box-shadow: none; }
.shadow-sm { box-shadow: var(--shadow-sm); }
.shadow { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }
.shadow-xl { box-shadow: var(--shadow-xl); }

/**
 * Position Utilities
 */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }

.inset-0 { inset: 0; }
.top-0 { top: 0; }
.right-0 { right: 0; }
.bottom-0 { bottom: 0; }
.left-0 { left: 0; }

/**
 * Z-Index Utilities
 */
.z-0 { z-index: 0; }
.z-10 { z-index: 10; }
.z-20 { z-index: 20; }
.z-30 { z-index: 30; }
.z-40 { z-index: 40; }
.z-50 { z-index: 50; }

/**
 * Overflow Utilities
 */
.overflow-auto { overflow: auto; }
.overflow-hidden { overflow: hidden; }
.overflow-visible { overflow: visible; }
.overflow-x-auto { overflow-x: auto; }
.overflow-y-auto { overflow-y: auto; }

/**
 * Cursor Utilities
 */
.cursor-pointer { cursor: pointer; }
.cursor-default { cursor: default; }
.cursor-not-allowed { cursor: not-allowed; }

/**
 * Pointer Events
 */
.pointer-events-none { pointer-events: none; }
.pointer-events-auto { pointer-events: auto; }

/**
 * User Select
 */
.select-none { user-select: none; }
.select-text { user-select: text; }
.select-all { user-select: all; }

/**
 * Visibility
 */
.visible { visibility: visible; }
.invisible { visibility: hidden; }

.opacity-0 { opacity: 0; }
.opacity-50 { opacity: 0.5; }
.opacity-75 { opacity: 0.75; }
.opacity-100 { opacity: 1; }


/* ==========================================================================
   12. ACCESSIBILITY
   Screen reader utilities and reduced motion support.
   ========================================================================== */

/**
 * Skip Link
 * Allows keyboard users to skip to main content
 */
.skip-link {
    position: absolute;
    top: -100%;
    left: var(--space-4);
    z-index: var(--z-tooltip);
    padding: var(--space-3) var(--space-4);
    background-color: var(--color-primary);
    color: var(--text-on-primary);
    border-radius: var(--radius-md);
    font-weight: var(--font-weight-medium);
    text-decoration: none;
    transition: top var(--transition-fast);
}

.skip-link:focus {
    top: var(--space-4);
    outline: var(--focus-ring-width) solid var(--text-on-primary);
    outline-offset: var(--focus-ring-offset);
}

/**
 * Screen Reader Only
 * Visually hidden but accessible to screen readers
 */
.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;
}

/* Make element visible when focused (for skip links) */
.sr-only-focusable:focus,
.sr-only-focusable:focus-within {
    position: static;
    width: auto;
    height: auto;
    padding: inherit;
    margin: inherit;
    overflow: visible;
    clip: auto;
    white-space: normal;
}

/**
 * Focus Visible
 * Consistent focus styles for keyboard navigation.
 * The :focus rule provides a fallback for older browsers that don't support
 * :focus-visible. The :focus:not(:focus-visible) rule hides the outline on
 * mouse click in modern browsers, while :focus-visible shows it for keyboard.
 */
:focus {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

:focus:not(:focus-visible) {
    outline: none;
}

:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
}

/**
 * Reduced Motion
 * Respects user preference for reduced motion
 */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    
    .spinner {
        animation: none;
        border-color: var(--color-primary);
        border-top-color: transparent;
    }
    
    .skeleton::after {
        animation: none;
    }
    
    .status-dot-pulse::before {
        animation: none;
    }
}

/**
 * High Contrast Mode Support
 */
@media (forced-colors: active) {
    .btn {
        border: 2px solid currentColor;
    }
    
    .badge,
    .alert {
        border: 1px solid currentColor;
    }
    
    .input,
    .select,
    .textarea,
    .checkbox,
    .radio {
        border: 2px solid currentColor;
    }
}

/**
 * Print Styles
 */
@media print {
    .d-print-none {
        display: none !important;
    }
    
    .d-print-block {
        display: block !important;
    }
    
    .modal-overlay,
    .spinner,
    .skeleton {
        display: none !important;
    }
    
    .btn {
        border: 1px solid currentColor;
    }
}


/* ==========================================================================
   SCROLLABLE TAB STRIPS
   Opt-in helper: keeps a flex tab nav on a single line and scrolls
   horizontally when its labels exceed the viewport width.
   Apply alongside an existing tab nav class, e.g.
   <div class="tabs tabs-scrollable"> or <nav class="admin-nav tabs-scrollable">.
   ========================================================================== */

.tabs-scrollable {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.tabs-scrollable > * {
    flex-shrink: 0;
}

/* ==========================================================================
   PERMISSION MATRIX
   The brigade Positions & Permissions grid: capabilities as grouped rows,
   positions as columns, a toggle cell at each intersection. Deliberately the
   app's one matrix surface — pair with .perm-savebar for dirty-state saves.
   Mobile: scrolls horizontally inside .perm-matrix-scroll with the capability
   column pinned (no card collapse — a matrix has no meaningful card form).
   ========================================================================== */

.perm-matrix-scroll {
    overflow: auto;
    max-height: 72vh;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    background-color: var(--surface-primary);
    -webkit-overflow-scrolling: touch;
}

.perm-matrix {
    border-collapse: separate;
    border-spacing: 0;
    width: max-content;
    min-width: 100%;
    font-size: var(--text-sm);
}

.perm-matrix th,
.perm-matrix td {
    border-bottom: 1px solid var(--border-default);
}

/* Column heads: sticky under the scroll container's top edge */
.perm-matrix thead th {
    position: sticky;
    top: 0;
    z-index: 3;
    background-color: var(--surface-primary);
    padding: var(--space-2) var(--space-2) var(--space-2);
    text-align: center;
    vertical-align: bottom;
    min-width: 84px;
    box-shadow: inset 0 -1px 0 var(--border-default);
}

/* Capability column: sticky against the left edge */
.perm-matrix .perm-rowhead {
    position: sticky;
    left: 0;
    z-index: 2;
    background-color: var(--surface-primary);
    text-align: left;
    min-width: 220px;
    max-width: 300px;
    padding: var(--space-2) var(--space-4);
    border-right: 1px solid var(--border-default);
}

.perm-matrix thead th.perm-rowhead {
    z-index: 4;
}

.perm-matrix .perm-pos-name {
    appearance: none;
    border: none;
    background: transparent;
    font: inherit;
    font-size: var(--text-xs);
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
    cursor: pointer;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    max-width: 110px;
    white-space: normal;
    line-height: var(--line-height-tight);
}

.perm-pos-name:hover {
    background-color: var(--surface-tertiary);
    color: var(--color-primary);
}

.perm-pos-holders {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 24px;
    margin-top: var(--space-1);
}

.perm-pos-vacant {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    font-style: italic;
}

/* Capability row labels */
.perm-cap-label {
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
    font-weight: var(--font-weight-semibold);
    font-size: var(--text-xs);
    color: var(--text-primary);
}

.perm-cap-desc {
    display: block;
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    line-height: var(--line-height-tight);
    margin-top: var(--space-0-5);
}

.perm-feature-tag {
    flex: none;
    font-size: 0.625rem;
    font-weight: var(--font-weight-bold);
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
    color: var(--color-info);
    background-color: var(--color-info-light);
    border-radius: var(--radius-sm);
    padding: 0 var(--space-1);
}

/* Area group rows: full-width, collapsible */
.perm-matrix tr.perm-area th {
    position: sticky;
    left: 0;
    z-index: 2;
    background-color: var(--surface-secondary);
    text-align: left;
    padding: var(--space-1-5) var(--space-4);
    cursor: pointer;
    font-size: var(--text-xs);
    letter-spacing: var(--tracking-wider);
    text-transform: uppercase;
    color: var(--text-secondary);
    user-select: none;
}

.perm-matrix tr.perm-area th .perm-chev {
    display: inline-block;
    width: 14px;
    color: var(--text-tertiary);
    transition: transform var(--transition-fast);
}

.perm-matrix tr.perm-area.collapsed th .perm-chev {
    transform: rotate(-90deg);
}

.perm-matrix tr.perm-area th .perm-area-count {
    color: var(--text-tertiary);
    font-weight: var(--font-weight-normal);
    letter-spacing: var(--tracking-normal);
    text-transform: none;
    margin-left: var(--space-2);
}

/* Toggle cells */
.perm-matrix td.perm-cell {
    text-align: center;
    padding: 0;
}

.perm-matrix td.perm-cell button {
    appearance: none;
    border: none;
    background: transparent;
    cursor: pointer;
    width: 100%;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: transparent;
    font-size: var(--text-sm);
    font-weight: var(--font-weight-bold);
}

.perm-matrix td.perm-cell button:hover {
    background-color: var(--surface-tertiary);
}

.perm-matrix td.perm-cell button[aria-pressed="true"] {
    color: var(--color-success);
}

.perm-matrix td.perm-cell button[aria-pressed="true"]::before {
    content: "\2713";
}

/* A column whose position confers Full brigade admin: locked + tinted */
.perm-matrix td.perm-cell.perm-locked button {
    cursor: default;
    color: var(--color-primary);
    background-color: var(--color-primary-50);
}

.perm-matrix td.perm-cell.perm-locked button::before {
    content: "\2713";
    opacity: 0.85;
}

/* The special Full brigade admin row */
.perm-matrix tr.perm-admin-row .perm-rowhead,
.perm-matrix tr.perm-admin-row td.perm-cell {
    background-color: var(--color-primary-50);
}

.perm-matrix tr.perm-admin-row td.perm-cell button[aria-pressed="true"] {
    color: var(--color-primary);
}

/* Dirty-state save bar: sticks to the viewport bottom while changes pend */
.perm-savebar {
    position: sticky;
    bottom: var(--space-3);
    z-index: var(--z-sticky);
    display: none;
    align-items: center;
    gap: var(--space-3);
    background-color: var(--color-neutral-900);
    color: var(--color-neutral-50);
    padding: var(--space-2-5) var(--space-4);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    margin-top: var(--space-3);
    font-size: var(--text-sm);
}

.dark-mode .perm-savebar {
    background-color: var(--color-neutral-100);
    /* The neutral scale INVERTS in dark mode (--color-neutral-50 is #0F172A
       there, not #F8FAFC), so the base rule's `color: var(--color-neutral-50)`
       would paint near-black text on this dark slab — ~1.2:1, leaving the
       dirty-count label invisible. Re-state the colour for the inverted scale. */
    color: var(--color-neutral-900);
    border: 1px solid var(--border-strong);
}

.perm-savebar.show {
    display: flex;
}

.perm-savebar .perm-savebar-spacer {
    flex: 1;
}

/* ==========================================================================
   AVATAR INITIALS
   Small identity discs (member initials). Deterministic colour via the
   .avatar-c0 … .avatar-c7 classes — pick by hashing the display name.
   ========================================================================== */

.avatar-initials {
    width: var(--avatar-sm);
    height: var(--avatar-sm);
    border-radius: var(--radius-full);
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-xs);
    font-weight: var(--font-weight-bold);
    color: var(--text-on-primary);
    letter-spacing: var(--tracking-wide);
}

.avatar-initials.avatar-xs {
    width: var(--avatar-xs);
    height: var(--avatar-xs);
    font-size: 0.625rem;
}

.avatar-stack {
    display: inline-flex;
}

.avatar-stack .avatar-initials {
    border: 2px solid var(--surface-primary);
}

.avatar-stack .avatar-initials + .avatar-initials {
    margin-left: calc(-1 * var(--space-2));
}

.avatar-c0 { background-color: #DC2626; }
.avatar-c1 { background-color: #0284C7; }
.avatar-c2 { background-color: #16A34A; }
.avatar-c3 { background-color: #7C3AED; }
.avatar-c4 { background-color: #D97706; }
.avatar-c5 { background-color: #0891B2; }
.avatar-c6 { background-color: #DB2777; }
.avatar-c7 { background-color: #64748B; }

/* ==========================================================================
   DRAWER (modal variant)
   A right-hand slide-over panel reusing the whole .modal chassis (open/close
   wiring, header, body, footer). Use via the modal components:
     modal-open  { id, title, modalClass: 'modal-drawer',
                   overlayClass: 'modal-overlay-drawer', ... }
   ≤640px the base bottom-sheet styles apply unchanged.
   ========================================================================== */

@media (min-width: 641px) {
    .modal-overlay.modal-overlay-drawer {
        align-items: stretch;
        justify-content: flex-end;
        padding: 0;
    }

    .modal.modal-drawer {
        max-width: 440px;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
        transform: translateX(100%);
    }

    .modal-overlay.is-open .modal.modal-drawer {
        transform: translateX(0);
    }

    .modal.modal-drawer .modal-body {
        flex: 1;
    }
}

/* ==========================================================================
   STATUS DOT CHIP
   Inline label with a small coloured status dot (member standing, service
   state, …). Compose: <span class="dotchip"><i class="dot-green"></i>Active</span>
   ========================================================================== */

.dotchip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    color: var(--text-secondary);
    white-space: nowrap;
}

.dotchip i {
    width: 7px;
    height: 7px;
    border-radius: var(--radius-full);
    display: inline-block;
    flex: none;
}

.dotchip .dot-green { background-color: var(--status-available); }
.dotchip .dot-amber { background-color: var(--status-responding); }
.dotchip .dot-blue { background-color: var(--color-info); }
.dotchip .dot-gray { background-color: var(--status-unavailable); }
