<?php
/**
 * Slate — UI design system.
 *
 * Dark-sidebar / light-canvas theme. Off-white canvas, white cards on
 * 1px hairline borders (no shadow), blue accent. DM Sans body, Syne
 * display headings, DM Mono code. Hand-crafted vanilla CSS — no build
 * step. Plugins should re-use these classes (.btn, .card, .field)
 * rather than inventing parallel styles.
 *
 * Design philosophy:
 *   - Light, neutral, professional. Closer to a Linear/Stripe-era
 *     admin than to the previous warm-cream Slate.
 *   - Hairline borders over shadows. Cards rest on 1px lines, not
 *     halo shadows. Crisp, modern, prints fine.
 *   - Blue accent reserved for primary actions and active states.
 *   - Native font stack with DM Sans + Syne + DM Mono via Google
 *     Fonts (loaded once from this file's CSS @import).
 *   - 44px minimum tap targets on mobile.
 *   - Real focus rings, accessible.
 *
 * The CSS block at the bottom is gated by SLATE_UI_COMPONENTS_EMITTED
 * so it only appears once per response. The helper functions
 * (slate_data_row, slate_data_list_script, slate_page_layout) are
 * defined unconditionally so callers can use them whether or not
 * the styles have been emitted.
 */

/**
 * Render one data-row inside a .data-list. See PLUGIN-API.md Section 16.
 *
 * $args keys:
 *   title         (string, required) — plain text, will be HTML-escaped
 *   title_html    (string, optional) — raw HTML, used instead of title when set.
 *                  Caller is responsible for escaping any user data inside.
 *   meta          (string, optional secondary line) — plain text, will be escaped
 *   meta_html     (string, optional) — raw HTML version of meta
 *   avatar        (string, 1-2 chars for the initial badge)
 *   avatar_color  (string, optional: accent|success|warning|danger|info|muted)
 *   value         (string, optional pill on the right of summary)
 *   badge         ([label, modifier] for the status badge — preferred over
 *                  putting badge HTML in title_html)
 *   detail        (array of label=>value pairs OR list of
 *                  ['label'=>..., 'value'=>..., 'muted'=>bool] OR
 *                  ['label'=>..., 'html'=>raw_html])
 *   actions       (raw HTML for the bottom action row)
 */
if (!function_exists('slate_data_row')) {
    function slate_data_row(array $args): void {
        $title       = (string)($args['title'] ?? '');
        $titleHtml   = isset($args['title_html']) ? (string)$args['title_html'] : null;
        $meta        = (string)($args['meta']  ?? '');
        $metaHtml    = isset($args['meta_html']) ? (string)$args['meta_html'] : null;
        $avatar      = (string)($args['avatar'] ?? '');
        $avatarColor = (string)($args['avatar_color'] ?? '');
        $value       = $args['value'] ?? null;
        $badge       = $args['badge'] ?? null; // [label, modifier]
        $detail      = (array)($args['detail'] ?? []);
        $actions     = $args['actions'] ?? null;
        $hasDetail   = !empty($detail) || !empty($actions);

        $avatarClass = 'data-row-avatar';
        if ($avatarColor !== '') $avatarClass .= ' is-' . preg_replace('/[^a-z]/', '', strtolower($avatarColor));

        echo '<article class="data-row">';
        echo '<button type="button" class="data-row-summary" aria-expanded="false"'
           . ($hasDetail ? '' : ' disabled') . '>';

        if ($avatar !== '') {
            echo '<span class="' . $avatarClass . '" aria-hidden="true">'
               . e(mb_substr($avatar, 0, 2)) . '</span>';
        }

        echo '<span class="data-row-main">';
        echo   '<span class="data-row-title">'
             . ($titleHtml !== null ? $titleHtml : e($title))
             . '</span>';
        if ($metaHtml !== null) {
            echo '<span class="data-row-meta">' . $metaHtml . '</span>';
        } elseif ($meta !== '') {
            echo '<span class="data-row-meta">' . e($meta) . '</span>';
        }
        echo '</span>';

        if ($value !== null && $value !== '') {
            echo '<span class="data-row-value">' . e((string)$value) . '</span>';
        }

        if (is_array($badge) && isset($badge[0])) {
            $mod = isset($badge[1]) ? preg_replace('/[^a-z0-9_-]/', '', (string)$badge[1]) : '';
            echo '<span class="badge' . ($mod !== '' ? ' badge-' . e($mod) : '') . '">'
               . e((string)$badge[0]) . '</span>';
        }

        if ($hasDetail) {
            echo '<svg class="data-row-chevron" viewBox="0 0 24 24" fill="none" '
               . 'stroke="currentColor" stroke-width="2" stroke-linecap="round" '
               . 'stroke-linejoin="round" aria-hidden="true">'
               . '<polyline points="6 9 12 15 18 9"/></svg>';
        }
        echo '</button>';

        if ($hasDetail) {
            echo '<div class="data-row-detail" hidden>';
            if (!empty($detail)) {
                echo '<dl class="data-row-grid">';
                foreach ($detail as $k => $v) {
                    if (is_array($v)) {
                        $label = (string)($v['label'] ?? $k);
                        if (isset($v['html'])) {
                            echo '<div><dt>' . e($label) . '</dt><dd>' . $v['html'] . '</dd></div>';
                        } else {
                            $cls = !empty($v['muted']) ? ' class="is-muted"' : '';
                            echo '<div><dt>' . e($label) . '</dt><dd' . $cls . '>'
                               . e((string)($v['value'] ?? '')) . '</dd></div>';
                        }
                    } else {
                        echo '<div><dt>' . e((string)$k) . '</dt><dd>' . e((string)$v) . '</dd></div>';
                    }
                }
                echo '</dl>';
            }
            if (!empty($actions)) {
                echo '<div class="data-row-actions">' . $actions . '</div>';
            }
            echo '</div>';
        }

        echo '</article>';
    }
}

