<?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>';
    }
}

/**
 * Render one app-style "recent list" row inside a .dlist. Used by dashboard
 * plugin widgets to show latest records (orders, submissions, charges, …).
 * $a: title, sub, avatar, avatar_color (success|danger|warning|info|muted),
 *     amount, time, href (whole row becomes a link).
 */
if (!function_exists('slate_dlist_row')) {
    function slate_dlist_row(array $a): void {
        $href = isset($a['href']) ? (string)$a['href'] : '';
        $tag  = $href !== '' ? 'a' : 'div';
        $ava  = (string)($a['avatar'] ?? '');
        $cls  = 'dlist-ava';
        if (!empty($a['avatar_color'])) $cls .= ' is-' . preg_replace('/[^a-z]/', '', strtolower((string)$a['avatar_color']));
        echo '<' . $tag . ' class="dlist-row"' . ($href !== '' ? ' href="' . e($href) . '"' : '') . '>';
        if ($ava !== '') echo '<span class="' . $cls . '">' . e(mb_substr($ava, 0, 2)) . '</span>';
        echo '<span class="dlist-body">';
        echo   '<span class="dlist-title">' . e((string)($a['title'] ?? '')) . '</span>';
        if (isset($a['sub']) && $a['sub'] !== '') echo '<span class="dlist-sub">' . e((string)$a['sub']) . '</span>';
        echo '</span>';
        $amount = (string)($a['amount'] ?? '');
        $time   = (string)($a['time'] ?? '');
        if ($amount !== '' || $time !== '') {
            echo '<span class="dlist-trail">';
            if ($amount !== '') echo '<span class="dlist-amount">' . e($amount) . '</span>';
            if ($time !== '')   echo '<span class="dlist-time">' . e($time) . '</span>';
            echo '</span>';
        }
        echo '</' . $tag . '>';
    }
}

/**
 * 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>';
    }
}

/**
 * Reusable, compact pagination control.
 *
 *   slate_pagination($page, $totalPages, $_GET, ['total' => $count, 'label' => 'entries']);
 *
 * Preserves existing query params (minus `page`), renders Prev / numbered
 * pages with ellipses / Next, and an optional "X–Y of N" summary. No-ops
 * when there's a single page.
 */
if (!function_exists('slate_pagination')) {
    function slate_pagination(int $page, int $totalPages, array $queryParams = [], array $opts = []): void {
        $totalPages = max(1, $totalPages);
        $page       = max(1, min($totalPages, $page));

        $url = static function (int $p) use ($queryParams): string {
            $q = $queryParams;
            unset($q['page']);
            $q['page'] = $p;
            return '?' . http_build_query($q);
        };

        // Build the window of page numbers with ellipses.
        $pages = [];
        $win   = 1; // neighbours either side of current
        for ($p = 1; $p <= $totalPages; $p++) {
            if ($p === 1 || $p === $totalPages || ($p >= $page - $win && $p <= $page + $win)) {
                $pages[] = $p;
            } elseif (end($pages) !== '…') {
                $pages[] = '…';
            }
        }

        echo '<nav class="slate-pager" aria-label="Pagination">';

        if (!empty($opts['total'])) {
            $perPage = (int)($opts['per_page'] ?? 0);
            $label   = (string)($opts['label'] ?? 'items');
            $total   = (int)$opts['total'];
            if ($perPage > 0 && $total > 0) {
                $start = ($page - 1) * $perPage + 1;
                $end   = min($total, $page * $perPage);
                echo '<span class="slate-pager-summary">' . $start . '–' . $end
                   . ' of ' . number_format($total) . ' ' . e($label) . '</span>';
            } else {
                echo '<span class="slate-pager-summary">' . number_format($total) . ' ' . e($label) . '</span>';
            }
        }

        if ($totalPages > 1) {
            echo '<div class="slate-pager-pages">';
            // Prev
            if ($page > 1) {
                echo '<a class="slate-pager-btn" rel="prev" aria-label="Previous page" href="' . e($url($page - 1)) . '">‹</a>';
            } else {
                echo '<span class="slate-pager-btn is-disabled" aria-hidden="true">‹</span>';
            }
            foreach ($pages as $p) {
                if ($p === '…') {
                    echo '<span class="slate-pager-gap">…</span>';
                } elseif ($p === $page) {
                    echo '<span class="slate-pager-btn is-current" aria-current="page">' . $p . '</span>';
                } else {
                    echo '<a class="slate-pager-btn" href="' . e($url((int)$p)) . '">' . $p . '</a>';
                }
            }
            // Next
            if ($page < $totalPages) {
                echo '<a class="slate-pager-btn" rel="next" aria-label="Next page" href="' . e($url($page + 1)) . '">›</a>';
            } else {
                echo '<span class="slate-pager-btn is-disabled" aria-hidden="true">›</span>';
            }
            echo '</div>';
        }

        echo '</nav>';
    }
}

