/* =========================================================================
   Auto Parts Inventory — application shell
   Desktop: fixed left sidebar + topbar. Mobile: off-canvas drawer + bottom nav.
   Theming via Bootstrap's [data-bs-theme].
   ========================================================================= */

:root {
    --brand:        #0f766e;
    --brand-dark:   #115e59;
    --brand-light:  #ccfbf1;
    --sidebar-w:    250px;
    --topbar-h:     58px;
    --bottomnav-h:  60px;
    --app-bg:       #f4f6f8;
    --card-bg:      #ffffff;
    --border:       #e5e7eb;
    --text-muted:   #6b7280;
}

[data-bs-theme="dark"] {
    --app-bg:     #0b1220;
    --card-bg:    #131c2e;
    --border:     #26324a;
    --text-muted: #93a1b5;
}

* { box-sizing: border-box; }

/* User text-size preference: scales every rem-based size (Bootstrap + app). */
:root { --app-font-scale: 1; }
html { font-size: calc(16px * var(--app-font-scale)); }

body {
    margin: 0;
    background: var(--app-bg);
    font-family: "Hind Siliguri", "Segoe UI", system-ui, -apple-system, sans-serif;
    -webkit-font-smoothing: antialiased;
}

/* --- Appearance preferences (persisted per device) --- */

/* Hide the left sidebar on desktop so the page gets the full width.
   Uses display:none rather than a transform so it is instant and cannot be
   left half-applied if transitions don't run (background tab, reduced-motion). */
@media (min-width: 992px) {
    html.sidebar-hidden .app-sidebar { display: none; }
    html.sidebar-hidden .app-main    { margin-left: 0; }
}

/* Hide navigation icons (sidebar, bottom nav, home grid) — text only. */
html.icons-hidden .app-nav-link i,
html.icons-hidden .app-bottomnav a i,
html.icons-hidden .home-grid a i { display: none; }
html.icons-hidden .home-grid a { padding: 1.1rem .4rem; }

