:root {
    --bg: #f4f7f9;
    --surface: #ffffff;
    --surface-soft: #eef5f8;
    --ink: #202a30;
    --muted: #66747d;
    --line: #d7e1e6;
    --brand: #0874a8;
    --brand-dark: #04577f;
    --brand-soft: #e8f4f9;
    --charcoal: #253139;
    --success: #12806d;
    --amber: #a86d13;
    --shadow: 0 5px 18px rgba(20, 43, 56, 0.065);
}

:root[data-theme="dark"] {
    --bg: #10171b;
    --surface: #182126;
    --surface-soft: #1d2b32;
    --ink: #edf3f6;
    --muted: #a2afb6;
    --line: #2d3a41;
    --brand: #31a8dc;
    --brand-dark: #7bc9ea;
    --brand-soft: #163541;
    --success: #49b99b;
    --amber: #e4ad50;
    --shadow: 0 7px 22px rgba(0, 0, 0, 0.2);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--ink);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
}

.login-page {
    display: grid;
    min-height: 100vh;
    place-items: center;
    padding: 24px;
    background: linear-gradient(135deg, #eef5f8 0%, #f8fafb 48%, #e7f3f7 100%);
}

.login-shell { width: min(430px, calc(100vw - 48px)); }

.login-panel {
    display: grid;
    gap: 20px;
    padding: 34px;
    background: #fff;
    border: 1px solid var(--line);
    border-top: 4px solid var(--brand);
    border-radius: 8px;
    box-shadow: 0 18px 50px rgba(25, 52, 65, 0.13);
}

.login-panel img { width: min(100%, 260px); height: auto; margin-bottom: 8px; }
.login-panel span { color: var(--brand-dark); font-size: 12px; font-weight: 800; text-transform: uppercase; }
.login-panel h1 { margin: 5px 0 0; font-size: 27px; letter-spacing: 0; }
.login-panel label { display: grid; gap: 7px; color: var(--muted); font-size: 13px; font-weight: 700; }
.login-panel input { width: 100%; padding: 12px 13px; color: var(--ink); background: #fff; border: 1px solid #bdcbd2; border-radius: 5px; }
.login-panel input:focus { outline: 3px solid rgba(8, 116, 168, 0.14); border-color: var(--brand); }
.login-panel .primary-button { width: 100%; min-height: 46px; }
.login-error { margin: 0; padding: 11px 12px; color: #8f2e2e; background: #fff2f2; border: 1px solid #edc5c5; border-radius: 5px; }

button, input, select { font: inherit; }

.app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    display: flex;
    width: 276px;
    flex-direction: column;
    gap: 28px;
    padding: 24px 18px;
    overflow-y: auto;
    background: #ffffff;
    color: var(--ink);
    border-right: 1px solid var(--line);
}

.brand {
    display: block;
    padding: 4px 5px 18px;
    border-bottom: 1px solid var(--line);
}

.brand-logo {
    display: block;
    width: 100%;
    max-width: 225px;
    height: auto;
}

.brand-logo-dark { display: none; }

.side-nav { display: grid; gap: 5px; }

.side-nav a {
    position: relative;
    display: flex;
    align-items: center;
    gap: 11px;
    color: #47545d;
    text-decoration: none;
    padding: 12px 14px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 650;
    line-height: 1.2;
    transition: background-color 160ms ease, color 160ms ease;
}

.side-nav svg { width: 18px; height: 18px; flex: 0 0 18px; stroke-width: 1.9; }

.side-nav a:hover,
.side-nav a:focus,
.side-nav a.active {
    background: #eaf5fa;
    color: var(--brand-dark);
}

.side-nav a.active { box-shadow: inset 3px 0 0 var(--brand); }

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    min-height: 42px;
    margin: 0;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
}

.theme-toggle > svg { width: 17px; height: 17px; }

.nav-theme-toggle {
    width: 100%;
    justify-content: flex-start;
    margin-top: 10px;
}

.theme-toggle-label {
    margin-right: auto;
    color: inherit;
    font-size: 13px;
    font-weight: 700;
}

.theme-switch {
    position: relative;
    width: 38px;
    height: 22px;
    border-radius: 999px;
    background: #cad7dd;
    transition: background-color 160ms ease;
}

.theme-switch span {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 4px rgba(20, 43, 56, 0.22);
    transition: transform 160ms ease;
}

:root[data-theme="dark"] .theme-switch { background: var(--brand); }
:root[data-theme="dark"] .theme-switch span { transform: translateX(16px); }

.sidebar-card {
    margin-top: auto;
    padding: 15px;
    border: 1px solid #bcdbe9;
    border-radius: 7px;
    background: var(--brand-soft);
}

.sidebar-card span,
.sidebar-card strong,
.sidebar-card small { display: block; }

.sidebar-card span,
.sidebar-card small { color: #61717b; font-size: 12px; }
.sidebar-card strong { margin: 4px 0; font-size: 15px; }
.sidebar-card a { display: inline-block; margin-top: 10px; color: var(--brand-dark); font-size: 12px; font-weight: 800; text-decoration: none; }

.app-main {
    min-height: calc(100vh - 70px);
    margin-left: 276px;
    padding: 30px;
}

.page-heading {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 22px;
    max-width: 1560px;
    margin: 0 auto 22px;
}

.page-heading p,
.panel-heading span,
.metric-card > span,
.benefit-list span,
.gst-total span {
    margin: 0;
    color: var(--muted);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.page-heading p { color: var(--brand-dark); }

.page-heading h1 {
    margin: 5px 0 4px;
    font-size: 30px;
    line-height: 1.15;
    letter-spacing: 0;
}

.heading-note { color: var(--muted); font-size: 14px; }

.date-filter,
.view-toggle {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface);
}

.filter-group { position: relative; display: flex; flex-direction: column; align-items: flex-end; gap: 7px; flex: 0 0 auto; }
.filter-group .date-filter { flex: 1 1 auto; }

.active-filter-label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--muted);
    font-size: 12px;
}