/**
 * Emit the JS that drives the collapse/expand behaviour.
 * Call once per page after the .data-list markup. Idempotent.
 */
if (!function_exists('slate_data_list_script')) {
    function slate_data_list_script(): void {
        if (defined('SLATE_DATA_LIST_JS_EMITTED')) return;
        define('SLATE_DATA_LIST_JS_EMITTED', true);
        ?>
        <script>
        (function () {
            if (window.__slateDataList) return;
            window.__slateDataList = true;

            document.addEventListener('click', function (e) {
                var btn = e.target.closest('.data-row-summary');
                if (!btn || btn.disabled) return;
                var row    = btn.closest('.data-row');
                var detail = row.querySelector('.data-row-detail');
                if (!detail) return;
                var list   = row.closest('.data-list');
                var single = list && list.hasAttribute('data-single-open');
                var open   = btn.getAttribute('aria-expanded') === 'true';

                if (!open && single) {
                    list.querySelectorAll('.data-row.is-open').forEach(function (other) {
                        if (other === row) return;
                        other.classList.remove('is-open');
                        var ob = other.querySelector('.data-row-summary');
                        var od = other.querySelector('.data-row-detail');
                        if (ob) ob.setAttribute('aria-expanded', 'false');
                        if (od) od.hidden = true;
                    });
                }

                if (open) {
                    row.classList.remove('is-open');
                    btn.setAttribute('aria-expanded', 'false');
                    detail.hidden = true;
                } else {
                    row.classList.add('is-open');
                    btn.setAttribute('aria-expanded', 'true');
                    detail.hidden = false;
                }
            });
        })();
        </script>
        <?php
    }
}

/**
 * Open a page layout wrapper.
 *
 *   $variant = 'default'     — single-column main content
 *   $variant = 'with-aside'  — main + 320px right rail (CSS grid)
 *
 * Pair with slate_page_layout_end(). Inside a 'with-aside' layout,
 * wrap primary content in <div class="page-main">...</div> and the
 * sidebar metadata in <aside class="page-aside">...</aside>.
 */
if (!function_exists('slate_page_layout')) {
    function slate_page_layout(string $variant = 'default'): void {
        $cls = 'page-layout';
        if ($variant === 'with-aside') $cls .= ' page-layout-with-aside';
        echo '<div class="' . e($cls) . '">';
    }
}
if (!function_exists('slate_page_layout_end')) {
    function slate_page_layout_end(): void {
        echo '</div>';
    }
}