/* Text-size / colour picker in the topbar */
.fc-swatch {
    width: 26px; height: 26px;
    border-radius: 50%;
    border: 2px solid var(--border);
    padding: 0;
    cursor: pointer;
}
.fc-swatch.is-active { outline: 2px solid var(--brand); outline-offset: 2px; }
.fc-swatch--default {
    background: linear-gradient(135deg, #ffffff 0 50%, #212529 50% 100%);
}

.app-layout { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.app-sidebar {
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    background: var(--card-bg);
    border-right: 1px solid var(--border);
    position: fixed;
    inset: 0 auto 0 0;
    display: flex;
    flex-direction: column;
    z-index: 1045;
    transition: transform .25s ease;
}

.app-sidebar__brand {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: 0 1rem;
    font-weight: 700;
    color: var(--brand);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    overflow: hidden;
}
.app-sidebar__brand i { font-size: 1.4rem; }

.app-sidebar__nav {
    flex: 1;
    overflow-y: auto;
    padding: .5rem;
}

.app-nav-link {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .6rem .8rem;
    border-radius: .5rem;
    color: inherit;
    text-decoration: none;
    font-size: .93rem;
    margin-bottom: 2px;
}
.app-nav-link i { font-size: 1.1rem; width: 1.3rem; text-align: center; color: var(--text-muted); }
.app-nav-link:hover { background: rgba(15,118,110,.08); }
.app-nav-link.is-active { background: var(--brand); color: #fff; }
.app-nav-link.is-active i { color: #fff; }

/* ---------- Sidebar groups (collapsible categories) ---------- */
.app-nav-group { margin-bottom: 2px; }

.app-nav-group__btn {
    display: flex;
    align-items: center;
    gap: .75rem;
    width: 100%;
    padding: .6rem .8rem;
    border: 0;
    border-radius: .5rem;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: .93rem;
    font-weight: 600;
    text-align: left;
}
.app-nav-group__btn i { font-size: 1.1rem; width: 1.3rem; text-align: center; color: var(--text-muted); }
.app-nav-group__btn:hover { background: rgba(15,118,110,.08); }

/* Caret sits at the far right and turns when the group opens. */
.app-nav-group__caret {
    margin-left: auto;
    font-size: .8rem !important;
    width: auto !important;
    transition: transform .18s ease;
}
.app-nav-group.is-open > .app-nav-group__btn > .app-nav-group__caret { transform: rotate(180deg); }

/* A closed group takes no space; an open one indents its links under the
   heading so the hierarchy is readable at a glance. */
.app-nav-group__items { display: none; padding-left: .55rem; margin-left: .5rem; border-left: 2px solid var(--border); }
.app-nav-group.is-open > .app-nav-group__items { display: block; }
.app-nav-group__items .app-nav-link { padding-top: .45rem; padding-bottom: .45rem; }

/* A group whose page is open stays visibly marked even while collapsed. */
.app-nav-group.has-active > .app-nav-group__btn { color: var(--brand); }
.app-nav-group.has-active > .app-nav-group__btn i { color: var(--brand); }

html.icons-hidden .app-nav-group__btn i:not(.app-nav-group__caret) { display: none; }

.app-sidebar__backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 1044;
}

/* ---------- Main column ---------- */
.app-main {
    flex: 1;
    margin-left: var(--sidebar-w);
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.app-topbar {
    height: var(--topbar-h);
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: 0 1rem;
    position: sticky;
    top: 0;
    z-index: 1030;
}
.app-topbar__title { font-size: 1.1rem; font-weight: 600; margin: 0; flex: 1; }
.app-topbar__actions { display: flex; align-items: center; gap: .25rem; }

.btn-icon {
    width: 40px; height: 40px;
    display: inline-flex; align-items: center; justify-content: center;
    border: 0; background: transparent; border-radius: 50%;
    color: inherit; font-size: 1.15rem;
}
.btn-icon:hover { background: rgba(0,0,0,.06); }
[data-bs-theme="dark"] .btn-icon:hover { background: rgba(255,255,255,.08); }
.lang-pill { font-size: .8rem; font-weight: 700; }

.app-content {
    padding: 1.25rem;
    flex: 1;
}

/* ---------- Bottom nav (mobile) ---------- */
.app-bottomnav {
    position: fixed;
    inset: auto 0 0 0;
    height: var(--bottomnav-h);
    background: var(--card-bg);
    border-top: 1px solid var(--border);
    display: flex;
    z-index: 1040;
    padding-bottom: env(safe-area-inset-bottom);
}
.app-bottomnav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: .68rem;
}
.app-bottomnav a i { font-size: 1.25rem; }
.app-bottomnav a.is-active { color: var(--brand); }

/* ---------- KPI / widget cards ----------
   Fixed layout: 4 per row on desktop, 3 per row on mobile — so each box is
   large and the rows line up predictably. */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);   /* 3 per row — bigger boxes */
    gap: 1.1rem;
}
.kpi-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: .9rem;
    padding: 1.6rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1.25rem;
    min-width: 0;                 /* let long amounts shrink instead of overflowing */
}
.kpi-card__icon {
    width: 68px; height: 68px; flex: 0 0 68px;
    border-radius: .8rem;
    display: flex; align-items: center; justify-content: center;
    font-size: 2rem; color: #fff;
    background: var(--brand);
}
.kpi-card__label { font-size: 1.25rem; color: var(--text-muted); }
.kpi-card__value { font-size: 2.9rem; font-weight: 700; line-height: 1.15; word-break: break-word; }
/* A card that links somewhere should say so on hover. */
a.kpi-card:hover { border-color: var(--brand); }

.card { background: var(--card-bg); border-color: var(--border); }

/* ---------- Home grid (menu tiles) ----------
   4 per row on desktop, 3 per row on mobile. */
.home-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: .85rem;
}
.home-grid a {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: .8rem;
    padding: 1.35rem .5rem;
    text-align: center;
    text-decoration: none;
    color: inherit;
    font-size: .95rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 104px;
}
.home-grid a:hover { border-color: var(--brand); }
.home-grid a i { display: block; font-size: 2rem; margin-bottom: .45rem; color: var(--brand); }

/* Category heading above each block of tiles — same wording as the sidebar. */
.home-grid__head {
    display: flex;
    align-items: center;
    gap: .45rem;
    font-weight: 600;
    font-size: .9rem;
    color: var(--text-muted);
    text-transform: none;
    margin: .35rem 0 .5rem;
    padding-bottom: .3rem;
    border-bottom: 1px solid var(--border);
}
.home-grid__head i { color: var(--brand); }

