/* ============================================================================
   JFFFL Sankhya — shared responsive layer
   ----------------------------------------------------------------------------
   Loaded AFTER each template's inline <style> block, so rules of equal
   specificity here win by document order. Nothing outside a media query
   changes the desktop look except the fluid root size in section 1.

   Companion file: static/js/responsive.js (tags tables, builds the mobile nav).
   ========================================================================== */


/* ---------------------------------------------------------------------------
   1. Fluid root sizing  —  fixes "size is not same on PC vs laptop"
   ---------------------------------------------------------------------------
   Every template sizes its text in rem (0.62rem .. 1.5rem). rem resolves
   against the ROOT font size, not body, so scaling :root is what makes the
   whole UI track the viewport.

       360px phone  -> 14.0px      1920px desktop -> 16.1px
      1366px laptop -> 15.4px      2560px+        -> 17.0px

   The curve is deliberately shallow: paddings and the 60px navbar are still
   fixed px, so text must not outgrow them.
   ------------------------------------------------------------------------- */
:root { font-size: clamp(14px, 0.1364vw + 13.51px, 17px); }

/* body was hard-coded to 14px in all 10 dashboard templates. 0.875rem is the
   same 14px at the default root, but now scales with the clamp above. */
body { font-size: 0.875rem; }

/* Logos etc. never force a horizontal scrollbar. Templates that pin an
   explicit height (.navbar-brand img, .portal-header img) are more specific
   and keep winning. */
img { max-width: 100%; height: auto; }


/* ---------------------------------------------------------------------------
   2. Touch scrolling for the containers that scroll sideways
   ------------------------------------------------------------------------- */
.table-responsive,
.table-wrap,
.kpi-bar,
.chart-wrap-scroll,
.chart-wrap-scroll-v {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
}


/* ---------------------------------------------------------------------------
   3. Mobile navigation
   ---------------------------------------------------------------------------
   responsive.js wraps .nav-links + .navbar-right in .rt-menu and inserts the
   .rt-navtoggle button. On desktop .rt-menu is display:contents, so both
   children stay direct flex items of .navbar and the layout is untouched.
   ------------------------------------------------------------------------- */
.rt-menu { display: contents; }

.suite-signout {
    margin-left: auto;
    color: #c0392b;
    text-decoration: none;
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
}

.suite-signout:hover { text-decoration: underline; }

.rt-navtoggle {
    display: none;
    margin-left: auto;
    width: 38px; height: 34px;
    flex-direction: column; justify-content: center; gap: 5px;
    padding: 0 8px;
    background: #fff; border: 1.5px solid #d3dbe2; border-radius: 8px;
    cursor: pointer;
}
.rt-navtoggle span {
    display: block; height: 2px; width: 100%;
    background: #1a5c2e; border-radius: 2px;
    transition: transform 0.2s ease, opacity 0.2s ease;
}
.rt-navtoggle[aria-expanded="true"] span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.rt-navtoggle[aria-expanded="true"] span:nth-child(2) { opacity: 0; }
.rt-navtoggle[aria-expanded="true"] span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }


/* ---------------------------------------------------------------------------
   4. Pinned label column
   ---------------------------------------------------------------------------
   Below 1200px the wide tables scroll sideways, and without a pinned first
   column you lose track of which row you are reading.

   responsive.js tags each top-level table:
     .rt-pinned a cell is explicitly marked .rt-pin in the template — used
               where the first label is not the useful one (stock_report
               leads with a 4-digit plant code; Product is what identifies
               the row)
     .rt-num   first cell is the decorative {{ forloop.counter }} — dropped
               here, and the label (2nd cell) takes the pinned slot
     .rt-plain first cell IS the label — pinned directly

   The "#" header is always <th rowspan="2"> on the FIRST header row, so the
   hdr-cols row below must not be touched — hence tr:first-child.

   z-index budget. Several templates already stick their own header rows at
   z-index 5/6, which is exactly why an earlier pinned header rendered
   *underneath* the scrolling ones:
        12  pinned header cell   (must clear the page's own sticky headers)
         5  pinned cell in a sticky Grand Total row
         4  other cells of a sticky Grand Total row
         3  pinned body cell
   ------------------------------------------------------------------------- */
