/* ==========================================================================
   Shamelfy Admin — UI kit
   Loaded AFTER admin.css so it can extend and override without touching the
   837 lines the existing 34 views already depend on. Adds: a light theme, an
   icon system, a standard list toolbar, denser tables, charts, tabs and tiles.
   ========================================================================== */

/* ── Extra tokens ─────────────────────────────────────────────────────── */
:root {
    --bg-hover: rgba(255, 255, 255, .04);
    --bg-input: rgba(255, 255, 255, .04);
    --bg-subtle: rgba(255, 255, 255, .02);
    --accent-soft: rgba(59, 130, 246, .14);
    --success-soft: rgba(16, 185, 129, .14);
    --warning-soft: rgba(245, 158, 11, .14);
    --danger-soft: rgba(239, 68, 68, .14);
    --info-soft: rgba(6, 182, 212, .14);
    --font-mono: 'JetBrains Mono', ui-monospace, 'Cascadia Mono', Consolas, monospace;
    --header-h: 64px;
}

/* ── Light theme ──────────────────────────────────────────────────────────
   The operator picks it; it is stamped on <html> and remembered per browser. */
html[data-theme="light"] {
    --bg-app: #f4f6fb;
    --bg-surface: #ffffff;
    --bg-surface-elevated: #ffffff;
    --bg-card: #ffffff;
    --border-color: #e2e6f0;
    --border-hover: #c9d0e0;

    --text-primary: #131826;
    --text-secondary: #545c73;
    --text-muted: #7b839a;

    --bg-hover: rgba(19, 24, 38, .04);
    --bg-input: #ffffff;
    --bg-subtle: #f7f8fc;

    --shadow-card: 0 1px 2px rgba(19, 24, 38, .05), 0 8px 24px -18px rgba(19, 24, 38, .35);
    --shadow-glow: 0 0 0 3px var(--accent-soft);
}

/* The sidebar keeps its dark identity in light mode; the content area flips. */
html[data-theme="light"] .sidebar {
    background: #0e1423;
    border-right-color: rgba(255, 255, 255, .06);
}

html[data-theme="light"] .top-navbar,
html[data-theme="light"] .card,
html[data-theme="light"] .metric-card {
    background: var(--bg-surface);
    border-color: var(--border-color);
}

html[data-theme="light"] .data-table thead th {
    background: var(--bg-subtle);
    color: var(--text-secondary);
}

html[data-theme="light"] .form-control,
html[data-theme="light"] .form-control:focus {
    background: var(--bg-input);
    color: var(--text-primary);
    border-color: var(--border-color);
}

html[data-theme="light"] .btn-outline {
    color: var(--text-secondary);
    border-color: var(--border-color);
}

html[data-theme="light"] .btn-outline:hover { background: var(--bg-hover); color: var(--text-primary); }

/* ── Icons ────────────────────────────────────────────────────────────────
   One sprite, stroked with currentColor, so an icon always matches its text. */
.icon {
    width: 18px; height: 18px;
    flex-shrink: 0;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    vertical-align: -3px;
}
.icon-sm { width: 15px; height: 15px; vertical-align: -2px; }
.icon-lg { width: 22px; height: 22px; }
.icon-xl { width: 28px; height: 28px; }
.nav-icon .icon { width: 19px; height: 19px; vertical-align: -4px; }

/* ── Page header ──────────────────────────────────────────────────────── */
.page-header { align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.page-actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

/* ── Toolbar: search + filters + actions above every list ─────────────── */
.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: .75rem;
    padding: 1rem 1.25rem;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    margin-bottom: 1.25rem;
}
.toolbar form { display: contents; }
.toolbar-field { display: flex; flex-direction: column; gap: .35rem; min-width: 150px; }
.toolbar-field.grow { flex: 1 1 240px; }
.toolbar-label {
    font-size: .68rem; font-weight: 600; letter-spacing: .08em;
    text-transform: uppercase; color: var(--text-muted);
}
.toolbar-actions { display: flex; gap: .5rem; margin-left: auto; align-items: center; }
.search-field { position: relative; display: flex; align-items: center; }
.search-field .icon { position: absolute; left: .7rem; color: var(--text-muted); pointer-events: none; }
.search-field .form-control { padding-left: 2.25rem; }