// ── 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=Inter:wght@400;500;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 — premium light theme. A soft off-white canvas lets
       pure-white cards lift with a whisper of depth (no heavy shadows), while
       muted cool-grey secondary surfaces keep table headers, hovers, inputs
       and filters legible. */
    --bg:                #FAFAFA;
    --surface:           #FFFFFF;
    --surface-2:         #F5F6F8;
    --surface-sunken:    #EEF0F2;
    --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) — slightly muted, slate-leaning. */
    --text:              #16181D;
    --text-2:            #3F4450;
    --muted:             #71757E;
    --subtle:            #A2A6AE;
    --faint:             #D3D5DA;

    /* Lines — solid premium greys (crisper than translucent black). */
    --border:            #ECEDEF;
    --border-strong:     #E0E2E6;
    --border-stronger:   #CFD2D8;

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

    /* 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 */
    /* One cohesive typeface across the whole UI — Inter, the premium
       dashboard standard. Headings differ by weight + tracking only, so
       nothing clashes with the muted palette. */
    --font-sans:         "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI",
                         Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-display:      "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI",
                         Roboto, "Helvetica Neue", Arial, 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;
}

/* Scrollbars hidden entirely across the UI — scrolling still works (wheel,
   trackpad, touch, keyboard), there's just no visible scrollbar chrome. */
* { scrollbar-width: none; -ms-overflow-style: none; }
*::-webkit-scrollbar { width: 0; height: 0; display: none; }
html, body { scrollbar-width: none; -ms-overflow-style: none; }
html::-webkit-scrollbar, body::-webkit-scrollbar { width: 0; height: 0; display: none; }

/* ── Dashboard plugin widgets — dense KPI cells that fill the card width
   (no sprawling gaps). Used by the Shop / Forms / Booking dashboard cards. */
/* One unified strip split into equal segments by hairline dividers (instead
   of separate rounded boxes that look busy/cramped). All KPIs share the width
   evenly on a single row — no gaps, no half-empty wrapped cell. */