/* ---------- Responsive ---------- */
@media (max-width: 991.98px) {
    .app-sidebar { transform: translateX(-100%); }
    .app-sidebar.is-open { transform: translateX(0); }
    .app-sidebar__backdrop.is-open { display: block; }
    .app-main { margin-left: 0; }
    .app-content { padding: 1rem; padding-bottom: calc(var(--bottomnav-h) + 1rem); }

    /* 3 tiles per row on mobile */
    .kpi-grid, .home-grid { grid-template-columns: repeat(3, 1fr); gap: .6rem; }

    /* Stack the KPI card (icon above the text) so it fits the narrower box */
    .kpi-card {
        flex-direction: column;
        align-items: center;
        text-align: center;
        gap: .45rem;
        padding: .85rem .45rem;
    }
    .kpi-card__icon { width: 42px; height: 42px; flex: 0 0 42px; font-size: 1.2rem; }
    .kpi-card__label { font-size: .88rem; }
    .kpi-card__value { font-size: 1.4rem; }

    .home-grid a { padding: 1rem .35rem; font-size: .8rem; min-height: 92px; }
    .home-grid a i { font-size: 1.7rem; margin-bottom: .35rem; }
}

/* ---------- Login ---------- */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--brand) 0%, var(--brand-dark) 100%);
    padding: 1rem;
}
.login-card {
    width: 100%;
    max-width: 400px;
    background: #fff;
    border-radius: 1rem;
    padding: 2rem;
    box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.login-card__brand { text-align: center; margin-bottom: 1.5rem; color: var(--brand); }
.login-card__brand i { font-size: 2.5rem; }

/* =========================================================================
   Printing — applies to EVERY admin page via the shared shell.
   The app chrome (sidebar, topbar, bottom bar, buttons, filter forms) is
   dropped and a letterhead is revealed, so any list or ledger prints as a
   clean document. Add .no-print to anything else a page shouldn't print,
   or .print-only to something that should appear on paper alone.
   ========================================================================= */
.print-only { display: none !important; }

@media print {
    /* Paper, not screen: full width, no shadows, black on white. */
    :root { --app-bg: #fff; }
    html, body {
        background: #fff !important;
        color: #000 !important;
        font-size: 12px;
    }
    .app-sidebar,
    .app-sidebar__backdrop,
    .app-topbar,
    .app-bottomnav,
    .no-print,
    .btn,
    .dropdown,
    .pagination,
    .app-content > form,          /* search / date-filter bars */
    .card > form,
    input[type="search"] {
        display: none !important;
    }
    .app-layout, .app-main, .app-content {
        display: block !important;
        margin: 0 !important;
        padding: 0 !important;
        width: 100% !important;
        max-width: none !important;
    }
    .print-only { display: block !important; }

    /* Cards flatten into plain sections. Only small summary cards are kept
       whole — a card wrapping a long table must be allowed to split, or the
       whole table is pushed to the next page and page 1 prints empty. */
    .card, .kpi-card {
        border: 0 !important;
        box-shadow: none !important;
    }
    .kpi-card { break-inside: avoid; }
    .card, .card-body, .table-responsive { break-inside: auto; }
    .card + .card { margin-top: .5rem; }

    /* Tables: ruled, repeating header on every page, never split a row. */
    .table-responsive { overflow: visible !important; }
    table { width: 100% !important; border-collapse: collapse !important; }
    table th, table td {
        border: 1px solid #999 !important;
        padding: .2rem .35rem !important;
        color: #000 !important;
    }
    thead { display: table-header-group; }
    tfoot { display: table-footer-group; }
    tr, img { break-inside: avoid; }
    .table-hover tbody tr:hover { background: transparent !important; }

    /* Keep meaning that colour alone would carry. */
    .badge {
        border: 1px solid #666 !important;
        color: #000 !important;
        background: transparent !important;
    }
    a { text-decoration: none !important; color: #000 !important; }

}
/* Paper size itself is written by the print menu into #appPageSize (see
   footer.php), so A4 / A5 / landscape can be chosen per print job. */

/* 80mm receipt roll: squeeze type and drop non-essential table columns. */
@media print {
    body.print-pos { font-size: 9px; }
    body.print-pos table th,
    body.print-pos table td { padding: .08rem .12rem !important; }
    body.print-pos .print-head h4 { font-size: 1rem; }
    body.print-pos .kpi-grid { display: block !important; }
}

/* Letterhead shown only on paper. */
.print-head { text-align: center; margin-bottom: .75rem; }
.print-head h4 { margin: 0; font-size: 1.25rem; }
.print-head .ph-sub { font-size: .8rem; }
.print-head .ph-title {
    margin-top: .4rem;
    font-weight: 700;
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    padding: .2rem 0;
}
.print-foot {
    display: none;
    margin-top: 1rem;
    font-size: .75rem;
    border-top: 1px solid #999;
    padding-top: .25rem;
}
@media print { .print-foot { display: flex !important; justify-content: space-between; } }
