*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #f1f5f9;
    --sidebar-bg: #0f172a;
    --sidebar-text: #94a3b8;
    --sidebar-active: #38bdf8;
    --card-bg: #ffffff;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #16a34a;
    --warning: #ea580c;
    --danger: #dc2626;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --sidebar-w: 250px;
}

body { font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; -webkit-text-size-adjust: 100%; }

/* ===== LAYOUT ===== */
.layout { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w); background: var(--sidebar-bg); color: var(--sidebar-text);
    display: flex; flex-direction: column; position: fixed; top: 0; left: 0;
    height: 100vh; z-index: 200; overflow-y: auto;
    transform: translateX(-100%);
    transition: transform .25s ease;
}
.sidebar.open { transform: translateX(0); }

.sidebar-brand { padding: 24px 20px 16px; border-bottom: 1px solid rgba(255,255,255,.08); position: relative; }
.sidebar-brand h2 { color: #fff; font-size: 1.25rem; font-weight: 700; }
.sidebar-brand .badge { font-size: .7rem; background: var(--primary); color: #fff; padding: 2px 8px; border-radius: 10px; display: inline-block; margin-top: 4px; }

.sidebar-logos { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.sidebar-logo { height: 44px; width: auto; border-radius: 4px; }
.sidebar-logo-flag { height: 34px; }

.sidebar-close {
    display: block; position: absolute; top: 16px; right: 16px;
    background: none; border: none; color: #fff; font-size: 1.5rem; cursor: pointer;
    width: 36px; height: 36px; line-height: 36px; text-align: center; border-radius: 50%;
}
.sidebar-close:hover { background: rgba(255,255,255,.1); }

.nav-menu { list-style: none; padding: 12px 0; flex: 1; }
.nav-menu li a {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 20px; color: var(--sidebar-text);
    text-decoration: none; font-size: .9rem; transition: all .15s;
}
.nav-menu li a:hover { color: #fff; background: rgba(255,255,255,.06); }
.nav-menu li a.active { color: var(--sidebar-active); background: rgba(56,189,248,.08); border-right: 3px solid var(--sidebar-active); }
.nav-menu .icon { font-size: .75rem; width: 20px; text-align: center; }

.sidebar-footer { padding: 16px 20px; border-top: 1px solid rgba(255,255,255,.08); }
.sidebar-footer a { color: var(--sidebar-text); text-decoration: none; font-size: .85rem; }
.sidebar-footer a:hover { color: #fff; }

.mobile-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 150;
}
.mobile-overlay.open { display: block; }

body.sidebar-open { overflow: hidden; }

.content { flex: 1; padding: 24px 32px; min-width: 0; max-width: 100%; }

.page-header { margin-bottom: 24px; display: flex; align-items: center; gap: 12px; }
.page-header h1 { font-size: 1.5rem; font-weight: 700; color: var(--text); }

.menu-toggle {
    display: flex; align-items: center; justify-content: center;
    background: var(--sidebar-bg); color: #fff; border: none; border-radius: var(--radius);
    width: 42px; height: 42px; font-size: 1.3rem; cursor: pointer; flex-shrink: 0;
    transition: background .15s;
    touch-action: manipulation; -webkit-tap-highlight-color: rgba(0,0,0,.1);
    position: relative; z-index: 10; user-select: none;
}
.menu-toggle:hover, .menu-toggle:active { background: var(--primary); }

.sidebar-close {
    touch-action: manipulation; -webkit-tap-highlight-color: rgba(255,255,255,.2); user-select: none;
}

/* ===== STAT CARDS ===== */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card {
    background: var(--card-bg); border-radius: var(--radius); padding: 16px 20px; box-shadow: var(--shadow);
    border-left: 4px solid var(--primary);
}
.stat-card.green { border-left-color: var(--success); }
.stat-card.orange { border-left-color: var(--warning); }
.stat-card.red { border-left-color: var(--danger); }
.stat-card .label { font-size: .75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 4px; }
.stat-card .value { font-size: 1.5rem; font-weight: 700; }
.stat-card .sub { font-size: .75rem; color: var(--text-muted); margin-top: 2px; }

/* ===== CHARTS ===== */
.charts-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(400px, 1fr)); gap: 20px; margin-bottom: 24px; }

/* ===== CARDS ===== */
.card {
    background: var(--card-bg); border-radius: var(--radius); padding: 20px;
    box-shadow: var(--shadow); margin-bottom: 20px; overflow: hidden;
}
.card h3 { font-size: 1rem; font-weight: 600; margin-bottom: 16px; color: var(--text); }
.card canvas { max-height: 400px; }

/* ===== TABLES ===== */
.card > table.data-table,
.card > .scroll-table { overflow-x: auto; -webkit-overflow-scrolling: touch; display: block; }
.scroll-table > table.data-table { min-width: 600px; }
table.data-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
table.data-table th {
    background: #f8fafc; padding: 10px 12px; text-align: left;
    font-weight: 600; color: var(--text-muted); border-bottom: 2px solid var(--border);
    font-size: .7rem; text-transform: uppercase; letter-spacing: .5px; position: sticky; top: 0;
    white-space: nowrap;
}
table.data-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data-table td a { white-space: normal; }
table.data-table tr:hover { background: #f8fafc; }
table.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }

.grade-badge {
    display: inline-block; padding: 2px 8px; border-radius: 4px;
    font-size: .7rem; font-weight: 600; color: #fff; min-width: 24px; text-align: center;
}
.grade-A { background: var(--success); }
.grade-B { background: #2563eb; }
.grade-C { background: #7c3aed; }
.grade-S { background: var(--warning); }
.grade-W { background: var(--danger); }

.bar-fill { height: 8px; border-radius: 4px; background: var(--primary); transition: width .5s; }
.bar-container { background: var(--border); border-radius: 4px; height: 8px; width: 120px; display: inline-block; vertical-align: middle; }

/* ===== BUTTONS ===== */
.btn {
    display: inline-block; padding: 8px 20px; border: none; border-radius: var(--radius);
    font-size: .875rem; font-weight: 500; cursor: pointer; text-decoration: none; transition: all .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ===== ALERTS & BADGES ===== */
.alert { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; font-size: .9rem; }
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

.badge { font-size: .7rem; padding: 3px 8px; border-radius: 10px; font-weight: 500; white-space: nowrap; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-warning { background: #fff7ed; color: #9a3412; }

/* ===== FILTERS ===== */
.filter-bar {
    display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; align-items: flex-end;
}
.filter-bar select, .filter-bar input {
    padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius);
    font-size: .875rem; background: #fff; min-width: 0;
}
.filter-bar label { font-size: .8rem; color: var(--text-muted); font-weight: 500; }
.filter-group { display: flex; flex-direction: column; gap: 4px; }

/* ===== MISC ===== */
.import-page .card { max-width: 600px; }
.import-page .card + .card { margin-top: 16px; }
.rank-medal { font-size: 1.1rem; }
.scroll-table { max-height: 600px; overflow-y: auto; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

.progress-bar-stacked {
    display: flex; height: 20px; border-radius: 4px; overflow: hidden; background: #e5e7eb; min-width: 80px;
}
.progress-bar-stacked .seg { height: 100%; transition: width .5s; }

.comparison-row { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.comparison-row .label-text { width: 120px; font-size: .85rem; text-align: right; }
.comparison-row .bar-outer { flex: 1; background: var(--border); border-radius: 4px; height: 24px; position: relative; }
.comparison-row .bar-inner { height: 100%; border-radius: 4px; display: flex; align-items: center; padding-left: 8px; font-size: .75rem; color: #fff; font-weight: 600; min-width: 40px; transition: width .5s; }
.comparison-row .val { width: 60px; font-size: .85rem; font-weight: 600; }

/* ====================================================
   TABLET (max 1024px)
   ==================================================== */
@media (max-width: 1024px) {
    .content { padding: 20px; }
    .charts-grid { grid-template-columns: 1fr; }
    .two-col { grid-template-columns: 1fr; }
    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
}

/* ====================================================
   MOBILE (max 768px)
   ==================================================== */
@media (max-width: 768px) {
    :root { --sidebar-w: 280px; }

    .content { padding: 16px 12px; }
    .page-header h1 { font-size: 1.15rem; }
    .menu-toggle { width: 38px; height: 38px; font-size: 1.1rem; }

    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .stat-card { padding: 12px 14px; }
    .stat-card .value { font-size: 1.25rem; }
    .stat-card .label { font-size: .65rem; }
    .stat-card .sub { font-size: .65rem; }

    .charts-grid { grid-template-columns: 1fr; gap: 12px; }

    .card { padding: 14px; margin-bottom: 14px; overflow-x: auto; }
    .card h3 { font-size: .9rem; margin-bottom: 12px; }
    .card canvas { max-height: 280px; }

    table.data-table { font-size: .75rem; }
    table.data-table th { padding: 8px 8px; font-size: .6rem; }
    table.data-table td { padding: 6px 8px; }
    .grade-badge { font-size: .6rem; padding: 1px 5px; min-width: 20px; }
    .bar-container { width: 80px; }
    .progress-bar-stacked { height: 16px; min-width: 60px; }

    .filter-bar { flex-direction: column; align-items: stretch; gap: 8px; }
    .filter-bar select, .filter-bar input { width: 100%; padding: 10px 12px; font-size: 1rem; }
    .filter-bar .btn { width: 100%; text-align: center; padding: 10px; }

    .two-col { grid-template-columns: 1fr; gap: 12px; }
    .scroll-table { max-height: 400px; }
    .btn { padding: 10px 16px; }
    .nav-menu li a { padding: 14px 20px; font-size: .95rem; }
    .import-page .card { max-width: 100%; }
}

/* ====================================================
   SMALL MOBILE (max 480px)
   ==================================================== */
@media (max-width: 480px) {
    .content { padding: 10px 8px; }
    .page-header h1 { font-size: 1rem; }

    .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
    .stat-card { padding: 10px 12px; }
    .stat-card .value { font-size: 1.1rem; }

    .card { padding: 10px; }
    .card h3 { font-size: .85rem; }

    table.data-table { font-size: .7rem; }
    table.data-table th { padding: 6px 5px; }
    table.data-table td { padding: 5px 5px; }
}

/* ===== EXPORT BAR ===== */
.export-bar { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.export-bar .btn { padding: 4px 10px; font-size: .75rem; }

/* ===== PAGE FOOTER (on-screen, every page) ===== */
.page-footer {
    display: flex; align-items: center; justify-content: center; gap: 16px;
    margin-top: 32px; padding: 16px 0; border-top: 1px solid var(--border);
}
.page-footer img { height: 32px; width: auto; }
.page-footer-text { text-align: center; }
.page-footer-text strong { display: block; font-size: .8rem; color: var(--text); }
.page-footer-text span { display: block; font-size: .7rem; color: var(--text-muted); margin-top: 2px; }

/* ===== PRINT HEADER / FOOTER (hidden on screen, shown only when printing/exporting) ===== */
.print-header, .print-footer { display: none; }

/* ===== PRINT ===== */
@media print {
    .sidebar, .mobile-overlay, .filter-bar, .export-bar, .menu-toggle, .sidebar-footer, .page-header, .page-footer { display: none !important; }
    .layout { display: block; }
    .content { margin: 0; padding: 0; max-width: 100%; }
    .card { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; }
    .scroll-table { max-height: none; overflow: visible; }
    body { background: #fff; }

    .print-header {
        display: flex; align-items: center; gap: 16px;
        border-bottom: 2px solid #8b1a1a; padding-bottom: 12px; margin-bottom: 20px;
    }
    .print-header img { height: 60px; width: auto; }
    .print-header-text { flex: 1; text-align: center; }
    .print-header-text strong { display: block; font-size: 1.1rem; color: #1e293b; }
    .print-header-text span { display: block; font-size: .9rem; color: #64748b; margin-top: 2px; }

    .print-footer {
        display: flex; align-items: center; justify-content: center; gap: 10px;
        border-top: 2px solid #8b1a1a; padding-top: 10px; margin-top: 24px;
        font-size: .75rem; color: #64748b;
    }
    .print-footer img { height: 28px; width: auto; }
}