// ── CSS emission (call once per response from templates) ─────
if (!function_exists('slate_ui_emit_css')) {
    function slate_ui_emit_css(): void {
        if (defined('SLATE_UI_COMPONENTS_EMITTED')) return;
        define('SLATE_UI_COMPONENTS_EMITTED', true);
        ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=DM+Sans:wght@400;500;600;700&family=Syne:wght@600;700&display=swap">
<style>
/* ═════════════════════════════════════════════════════════════
   Slate — dark-sidebar / light-canvas theme
   Phase 0 redesign. Off-white canvas, white cards on hairline
   borders, blue accent for primary actions. DM Sans body, Syne
   display, DM Mono code.
   ═════════════════════════════════════════════════════════════ */
:root {
    /* Canvas + surfaces */
    --bg:                #F5F4F1;
    --surface:           #FFFFFF;
    --surface-2:         #FAFAF7;
    --surface-sunken:    #F1F0EC;
    --card:              #FFFFFF;   /* alias for --surface, back-compat */
    --overlay:           rgba(14, 17, 23, 0.45);

    /* Sidebar (dark) */
    --sidebar-bg:        #0E1117;
    --sidebar-border:    rgba(255, 255, 255, 0.07);
    --sidebar-hover:     rgba(255, 255, 255, 0.06);
    --sidebar-active:    rgba(255, 255, 255, 0.10);
    --sidebar-text:      rgba(255, 255, 255, 0.86);
    --sidebar-muted:     rgba(255, 255, 255, 0.55);
    --sidebar-subtle:    rgba(255, 255, 255, 0.40);

    /* Ink ladder (on light surfaces) */
    --text:              #111217;
    --text-2:            #1F2937;
    --muted:             #6B7280;
    --subtle:            #9CA3AF;
    --faint:             #D1D5DB;

    /* Lines */
    --border:            rgba(0, 0, 0, 0.08);
    --border-strong:     rgba(0, 0, 0, 0.13);
    --border-stronger:   rgba(0, 0, 0, 0.20);

    /* Accent — blue, used for primary actions and active states */
    --accent:            #2563EB;
    --accent-deep:       #1D4ED8;
    --accent-soft:       #EFF6FF;
    --accent-hover:      #1D4ED8;
    --on-accent:         #FFFFFF;
    --ring:              rgba(37, 99, 235, 0.18);

    /* Semantic */
    --success:           #16A34A;
    --success-soft:      #ECFDF5;
    --warning:           #D97706;
    --warning-soft:      #FEF3C7;
    --danger:            #DC2626;
    --danger-soft:       #FEE2E2;
    --info:              #2563EB;
    --info-soft:         #EFF6FF;

    /* Radii */
    --radius-sm:         6px;
    --radius:            8px;
    --radius-lg:         12px;
    --radius-xl:         16px;
    --radius-2xl:        20px;
    --radius-full:       9999px;

    /* Shadows — minimal. Cards rely on hairline borders. */
    --shadow-xs:         none;
    --shadow-sm:         0 1px 2px rgba(15, 17, 23, 0.04);
    --shadow:            0 1px 2px rgba(15, 17, 23, 0.04);
    --shadow-md:         0 4px 12px rgba(15, 17, 23, 0.06);
    --shadow-lg:         0 16px 48px rgba(15, 17, 23, 0.18);

    /* Typography */
    --font-sans:         "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI",
                         Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-display:      "Syne", "DM Sans", -apple-system, BlinkMacSystemFont,
                         "Segoe UI", Roboto, sans-serif;
    --font-mono:         "DM Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;

    /* Spacing */
    --space-1:           4px;
    --space-2:           8px;
    --space-3:           12px;
    --space-4:           16px;
    --space-5:           20px;
    --space-6:           24px;
    --space-8:           32px;
    --space-10:          40px;
    --space-12:          48px;

    /* Layout */
    --sidebar-width:     232px;
    --topbar-height:     56px;
    --tabbar-height:     58px;
    --content-max:       1280px;
    --right-rail-width:  320px;
}

/* ─── Reset & base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
    font-family: var(--font-sans);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    font-feature-settings: "ss01", "tnum";
    overflow-x: hidden;
}

a {
    color: var(--accent);
    text-decoration: none;
    transition: color 0.15s ease;
}
a:hover { color: var(--accent-deep); }

/* Accent link helper (back-compat) */
a.link-accent { color: var(--accent); }
a.link-accent:hover { color: var(--accent-hover); }

h1, h2, h3, h4, h5 {
    margin: 0 0 0.5em;
    font-weight: 600;
    letter-spacing: -0.015em;
    color: var(--text);
    font-family: var(--font-display);
}
h1 { font-size: 22px; line-height: 1.15; }
h2 { font-size: 16px; letter-spacing: -0.01em; line-height: 1.3; }
h3 { font-size: 14px; line-height: 1.4; font-family: var(--font-sans); }
h4 { font-size: 13px; line-height: 1.4; color: var(--text-2); font-family: var(--font-sans); }

p { margin: 0 0 1em; }
p:last-child { margin-bottom: 0; }

code, pre {
    font-family: var(--font-mono);
    font-size: 0.92em;
}
code {
    background: var(--surface-2);
    padding: 1px 6px;
    border-radius: var(--radius-sm);
    color: var(--text-2);
    font-size: 12px;
    border: 1px solid var(--border);
}

hr {
    border: 0;
    height: 1px;
    background: var(--border);
    margin: var(--space-6) 0;
}

.section-divider {
    border-top: 1px solid var(--border);
    margin: var(--space-6) 0;
}

/* ─── Cards ────────────────────────────────────────────── */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 18px;
    box-shadow: none;
    margin-bottom: var(--space-4);
}
.card.tight { padding: 12px; }

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    margin: 0 0 var(--space-3);
}
.card-header h2,
.card-header h3 { margin: 0; font-size: 14px; font-weight: 600; letter-spacing: -0.01em; font-family: var(--font-sans); }