.dwidget-kpis {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 1px;
    margin: 2px 0 14px;
    background: var(--border);
    border: 1px solid var(--border);
    border-radius: 11px;
    overflow: hidden;
}
.dwidget-kpi {
    min-width: 0;
    background: var(--surface-2);
    padding: 10px 13px;
}
.dwidget-kpi-v { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dwidget-kpi-k {
    font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.08em;
    text-transform: uppercase; color: var(--subtle);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dwidget-kpi-v {
    font-size: 20px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.2;
    font-variant-numeric: tabular-nums; margin-top: 5px;
}
.dwidget-kpi-note { font-size: 11px; margin-top: 3px; color: var(--muted); }
.dwidget-actions { display: flex; gap: 8px; flex-wrap: wrap; }
/* Header "view all" link, right-aligned in the card-header (fills the header). */
.dwidget-all {
    font-size: 12px; font-weight: 600; color: var(--accent);
    white-space: nowrap; flex: none;
}
.dwidget-all:hover { color: var(--accent-deep); text-decoration: none; }

/* ── App-style "recent list" — latest records inside a dashboard widget.
   Each row spans the full card width: avatar · title/sub · amount/time. */
.dlist { list-style: none; margin: 6px 0 0; padding: 0; }
.dlist-row {
    display: flex; align-items: center; gap: 11px;
    padding: 9px 8px; margin: 0 -8px;
    border-radius: 9px;
    color: var(--text); text-decoration: none;
    transition: background .12s ease;
}
.dlist-row + .dlist-row { box-shadow: 0 -1px 0 var(--border); }
a.dlist-row:hover { background: var(--surface-2); box-shadow: none; }
a.dlist-row:hover + .dlist-row { box-shadow: none; }
.dlist-ava {
    flex: none; width: 36px; height: 36px; border-radius: 10px;
    display: grid; place-items: center; font-weight: 700; font-size: 12px;
    text-transform: uppercase; letter-spacing: -.01em;
    background: var(--accent-soft); color: var(--accent);
}
.dlist-ava.is-success { background: var(--success-soft); color: var(--success); }
.dlist-ava.is-danger  { background: var(--danger-soft);  color: var(--danger); }
.dlist-ava.is-warning { background: var(--warning-soft); color: var(--warning); }
.dlist-ava.is-info    { background: var(--info-soft);    color: var(--info); }
.dlist-ava.is-muted   { background: var(--surface-sunken); color: var(--muted); }
.dlist-body { flex: 1 1 auto; min-width: 0; }
.dlist-title, .dlist-sub { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dlist-title { font-size: 13px; font-weight: 600; letter-spacing: -.01em; }
.dlist-sub { font-size: 11.5px; color: var(--muted); margin-top: 1px; }
.dlist-trail {
    flex: none; max-width: 44%;
    display: flex; flex-direction: column; align-items: flex-end; gap: 2px;
    text-align: right;
}
.dlist-amount { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.dlist-time { font-size: 10.5px; color: var(--subtle); font-family: var(--font-mono); white-space: nowrap; }
.dlist-empty { font-size: 12.5px; color: var(--muted); text-align: center; padding: 16px 0; }
.dwidget-subhead {
    font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.08em;
    text-transform: uppercase; color: var(--subtle); margin: 4px 2px 2px;
}

/* ── Recent-activity lists (app-style rows) used inside dashboard widgets.
   Leading avatar/icon · two-line body · trailing amount + time. Truncates,
   tap-feedback, comfortable touch targets on mobile. */
.dlist { list-style: none; margin: 0; padding: 0; }
.dlist-row {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 6px; border-bottom: 1px solid var(--border);
    border-radius: 10px; color: var(--text); text-decoration: none;
    transition: background .14s ease;
}
.dlist-row:last-child { border-bottom: 0; }
a.dlist-row:hover { background: var(--surface-2); text-decoration: none; }
a.dlist-row:active { background: var(--surface-sunken); }
.dlist-ava {
    flex: none; width: 38px; height: 38px; border-radius: 11px;
    display: grid; place-items: center;
    font-size: 13px; font-weight: 700; letter-spacing: -.02em; text-transform: uppercase;
    background: var(--accent-soft); color: var(--accent);
}
.dlist-ava svg { width: 18px; height: 18px; color: var(--accent); }
.dlist-body { flex: 1 1 auto; min-width: 0; }
.dlist-title {
    font-size: 13px; font-weight: 600; color: var(--text);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dlist-sub {
    font-size: 11.5px; color: var(--muted); margin-top: 2px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dlist-trail {
    flex: none; display: flex; flex-direction: column; align-items: flex-end;
    gap: 3px; text-align: right; max-width: 46%;
}
.dlist-amount {
    font-size: 13px; font-weight: 700; color: var(--text);
    font-variant-numeric: tabular-nums; white-space: nowrap;
}
.dlist-time { font-size: 10.5px; color: var(--subtle); font-family: var(--font-mono); white-space: nowrap; }
.dlist-trail .badge { font-size: 9.5px; }
.dlist-empty { padding: 16px 6px; text-align: center; color: var(--muted); font-size: 12.5px; }

@media (max-width: 600px) {
    .dlist-row { padding: 12px 6px; gap: 12px; }
    .dlist-ava { width: 42px; height: 42px; border-radius: 12px; }
}

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: 20px; line-height: 1.2; font-weight: 600; letter-spacing: -0.012em; color: var(--text-2); }
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); }
/* Headings ease down on phones so they don't overpower small screens. */
@media (max-width: 600px) {
    h1 { font-size: 20px; }
    h2 { font-size: 15px; }
}

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;
    /* Whisper-soft elevation so white cards read on the white canvas. */
    box-shadow: 0 1px 2px rgba(15,17,23,0.04), 0 1px 1px rgba(15,17,23,0.03);
    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: 38px;
    padding: 8px 14px;
    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, box-shadow 0.15s ease, transform 0.05s ease;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    white-space: nowrap;
}
.btn:hover {
    background: var(--surface-2);
    border-color: var(--border-stronger);
    color: var(--text);
    text-decoration: none;
}
.btn:active { background: var(--surface-2); transform: translateY(1px); }
.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);
    box-shadow: 0 2px 8px rgba(37,99,235,0.25);
}
.btn-primary:active { background: var(--accent-deep); }
/* Filled buttons need a high-contrast ring — the faint --ring is invisible
   on the accent fill. A surface-coloured gap + solid ring reads on any bg. */