.active-filter-label svg { width: 14px; height: 14px; color: var(--brand); }
.active-filter-label strong { color: var(--ink); }

.custom-filter-button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.custom-filter-button svg { width: 15px; height: 15px; }

.custom-date-panel {
    position: absolute;
    z-index: 30;
    top: calc(100% + 8px);
    right: 0;
    width: min(360px, calc(100vw - 32px));
    padding: 15px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 18px 45px rgba(20, 43, 56, 0.16);
}

.custom-date-panel[hidden] { display: none; }
.custom-date-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }

.custom-date-fields label {
    color: var(--muted);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.custom-date-fields input {
    width: 100%;
    height: 40px;
    margin-top: 6px;
    padding: 0 9px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--ink);
}

.custom-date-fields input:focus { border-color: var(--brand); outline: 3px solid rgba(8, 116, 168, 0.12); }
.filter-message { min-height: 16px; margin: 8px 0 0; color: #a43d2d; font-size: 12px; }
.custom-date-actions { display: flex; justify-content: flex-end; gap: 8px; }

.primary-button,
.secondary-button {
    min-height: 36px;
    padding: 0 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 800;
}

.primary-button { border: 1px solid var(--brand); background: var(--brand); color: #fff; }
.secondary-button { border: 1px solid var(--line); background: #fff; color: var(--muted); }

.date-filter button,
.view-toggle button {
    min-height: 34px;
    padding: 0 12px;
    border: 0;
    border-radius: 5px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 13px;
    font-weight: 700;
}

.date-filter button:hover,
.view-toggle button:hover { color: var(--brand-dark); }

.date-filter button.active,
.view-toggle button.active {
    background: var(--brand);
    color: #fff;
    box-shadow: 0 2px 7px rgba(8, 116, 168, 0.22);
}

button:focus-visible,
a:focus-visible,
input:focus-visible { outline: 3px solid rgba(8, 116, 168, 0.2); outline-offset: 2px; }

.kpi-grid,
.dashboard-grid,
.value-overview-row,
.procurement-grid {
    display: grid;
    max-width: 1560px;
    margin-right: auto;
    margin-left: auto;
    gap: 14px;
}

.value-overview-row { grid-template-columns: 1fr; margin-bottom: 14px; }
.kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.dashboard-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); align-items: stretch; margin-top: 14px; }
.dashboard-grid #savings { grid-column: 1 / span 2; grid-row: 1; }
.dashboard-grid .savings-category-panel { grid-column: 3; grid-row: 1; }

.procurement-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: auto auto;
    align-items: stretch;
    margin-top: 14px;
}

