/* ══ Mesa Design Tokens ══ */
:root {
    --green: #0E3B2F;
    --green-700: #0b2f26;
    --saffron: #FF7A3D;
    --saffron-600: #e9692f;
    --paper: #FBFAF7;
    --ink: #1c2421;
    --muted: #5e6b64;
    --line: #e6e2d8;
    --card: #ffffff;
    --sidebar-w: 224px;
    --sidebar-col-w: 52px;
    --mob-topbar-h: 48px;
    --bottom-nav-h: 56px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--ink);
    background: var(--paper);
    line-height: 1.5;
}

a { color: var(--green); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ══ APP SHELL ══ */
.app-shell {
    display: flex;
    height: 100dvh;
    overflow: hidden;
}

/* ══ SIDEBAR ══ */
.sidebar {
    width: var(--sidebar-w);
    background: var(--card);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: width 0.22s ease;
    overflow: hidden;
    z-index: 100;
}
.sidebar.collapsed { width: var(--sidebar-col-w); }

/* Logo */
.sb-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
    min-height: 50px;
    overflow: hidden;
    flex-shrink: 0;
}
.sb-logo-mark {
    width: 32px; height: 32px;
    border-radius: 7px;
    background: var(--green);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 10px; letter-spacing: 0.5px;
    flex-shrink: 0;
}
.sb-logo-text { overflow: hidden; white-space: nowrap; transition: opacity 0.15s; }
.sidebar.collapsed .sb-logo-text { opacity: 0; width: 0; }
.sb-logo-name { font-size: 14px; font-weight: 700; color: var(--ink); }
.sb-logo-sub  { font-size: 10px; color: var(--muted); }

/* Toggle button */
.sb-toggle {
    display: flex; align-items: center; justify-content: center;
    margin: 6px 8px 2px;
    padding: 5px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--paper);
    cursor: pointer;
    color: var(--muted);
    font-size: 16px;
    flex-shrink: 0;
    transition: background 0.15s;
}
.sb-toggle:hover { background: var(--line); }
.sidebar.collapsed .sb-toggle i { transform: rotate(180deg); }

/* Nav scroll area */
.sb-nav {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 4px 0;
}
.sb-nav::-webkit-scrollbar { width: 3px; }
.sb-nav::-webkit-scrollbar-thumb { background: var(--line); border-radius: 2px; }

/* Section headers (collapsible group labels) */
.sb-section {
    display: flex; align-items: center;
    padding: 10px 14px 3px;
    font-size: 10px; color: var(--muted);
    letter-spacing: 0.07em; text-transform: uppercase;
    cursor: pointer; user-select: none;
    white-space: nowrap; overflow: hidden;
}
.sb-section .chevron { font-size: 11px; transition: transform 0.2s; margin-left: auto; flex-shrink: 0; }
.sb-section.closed .chevron { transform: rotate(-90deg); }
.sidebar.collapsed .sb-section { opacity: 0; height: 0; padding: 0; overflow: hidden; pointer-events: none; }

/* Collapsible groups */
.sb-group {
    overflow: hidden;
    transition: max-height 0.25s ease;
}

/* Nav items */
.nav-item {
    display: flex; align-items: center; gap: 9px;
    padding: 7px 14px;
    font-size: 13px; color: var(--muted);
    text-decoration: none;
    white-space: nowrap; overflow: hidden;
    transition: background 0.1s, color 0.1s;
    border-left: 3px solid transparent;
}
.nav-item:hover { background: var(--paper); color: var(--ink); text-decoration: none; }
.nav-item.active {
    background: #e7f3ec;
    color: var(--green);
    font-weight: 600;
    border-left-color: var(--green);
}
.nav-item i { font-size: 16px; flex-shrink: 0; width: 18px; text-align: center; }
.nav-item .label { overflow: hidden; white-space: nowrap; }
.nav-item .badge {
    margin-left: auto;
    background: var(--saffron); color: #fff;
    font-size: 10px; padding: 1px 6px; border-radius: 10px;
    flex-shrink: 0;
}

/* Collapsed icon-only mode */
.sidebar.collapsed .nav-item { justify-content: center; padding: 8px 0; border-left: none; }
.sidebar.collapsed .nav-item .label,
.sidebar.collapsed .nav-item .badge { display: none; }
.sidebar.collapsed .sb-group { overflow: visible; }

/* Divider */
.sb-divider { height: 1px; background: var(--line); margin: 6px 10px; }
.sidebar.collapsed .sb-divider { margin: 6px 6px; }