.btn-primary:focus-visible {
    box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent);
}

.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-danger:focus-visible {
    border-color: var(--danger);
    box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--danger);
}

/* Secondary button: a real surface fill + border so it reads as a button
   (not bare text) anywhere it's used. For a text-only control use .btn-link. */
.btn-ghost {
    background: var(--surface-2);
    color: var(--text-2);
    border-color: var(--border);
}
.btn-ghost:hover {
    background: var(--surface-sunken);
    color: var(--text);
    border-color: var(--border-strong);
}

.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;
}

/* ─── Modern inputs everywhere in admin content ──────────
   Many admin pages render bare <input>/<select>/<textarea> that aren't
   wrapped in .field (search bars, status filters, inline forms), so they
   used to fall back to the browser default and looked inconsistent. Give
   every text-like control inside the main content area the same compact,
   modern styling — rounded, hairline-bordered, with a hover + focus ring. */
:is(main.content, main.cust-content) input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]):not([type="image"]):not([type="hidden"]),
:is(main.content, main.cust-content) select,
:is(main.content, main.cust-content) textarea {
    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);
    min-height: 38px;
    padding: 9px 12px;
    outline: none;
    transition: border-color .12s ease, box-shadow .12s ease, background .12s ease;
    -webkit-appearance: none;
    appearance: none;
}
:is(main.content, main.cust-content) textarea { min-height: 88px; resize: vertical; line-height: 1.5; }
:is(main.content, main.cust-content) input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):hover,
:is(main.content, main.cust-content) select:hover,
:is(main.content, main.cust-content) textarea:hover { border-color: var(--border-stronger); }
:is(main.content, main.cust-content) input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):focus,
:is(main.content, main.cust-content) select:focus,
:is(main.content, main.cust-content) textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--ring);
}
:is(main.content, main.cust-content) select {
    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;
}
:is(main.content, main.cust-content) input::placeholder,
:is(main.content, main.cust-content) textarea::placeholder { color: var(--subtle); }

.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; }
/* Compact filter bar: tiny uppercase labels, shorter controls, less air. */
.filter-row .field-label {
    font-size: 10px; font-weight: 600; letter-spacing: 0.04em;
    text-transform: uppercase; color: var(--subtle); margin-bottom: 4px;
}
.filter-row .field input,
.filter-row .field select { min-height: 34px; padding: 7px 11px; font-size: 12.5px; }
.filter-row .field select { padding-right: 30px; }
/* The card that wraps a filter bar hugs its contents. */
.card.tight.filter-card,
.filter-card { padding: 12px 14px; margin-bottom: var(--space-3); }

/* ─── 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;
    min-width: 0;
    overflow: hidden;
}

/* ─── Overflow containment ────────────────────────────────
   body{overflow-x:hidden} clips the page, so any child wider than the
   viewport (long URLs, code/iframe snippets, wide tables) gets visually
   *cut off* instead of wrapping. Keep wide content inside its column. */
:is(main.content, main.cust-content) { min-width: 0; max-width: 100%; }
.page-layout, .page-layout-with-aside, .page-main, .page-aside, .card { min-width: 0; }
:is(main.content, main.cust-content) pre,
:is(main.content, main.cust-content) .snippet {
    max-width: 100%;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    overflow-x: auto;
}
:is(main.content, main.cust-content) code { overflow-wrap: anywhere; word-break: break-word; }
:is(main.content, main.cust-content) .table-wrap { max-width: 100%; overflow-x: auto; }
.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); }

/* ─── Segmented control ───────────────────────────────────
   <div class="segmented">
     <a class="segmented-item is-active">Week</a> …
   </div>  (use <button>/<a>/<label>) */
.segmented {
    display: inline-flex; align-items: center;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px; padding: 3px; gap: 2px;
    vertical-align: middle;
}
.segmented-item {
    appearance: none; border: 0; background: transparent;
    padding: 6px 14px; border-radius: 7px;
    font: inherit; font-size: 12.5px; font-weight: 500;
    color: var(--muted); cursor: pointer; text-decoration: none;
    white-space: nowrap; line-height: 1.2;
    display: inline-flex; align-items: center; gap: 6px;
    transition: background .12s, color .12s, box-shadow .12s;
}
.segmented-item:hover { color: var(--text-2); }
.segmented-item.is-active,
.segmented-item[aria-selected="true"] {
    background: var(--surface); color: var(--text);
    box-shadow: 0 1px 2px rgba(15,17,23,0.06);
}
.segmented-item svg { width: 16px; height: 16px; }
/* Icon-only variant (e.g. grid/list view toggle) */
.segmented.segmented-icons .segmented-item { padding: 6px 9px; }