.card-muted { color: var(--muted); font-size: 12px; }
.card-sub { color: var(--muted); font-size: 11.5px; }

.card-link {
    display: block;
    text-decoration: none;
    color: inherit;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.card-link:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-sm);
    color: inherit;
}

/* ─── Stat cards ───────────────────────────────────────── */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 10px;
    margin-bottom: var(--space-5);
}
.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 14px 14px 16px;
    position: relative;
    overflow: hidden;
    transition: border-color 160ms ease;
}
.stat:hover { border-color: var(--border-strong); }
.stat-label {
    font-size: 10.5px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: var(--space-2);
}
.stat-value {
    font-family: var(--font-display);
    font-size: 28px;
    font-weight: 600;
    letter-spacing: -0.025em;
    color: var(--text);
    line-height: 1.05;
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum";
}
.stat-sub {
    font-size: 11.5px;
    color: var(--muted);
    margin-top: 4px;
    font-weight: 500;
}

/* ─── Buttons ──────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 34px;
    padding: 7px 12px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    line-height: 1.2;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    white-space: nowrap;
}
.btn:hover {
    background: var(--surface);
    border-color: var(--border-stronger);
    color: var(--text);
    text-decoration: none;
}
.btn:active { background: var(--surface-2); }
.btn:focus-visible {
    outline: 0;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--ring);
}
.btn:disabled, .btn[disabled] {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
}

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--on-accent);
}
.btn-primary:hover {
    background: var(--accent-deep);
    border-color: var(--accent-deep);
    color: var(--on-accent);
}
.btn-primary:active { background: var(--accent-deep); }
.btn-primary:focus-visible {
    box-shadow: 0 0 0 3px var(--ring);
}

.btn-danger {
    color: var(--danger);
    background: var(--surface);
    border-color: var(--danger);
}
.btn-danger:hover {
    background: var(--danger-soft);
    color: var(--danger);
    border-color: var(--danger);
}

.btn-ghost {
    background: transparent;
    color: var(--muted);
    border-color: transparent;
}
.btn-ghost:hover {
    background: var(--surface-2);
    color: var(--text);
    border-color: transparent;
}

.btn-sm { min-height: 28px; padding: 4px 10px; font-size: 12px; border-radius: var(--radius-sm); }
.btn-lg { min-height: 42px; padding: 10px 16px; font-size: 14px; border-radius: var(--radius); }
.btn-block { display: flex; width: 100%; }

.btn-link {
    background: none;
    border: 0;
    padding: 0;
    color: var(--accent);
    font: inherit;
    cursor: pointer;
    min-height: 0;
}
.btn-link:hover { background: none; color: var(--accent-deep); text-decoration: underline; }

/* ─── Forms ────────────────────────────────────────────── */
.field { margin-bottom: var(--space-4); }
.field-label {
    display: block;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-2);
    margin-bottom: 6px;
    letter-spacing: 0;
}
.field-required { color: var(--danger); }

.input,
.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="number"],
.field input[type="url"],
.field input[type="search"],
.field input[type="tel"],
.field input[type="date"],
.field input[type="datetime-local"],
.field input[type="time"],
.field select,
.field textarea {
    display: block;
    width: 100%;
    min-height: 36px;
    padding: 8px 11px;
    font: inherit;
    font-size: 13px;
    line-height: 1.4;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    outline: none;
    transition: border-color 0.12s ease, box-shadow 0.12s ease;
    -webkit-appearance: none;
    appearance: none;
}
.field textarea { min-height: 88px; resize: vertical; line-height: 1.5; }
.field input::placeholder,
.field textarea::placeholder { color: var(--subtle); }

