/* ============================================================================
   Master DB — V2 design system (ERG-36)
   Loads AFTER style.css, only in the V2 shell (base_v2.html). Re-themes the
   shared tokens + component classes every page already uses, and adds the
   sidebar/topbar shell layout. V1 is untouched.
   ========================================================================== */

/* ── Tokens ───────────────────────────────────────────────────────────── */
:root {
    --primary: #3b6ef6;
    --primary-dark: #2b56d9;
    --primary-light: #eef3ff;
    --accent-ring: rgba(59, 110, 246, 0.22);
    --success: #16a34a;
    --success-light: #ecfdf3;
    --danger: #dc2626;
    --danger-light: #fef1f1;
    --warning: #d97706;
    --warning-light: #fef8e7;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;
    --gray-900: #0f172a;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.08);
    --sidebar-w: 232px;
    --topbar-h: 56px;
}

body.v2 {
    font-family: "Inter", "Segoe UI Variable Text", -apple-system, BlinkMacSystemFont,
        "Segoe UI", Roboto, sans-serif;
    background: var(--gray-50);
    color: var(--gray-800);
    font-size: 13.5px;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

/* ── App shell ────────────────────────────────────────────────────────── */
.v2-app { display: flex; min-height: 100vh; }

.v2-sidebar {
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    background: #fff;
    border-right: 1px solid var(--gray-200);
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    z-index: 120;
}
.v2-sidebar-brand { padding: 0.9rem 1rem 0.7rem; }
.v2-sidebar-brand a {
    display: flex; align-items: center; gap: 0.55rem;
    font-weight: 700; font-size: 1rem; color: var(--gray-900);
}
.v2-sidebar-brand a:hover { text-decoration: none; }
.v2-logo {
    display: inline-flex; align-items: center; justify-content: center;
    width: 28px; height: 28px; border-radius: 8px;
    background: linear-gradient(135deg, var(--primary), #7c5cf0);
    color: #fff; font-size: 0.95rem;
}

.v2-nav { flex: 1; overflow-y: auto; padding: 0.25rem 0.6rem 1rem; scrollbar-width: thin; }
.v2-nav-group {
    margin: 0.95rem 0.45rem 0.25rem;
    font-size: 0.66rem; font-weight: 600; letter-spacing: 0.07em;
    text-transform: uppercase; color: var(--gray-400);
}
.v2-nav-item {
    display: flex; align-items: center; gap: 0.55rem;
    padding: 0.42rem 0.6rem; margin: 1px 0;
    border-radius: var(--radius);
    color: var(--gray-600); font-size: 0.82rem; font-weight: 500;
    transition: background 0.12s, color 0.12s;
}
.v2-nav-item:hover { background: var(--gray-100); color: var(--gray-900); text-decoration: none; }
.v2-nav-item.active {
    background: var(--primary-light); color: var(--primary-dark); font-weight: 600;
}
.v2-ico { width: 1.15rem; text-align: center; opacity: 0.75; font-size: 0.9rem; }
.v2-nav-item.active .v2-ico { opacity: 1; }

/* ── Sidebar favorites (ERG-79) ───────────────────────────────────────── */
.v2-fav-star {
    margin-left: auto; background: none; border: none; cursor: pointer;
    color: var(--gray-300); font-size: 0.95rem; line-height: 1; padding: 0 0.1rem;
    opacity: 0; transition: opacity 0.12s, color 0.12s; flex: none;
}
.v2-nav-item:hover .v2-fav-star { opacity: 0.8; }
.v2-fav-star:hover { color: #f59e0b; opacity: 1; }
.v2-fav-star.active { opacity: 1; color: #f59e0b; }  /* favorited = always-visible gold star */
.v2-favs-group { color: #f59e0b; }

.v2-sidebar-foot { padding: 0.6rem 1rem; border-top: 1px solid var(--gray-100); }
.v2-back-link { font-size: 0.74rem; color: var(--gray-400); }
.v2-back-link:hover { color: var(--gray-600); text-decoration: none; }

.v2-maincol { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* ── Topbar ───────────────────────────────────────────────────────────── */
.v2-topbar {
    height: var(--topbar-h);
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0 1.25rem;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--gray-200);
    position: sticky; top: 0; z-index: 110;
}
.v2-burger {
    display: none; background: none; border: none; font-size: 1.25rem;
    color: var(--gray-600); cursor: pointer; padding: 0.25rem 0.4rem; border-radius: var(--radius);
}
.v2-burger:hover { background: var(--gray-100); }

.v2-crumbs { display: flex; align-items: center; min-width: 0; }
.v2-crumbs .breadcrumb { display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; white-space: nowrap; overflow: hidden; }
.v2-crumbs .breadcrumb a { color: var(--gray-500); }
.v2-crumbs .breadcrumb a:hover { color: var(--primary); text-decoration: none; }
.v2-crumbs .breadcrumb .separator { color: var(--gray-300); }
.v2-crumbs .breadcrumb .current { color: var(--gray-800); font-weight: 600; }
.v2-topbar-spacer { flex: 1; }

.v2-search .nav-search { display: flex; }
.v2-search .nav-search input {
    width: 230px; height: 32px;
    border: 1px solid var(--gray-200); border-radius: 8px 0 0 8px;
    background: var(--gray-50); padding: 0 0.7rem; font-size: 0.8rem;
    color: var(--gray-800); outline: none; transition: border-color 0.12s, box-shadow 0.12s, width 0.18s;
}
.v2-search .nav-search input:focus {
    border-color: var(--primary); box-shadow: 0 0 0 3px var(--accent-ring);
    background: #fff; width: 300px;
}
.v2-search .nav-search button {
    height: 32px; border: 1px solid var(--gray-200); border-left: none;
    border-radius: 0 8px 8px 0; background: #fff; color: var(--gray-500);
    padding: 0 0.6rem; cursor: pointer;
}
.v2-search .nav-search button:hover { color: var(--primary); }
.v2-search .search-dropdown { top: calc(var(--topbar-h) - 12px); border-radius: var(--radius-lg); box-shadow: var(--shadow-md); }

.v2-bell { position: relative; color: var(--gray-500); font-size: 1.05rem; padding: 0.3rem 0.4rem; border-radius: var(--radius); }
.v2-bell:hover { background: var(--gray-100); text-decoration: none; }

.v2-pill {
    font-size: 0.62rem; font-weight: 700; letter-spacing: 0.06em;
    background: linear-gradient(135deg, var(--primary), #7c5cf0);
    color: #fff; border-radius: 999px; padding: 0.18rem 0.55rem;
}

.v2-user .user-dropdown-toggle {
    background: none; border: none; cursor: pointer; display: flex; align-items: center;
    gap: 0.3rem; color: var(--gray-600); padding: 0.2rem; border-radius: 999px;
}
.v2-avatar {
    display: inline-flex; align-items: center; justify-content: center;
    width: 30px; height: 30px; border-radius: 50%;
    background: var(--gray-800); color: #fff; font-size: 0.8rem; font-weight: 600;
}
.v2-user .user-dropdown-menu { right: 0; top: calc(100% + 8px); border-radius: var(--radius-lg); box-shadow: var(--shadow-md); }

/* ── Main content ─────────────────────────────────────────────────────── */
.v2-main { flex: 1; padding: 1.4rem 1.5rem 3rem; animation: v2up 0.18s ease-out; }
.v2-content { max-width: 1480px; margin: 0 auto; }
@keyframes v2up { from { opacity: 0.35; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .v2-main { animation: none; } }

body.v2 h1 { font-size: 1.35rem; font-weight: 700; letter-spacing: -0.015em; color: var(--gray-900); }
body.v2 h2 { font-size: 1.05rem; font-weight: 650; color: var(--gray-900); }
body.v2 h3 { font-size: 0.92rem; font-weight: 600; }

/* legacy in-page breadcrumbs (V1 pages render block breadcrumb in the topbar now,
   but a few templates also print .breadcrumb inside content) */
body.v2 .v2-main .breadcrumb-bar { display: none; }

/* ── Cards & panels ───────────────────────────────────────────────────── */
body.v2 .card {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}
body.v2 .card:hover { box-shadow: var(--shadow); }

/* ── Tables ───────────────────────────────────────────────────────────── */
body.v2 table { font-variant-numeric: tabular-nums; }
body.v2 th {
    background: var(--gray-50);
    font-size: 0.68rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--gray-500); padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--gray-200);
}
body.v2 td { padding: 0.5rem 0.75rem; font-size: 0.815rem; border-bottom: 1px solid var(--gray-100); }
body.v2 tbody tr { transition: background 0.08s; }
body.v2 tbody tr:hover { background: #f6f9ff; }
body.v2 .sticky-header thead th { --sticky-top: var(--topbar-h); }
/* NOTE: no `overflow: hidden` here — it would make the table its header's clipping
   container and silently kill EVERY sticky header (learned the hard way, ERG-65). */
body.v2 .table-wrapper > table,
body.v2 table.table {
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius-lg);
}
body.v2 .table-wrapper > table thead tr:first-child th:first-child,
body.v2 table.table thead tr:first-child th:first-child { border-top-left-radius: var(--radius-lg); }
body.v2 .table-wrapper > table thead tr:first-child th:last-child,
body.v2 table.table thead tr:first-child th:last-child { border-top-right-radius: var(--radius-lg); }

/* ── Buttons ──────────────────────────────────────────────────────────── */
body.v2 .btn {
    border-radius: var(--radius);
    font-weight: 550; font-size: 0.8rem;
    transition: background 0.12s, border-color 0.12s, color 0.12s, box-shadow 0.12s, transform 0.05s;
}
body.v2 .btn:active { transform: translateY(0.5px); }
body.v2 .btn-primary { background: var(--primary); border-color: var(--primary); box-shadow: var(--shadow); }
body.v2 .btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
body.v2 .btn-outline:hover { border-color: var(--gray-400); background: var(--gray-50); }
body.v2 .btn:focus-visible,
body.v2 a:focus-visible,
body.v2 input:focus-visible,
body.v2 select:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--accent-ring); }

/* ── Forms ────────────────────────────────────────────────────────────── */
body.v2 input[type="text"], body.v2 input[type="number"], body.v2 input[type="email"],
body.v2 input[type="password"], body.v2 input[type="date"], body.v2 input[type="time"],
body.v2 select, body.v2 textarea {
    border-radius: var(--radius);
    border: 1px solid var(--gray-300);
    background: #fff;
    transition: border-color 0.12s, box-shadow 0.12s;
}
body.v2 input:focus, body.v2 select:focus, body.v2 textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--accent-ring);
    outline: none;
}

/* ── Alerts (in-content) ──────────────────────────────────────────────── */
body.v2 .alert { border-radius: var(--radius-lg); border: 1px solid transparent; font-size: 0.84rem; }
body.v2 .alert-success { background: var(--success-light); border-color: #bbf0cd; }
body.v2 .alert-error, body.v2 .alert-danger { background: var(--danger-light); border-color: #f6caca; }
body.v2 .alert-info { background: var(--primary-light); border-color: #cdddfd; }
body.v2 .alert-warning { background: var(--warning-light); border-color: #f3e3b5; }

/* ── Toasts (flash messages, V2 delivery) ─────────────────────────────── */
.v2-toasts {
    position: fixed; top: calc(var(--topbar-h) + 10px); right: 16px;
    display: flex; flex-direction: column; gap: 8px; z-index: 4000; max-width: 380px;
}
.v2-toast {
    display: flex; align-items: flex-start; gap: 0.6rem;
    background: #fff; border: 1px solid var(--gray-200); border-left: 3px solid var(--gray-400);
    border-radius: var(--radius-lg); box-shadow: var(--shadow-md);
    padding: 0.65rem 0.8rem; font-size: 0.82rem; color: var(--gray-800);
    animation: v2toast 0.22s ease-out;
}
.v2-toast button {
    margin-left: auto; background: none; border: none; color: var(--gray-400);
    font-size: 1rem; cursor: pointer; line-height: 1;
}
.v2-toast button:hover { color: var(--gray-700); }
.v2-toast-success { border-left-color: var(--success); }
.v2-toast-error, .v2-toast-danger { border-left-color: var(--danger); }
.v2-toast-info { border-left-color: var(--primary); }
.v2-toast-warning { border-left-color: var(--warning); }
.v2-toast.v2-out { opacity: 0; transform: translateX(12px); transition: opacity 0.25s, transform 0.25s; }
@keyframes v2toast { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }

/* ── Badges / pagination / misc ───────────────────────────────────────── */
body.v2 .badge, body.v2 .role-badge { border-radius: 999px; font-weight: 600; }
body.v2 .pagination a, body.v2 .pagination span { border-radius: var(--radius); }

/* thin scrollbars in the shell */
body.v2 ::-webkit-scrollbar { width: 10px; height: 10px; }
body.v2 ::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 99px; border: 2px solid transparent; background-clip: content-box; }
body.v2 ::-webkit-scrollbar-thumb:hover { background-color: var(--gray-400); }
body.v2 ::-webkit-scrollbar-track { background: transparent; }

/* ── Navigation progress bar (ERG-65) ─────────────────────────────────── */
.v2-progress {
    position: fixed; top: 0; left: 0; height: 3px; width: 0; opacity: 0;
    background: linear-gradient(90deg, var(--primary), #7c5cf0);
    box-shadow: 0 0 10px var(--accent-ring);
    z-index: 6000; pointer-events: none;
}
.v2-nav-item.v2-loading { background: var(--primary-light); animation: v2pulse 1s ease-in-out infinite; }
@keyframes v2pulse { 50% { opacity: 0.55; } }

/* ── Frozen table headers site-wide (ERG-65) ──────────────────────────── */
/* Pin the WHOLE thead (header row + filter row together — many tables carry a
   tr.filter-row of <td> inputs inside the thead). */
body.v2 table.v2-stick { border-collapse: separate; border-spacing: 0; }
body.v2 table.v2-stick > thead {
    position: sticky; top: var(--topbar-h); z-index: 20;
}
body.v2 table.v2-stick > thead th { background: var(--gray-50); box-shadow: 0 1px 0 var(--gray-200); }
body.v2 table.v2-stick > thead td { background: #fff; box-shadow: 0 1px 0 var(--gray-200); }
body.v2 .table-wrapper.v2-clip { overflow-x: clip; }
/* wide tables: the wrapper scrolls (both axes) with the header pinned to its top */
body.v2 .table-wrapper.v2-pane {
    overflow: auto;
    max-height: calc(100vh - var(--topbar-h) - 84px);
    background: #fff; border: 1px solid var(--gray-200); border-radius: var(--radius-lg);
}
body.v2 .table-wrapper.v2-pane table { border-collapse: separate; border-spacing: 0; border: none; }
body.v2 .table-wrapper.v2-pane thead {
    position: sticky; top: 0; z-index: 20;
}
body.v2 .table-wrapper.v2-pane thead th { background: var(--gray-50); box-shadow: 0 1px 0 var(--gray-200); }
body.v2 .table-wrapper.v2-pane thead td { background: #fff; box-shadow: 0 1px 0 var(--gray-200); }

/* ── Auth (login) ─────────────────────────────────────────────────────── */
body.v2 .v2-auth { max-width: 420px; margin: 9vh auto; }

/* ── Responsive ───────────────────────────────────────────────────────── */
.v2-backdrop { display: none; }
@media (max-width: 1024px) {
    .v2-sidebar {
        position: fixed; left: 0; top: 0; transform: translateX(-100%);
        transition: transform 0.2s ease-out; box-shadow: var(--shadow-md);
    }
    .v2-sidebar.open { transform: none; }
    /* dim backdrop while the drawer is open (sibling after the sidebar); tap closes it */
    .v2-sidebar.open ~ .v2-backdrop {
        display: block; position: fixed; inset: 0;
        background: rgba(15, 23, 42, 0.45); z-index: 110; animation: v2fade 0.18s ease-out;
    }
    .v2-burger { display: inline-flex; }
    .v2-search .nav-search input { width: 150px; }
    .v2-search .nav-search input:focus { width: 190px; }
}
@keyframes v2fade { from { opacity: 0; } to { opacity: 1; } }

/* phone-width content pass (≤640px): reclaim width, scroll wide tables, wrap dense rows.
   All scoped to the media query so desktop is untouched. */
@media (max-width: 640px) {
    /* tighter content padding so dense pages get usable width on a phone */
    .v2-main { padding: 0.8rem 0.7rem 2.5rem; }
    /* trim the larger inline card paddings (1–1.5rem) on small screens */
    body.v2 .card { padding: 0.85rem !important; }
    /* wide tables scroll horizontally inside their wrapper instead of breaking the layout */
    body.v2 .table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    body.v2 .table-wrapper > table { min-width: max-content; }
    /* let action/badge rows wrap instead of overflowing the screen */
    body.v2 .card-header { flex-wrap: wrap; gap: 0.5rem; }
    body.v2 .btn-group { flex-wrap: wrap; gap: 0.4rem; }
    /* slightly larger tap targets */
    body.v2 .btn-sm { padding: 0.4rem 0.6rem; }
    /* 16px form fields so iOS Safari doesn't zoom in on focus (only fields without an
       inline font-size; leaves intentionally-tiny inline inputs alone) */
    body.v2 input, body.v2 select, body.v2 textarea { font-size: 16px; }
    /* Full-screen modals on phones: any "*modal*" overlay's card fills the screen + scrolls,
       instead of a cramped centered box. (The composer modals are inline-styled, so this
       overrides their inline max-width/width/border-radius for small screens.) */
    body.v2 [id*="modal" i] > div {
        width: 100% !important; max-width: 100% !important;
        height: 100% !important; max-height: 100% !important;
        border-radius: 0 !important; margin: 0 !important;
    }
    /* keep headings + the topbar search from overflowing */
    body.v2 h1 { font-size: 1.18rem; }
    .v2-search .nav-search input { width: 108px; }
    .v2-search .nav-search input:focus { width: 150px; }

    /* Stacked-card tables (opt-in via class "v2-stack" + JS data-labels): each row becomes a
       labeled card, so there's NO awkward horizontal scrolling on a phone. Neutralizes the
       v2-pane scroll-pane the frozen-head JS would otherwise apply. */
    body.v2 .table-wrapper:has(table.v2-stack) {
        overflow: visible; max-height: none; border: none; background: transparent; border-radius: 0;
    }
    body.v2 table.v2-stack { display: block; width: 100%; min-width: 0 !important; border: none; background: transparent; }
    body.v2 table.v2-stack thead { display: none; }
    body.v2 table.v2-stack tbody { display: block; }
    body.v2 table.v2-stack > tbody > tr {
        display: block; background: #fff; border: 1px solid var(--gray-200);
        border-radius: var(--radius-lg); box-shadow: var(--shadow);
        margin: 0 0 0.55rem; padding: 0.4rem 0.7rem;
    }
    body.v2 table.v2-stack > tbody > tr:hover { background: #fff; }
    body.v2 table.v2-stack > tbody > tr > td {
        display: flex; justify-content: space-between; gap: 0.8rem; align-items: baseline;
        padding: 0.26rem 0; border: none; border-bottom: 1px solid var(--gray-100);
        text-align: right; font-size: 0.84rem; white-space: normal;
    }
    body.v2 table.v2-stack > tbody > tr > td:last-child { border-bottom: none; }
    body.v2 table.v2-stack > tbody > tr > td::before {
        content: attr(data-label); flex: 0 0 40%; text-align: left;
        font-weight: 600; color: var(--gray-500);
        font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em; white-space: nowrap;
    }
    /* a cell explicitly labeled empty (or with no label) drops the label gutter */
    body.v2 table.v2-stack > tbody > tr > td[data-label=""]::before { content: ""; flex: 0; }
}

/* ── Print ────────────────────────────────────────────────────────────── */
@media print {
    .v2-sidebar, .v2-topbar, .v2-toasts, .chatbot-toggle { display: none !important; }
    .v2-main { padding: 0; }
}