/* Footer */
.sb-footer {
    padding: 10px 14px;
    border-top: 1px solid var(--line);
    display: flex; align-items: center; gap: 10px;
    flex-shrink: 0; overflow: hidden;
}
.sb-avatar {
    width: 28px; height: 28px;
    border-radius: 50%;
    background: var(--green); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 11px; font-weight: 700;
    flex-shrink: 0;
}
.sb-user-info { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; }
.sb-user-name { font-size: 12px; font-weight: 600; color: var(--ink); }
.sb-user-role { font-size: 10px; color: var(--muted); }
.sb-logout { display: inline; margin: 0; }
.sb-logout-btn {
    background: none; border: none; cursor: pointer;
    color: var(--muted); font-size: 16px; padding: 4px;
    display: flex; align-items: center;
}
.sb-logout-btn:hover { color: var(--ink); }
.sidebar.collapsed .sb-user-info,
.sidebar.collapsed .sb-logout { display: none; }
.sidebar.collapsed .sb-footer { justify-content: center; }

/* ══ MAIN AREA ══ */
.main-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

/* Mobile topbar (hidden on desktop) */
.mob-topbar {
    display: none;
    align-items: center; gap: 10px;
    padding: 0 14px;
    height: var(--mob-topbar-h);
    background: var(--card);
    border-bottom: 1px solid var(--line);
    flex-shrink: 0;
}
.mob-hamburger {
    background: none; border: none; cursor: pointer;
    font-size: 20px; color: var(--muted); padding: 4px;
}
.mob-page-title { font-size: 14px; font-weight: 600; flex: 1; color: var(--ink); }
.mob-actions { display: flex; gap: 6px; }
.mob-icon-btn {
    width: 32px; height: 32px;
    border: 1px solid var(--line); border-radius: 8px;
    background: var(--paper);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; color: var(--muted); font-size: 16px;
}

/* Page content */
.main-content {
    flex: 1;
    overflow-y: auto;
    padding: 24px 28px;
}

/* Sidebar overlay (mobile) */
.sidebar-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 99;
}
.sidebar-overlay.show { display: block; }

/* Bottom nav (mobile only) */
.bottom-nav { display: none; }

/* ══ MOBILE ══ */
@media (max-width: 767px) {
    /* Column-flex keeps the height chain intact so main-content can scroll */
    .app-shell { flex-direction: column; }

    .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        width: 240px !important;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 200;
        box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    }
    .sidebar.open { transform: translateX(0); }

    /* Drawer always shows full items regardless of collapsed state */
    .sidebar .sb-section { opacity: 1 !important; height: auto !important; padding: 10px 14px 3px !important; pointer-events: auto !important; }
    .sidebar .nav-item   { justify-content: flex-start !important; padding: 7px 14px !important; border-left: 3px solid transparent; }
    .sidebar .nav-item.active { border-left-color: var(--green); }
    .sidebar .nav-item .label { display: inline !important; }
    .sidebar .nav-item .badge { display: inline-block !important; }
    .sidebar .sb-user-info,
    .sidebar .sb-logout { display: flex !important; }
    .sidebar .sb-footer   { justify-content: flex-start !important; }

    .mob-topbar { display: flex; flex-shrink: 0; }

    /* main-area is a flex column; topbar is in-flow — no padding-top needed */
    .main-area {
        flex: 1;
        display: flex;
        flex-direction: column;
        min-height: 0;
        padding-top: 0;
    }

    /* main-content scrolls within its bounded flex cell */
    .main-content {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 16px;
        padding-bottom: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 8px));
    }

    .bottom-nav {
        display: flex;
        position: fixed; bottom: 0; left: 0; right: 0;
        height: var(--bottom-nav-h);
        background: var(--card);
        border-top: 1px solid var(--line);
        z-index: 50;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    .bn-item {
        flex: 1;
        display: flex; flex-direction: column;
        align-items: center; justify-content: center;
        gap: 2px;
        color: var(--muted); font-size: 10px;
        text-decoration: none; padding: 4px 0;
        border: none; background: none; cursor: pointer;
    }
    .bn-item i { font-size: 20px; }
    .bn-item.active { color: var(--green); }
    .bn-item:hover { color: var(--ink); text-decoration: none; }
}