.field input:hover,
.field select:hover,
.field textarea:hover,
.input:hover { border-color: var(--border-stronger); }
.field input:focus,
.field select:focus,
.field textarea:focus,
.input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--ring);
}

.field select {
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'><path d='M1 1.5L6 6.5L11 1.5' stroke='%236B7280' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/></svg>");
    background-repeat: no-repeat;
    background-position: right 11px center;
    padding-right: 32px;
}

.field-hint {
    font-size: 11.5px;
    color: var(--muted);
    margin-top: 6px;
}
.field-error {
    font-size: 11.5px;
    color: var(--danger);
    margin-top: 6px;
}

.field-row {
    display: grid;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
}
@media (min-width: 640px) {
    .field-row-2 { grid-template-columns: 1fr 1fr; }
    .field-row-3 { grid-template-columns: repeat(3, 1fr); }
}

.field-row-variant {
    grid-template-columns: 1fr 1fr;
}
@media (min-width: 640px) {
    .field-row-variant { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 960px) {
    .field-row-variant { grid-template-columns: repeat(5, 1fr); }
}

/* ─── Filter bar ──────────────────────────────────────── */
.filter-row {
    display: grid;
    gap: var(--space-2);
    align-items: end;
    grid-template-columns: 1fr;
}
@media (min-width: 640px) {
    .filter-row {
        grid-template-columns: repeat(2, 1fr) auto;
    }
    .filter-row .filter-search { grid-column: 1 / -1; }
    .filter-row .filter-actions {
        grid-column: 1 / -1;
        display: flex;
        gap: var(--space-2);
        justify-content: flex-end;
    }
}
@media (min-width: 960px) {
    .filter-row {
        grid-template-columns: 2fr 1fr 1fr auto;
    }
    .filter-row .filter-search { grid-column: auto; }
    .filter-row .filter-actions {
        grid-column: auto;
        justify-content: flex-start;
    }
}
.filter-row .field { margin: 0; }

/* ─── Toolbar ─────────────────────────────────────────── */
.toolbar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
}
.toolbar > * { flex-shrink: 0; }
.toolbar-spacer { flex: 1; min-width: 0; }

/* ─── Split layout (main + side meta, back-compat) ───── */
.split-layout {
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
}
@media (min-width: 960px) {
    .split-layout {
        grid-template-columns: 2fr 1fr;
        align-items: flex-start;
    }
}

/* ─── Page layout w/ right rail (canonical, Phase 0+) ───
   slate_page_layout('with-aside') opens .page-layout
   .page-layout-with-aside. Inside, the primary column is
   <div class="page-main"> and the rail is
   <aside class="page-aside"> holding .aside-card blocks. */
.page-layout { display: block; }
.page-layout-with-aside {
    display: grid;
    gap: var(--space-6);
    grid-template-columns: 1fr;
    align-items: flex-start;
}
@media (min-width: 1024px) {
    .page-layout-with-aside {
        grid-template-columns: minmax(0, 1fr) var(--right-rail-width);
    }
}
.page-main { min-width: 0; }
.page-aside {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}
.aside-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: none;
}
.aside-card-title {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--subtle);
    margin: 0 0 var(--space-3);
}

/* ─── KV list (label/value pairs, hairline dividers) ─── */
.kv-list {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
}
.kv-row {
    display: flex;
    justify-content: space-between;
    gap: var(--space-4);
    padding: 9px 0;
    border-bottom: 1px solid var(--border);
    font-size: 12.5px;
    line-height: 1.45;
}
.kv-row:first-child { padding-top: 0; }
.kv-row:last-child  { border-bottom: 0; padding-bottom: 0; }
.kv-row .kv-label {
    color: var(--muted);
    font-weight: 500;
    flex-shrink: 0;
}
.kv-row .kv-value {
    color: var(--text);
    text-align: right;
    min-width: 0;
    overflow-wrap: break-word;
    word-break: break-word;
}
.kv-row .kv-value.kv-mono {
    font-family: var(--font-mono);
    font-size: 12px;
}