/* ── Segmented control (quick status filters) ─────────────────────────── */
.segmented {
    display: inline-flex; gap: 2px; padding: 3px;
    background: var(--bg-subtle); border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
}
.segmented a {
    padding: .35rem .85rem; border-radius: var(--radius-full);
    font-size: .8rem; font-weight: 600; color: var(--text-secondary);
    text-decoration: none; white-space: nowrap; transition: all .15s ease;
}
.segmented a:hover { color: var(--text-primary); background: var(--bg-hover); }
.segmented a.active { background: var(--color-primary); color: #fff; }

/* ── Stat tiles with trend ────────────────────────────────────────────── */
.stat-grid {
    display: grid; gap: 1rem;
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
    margin-bottom: 1.5rem;
}
.stat-tile {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.1rem 1.25rem;
    display: flex; flex-direction: column; gap: .5rem;
    transition: border-color .15s ease;
}
.stat-tile:hover { border-color: var(--border-hover); }
.stat-tile-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.stat-tile-label {
    font-size: .74rem; font-weight: 600; letter-spacing: .06em;
    text-transform: uppercase; color: var(--text-muted);
}
.stat-tile-icon {
    width: 32px; height: 32px; border-radius: var(--radius-md);
    display: grid; place-items: center;
    background: var(--accent-soft); color: var(--color-primary);
}
.stat-tile-icon.ok { background: var(--success-soft); color: var(--color-success); }
.stat-tile-icon.warn { background: var(--warning-soft); color: var(--color-warning); }
.stat-tile-icon.bad { background: var(--danger-soft); color: var(--color-danger); }
.stat-tile-icon.info { background: var(--info-soft); color: var(--color-info); }
.stat-tile-value {
    font-family: var(--font-heading); font-size: 1.85rem; font-weight: 700;
    line-height: 1.1; color: var(--text-primary); font-variant-numeric: tabular-nums;
}
.stat-tile-value .unit { font-size: .9rem; font-weight: 500; color: var(--text-muted); margin-left: .25rem; }
.stat-tile-foot { display: flex; align-items: center; gap: .5rem; font-size: .78rem; color: var(--text-muted); flex-wrap: wrap; }
.trend { display: inline-flex; align-items: center; gap: .2rem; font-weight: 700; }
.trend.up { color: var(--color-success); }
.trend.down { color: var(--color-danger); }
.trend.flat { color: var(--text-muted); }

/* ── Charts: CSS columns + inline sparkline ───────────────────────────── */
.chart {
    display: flex; align-items: flex-end; gap: 4px;
    height: 160px; padding-top: .5rem;
}
.chart-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; gap: 4px; min-width: 0; }
.chart-bar {
    background: linear-gradient(180deg, var(--color-primary), rgba(59, 130, 246, .35));
    border-radius: 4px 4px 2px 2px;
    min-height: 2px;
    transition: opacity .15s ease;
}
.chart-col:hover .chart-bar { opacity: .75; }
.chart-tick {
    font-size: .64rem; color: var(--text-muted); text-align: center;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chart-legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .76rem; color: var(--text-secondary); margin-top: .75rem; }
.chart-legend span { display: inline-flex; align-items: center; gap: .35rem; }
.legend-swatch { width: 10px; height: 10px; border-radius: 3px; }

/* Horizontal breakdown bars (status splits, top lists) */
.bars { display: flex; flex-direction: column; gap: .7rem; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 30%) 1fr auto; gap: .75rem; align-items: center; }
.bar-label { font-size: .82rem; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 8px; background: var(--bg-subtle); border-radius: var(--radius-full); overflow: hidden; border: 1px solid var(--border-color); }
.bar-fill { height: 100%; border-radius: var(--radius-full); background: var(--color-primary); }
.bar-fill.ok { background: var(--color-success); }
.bar-fill.warn { background: var(--color-warning); }
.bar-fill.bad { background: var(--color-danger); }
.bar-fill.info { background: var(--color-info); }
.bar-value { font-size: .82rem; font-weight: 600; color: var(--text-primary); font-variant-numeric: tabular-nums; }