@media (max-width: 1200px) {

    table.rt-num > thead > tr:first-child > th:first-child,
    table.rt-num > tbody > tr > td:first-child:not([colspan]) {
        display: none;
    }

    /* A pinned cell shows whatever is behind it unless the row is opaque.
       Rows that already declare their own background are more specific and
       keep it; this only fills in the transparent ones. */
    .table-responsive tbody tr,
    .table-wrap tbody tr { background-color: #fff; }

    /* Several pages put the dark green only on tr.total-row td, which the
       inherit below cannot pick up — restate it on the row. */
    .table-responsive tr.total-row,
    .table-wrap tr.total-row { background-color: #1a5c2e; }

    table.rt-num > tbody > tr > td:first-child + td.lbl,
    table.rt-plain > tbody > tr > td.lbl:first-child,
    table.rt-plain > tbody > tr > td:first-child,
    table.rt-pinned > tbody > tr > td.rt-pin {
        position: sticky; left: 0; z-index: 3;
        background-color: inherit;
        box-shadow: 1px 0 0 rgba(26, 92, 46, 0.16);
    }

    table.rt-num > thead > tr:first-child > th:nth-child(2),
    table.rt-plain > thead > tr:first-child > th:first-child,
    table.rt-pinned > thead > tr > th.rt-pin {
        position: sticky; left: 0; z-index: 12;
        box-shadow: 1px 0 0 rgba(255, 255, 255, 0.22);
    }

    /* customer_breakdown, stock_report and friends stick their Grand Total to
       the bottom with no z-index, so body cells pinned above would slide over
       it. Lift the whole row, and its pinned cell one step further. */
    .table-responsive tr.total-row > td,
    .table-wrap tr.total-row > td { z-index: 4; }

    table.rt-num > tbody > tr.total-row > td:first-child + td.lbl,
    table.rt-plain > tbody > tr.total-row > td.lbl:first-child,
    table.rt-plain > tbody > tr.total-row > td:first-child,
    table.rt-pinned > tbody > tr.total-row > td.rt-pin { z-index: 5; }
}


/* ---------------------------------------------------------------------------
   5. Tablet / small laptop  (<= 1200px)
   ------------------------------------------------------------------------- */
@media (max-width: 1200px) {

    .navbar        { padding-left: 16px; padding-right: 16px; }
    .main-content  { padding-left: 16px; padding-right: 16px; }
    .page-header   { padding-left: 16px; padding-right: 16px; }

    /* One long horizontal strip of KPI cards becomes a wrapping grid. */
    .kpi-bar {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
        overflow-x: visible;
        padding-left: 16px; padding-right: 16px;
    }
    .kpi-card { min-width: 0; }

    .charts-row { padding-left: 16px; padding-right: 16px; }
    .chart-card { min-width: 0; }

    .filter-row { min-width: 0; }
}


/* ---------------------------------------------------------------------------
   6. Mobile  (<= 900px)  —  navbar collapses
   ------------------------------------------------------------------------- */
@media (max-width: 900px) {

    .navbar {
        height: auto; min-height: 56px;
        padding: 8px 12px;
        flex-wrap: wrap; row-gap: 8px;
    }
    .navbar-brand img  { height: 34px; }
    .navbar-brand span { font-size: 0.95rem; }

    .rt-navtoggle { display: flex; }

    .rt-menu {
        display: none;
        flex-basis: 100%;
        flex-direction: column; align-items: stretch; gap: 10px;
        padding-top: 4px; border-top: 1px solid var(--border-soft, #e8ecf0);
    }
    .navbar.rt-open .rt-menu { display: flex; }

    .nav-links {
        flex-direction: column; align-items: stretch;
        gap: 2px; margin-left: 0;
    }
    .nav-links a { padding: 10px 12px; font-size: 0.9rem; }

    /* :hover dropdowns do not exist on touch — flatten to an inline list. */
    .nav-dropdown-menu {
        display: block; position: static;
        border: none; box-shadow: none; border-radius: 0;
        min-width: 0; margin: 0; padding: 0 0 0 14px;
        border-left: 2px solid var(--border-soft, #e8ecf0);
    }
    .nav-dropdown > a { pointer-events: none; color: #8b96a3; font-size: 0.75rem;
                        text-transform: uppercase; letter-spacing: 0.5px; padding-bottom: 2px; }
    /* Headings, not buttons, once flattened -- drop the desktop active pill. */
    .nav-dropdown > a.active { background: none; color: #8b96a3; }
    /* Nested fly-outs (Reports > Enquiries / Free Stock) flatten the same way. */
    .nav-submenu-panel {
        display: block; position: static;
        border: none; box-shadow: none; border-radius: 0;
        min-width: 0; padding: 0 0 0 14px;
        border-left: 2px solid var(--border-soft, #e8ecf0);
    }
    .nav-submenu > .nav-submenu-trigger { pointer-events: none; color: #8b96a3; }
    .nav-submenu > .nav-submenu-trigger::after { content: none; }

    /* On the dashboard this sits inside the collapsed panel; on the report
       pages there is no panel, so it simply wraps to its own row and keeps
       the Download Excel button one tap away. */
    .navbar-right {
        margin-left: 0; width: 100%;
        flex-wrap: wrap; justify-content: space-between; gap: 10px;
    }
    .data-updated { text-align: left; }

    .back-link { margin-left: 12px; padding: 6px 12px; font-size: 0.8rem; }

    /* Charts stack full width and get shorter so a phone shows the header,
       the chart and the start of the table together.

       !important is unavoidable here: home.html and stock_report.html tune
       individual cards with inline style="flex: 2" / "flex: 1.3;
       min-width: 380px", and an inline declaration cannot be beaten any
       other way. Without this the 380px card alone overflows a 375px phone. */
    .charts-row { padding-left: 12px; padding-right: 12px; gap: 12px; }
    .chart-card {
        flex: 1 1 100% !important;
        min-width: 0 !important;
        padding: 14px;
    }

    .chart-card .chart-wrap { height: 210px; }
    /* Keep the two specialised wrappers distinguishable rather than letting
       the blanket height above flatten them. */
    .chart-card .chart-wrap.chart-wrap-donut  { height: 230px; }
    .chart-card .chart-wrap.chart-wrap-scroll-v { height: 300px; }
    .chart-wrap-donut { height: 200px; }
    .chart-box { height: 220px; }

    /* home.html pairs two donuts (this year / last year) side by side. Each
       item is a flex child whose default min-width:auto floors it at the
       canvas's intrinsic 300px, so the pair forced a 262px sideways bleed. */
    .chart-donut-pair { flex-wrap: wrap; }
    .chart-donut-item { min-width: 0; }
}


/* ---------------------------------------------------------------------------
   7. Phone  (<= 768px)
   ------------------------------------------------------------------------- */
@media (max-width: 768px) {

    .main-content { padding-left: 10px; padding-right: 10px; }

    .page-header {
        flex-direction: column; align-items: stretch;
        padding: 12px 12px 6px; gap: 10px;
    }
    /* stock_report / product_realisation put an inline display:flex on the
       right-hand block of the header; only !important can add wrapping. */
    .page-header > div { flex-wrap: wrap !important; }
    .page-header h1 { font-size: 1.05rem; }

    .kpi-bar {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px; padding: 12px 10px;
    }
    .kpi-card  { padding: 14px 12px 12px 16px; }
    .kpi-value { font-size: 1.18rem; }
    .kpi-ly    { padding: 6px 8px; margin-top: 8px; }

    .filter-bar { padding: 10px 10px 0; gap: 6px; }
    .filter-tab { padding: 5px 12px; font-size: 0.78rem; }
    .action-btn, .download-btn { padding: 5px 10px; font-size: 0.76rem; }
    .control-bar { padding: 10px 12px; }

    /* Search inputs stop being fixed-width and take the row. */
    .table-card-header { flex-wrap: wrap; gap: 8px; }
    .table-search { margin-left: 0; width: 100%; }
    .table-search input { width: 100%; flex: 1 1 auto; }
    .search-box { min-width: 0; width: 100%; }

    /* Tighter cells, and the pinned label is capped so it cannot swallow the
       screen — long customer names ellipsize instead. */
    .table-responsive tbody tr td,
    .table-wrap tbody tr td { padding-top: 7px; padding-bottom: 7px; }

    tr.cat-row > td.lbl,
    tr.user-row > td.lbl,
    tr.subgroup-row > td.lbl { padding-left: 20px; }
    tr.div-row > td.lbl { padding-left: 32px; }

    table.rt-num > tbody > tr > td:first-child + td.lbl,
    table.rt-plain > tbody > tr > td.lbl:first-child,
    table.rt-plain > tbody > tr > td:first-child,
    table.rt-pinned > tbody > tr > td.rt-pin,
    table.rt-pinned > thead > tr > th.rt-pin {
        max-width: 46vw;
        overflow: hidden; text-overflow: ellipsis;
    }

    /* Chart.js already runs responsive:true / maintainAspectRatio:false, so
       only the wrapper height needs adjusting. */
    .chart-card .chart-wrap { height: 190px; }
    .chart-card .chart-wrap.chart-wrap-scroll-v { height: 280px; }

    /* Two donuts at ~155px each are unreadable on a phone — stack them and
       give each one back its full height. */
    .chart-donut-pair { flex-direction: column; gap: 14px; }
    .chart-card .chart-wrap.chart-wrap-donut { height: 200px; }
    .chart-wrap-donut { height: 185px; }
}


/* ---------------------------------------------------------------------------
   8. Small phone  (<= 420px)
   ------------------------------------------------------------------------- */
@media (max-width: 420px) {
    .kpi-bar { grid-template-columns: minmax(0, 1fr); }
    .navbar-brand span  { display: none; }
    .kpi-value { font-size: 1.3rem; }

    table.rt-num > tbody > tr > td:first-child + td.lbl,
    table.rt-plain > tbody > tr > td.lbl:first-child,
    table.rt-plain > tbody > tr > td:first-child,
    table.rt-pinned > tbody > tr > td.rt-pin,
    table.rt-pinned > thead > tr > th.rt-pin { max-width: 40vw; }
}


/* ---------------------------------------------------------------------------
   9. Login portal  (body carries .portal-page)
   ------------------------------------------------------------------------- */
@media (max-width: 768px) {
    body.portal-page { padding: 28px 14px; }
    body.portal-page .portal-header .brand-row { flex-wrap: wrap; }
    body.portal-page .portal-header h1 { font-size: 1.2rem; }
    body.portal-page .tile-row { gap: 14px; margin-top: 26px; }
    body.portal-page .tile { width: 100%; max-width: 360px; }
    body.portal-page .tile p.tile-desc { min-height: 0; }
    body.portal-page .portal-footer { margin-top: 30px; }
}