/* ─── Audit trail (vertical timeline) ─────────────────── */
.audit-trail {
    list-style: none;
    margin: 0;
    padding: 0;
    position: relative;
}
.audit-trail::before {
    content: '';
    position: absolute;
    left: 5px;
    top: 8px;
    bottom: 8px;
    width: 1px;
    background: var(--border-strong);
}
.audit-trail-item {
    position: relative;
    padding: 0 0 14px 22px;
}
.audit-trail-item:last-child { padding-bottom: 0; }
.audit-trail-item::before {
    content: '';
    position: absolute;
    left: 0;
    top: 4px;
    width: 11px;
    height: 11px;
    border-radius: 999px;
    background: var(--surface);
    border: 2px solid var(--accent);
    box-sizing: border-box;
}
.audit-trail-item.is-muted::before { border-color: var(--faint); }
.audit-trail-action {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text);
    line-height: 1.35;
}
.audit-trail-meta {
    font-size: 11.5px;
    color: var(--muted);
    margin-top: 2px;
    line-height: 1.4;
}
.audit-trail-detail {
    font-size: 12px;
    color: var(--text-2);
    margin-top: 4px;
    line-height: 1.5;
}

/* ─── Tables ──────────────────────────────────────────── */
.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(-1 * 18px);
    padding: 0 18px;
}
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.table th,
.table td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.table th {
    font-weight: 600;
    font-size: 10.5px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    background: var(--surface-2);
}
.table th:first-child { border-top-left-radius: var(--radius-sm); }
.table th:last-child  { border-top-right-radius: var(--radius-sm); }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }

/* ─── Alerts ──────────────────────────────────────────── */
.alert {
    display: flex;
    gap: var(--space-3);
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: var(--space-4);
    font-size: 13px;
    line-height: 1.5;
    align-items: flex-start;
}
.alert-success { background: var(--success-soft); border-color: rgba(22,163,74,0.18); color: #14532D; }
.alert-error   { background: var(--danger-soft);  border-color: rgba(220,38,38,0.20);  color: #7F1D1D; }
.alert-warning { background: var(--warning-soft); border-color: rgba(217,119,6,0.22);  color: #78350F; }
.alert-info    { background: var(--info-soft);    border-color: rgba(37,99,235,0.18);  color: #1E3A8A; }

/* ─── Badges ──────────────────────────────────────────── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    font-size: 10.5px;
    font-weight: 500;
    line-height: 1.5;
    border-radius: 999px;
    background: var(--surface);
    color: var(--text-2);
    border: 1px solid var(--border);
}
.badge .dot { width: 5px; height: 5px; border-radius: 999px; background: currentColor; }
.badge-active    { background: var(--success-soft); color: #14532D;    border-color: transparent; }
.badge-inactive  { background: var(--surface-2);    color: var(--muted); border-color: transparent; }
.badge-installed { background: var(--info-soft);    color: #1E3A8A;    border-color: transparent; }
.badge-warning   { background: var(--warning-soft); color: #78350F;    border-color: transparent; }
.badge-danger    { background: var(--danger-soft);  color: #7F1D1D;    border-color: transparent; }
.badge-accent    { background: var(--accent-soft);  color: var(--accent-deep); border-color: transparent; }

/* Mono pill tag */
.tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 7px;
    font-family: var(--font-mono);
    font-size: 10.5px;
    background: var(--surface);
    color: var(--text-2);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}
.tag.danger { color: var(--danger); }
.tag.ok { color: var(--success); }

/* ─── Page header ─────────────────────────────────────── */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    margin-bottom: var(--space-5);
    flex-wrap: wrap;
}
.page-header h1 { margin: 0; font-family: var(--font-display); }
.page-header-sub {
    color: var(--muted);
    margin: 3px 0 0;
    font-size: 13px;
}

/* ─── Empty state ─────────────────────────────────────── */
.empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--muted);
}
.empty-icon {
    width: 44px;
    height: 44px;
    margin: 0 auto 10px;
    border-radius: var(--radius-lg);
    background: var(--surface-2);
    color: var(--muted);
    display: grid;
    place-items: center;
    border: 1px solid var(--border);
}
.empty-icon svg { width: 22px; height: 22px; opacity: 0.6; }
.empty-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 4px;
}

/* ─── Segmented control ───────────────────────────────── */
.segmented {
    display: inline-flex;
    padding: 2px;
    background: var(--surface-2);
    border-radius: var(--radius);
    gap: 0;
    border: 1px solid var(--border);
}
.segmented button, .segmented .seg-btn {
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 500;
    border-radius: var(--radius-sm);
    color: var(--muted);
    background: transparent;
    border: 0;
    cursor: pointer;
    transition: all 140ms ease;
}
.segmented button.on, .segmented .seg-btn.on {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}
.segmented button:not(.on):hover, .segmented .seg-btn:not(.on):hover {
    color: var(--text-2);
    background: rgba(255,255,255,0.6);
}

