:root {
    --bg: #f5f7fb;
    --panel: #ffffff;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --primary: #173b57;
    --primary-soft: #e8f1f6;
    --success: #0f766e;
    --warning: #b45309;
    --danger: #b91c1c;
    --info: #2563eb;
    --shadow: 0 18px 50px rgba(17, 24, 39, 0.08);
    --radius: 18px;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--ink);
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }

.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: 270px;
    background: #0f2638;
    color: #ffffff;
    padding: 24px;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
}
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 36px; }
.brand-icon {
    width: 42px;
    height: 42px;
    border-radius: 14px;
    display: grid;
    place-items: center;
    background: #ffffff;
    color: #0f2638;
    font-weight: 800;
}
.brand small, .sidebar-footer small { color: rgba(255,255,255,.68); display: block; margin-top: 2px; }
.nav-list { display: grid; gap: 8px; }
.nav-list a {
    padding: 12px 14px;
    border-radius: 12px;
    color: rgba(255,255,255,.82);
}
.nav-list a:hover, .nav-list a.active { background: rgba(255,255,255,.11); color: #ffffff; }
.sidebar-footer { margin-top: auto; display: grid; gap: 12px; }
.logout { padding: 10px 12px; background: rgba(255,255,255,.12); border-radius: 12px; text-align: center; }

.main-content { flex: 1; padding: 30px; min-width: 0; }
.topbar {
    display: flex;
    justify-content: space-between;
    gap: 18px;
    align-items: center;
    margin-bottom: 24px;
}
.eyebrow { margin: 0 0 4px; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .08em; }
h1 { margin: 0; font-size: clamp(28px, 3vw, 42px); }
.user-pill { display: flex; align-items: center; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 10px 14px; min-width: 210px; box-shadow: var(--shadow); }
.user-pill small { display: block; color: var(--muted); }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); font-weight: 800; }