/* ══ PUBLIC LAYOUT ══ */
.pub-topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 22px;
    background: var(--green); color: #fff;
}
.pub-brand { color: #fff; font-weight: 800; font-size: 20px; letter-spacing: .3px; }
.pub-brand:hover { text-decoration: none; color: #fff; }
.pub-nav { display: flex; align-items: center; gap: 18px; }
.pub-nav a { color: #dceae3; }
.pub-nav a:hover { color: #fff; text-decoration: none; }
.pub-content { max-width: 920px; margin: 0 auto; padding: 32px 22px; }
.pub-foot { max-width: 920px; margin: 0 auto; padding: 24px 22px; color: var(--muted); font-size: 13px; border-top: 1px solid var(--line); }

/* ══ PAGE HEADERS ══ */
.page-header {
    display: flex; align-items: flex-start; justify-content: space-between;
    margin-bottom: 24px; gap: 16px; flex-wrap: wrap;
}
.page-header h1 { font-size: 22px; font-weight: 700; margin: 0 0 4px; }
.page-header p  { font-size: 14px; color: var(--muted); margin: 0; }
.page-actions   { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

/* ══ STAT CARDS ══ */
.stats { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 24px; }
.stat {
    background: var(--card); border: 1px solid var(--line);
    border-radius: 12px; padding: 16px 20px; min-width: 110px;
}
.stat .n { font-size: 28px; font-weight: 800; color: var(--green); }
.stat .l { color: var(--muted); font-size: 12px; }

/* ══ CARDS & PANELS ══ */
.card {
    background: var(--card); border: 1px solid var(--line);
    border-radius: 14px; padding: 20px 22px;
    box-shadow: 0 1px 2px rgba(0,0,0,.03);
}
.card.narrow { max-width: 440px; margin: 8px auto; }
.card-section-title { margin: 0 0 16px; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.panel {
    background: var(--card); border: 1px solid var(--line);
    border-radius: 14px; padding: 18px 22px; margin: 14px 0;
}
.panel ul { margin: .4em 0 .8em; padding-left: 1.1em; }
.panel li { margin: .25em 0; }

/* ══ BUTTONS ══ */
.btn {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 9px 16px; border-radius: 9px;
    border: 1px solid transparent;
    font-weight: 600; cursor: pointer; font-size: 14px;
    text-decoration: none; transition: background 0.15s;
}
.btn:hover { text-decoration: none; }
.btn-accent  { background: var(--saffron); color: #3a1c0c; border-color: var(--saffron-600); }
.btn-accent:hover { background: var(--saffron-600); }
.btn-ghost   { background: transparent; color: var(--green); border-color: var(--green); }
.btn-ghost:hover { background: #e7f3ec; }
.btn-sm      { padding: 6px 12px; font-size: 13px; }
.btn.block   { width: 100%; justify-content: center; margin-top: 8px; }

/* ══ FORMS ══ */
form label { display: block; margin: 12px 0 5px; font-weight: 600; font-size: 14px; }
form input, form select, form textarea {
    width: 100%; padding: 10px 12px;
    border: 1px solid var(--line); border-radius: 9px;
    font-size: 15px; background: #fff; color: var(--ink);
}
form input:focus, form select:focus, form textarea:focus {
    outline: 2px solid #cfe0d9; border-color: var(--green);
}
.field-validation-error, .validation {
    color: #b3261e; font-size: 13px; display: block; margin-top: 4px;
}
.validation { margin-bottom: 6px; }

/* ══ DATA TABLE ══ */
.data-table-wrap { overflow-x: auto; }
table.grid {
    width: 100%; border-collapse: collapse;
    background: var(--card); border: 1px solid var(--line);
    border-radius: 12px; overflow: hidden;
}
table.grid th, table.grid td {
    text-align: left; padding: 11px 14px;
    border-bottom: 1px solid var(--line); font-size: 14px;
}
table.grid th { background: #f4f1ea; color: var(--muted); font-weight: 600; }
table.grid tr:last-child td { border-bottom: 0; }
table.grid tr:hover td { background: #faf9f6; }

/* ══ BADGES ══ */
.badge-active   { background: #d4edda; color: #155724; font-size: 11px; padding: 2px 8px; border-radius: 10px; }
.badge-inactive { background: #f4f1ea; color: var(--muted); font-size: 11px; padding: 2px 8px; border-radius: 10px; }

/* ══ PLACEHOLDER / COMING SOON ══ */
.placeholder-empty {
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 12px; padding: 60px 20px;
    background: var(--card); border: 1px dashed var(--line);
    border-radius: 14px; text-align: center; color: var(--muted);
}
.placeholder-empty i  { font-size: 48px; opacity: .3; }
.placeholder-empty h3 { font-size: 16px; color: var(--ink); margin: 0; }
.placeholder-empty p  { font-size: 14px; max-width: 36ch; margin: 0; }

/* ══ TOAST ══ */
.toast {
    display: flex; align-items: center; gap: 8px;
    padding: 10px 16px; border-radius: 10px;
    font-size: 14px; font-weight: 500;
    margin-bottom: 16px;
}
.toast-ok  { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.toast-err { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }

/* ══ MISC ══ */
h1 { font-size: 24px; margin: .1em 0 .5em; }
h2 { font-size: 19px; margin: .1em 0 .6em; }
h3 { font-size: 16px; margin: 1.2em 0 .5em; }
.muted { color: var(--muted); }
code { background: #f0ede5; padding: 1px 6px; border-radius: 6px; font-size: .92em; }

dl.kv { display: grid; grid-template-columns: 140px 1fr; gap: 8px 12px; margin: 0; }
dl.kv dt { color: var(--muted); font-size: 14px; }
dl.kv dd { margin: 0; font-weight: 600; }

.row-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 12px 0 18px; }
.row-form input { width: auto; flex: 1; min-width: 140px; }