/* ─── Dropzone ────────────────────────────────────────── */
.dropzone {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 24px 16px;
    text-align: center;
    border: 1px dashed var(--border-stronger);
    cursor: pointer;
    transition: border-color 160ms ease, background 160ms ease;
}
.dropzone:hover { border-color: var(--accent); background: var(--accent-soft); }

/* ─── Snippet (code block) ────────────────────────────── */
.snippet {
    font-family: var(--font-mono);
    font-size: 12px;
    background: #0E1117;
    color: #D8DAE0;
    padding: 12px 14px;
    border-radius: var(--radius);
    line-height: 1.55;
    overflow: auto;
    margin: 0;
    border: 1px solid #1F2329;
}

/* ─── Switch ──────────────────────────────────────────── */
.switch {
    position: relative;
    width: 32px; height: 18px;
    border-radius: 999px;
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    transition: background 160ms ease, border-color 160ms ease;
    flex-shrink: 0;
    cursor: pointer;
    display: inline-block;
    padding: 0;
}
.switch::after {
    content: '';
    position: absolute;
    top: 1px; left: 1px;
    width: 14px; height: 14px;
    border-radius: 999px;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0,0,0,0.15);
    transition: transform 180ms cubic-bezier(.4,0,.2,1);
}
.switch.on { background: var(--accent); border-color: var(--accent); }
.switch.on::after { transform: translateX(14px); }

/* ─── Utilities ───────────────────────────────────────── */
.text-muted   { color: var(--muted); }
.text-sub     { color: var(--text-2); }
.text-danger  { color: var(--danger); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }
.text-accent  { color: var(--accent); }
.text-sm      { font-size: 12.5px; }
.text-xs      { font-size: 11.5px; }
.text-right   { text-align: right; }
.text-center  { text-align: center; }
.text-mono    { font-family: var(--font-mono); font-size: 12px; }

.eyebrow {
    font-size: 11px;
    letter-spacing: 0.06em;
    color: var(--subtle);
    font-weight: 600;
    text-transform: uppercase;
}