/* ─── Chips (filter pills) ────────────────────────────────
   <div class="chip-group"><a class="chip is-active">All</a> …</div> */
.chip-group { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chip {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 6px 13px; border-radius: 999px;
    border: 1px solid var(--border-strong); background: var(--surface);
    font: inherit; font-size: 12.5px; font-weight: 500; color: var(--text-2);
    cursor: pointer; text-decoration: none; line-height: 1.2;
    transition: border-color .12s, background .12s, color .12s;
}
.chip:hover { border-color: var(--border-stronger); color: var(--text); }
.chip.is-active {
    background: var(--accent-soft); border-color: transparent; color: var(--accent-deep);
}
.chip-x {
    display: inline-flex; width: 14px; height: 14px;
    align-items: center; justify-content: center; opacity: .7;
}
.chip-x:hover { opacity: 1; }

/* ─── Switch (iOS-style toggle) ───────────────────────────
   <label class="switch"><input type="checkbox"><span class="switch-track"></span></label> */
.switch { position: relative; display: inline-flex; align-items: center; flex: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
    width: 42px; height: 24px; border-radius: 999px;
    background: var(--faint); transition: background .15s ease; position: relative;
    flex: none; cursor: pointer;
}
.switch-track::after {
    content: ""; position: absolute; top: 2px; left: 2px;
    width: 20px; height: 20px; border-radius: 999px; background: #fff;
    box-shadow: 0 1px 3px rgba(15,17,23,0.25); transition: transform .15s ease;
}
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { box-shadow: 0 0 0 3px var(--ring); }
.switch input:disabled + .switch-track { opacity: .5; cursor: not-allowed; }
.switch-label { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; font-size: 13px; }

/* ─── Collapsible filter bar ──────────────────────────────
   A filter card auto-collapses behind a compact "Filters" toggle
   (see footer JS). Collapsed = just the toggle row; expanded = fields. */
.card.is-filter { padding: 0; overflow: hidden; margin-bottom: var(--space-3); }
.filter-toggle {
    display: flex; align-items: center; gap: 8px; width: 100%;
    padding: 10px 14px; border: 0; background: transparent;
    font: inherit; font-size: 12.5px; font-weight: 600; color: var(--text-2);
    cursor: pointer; text-align: left;
}
.filter-toggle:hover { background: var(--surface-2); }
.filter-toggle > .filter-ico { width: 15px; height: 15px; color: var(--muted); flex: none; }
.filter-toggle .filter-count {
    background: var(--accent); color: #fff; border-radius: 999px;
    min-width: 17px; height: 17px; padding: 0 5px; font-size: 10px; font-weight: 700;
    display: inline-flex; align-items: center; justify-content: center;
}
.filter-toggle .filter-caret { margin-left: auto; color: var(--subtle); transition: transform .15s ease; flex: none; }
.filter-toggle[aria-expanded="true"] .filter-caret { transform: rotate(180deg); }
.card.is-filter .filter-row { padding: 4px 14px 14px; }
.card.is-filter .filter-row[hidden] { display: none; }

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

/* ─── Pagination ──────────────────────────────────────── */
.slate-pager {
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 10px;
    margin-top: var(--space-4);
}
.slate-pager-summary { font-size: 12px; color: var(--muted); }
.slate-pager-pages { display: inline-flex; align-items: center; gap: 4px; margin-left: auto; }
.slate-pager-btn {
    min-width: 32px; height: 32px; padding: 0 8px;
    display: inline-flex; align-items: center; justify-content: center;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--text-2);
    font-size: 12.5px; font-weight: 500; text-decoration: none;
    font-variant-numeric: tabular-nums;
    transition: border-color .12s, background .12s, color .12s;
}
.slate-pager-btn:hover { border-color: var(--border-stronger); background: var(--surface-2); color: var(--text); }
.slate-pager-btn.is-current { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.slate-pager-btn.is-disabled { opacity: .4; pointer-events: none; }
.slate-pager-gap { padding: 0 2px; color: var(--subtle); font-size: 12.5px; }
@media (max-width: 767px) {
    .slate-pager { justify-content: center; }
    .slate-pager-pages { margin: 0 auto; }
    .slate-pager-summary { width: 100%; text-align: center; }
}

/* ─── 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;
}

/* (A second, conflicting .segmented definition used to live here — its base
   rule clashed with the canonical .segmented / .segmented-item above. Removed.) */

/* ─── 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;
}

/* The canonical switch component is the iOS-style .switch / .switch-track
   defined above. (A second, conflicting .switch definition used to live here
   and broke every real toggle — removed.) */

/* ─── 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;
}

/* The skip link is defined once in includes/a11y_head.php (emitted after this
   stylesheet), so it owns the canonical styling. Duplicate removed here. */

/* ─── 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;
    /* Auto-fit compact columns so wide detail panels fill end-to-end instead
       of leaving two very wide half-empty columns. */
    grid-template-columns: repeat(auto-fit, minmax(160px, 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 — app-style layout (≤767px)
   Full-width touch targets, stacked headers/filters, no
   horizontal overflow, comfortable tap sizes.
   ═══════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    h1 { font-size: 19px; font-weight: 600; line-height: 1.25; }
    h2 { font-size: 15px; }
    .stat-value { font-size: 22px; }
    .card { border-radius: 14px; padding: 14px; margin-bottom: var(--space-3); }

    /* Page header: title stacks above a full-width action row. */
    .page-header { flex-direction: column; align-items: stretch; gap: 10px; }
    .page-header > div:first-child { min-width: 0; }
    .page-header .toolbar,
    .page-header .flex,
    .page-header form { width: 100%; }
    /* Primary call-to-action spans the width like a native app button. */
    .page-header .btn-primary,
    .page-header .toolbar > .btn:only-child,
    .page-header > .btn:last-child { width: 100%; justify-content: center; }

    /* Buttons + inputs: tidy, not chunky. */
    .btn { min-height: 40px; padding: 9px 14px; font-size: 13px; border-radius: 9px; }
    .btn-lg { min-height: 46px; padding: 12px 16px; font-size: 14px; }
    .btn-sm { min-height: 30px; padding: 6px 11px; font-size: 12px; }
    :is(main.content, main.cust-content) input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]),
    :is(main.content, main.cust-content) select,
    :is(main.content, main.cust-content) textarea {
        min-height: 44px; padding: 11px 12px; font-size: 15px; border-radius: 10px;
    }

    /* Filter / search bars: stack into a clean app-style column. The search
       field goes full width, secondary controls (status, Filter) sit below. */
    .toolbar { flex-wrap: wrap; gap: 8px; }
    .toolbar > * { flex: 1 1 auto; }
    :is(main.content, main.cust-content) form[style*="flex"] { flex-wrap: wrap; gap: 8px !important; }
    :is(main.content, main.cust-content) form[style*="flex"] > input[type="text"],
    :is(main.content, main.cust-content) form[style*="flex"] > input[type="search"],
    :is(main.content, main.cust-content) form[style*="flex"] > .field { flex: 1 1 100% !important; min-width: 0; }
    .filter-row { gap: 8px; }
    .filter-row .filter-actions { justify-content: stretch; }
    .filter-row .filter-actions .btn { flex: 1 1 auto; }

    /* Collapsible list rows: bigger tap area, tidy detail grid. */
    .data-row-summary { min-height: 56px; padding: 12px 14px; }
    .data-row-grid { grid-template-columns: 1fr; }
    .data-row-actions { flex-wrap: wrap; }
    .data-row-actions .btn { flex: 1 1 auto; }

    /* Key/value detail rows stack label over value. */
    .kv-row .kv-value { text-align: left; }
    .kv-row { flex-direction: column; gap: 2px; }

    /* Two-up field rows collapse to one column. */
    .field-row-2, .field-row-3, .field-row-variant { grid-template-columns: 1fr; }

    /* Stat/summary card grids go single-column for readability. */
    .stat-grid, .grid-3, .grid-2 { grid-template-columns: 1fr !important; }
}

/* Smooth momentum scrolling for any intentionally-scrollable strip. */
.table-wrap, .topbar-pop-body, .snippet { -webkit-overflow-scrolling: touch; }
</style>
        <?php
    }
}