.grid { display: grid; gap: 18px; }
.grid.cards { grid-template-columns: repeat(4, minmax(160px, 1fr)); }
.grid.two { grid-template-columns: 1.1fr .9fr; }
.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}
.metric-card small { color: var(--muted); display: block; }
.metric-card strong { display: block; font-size: 32px; margin-top: 10px; }
.card-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 16px; }
.card-header h2 { margin: 0; font-size: 20px; }
.card-header p { margin: 4px 0 0; color: var(--muted); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 780px; }
th, td { text-align: left; padding: 13px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-size: 13px; font-weight: 700; background: #fafafa; }
td small { color: var(--muted); display: block; margin-top: 3px; }

.badge { display: inline-flex; align-items: center; border-radius: 999px; padding: 5px 10px; font-size: 12px; font-weight: 700; }
.badge.success { background: #dcfce7; color: #166534; }
.badge.warning { background: #fef3c7; color: #92400e; }
.badge.danger { background: #fee2e2; color: #991b1b; }
.badge.info { background: #dbeafe; color: #1d4ed8; }
.badge.neutral { background: #f3f4f6; color: #374151; }

.btn, button.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 0;
    border-radius: 12px;
    padding: 11px 14px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    font-weight: 700;
}
.btn.secondary { background: #eef2f7; color: var(--ink); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 7px 10px; font-size: 13px; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
label { display: grid; gap: 7px; color: #374151; font-weight: 700; font-size: 14px; }
input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    background: #ffffff;
    border-radius: 12px;
    padding: 11px 12px;
    color: var(--ink);
}
textarea { min-height: 96px; resize: vertical; }
.form-actions { margin-top: 14px; display: flex; gap: 10px; flex-wrap: wrap; }
.alert { border-radius: 14px; padding: 13px 15px; margin-bottom: 18px; background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.alert.error { background: #fef2f2; color: #991b1b; border-color: #fecaca; }

.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin-bottom: 18px; }
.filters label { min-width: 180px; }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(140deg, #0f2638, #173b57 48%, #f5f7fb 48%); }
.login-card { width: min(440px, 100%); background: white; border-radius: 26px; padding: 32px; box-shadow: 0 30px 80px rgba(0,0,0,.22); }
.login-card h1 { font-size: 30px; }
.login-card p { color: var(--muted); margin-bottom: 24px; }
.demo-box { margin-top: 18px; background: #f8fafc; border: 1px dashed #cbd5e1; border-radius: 14px; padding: 14px; color: var(--muted); font-size: 14px; }

.chart-box { min-height: 320px; }
.kpi-row { display: flex; gap: 10px; flex-wrap: wrap; }

@media (max-width: 980px) {
    .app-shell { display: block; }
    .sidebar {
        width: 100%;
        height: auto;
        position: sticky;
        z-index: 10;
        padding: 14px;
        border-bottom-left-radius: 18px;
        border-bottom-right-radius: 18px;
    }
    .brand { margin-bottom: 12px; }
    .nav-list { display: flex; overflow-x: auto; padding-bottom: 4px; }
    .nav-list a { white-space: nowrap; }
    .sidebar-footer { display: none; }
    .main-content { padding: 18px; }
    .topbar { align-items: flex-start; }
    .user-pill { min-width: auto; }
    .grid.cards { grid-template-columns: repeat(2, 1fr); }
    .grid.two { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
    .topbar { display: grid; }
    .grid.cards { grid-template-columns: 1fr; }
    .form-grid { grid-template-columns: 1fr; }
    .login-page { background: #f5f7fb; }
    .login-card { padding: 24px; }
}
.hidden { display: none !important; }
.inline-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.inline-form select { min-width: 130px; }
@media print {
    .sidebar, .topbar .user-pill, .filters .btn, .logout { display: none !important; }
    .main-content { padding: 0; }
    .card { box-shadow: none; break-inside: avoid; }
}
.muted-note { color: var(--muted); margin: 14px 0 0; line-height: 1.5; }

/* Mobile overflow control: keep the whole app inside the viewport */
html,
body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

img,
svg,
canvas,
video,
iframe {
    max-width: 100%;
}

@media (max-width: 980px) {
    .sidebar {
        max-width: 100%;
        overflow-x: hidden;
    }

    .nav-list {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 8px;
        overflow: visible;
        padding-bottom: 0;
    }

    .nav-list a {
        white-space: normal;
        text-align: center;
        min-width: 0;
        line-height: 1.25;
    }

    .main-content,
    .card,
    .topbar,
    .table-wrap,
    .filters,
    .form-grid,
    .inline-form {
        max-width: 100%;
        min-width: 0;
    }

    table {
        min-width: 0;
    }
}

@media (max-width: 640px) {
    .app-shell,
    .main-content,
    .sidebar,
    .card,
    .login-card {
        width: 100%;
        max-width: 100%;
    }

    .sidebar {
        padding: 12px;
        border-bottom-left-radius: 14px;
        border-bottom-right-radius: 14px;
    }

    .brand {
        gap: 10px;
        margin-bottom: 10px;
    }

    .brand-icon {
        width: 36px;
        height: 36px;
        border-radius: 12px;
        flex: 0 0 auto;
    }

    .nav-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    .nav-list a {
        padding: 9px 8px;
        font-size: 12px;
        border-radius: 10px;
        overflow-wrap: anywhere;
    }

    .main-content {
        padding: 12px;
    }

    .topbar {
        gap: 12px;
        margin-bottom: 14px;
    }

    h1 {
        font-size: 24px;
        line-height: 1.15;
        overflow-wrap: anywhere;
    }

    .eyebrow {
        font-size: 11px;
        overflow-wrap: anywhere;
    }

    .user-pill {
        width: 100%;
        min-width: 0;
        border-radius: 16px;
        padding: 10px;
        box-shadow: none;
    }

    .user-pill div {
        min-width: 0;
    }

    .user-pill strong,
    .user-pill small {
        overflow-wrap: anywhere;
    }

    .card {
        padding: 14px;
        border-radius: 16px;
    }

    .card-header {
        display: grid;
        align-items: start;
        gap: 10px;
    }

    .card-header h2 {
        font-size: 18px;
        line-height: 1.2;
    }

    .card-header p {
        font-size: 13px;
        line-height: 1.4;
    }

    .card-header .btn,
    .form-actions .btn,
    .filters .btn,
    .kpi-row .btn {
        width: 100%;
    }

    .grid {
        gap: 12px;
    }

    .grid.cards,
    .grid.two,
    .form-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .metric-card strong {
        font-size: 26px;
        overflow-wrap: anywhere;
    }

    .filters {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 10px;
        align-items: stretch;
    }

    .filters label,
    label,
    input,
    select,
    textarea,
    button,
    .btn {
        min-width: 0;
        max-width: 100%;
    }

    .filters label,
    .filters input,
    .filters select,
    .filters button,
    .form-actions,
    .form-actions .btn {
        width: 100%;
    }

    .inline-form {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 8px;
        width: 100%;
    }

    .inline-form select,
    .inline-form .btn {
        width: 100%;
        min-width: 0;
    }

    .kpi-row {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 8px;
    }

    .kpi-row .badge {
        justify-content: center;
        width: 100%;
        white-space: normal;
        text-align: center;
    }

    .table-wrap {
        overflow-x: visible;
    }

    .table-wrap table,
    .table-wrap thead,
    .table-wrap tbody,
    .table-wrap tr,
    .table-wrap th,
    .table-wrap td {
        width: 100%;
        max-width: 100%;
    }

    .table-wrap table {
        display: block;
        border-collapse: separate;
        border-spacing: 0;
        min-width: 0;
    }

    .table-wrap thead {
        display: none;
    }

    .table-wrap tbody,
    .table-wrap tr,
    .table-wrap td {
        display: block;
    }

    .table-wrap tr {
        margin-bottom: 12px;
        padding: 10px;
        border: 1px solid var(--line);
        border-radius: 14px;
        background: #ffffff;
    }

    .table-wrap td {
        border-bottom: 1px solid #f1f5f9;
        padding: 9px 0;
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    .table-wrap td:last-child {
        border-bottom: 0;
    }

    .table-wrap td[data-label] {
        display: grid;
        grid-template-columns: minmax(88px, 38%) minmax(0, 1fr);
        gap: 10px;
        align-items: start;
    }

    .table-wrap td[data-label]::before {
        content: attr(data-label);
        color: var(--muted);
        font-size: 11px;
        font-weight: 800;
        line-height: 1.3;
        text-transform: uppercase;
        letter-spacing: .04em;
        overflow-wrap: anywhere;
    }

    .table-wrap td[colspan] {
        text-align: center;
    }

    .chart-box {
        min-height: 260px;
        overflow: hidden;
    }

    .login-page {
        padding: 12px;
    }

    .login-card {
        padding: 22px;
        border-radius: 20px;
    }
}

/* Keep logout visible on tablet/mobile without causing horizontal scroll */
@media (max-width: 980px) {
    .sidebar-footer {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        gap: 8px;
        margin-top: 10px;
    }

    .sidebar-footer small {
        min-width: 0;
        font-size: 11px;
        line-height: 1.3;
        overflow-wrap: anywhere;
    }

    .logout {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        padding: 9px 12px;
        border-radius: 10px;
        white-space: nowrap;
    }
}

@media (max-width: 640px) {
    .sidebar-footer {
        grid-template-columns: minmax(0, 1fr);
        margin-top: 8px;
    }

    .sidebar-footer small {
        text-align: center;
    }

    .logout {
        width: 100%;
        font-size: 12px;
    }
}

/* Line graph analytics summary */
.chart-stats {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px;
    margin: 0 0 14px;
}

.chart-stats.premium-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.chart-stats span {
    display: grid;
    gap: 3px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #f8fafc;
    min-width: 0;
}

.chart-stats strong {
    font-size: 18px;
    line-height: 1.1;
    overflow-wrap: anywhere;
}

.chart-stats small {
    color: var(--muted);
    font-size: 11px;
    line-height: 1.25;
}

.line-analytics-card canvas {
    min-height: 250px;
}

@media (max-width: 640px) {
    .chart-stats {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }
    .chart-stats.premium-stats {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }


    .chart-stats span {
        padding: 9px;
    }

    .line-analytics-card canvas {
        min-height: 230px;
    }
}

/* Compact line graph sizing */
.compact-chart-frame {
    position: relative;
    width: min(100%, 620px);
    height: 220px;
    margin: 0 auto;
}

.line-analytics-card .compact-chart-frame canvas {
    width: 100% !important;
    height: 100% !important;
    min-height: 0;
    display: block;
}

@media (max-width: 640px) {
    .compact-chart-frame {
        width: 100%;
        height: 180px;
    }
}

/* Stat grids: keep metric cards in exactly two rows */
.grid.cards {
    grid-template-columns: none;
    grid-template-rows: repeat(2, minmax(0, auto));
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    align-items: stretch;
}

.grid.cards .metric-card {
    min-width: 0;
}

@media (max-width: 980px) {
    .grid.cards {
        grid-template-columns: none;
        grid-template-rows: repeat(2, minmax(0, auto));
        grid-auto-flow: column;
        grid-auto-columns: minmax(0, 1fr);
    }
}

@media (max-width: 640px) {
    .grid.cards {
        grid-template-columns: none;
        grid-template-rows: repeat(2, minmax(76px, auto));
        grid-auto-flow: column;
        grid-auto-columns: minmax(0, 1fr);
        gap: 8px;
    }

    .grid.cards .card {
        padding: 10px 8px;
    }

    .metric-card small {
        font-size: 10px;
        line-height: 1.2;
    }

    .metric-card strong {
        font-size: 18px;
        line-height: 1.15;
        margin-top: 6px;
    }
}


/* Mobile logout + compact stat cards fix */
.topbar-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.mobile-top-logout {
    display: none;
}

@media (max-width: 980px) {
    .topbar-actions {
        width: 100%;
        justify-content: space-between;
        align-items: stretch;
    }

    .mobile-top-logout {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        flex: 0 0 auto;
        padding: 10px 12px;
        border-radius: 12px;
        background: var(--primary);
        color: #ffffff;
        font-size: 13px;
        font-weight: 800;
        line-height: 1;
        white-space: nowrap;
    }

    .topbar .user-pill {
        flex: 1 1 auto;
        width: auto;
    }
}

@media (max-width: 640px) {
    .topbar-actions {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 8px;
        width: 100%;
    }

    .mobile-top-logout {
        min-width: 74px;
        padding: 9px 10px;
        border-radius: 10px;
        font-size: 12px;
    }

    .topbar .user-pill {
        width: 100%;
        min-width: 0;
        padding: 8px;
        border-radius: 12px;
    }

    .avatar {
        width: 32px;
        height: 32px;
        font-size: 13px;
    }

    .user-pill strong {
        font-size: 12px;
        line-height: 1.15;
    }

    .user-pill small {
        font-size: 10px;
        line-height: 1.15;
    }

    .grid.cards {
        grid-template-rows: repeat(2, minmax(54px, auto));
        gap: 6px;
    }

    .grid.cards .card,
    .grid.cards .metric-card {
        padding: 8px 7px !important;
        border-radius: 12px;
        box-shadow: none;
        min-height: 54px;
    }

    .metric-card small {
        font-size: 9px;
        line-height: 1.1;
        letter-spacing: 0;
    }

    .metric-card strong {
        font-size: 16px;
        line-height: 1.05;
        margin-top: 4px;
    }
}

@media (max-width: 390px) {
    .grid.cards {
        grid-auto-columns: minmax(0, 1fr);
        gap: 5px;
    }

    .grid.cards .card,
    .grid.cards .metric-card {
        padding: 7px 6px !important;
    }

    .metric-card strong {
        font-size: 15px;
    }
}