.flex         { display: flex; }
.flex-col     { display: flex; flex-direction: column; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.flex-center  { display: flex; align-items: center; justify-content: center; }
.items-center { align-items: center; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }

.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-6 { margin-top: var(--space-6); }
.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-6 { margin-bottom: var(--space-6); }

.hidden { display: none; }
.w-full { width: 100%; }

@media (max-width: 767px) { .hide-mobile { display: none !important; } }
@media (min-width: 768px) { .show-mobile { display: none !important; } }

/* ─── Flash (sticky page-top notification) ───────────── */
.flash {
    position: sticky;
    top: var(--topbar-height);
    z-index: 20;
}

/* ─── Skip link (a11y) ────────────────────────────────── */
.skip-link {
    position: absolute;
    top: -100px;
    left: 8px;
    background: var(--text);
    color: #fff;
    padding: 8px 14px;
    border-radius: var(--radius);
    font-size: 13px;
    z-index: 100;
    transition: top 0.15s ease;
}
.skip-link:focus { top: 8px; color: #fff; }

/* ─── Data list (collapsible card-rows) ──────────────── */
.data-list {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    background: var(--surface);
    width: 100%;
}

.data-row {
    background: var(--surface);
    border-radius: 0;
    overflow: hidden;
    transition: background 0.12s ease;
    width: 100%;
}
.data-row:not(:first-child) { border-top: 1px solid var(--border); }
.data-row.is-open { background: var(--surface-2); }

.data-row-summary {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    padding: 12px 14px;
    background: transparent;
    border: 0;
    font: inherit;
    color: var(--text);
    text-align: left;
    cursor: pointer;
    min-height: 56px;
    -webkit-tap-highlight-color: transparent;
}
.data-row-summary:hover { background: var(--surface-2); }
.data-row.is-open .data-row-summary { background: transparent; }

.data-row-avatar {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 11.5px;
    letter-spacing: 0.02em;
    background: var(--accent-soft);
    color: var(--accent-deep);
    text-transform: uppercase;
}
.data-row-avatar.is-success { background: var(--success-soft); color: #14532D; }
.data-row-avatar.is-warning { background: var(--warning-soft); color: #78350F; }
.data-row-avatar.is-danger  { background: var(--danger-soft);  color: #7F1D1D; }
.data-row-avatar.is-info    { background: var(--info-soft);    color: #1E3A8A; }
.data-row-avatar.is-muted   { background: var(--surface-2);    color: var(--muted); }

.data-row-main {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
    min-width: 0;
}
.data-row-title {
    font-weight: 600;
    font-size: 13.5px;
    color: var(--text);
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.data-row-meta {
    font-size: 12px;
    color: var(--muted);
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.data-row-value {
    flex-shrink: 0;
    padding: 2px 10px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: 11.5px;
    font-weight: 500;
    color: var(--text-2);
    white-space: nowrap;
}

.data-row-summary > .badge {
    flex-shrink: 0;
    white-space: nowrap;
}

.data-row-chevron {
    flex-shrink: 0;
    width: 18px;
    height: 18px;
    color: var(--subtle);
    transition: transform 0.22s cubic-bezier(.4,0,.2,1), color 0.12s;
}
.data-row.is-open .data-row-chevron {
    transform: rotate(90deg);
    color: var(--text-2);
}

.data-row-detail {
    border-top: 1px solid var(--border);
    padding: 14px 18px 16px 60px;
    background: var(--surface-2);
}
.data-row-detail[hidden] { display: none; }

.data-row-grid {
    margin: 0;
    display: grid;
    gap: 12px 24px;
    grid-template-columns: repeat(2, 1fr);
}
.data-row-grid > div { min-width: 0; }
.data-row-grid dt {
    font-size: 10.5px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 3px;
}
.data-row-grid dd {
    margin: 0;
    font-size: 12.5px;
    color: var(--text);
    overflow-wrap: break-word;
    word-break: break-word;
}
.data-row-grid dd.is-muted { color: var(--muted); }

.data-row-progress {
    margin-top: 6px;
    height: 4px;
    background: var(--surface-2);
    border-radius: 999px;
    overflow: hidden;
}
.data-row-progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: inherit;
    transition: width 0.3s ease;
}

.data-row-actions {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
}

.data-list .empty {
    background: var(--surface);
}

@media (max-width: 960px) {
    .card-header {
        flex-wrap: wrap;
        row-gap: var(--space-2);
    }
    .card-header .flex,
    .card-header .toolbar {
        flex-wrap: wrap;
    }
    .page-header .flex.gap-2,
    .page-header .toolbar {
        flex-wrap: wrap;
    }
}

@media (max-width: 640px) {
    .data-row-summary { gap: var(--space-2); padding: 12px; }
    .data-row-value { display: none; }
    .data-row-grid { grid-template-columns: 1fr 1fr; }
    .data-row-detail { padding: 12px 14px 14px; }

    .card-header .flex,
    .card-header .toolbar {
        width: 100%;
    }
    .page-header .flex.gap-2,
    .page-header .toolbar {
        width: 100%;
    }

    .data-row-summary .badge {
        font-size: 10px;
        padding: 2px 7px;
        flex-shrink: 0;
    }

    .data-row-detail { padding-left: 14px; }
}
@media (max-width: 380px) {
    .data-row-meta { font-size: 11px; }
    .data-row-grid { grid-template-columns: 1fr; }
    .data-row-summary .badge {
        font-size: 9.5px;
        padding: 1px 5px;
    }
}

/* ─── Mobile typography bumps ──────────────────────────── */
@media (max-width: 767px) {
    h1 { font-size: 26px; font-weight: 700; line-height: 1.1; }
    .page-header { flex-direction: column; align-items: flex-start; gap: 4px; }
    .stat-value { font-size: 22px; }
    .card { border-radius: var(--radius-lg); padding: 14px; }
    .btn { min-height: 40px; padding: 10px 14px; font-size: 13.5px; border-radius: var(--radius); }
    .btn-sm { min-height: 30px; padding: 6px 10px; font-size: 12px; }
    .field input[type="text"],
    .field input[type="email"],
    .field input[type="password"],
    .field input[type="number"],
    .field input[type="search"],
    .field select,
    .field textarea {
        min-height: 44px; padding: 11px 12px; font-size: 14px; border-radius: var(--radius);
    }
    .kv-row .kv-value { text-align: left; }
    .kv-row { flex-direction: column; gap: 2px; }
}
</style>
        <?php
    }
}