/* ── Tables ───────────────────────────────────────────────────────────── */
.data-table td, .data-table th { vertical-align: middle; }
.data-table tbody tr { transition: background .12s ease; }
.data-table tbody tr:hover { background: var(--bg-hover); }
.data-table .num { font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.data-table .actions-cell { text-align: right; white-space: nowrap; }
.data-table .actions-cell .btn { margin-left: .3rem; }
.table-sort { color: inherit; text-decoration: none; display: inline-flex; align-items: center; gap: .25rem; }
.table-sort:hover { color: var(--color-primary); }
.table-sort .icon { opacity: .45; }
.table-sort.is-sorted .icon { opacity: 1; color: var(--color-primary); }
.cell-main { font-weight: 600; color: var(--text-primary); display: block; }
.cell-sub { font-size: .76rem; color: var(--text-muted); display: block; }
.mono { font-family: var(--font-mono); font-size: .82rem; }

/* Dense variant for tables that carry many figures per row (the AI credit tables), so they fit
   beside the sidebar instead of widening the whole page. */
.data-table.compact th { padding: 10px 9px; }
.data-table.compact td { padding: 10px 9px; }
.data-table.compact .cell-sub + .cell-sub { margin-top: .1rem; }

/* ── Tabs ─────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border-color); margin-bottom: 1.25rem; overflow-x: auto; }
.tabs a {
    padding: .65rem 1rem; font-size: .88rem; font-weight: 600;
    color: var(--text-secondary); text-decoration: none; white-space: nowrap;
    border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs a:hover { color: var(--text-primary); }
.tabs a.active { color: var(--color-primary); border-bottom-color: var(--color-primary); }
.tab-count {
    margin-left: .4rem; padding: .05rem .4rem; border-radius: var(--radius-full);
    background: var(--bg-subtle); font-size: .72rem; color: var(--text-muted);
}

/* ── Key/value detail panels ──────────────────────────────────────────── */
.kv { display: grid; gap: 0; }
.kv > div {
    display: grid; grid-template-columns: minmax(130px, 34%) 1fr; gap: 1rem;
    padding: .7rem 0; border-bottom: 1px solid var(--border-color);
}
.kv > div:last-child { border-bottom: none; }
.kv dt { font-size: .8rem; color: var(--text-muted); }
.kv dd { margin: 0; font-size: .88rem; color: var(--text-primary); word-break: break-word; }

/* ── Status pills (semantic, mapped from domain states) ───────────────── */
.pill {
    display: inline-flex; align-items: center; gap: .3rem;
    padding: .2rem .6rem; border-radius: var(--radius-full);
    font-size: .74rem; font-weight: 600; white-space: nowrap;
    background: var(--bg-subtle); color: var(--text-secondary);
    border: 1px solid var(--border-color);
}
.pill.ok { background: var(--success-soft); color: var(--color-success); border-color: transparent; }
.pill.warn { background: var(--warning-soft); color: var(--color-warning); border-color: transparent; }
.pill.bad { background: var(--danger-soft); color: var(--color-danger); border-color: transparent; }
.pill.info { background: var(--info-soft); color: var(--color-info); border-color: transparent; }
.pill.accent { background: var(--accent-soft); color: var(--color-primary); border-color: transparent; }
.pill .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* ── Timeline (order / subscription history) ──────────────────────────── */
.timeline { list-style: none; margin: 0; padding: 0 0 0 1.4rem; position: relative; }
.timeline::before { content: ''; position: absolute; left: 5px; top: .4rem; bottom: .4rem; width: 2px; background: var(--border-color); }
.timeline li { position: relative; padding: 0 0 1.1rem 0; }
.timeline li::before {
    content: ''; position: absolute; left: -1.4rem; top: .35rem;
    width: 12px; height: 12px; border-radius: 50%;
    background: var(--bg-app); border: 2px solid var(--color-primary);
}
.timeline li:last-child { padding-bottom: 0; }
.timeline-head { display: flex; gap: .6rem; align-items: baseline; flex-wrap: wrap; }
.timeline-time { font-size: .74rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.timeline-note { font-size: .82rem; color: var(--text-secondary); margin-top: .15rem; }

/* ── Theme toggle in the top bar ──────────────────────────────────────── */
.icon-btn {
    display: inline-grid; place-items: center;
    width: 34px; height: 34px; border-radius: var(--radius-md);
    background: transparent; border: 1px solid var(--border-color);
    color: var(--text-secondary); cursor: pointer; transition: all .15s ease;
}
.icon-btn:hover { color: var(--text-primary); border-color: var(--border-hover); background: var(--bg-hover); }
html[data-theme="light"] .theme-icon-dark { display: none; }
html:not([data-theme="light"]) .theme-icon-light { display: none; }

/* ── Layout helpers ───────────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1.25rem; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1.25rem; }
.split { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: 1.25rem; align-items: start; }
@media (max-width: 1100px) { .split { grid-template-columns: 1fr; } }
.stack { display: flex; flex-direction: column; gap: 1.25rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--text-muted); }
.text-right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ── Danger zone on edit pages ────────────────────────────────────────── */
.danger-zone {
    border: 1px solid rgba(239, 68, 68, .35);
    background: var(--danger-soft);
    border-radius: var(--radius-lg);
    padding: 1.1rem 1.25rem;
    display: flex; gap: 1rem; align-items: center; flex-wrap: wrap;
}
.danger-zone h3 { font-size: .95rem; margin: 0 0 .2rem; color: var(--color-danger); }
.danger-zone p { margin: 0; font-size: .82rem; color: var(--text-secondary); max-width: 60ch; }

/* ── Empty state ──────────────────────────────────────────────────────── */
.empty {
    display: flex; flex-direction: column; align-items: center; gap: .6rem;
    padding: 3rem 1rem; text-align: center; color: var(--text-muted);
}
.empty .icon { width: 34px; height: 34px; opacity: .5; }
.empty h3 { font-size: 1rem; color: var(--text-secondary); margin: 0; }
.empty p { font-size: .85rem; margin: 0; max-width: 42ch; }

/* ── Print ────────────────────────────────────────────────────────────── */
@media print {
    .sidebar, .top-navbar, .page-actions, .toolbar, .pagination-bar { display: none !important; }
    .main-content { margin-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

/* ── Dialog ────────────────────────────────────────────────────────────
   Native <dialog>: the browser supplies the modal behaviour — focus trap,
   Esc to close, inert background — so none of it is reimplemented here. */
.dialog {
    width: min(760px, calc(100vw - 2rem));
    max-height: calc(100vh - 4rem);
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    background: var(--bg-surface);
    color: var(--text-primary);
    box-shadow: 0 24px 60px rgba(0, 0, 0, .35);
}
.dialog::backdrop { background: rgba(15, 23, 42, .55); }

/* Only the body scrolls. Leaving the dialog scrollable too put a second scrollbar
   beside the first, both driving the same overflow. */
.dialog { overflow: hidden; }
.dialog-body { padding: 1.25rem 1.4rem 1.4rem; overflow-y: auto; max-height: calc(100vh - 4rem); }

/* The app has no base anchor style, so a bare link inside the dialog falls back to the
   browser default — bright blue and underlined against a dark panel. Scoped here rather
   than set globally, which would restyle every link in the admin at once. */
.dialog-body a { color: var(--color-primary); text-decoration: none; }
.dialog-body a:hover { text-decoration: underline; }

.dialog-head {
    display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem;
    padding-bottom: .9rem; margin-bottom: .4rem;
    border-bottom: 1px solid var(--border-color);
}
.dialog-title { margin: 0 0 .35rem; font-size: 1.05rem; }
.dialog-sub { margin: 0; display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; font-size: .82rem; }
.dialog-section { font-size: .85rem; margin: 1.2rem 0 .5rem; color: var(--text-secondary); }

.dialog-actions {
    display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
    margin-top: 1.25rem; padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

/* Action buttons sit in their own forms so each posts its own token; without this
   every form would start a new line and the row would grow four rows tall. */
.inline-form { display: inline; }

/* .data-table .actions-cell already sets alignment and spacing for buttons; these add the
   inline forms that a row of post-only actions needs beside them. */
.data-table .actions-cell > * { vertical-align: middle; }
.data-table .actions-cell .inline-form + .inline-form,
.data-table .actions-cell .btn + .inline-form { margin-left: .3rem; }
.data-table .actions-cell .inline-form .btn { margin-left: 0; }

.btn.danger { color: var(--color-danger); border-color: rgba(239, 68, 68, .35); }
.btn.danger:hover { background: var(--danger-soft); }

/* ══════════════════════════════════════════════════════════════════════════
   Responsive
   ──────────────────────────────────────────────────────────────────────────
   The admin was built for a desktop: a 270px fixed sidebar with the content
   pushed over by a matching margin, toolbars assuming they can lay several
   fields across a row, and tables of eight columns. Below roughly a laptop
   none of that fits, and the page could only be used by scrolling sideways.

   Three breakpoints, each for a real change in what fits:
     1100px — the sidebar becomes a drawer and the content takes the width
      820px — multi-column grids collapse and the toolbar stacks
      560px — phone: tighter padding, and the chrome that is not essential
               gets out of the way

   Nothing here changes the desktop rendering: every rule is inside a
   max-width query.
   ══════════════════════════════════════════════════════════════════════════ */

/* A flex item defaults to min-width:auto, which means it refuses to shrink below the
   intrinsic width of its content. .main-content is a flex child, so the moment anything
   inside it was wider than the viewport — a data table with a min-width, a long unbroken
   string — it stretched the whole page instead, and .table-responsive never got to scroll
   because it was never the narrower box. Everything else then overflowed with it: cards
   sized to 100% of a container wider than the screen hang off the right edge.

   Applied at every width, not just the small ones: a content column that cannot shrink is
   never what is wanted. */
.main-content { min-width: 0; }
.content-body { min-width: 0; max-width: 100%; }
.table-responsive { max-width: 100%; }

/* The hamburger and the drawer's own close button exist only below 1100px. */
.nav-toggle,
.sidebar-close { display: none; }

.sidebar-scrim {
    position: fixed; inset: 0; z-index: 99;
    background: rgba(2, 6, 23, .6);
    backdrop-filter: blur(2px);
}
.sidebar-scrim[hidden] { display: none; }

@media (max-width: 1100px) {
    /* Off-canvas. Transform rather than `left`, so it animates on the compositor
       and does not force a layout on every frame. */
    .sidebar {
        transform: translateX(-100%);
        transition: transform .22s ease;
        width: min(300px, 86vw);
        box-shadow: none;
    }
    .sidebar.is-open {
        transform: translateX(0);
        box-shadow: 0 0 40px rgba(0, 0, 0, .45);
    }

    .main-content { margin-left: 0; }

    .nav-toggle { display: inline-flex; }
    .sidebar-close {
        display: inline-flex;
        position: absolute; top: 18px; right: 14px;
    }
    .sidebar-brand { position: relative; padding-right: 56px; }

    .top-navbar { padding: 0 16px; gap: .75rem; }
    .navbar-left { display: flex; align-items: center; gap: .6rem; min-width: 0; }
    .breadcrumb { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    .content-body { padding: 20px 16px; }
}

@media (max-width: 820px) {
    /* Grids that were two or three across have nowhere to put the second column. */
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid-2, .grid-3, .grid-2-col, .grid-3-col, .metrics-grid { grid-template-columns: 1fr; }
    .split { grid-template-columns: 1fr; }

    /* The toolbar wraps on its own, but its fields have a 150px floor that makes
       two per row at best and leaves the actions stranded on a line of their own. */
    .toolbar { padding: .85rem 1rem; }
    .toolbar-field, .toolbar-field.grow { min-width: 0; flex: 1 1 100%; }
    .toolbar-actions { margin-left: 0; width: 100%; flex-wrap: wrap; }
    .segmented { flex: 1 1 100%; overflow-x: auto; }

    .page-header { flex-direction: column; align-items: flex-start; gap: .75rem; }
    .page-actions { width: 100%; flex-wrap: wrap; }

    /* A dialog that was a centred panel becomes the whole screen. */
    .dialog { width: 100vw; max-width: 100vw; max-height: 100vh; border-radius: 0; border: none; }
    .dialog-body { max-height: 100vh; }
    .kv > div { grid-template-columns: 1fr; gap: .2rem; }
}

@media (max-width: 560px) {
    .stat-grid { grid-template-columns: 1fr; }

    .top-navbar { height: 56px; padding: 0 12px; }
    /* "Platform Healthy" is reassurance, not information; on a phone the breadcrumb
       and the menu button are what the header is for. */
    .status-indicator { display: none; }
    .navbar-right { gap: .35rem; }
    .navbar-right .btn span:not(.icon) { display: none; }

    .content-body { padding: 16px 12px; }
    .page-title { font-size: 1.25rem; }

    /* Tables stay tables and scroll sideways inside their own box. Stacking each row
       into a card reads well for two or three columns and badly for eight, and these
       are reference lists people scan down rather than read across. */
    .table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .data-table { min-width: 640px; }

    .dialog-actions { flex-direction: column; align-items: stretch; }
    .dialog-actions .btn, .dialog-actions .inline-form, .dialog-actions .inline-form .btn { width: 100%; }

    .form-actions { flex-direction: column; align-items: stretch; }
    .form-actions .btn { width: 100%; }
}

/* A stack trace is long lines of mono text; it needs to scroll rather than
   stretch its card. */
.stack-trace {
    margin: 0;
    padding: 1rem;
    background: var(--bg-subtle);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: .76rem;
    line-height: 1.5;
    max-height: 60vh;
    overflow: auto;
    white-space: pre;
    color: var(--text-secondary);
}

/* The drawer is fixed, so the page behind it must not scroll with it open. */
body.sidebar-open { overflow: hidden; }