.products-panel { grid-column: 1 / -1; grid-row: 1; }
.procurement-grid .brands-panel { grid-column: 1; grid-row: 2; }
.procurement-grid .operations-panel { grid-column: 2; grid-row: 2; }

.metric-card,
.panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.panel { border-color: #d5e0e5; }

.metric-card {
    position: relative;
    min-height: 148px;
    padding: 18px 19px;
    overflow: hidden;
    border-top: 3px solid #c5d1d8;
}

.metric-card::after {
    position: absolute;
    top: 17px;
    right: 18px;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #c5d1d8;
    content: "";
}

.metric-card strong {
    display: block;
    margin-top: 11px;
    font-size: 28px;
    line-height: 1.12;
    letter-spacing: 0;
}

.metric-card small { display: block; margin-top: 10px; color: var(--muted); }

.metric-card dl { display: grid; gap: 7px; margin: 13px 0 0; }

.metric-card dl div {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    color: var(--muted);
    font-size: 13px;
}

.metric-card dt,
.metric-card dd { margin: 0; }
.metric-card dd { color: var(--ink); font-weight: 800; }

.savings-card {
    border-top-color: var(--success);
    background: #f0f9f6;
}

.savings-card::after { background: var(--success); }
.wallet-card { border-top-color: #c5d1d8; background: var(--surface); }
.wallet-card::after { background: #c5d1d8; }
.positive { color: var(--success) !important; font-weight: 800; }

.panel { padding: 19px; overflow: hidden; }
.panel.wide { grid-column: span 2; }
.panel.full { grid-column: 1 / -1; }

.panel-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.panel-heading h2 { margin: 4px 0 0; font-size: 19px; line-height: 1.25; letter-spacing: 0; }
.panel-heading > strong { color: var(--brand-dark); font-size: 21px; white-space: nowrap; }

canvas { display: block; width: 100%; max-width: 100%; }

.category-list,
.brand-list,
.benefit-list { display: grid; gap: 16px; }

.category-row > div:first-child,
.brand-row > div:first-child {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 8px;
}

.category-row span,
.brand-row span { color: var(--muted); font-weight: 800; }

.bar-track {
    height: 7px;
    overflow: hidden;
    border-radius: 999px;
    background: #e6eef2;
}

.bar-track span { display: block; height: 100%; border-radius: inherit; background: var(--brand); }
.bar-track.muted span { background: var(--success); }

.highlight-panel {
    display: flex;
    flex-direction: column;
    background: #f2f8fb;
    color: var(--ink);
    border-color: #b9d9e7;
    border-top: 3px solid var(--brand);
}
.highlight-panel .panel-heading span,
.highlight-panel p,
.highlight-panel small { color: var(--muted); }

.lifetime-content {
    display: grid;
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
    align-items: end;
    gap: 28px;
    flex: 1;
}

.lifetime-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-bottom: 0;
}

.lifetime-summary > div { min-width: 0; padding-right: 14px; }
.lifetime-summary > div + div { padding-right: 0; padding-left: 14px; border-left: 1px solid #c7dce5; }

.lifetime-summary span,
.price-row span {
    display: block;
    color: var(--muted);
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.lifetime-summary strong { display: block; margin: 7px 0 5px; color: var(--brand-dark); font-size: 28px; line-height: 1; }
.lifetime-summary > div:nth-child(2) strong { color: var(--success); }
.lifetime-summary small { display: block; line-height: 1.35; }

.price-comparison { display: grid; gap: 12px; margin-top: 0; }
.price-row > div:first-child { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.price-row strong { color: var(--ink); font-size: 13px; }
.price-track { height: 7px; overflow: hidden; border-radius: 999px; background: #dce9ee; }
.price-track span { display: block; height: 100%; border-radius: inherit; background: #7f919a; }
.price-row:nth-child(2) .price-track span { background: #1688bd; }
.price-row:nth-child(3) .price-track span { background: #29a083; }

.table-responsive { overflow-x: auto; }
table { width: 100%; min-width: 560px; border-collapse: collapse; }
.product-table table { min-width: 880px; }
th, td { padding: 12px; border-bottom: 1px solid var(--line); text-align: left; }
th { background: #f2f6f8; color: var(--muted); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; }
tbody tr:hover { background: #f1f8fb; }
td:first-child { width: 58px; color: var(--muted); }
td:last-child, th:last-child { text-align: right; font-weight: 800; }
td.numeric,
th.numeric { text-align: right; white-space: nowrap; }
td.savings-cell { color: var(--success); font-weight: 800; }
.product-table { display: none; }
.product-table.active { display: block; }

.benefit-list div,
.gst-total {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #f6f9fa;
}

.operations-panel .benefit-list { grid-template-columns: 1fr; }
.operations-panel .benefit-list div { min-height: 86px; }
.operations-panel .gst-benefit { border-color: #ead7b7; background: #fffaf1; }

.benefit-list strong,
.gst-total strong { font-size: 21px; }
.gst-panel { border-top: 3px solid var(--amber); }
.gst-total { background: #fffaf1; }

.app-footer {
    display: flex;
    justify-content: space-between;
    gap: 20px;
    margin-left: 276px;
    padding: 18px 30px;
    border-top: 1px solid var(--line);
    background: #fff;
    color: var(--muted);
    font-size: 12px;
}

.app-footer span:last-child { margin-left: auto; text-align: right; }

:root[data-theme="dark"] .app-sidebar,
:root[data-theme="dark"] .app-footer { background: var(--surface); }
:root[data-theme="dark"] .brand-logo-light { display: none; }
:root[data-theme="dark"] .brand-logo-dark { display: block; }
:root[data-theme="dark"] .side-nav a { color: #b8c4ca; }
:root[data-theme="dark"] .side-nav a:hover,
:root[data-theme="dark"] .side-nav a:focus,
:root[data-theme="dark"] .side-nav a.active { background: var(--brand-soft); color: var(--brand-dark); }
:root[data-theme="dark"] .sidebar-card { border-color: #31525f; background: var(--brand-soft); }
:root[data-theme="dark"] .sidebar-card span,
:root[data-theme="dark"] .sidebar-card small { color: var(--muted); }
:root[data-theme="dark"] .custom-date-panel,
:root[data-theme="dark"] .custom-date-fields input,
:root[data-theme="dark"] .secondary-button { background: var(--surface); color: var(--ink); }
:root[data-theme="dark"] .savings-card { background: #172c27; }
:root[data-theme="dark"] .wallet-card { border-top-color: #52636c; background: var(--surface); }
:root[data-theme="dark"] .wallet-card::after { background: #52636c; }
:root[data-theme="dark"] .highlight-panel { background: #162b35; border-color: #28566a; }
:root[data-theme="dark"] .lifetime-summary > div + div { border-color: #31515f; }
:root[data-theme="dark"] .price-track,
:root[data-theme="dark"] .bar-track { background: #29383f; }
:root[data-theme="dark"] th { background: #202d33; }
:root[data-theme="dark"] tbody tr:hover { background: #1c3039; }
:root[data-theme="dark"] .benefit-list div,
:root[data-theme="dark"] .gst-total { background: #202c32; }
:root[data-theme="dark"] .operations-panel .gst-benefit { border-color: #5c4b2c; background: #2c281f; }

@media (max-width: 1200px) {
    .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .dashboard-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .dashboard-grid > .panel { grid-column: 1 / -1; }
    .dashboard-grid #savings,
    .dashboard-grid .savings-category-panel { grid-column: 1 / -1; grid-row: auto; }
    .procurement-grid { grid-template-columns: 1fr; grid-template-rows: auto; }
    .products-panel,
    .procurement-grid .brands-panel,
    .procurement-grid .operations-panel { grid-column: 1; grid-row: auto; }
    .products-panel { order: 1; }
    .procurement-grid .brands-panel { order: 2; }
    .procurement-grid .operations-panel { order: 3; }
    .operations-panel .benefit-list { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
    .app-sidebar {
        position: sticky;
        inset: auto;
        top: 0;
        width: 100%;
        min-height: auto;
        flex-direction: row;
        align-items: center;
        gap: 16px;
        padding: 12px 16px;
        overflow-x: auto;
    }

    .brand { flex: 0 0 155px; padding: 0 14px 0 0; border-right: 1px solid var(--line); border-bottom: 0; }
    .side-nav { display: flex; min-width: max-content; }
    .side-nav { scrollbar-width: none; }
    .side-nav::-webkit-scrollbar { display: none; }
    .side-nav a { padding: 10px 12px; }
    .side-nav a::before { display: none; }
    .nav-theme-toggle { width: auto; flex: 0 0 auto; margin: 0; }
    .theme-toggle-label { display: none; }
    .sidebar-card { display: none; }
    .app-main { margin-left: 0; padding: 22px; }
    .app-footer { margin-left: 0; padding: 18px 22px; }
    .page-heading { align-items: stretch; flex-direction: column; }
    .lifetime-content { grid-template-columns: 1fr; align-items: stretch; gap: 20px; }
}

@media (max-width: 640px) {
    .app-sidebar { align-items: flex-start; flex-direction: column; gap: 8px; overflow-x: hidden; overflow-y: visible; }
    .brand { flex: 0 0 auto; width: 155px; border-right: 0; }
    .side-nav { width: 100%; min-width: 0; max-width: 100%; overflow-x: auto; }
    .app-main { padding: 16px; }
    .page-heading h1 { font-size: 26px; }
    .kpi-grid,
    .dashboard-grid,
    .procurement-grid { grid-template-columns: 1fr; }
    .panel.wide,
    .panel.full { grid-column: auto; }
    .date-filter,
    .view-toggle { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); width: 100%; }
    .filter-group { width: 100%; align-items: flex-start; }
    .panel-heading { flex-direction: column; }
    .operations-panel .benefit-list { grid-template-columns: 1fr; }
    .benefit-list div,
    .gst-total { align-items: flex-start; flex-direction: column; }
    .app-footer { align-items: flex-start; flex-direction: column; }
    .app-footer span:last-child { margin-left: 0; text-align: left; }
}

@media (max-width: 480px) {
    .custom-date-panel { position: static; width: 100%; margin-top: 8px; }
    .custom-date-fields { grid-template-columns: 1fr; }
}

@media (max-width: 420px) {
    .metric-card dl div,
    .category-row > div:first-child,
    .brand-row > div:first-child { flex-direction: column; gap: 2px; }
}

@media (max-width: 360px) {
    .lifetime-summary { grid-template-columns: 1fr; gap: 14px; }
    .lifetime-summary > div + div { padding: 14px 0 0; border-top: 1px solid #c7dce5; border-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    * { transition: none !important; }
}
