:root {
    --primary: #0f4c81;
    --primary-2: #1769aa;
    --primary-soft: #e8f2fb;
    --ink: #142033;
    --muted: #66758a;
    --line: #dce5ef;
    --bg: #f4f7fb;
    --surface: #ffffff;
    --danger: #b42318;
    --ok: #157347;
    --shadow: 0 18px 48px rgba(15, 76, 129, .12);
    --radius: 16px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    min-height: 100vh;
    background: radial-gradient(circle at top left, #e8f2fb 0, transparent 34%), var(--bg);
    color: var(--ink);
    font: 14px/1.5 Inter, "Segoe UI", Arial, sans-serif;
}

button, input, select, textarea { font: inherit; }
button { border: 0; cursor: pointer; }
.hidden { display: none !important; }
.muted, small { color: var(--muted); }

.loading {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-items: center;
    background: rgba(255,255,255,.72);
    backdrop-filter: blur(3px);
}
.spinner {
    width: 42px;
    height: 42px;
    border: 4px solid #dbeafe;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.login-screen {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background: linear-gradient(135deg, rgba(232,242,251,.88), rgba(249,251,253,.94)), url('login-background.jpeg') center/cover no-repeat fixed;
}
.login-card {
    width: min(430px, 100%);
    background: rgba(255,255,255,.94);
    border: 1px solid rgba(220,229,239,.9);
    border-radius: 24px;
    padding: 34px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
}
.login-logos { display: flex; justify-content: center; gap: 16px; }
.login-logos img { width: 66px; height: 66px; object-fit: contain; border-radius: 14px; }
.login-card h1 { text-align: center; margin: 18px 0 5px; color: var(--primary); font-size: 32px; }
.login-card p { text-align: center; color: var(--muted); margin: 0 0 24px; }
.login-card p.login-subtitle { margin-bottom: 14px; }
.login-school-info {
    display: grid;
    gap: 3px;
    margin: 0 0 20px;
    padding: 11px 13px;
    border: 1px solid #dbe8f3;
    border-radius: 13px;
    background: rgba(240,247,253,.76);
    text-align: center;
}
.login-school-info strong { color: #173f63; font-size: 13px; }
.login-school-info span { color: var(--muted); font-size: 11px; }
.login-contact-links {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 7px;
    margin: -9px 0 18px;
}
.login-contact-links a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 34px;
    padding: 7px 9px;
    border: 1px solid #d6e4ef;
    border-radius: 10px;
    background: #f8fbfe;
    color: #315a7d;
    font-size: 11px;
    font-weight: 750;
    text-decoration: none;
    transition: color .15s, border-color .15s, background .15s, transform .15s;
}
.login-contact-links a:hover {
    color: var(--primary);
    border-color: #9fc4df;
    background: #edf7ff;
    transform: translateY(-1px);
}
.login-contact-links svg {
    width: 16px;
    height: 16px;
    flex: 0 0 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.login-contact-links .contact-icon-dot,
#loginYoutube svg path:last-child { fill: currentColor; stroke: none; }
.login-card label, .settings label {
    display: block;
    margin: 13px 0;
    font-weight: 700;
}
input, select, textarea {
    border: 1px solid var(--line);
    border-radius: 11px;
    padding: 11px 12px;
    background: #fff;
    color: var(--ink);
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--primary-2);
    box-shadow: 0 0 0 4px rgba(23,105,170,.12);
}
.login-card input, .settings input, .toolbar input, .toolbar select { width: 100%; margin-top: 6px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    border-radius: 11px;
    padding: 10px 15px;
    font-weight: 800;
    text-decoration: none;
    transition: transform .12s, background .15s, box-shadow .15s;
}
.btn:hover { transform: translateY(-1px); }
.btn.primary { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; box-shadow: 0 8px 18px rgba(15,76,129,.20); }
.btn.secondary { background: #edf3f9; color: #24415f; }
.btn.danger { background: #fee4e2; color: #991b1b; }
.btn.small { min-height: 34px; padding: 7px 10px; font-size: 12px; }
.login-card .btn { width: 100%; margin-top: 12px; }
.error { min-height: 20px; color: var(--danger); margin-top: 12px; font-size: 13px; }

.app { min-height: 100vh; display: flex; }
.sidebar {
    width: 270px;
    min-height: 100vh;
    position: sticky;
    top: 0;
    padding: 20px 14px;
    display: flex;
    flex-direction: column;
    color: #dceaf6;
    background: linear-gradient(180deg, #0c2f50, #102a43);
    box-shadow: 10px 0 35px rgba(16,42,67,.08);
}
.brand { display: flex; align-items: center; gap: 11px; padding: 4px 8px 24px; }
.brand small, .user-box small { display: block; color: #9fb6c9; font-size: 11px; margin-top: 3px; }
.brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border-radius: 14px;
    background: rgba(255,255,255,.12);
    font-size: 20px;
}
.brand-mark img {
    width: 34px;
    height: 34px;
    object-fit: contain;
    border-radius: 10px;
}
.brand-copy { min-width: 0; }
.brand-copy strong, .brand-copy small {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
#nav {
    display: flex;
    flex: 1;
    min-height: 0;
    flex-direction: column;
    gap: 5px;
    overflow-y: auto;
    padding-right: 3px;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,.20) transparent;
}
#nav button {
    display: flex;
    align-items: center;
    gap: 9px;
    width: 100%;
    min-height: 44px;
    padding: 11px 12px;
    border-radius: 12px;
    background: transparent;
    color: #cbdbea;
    text-align: left;
}
#nav button:hover, #nav button.active, .nav-group.open > .nav-group-toggle {
    background: rgba(255,255,255,.12);
    color: #fff;
}
#nav button:focus-visible { outline: 2px solid rgba(147,197,253,.8); outline-offset: 2px; }
.nav-icon {
    width: 19px;
    height: 19px;
    flex: 0 0 19px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: .92;
}
.nav-label {
    min-width: 0;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.nav-group { display: grid; gap: 3px; }
.nav-group-toggle { font-weight: 750; }
.nav-chevron {
    width: 16px;
    height: 16px;
    flex: 0 0 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: transform .18s ease;
    opacity: .65;
}
.nav-group.open .nav-chevron { transform: rotate(90deg); }
.nav-submenu {
    display: grid;
    gap: 3px;
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transform: translateY(-4px);
    transition: max-height .25s ease, opacity .18s ease, transform .18s ease;
}
.nav-group.open .nav-submenu {
    max-height: 420px;
    opacity: 1;
    transform: none;
}
.nav-submenu .nav-item {
    min-height: 40px;
    margin-left: 12px;
    padding: 9px 11px 9px 17px;
    border-left: 1px solid rgba(255,255,255,.14);
    border-radius: 0 11px 11px 0;
    font-size: 13px;
}
.nav-submenu .nav-icon {
    width: 17px;
    height: 17px;
    flex-basis: 17px;
}
.nav-submenu .nav-item.active {
    border-left-color: #7dd3fc;
    background: linear-gradient(90deg, rgba(56,189,248,.18), rgba(255,255,255,.09));
}
.nav-profile {
    margin-top: auto;
    padding-top: 9px;
    border-top: 1px solid rgba(255,255,255,.10);
}
.side-footer { margin-top: auto; padding: 18px 8px 0; color: #8ca5ba; font-size: 11px; }

.main { flex: 1; min-width: 0; }
.topbar {
    min-height: 72px;
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 0 28px;
    background: rgba(255,255,255,.92);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(12px);
}
.topbar h1 { margin: 0; font-size: 23px; letter-spacing: -.02em; }
.topbar-school {
    display: flex;
    flex: 1;
    align-items: center;
    min-width: 0;
    gap: 11px;
}
.topbar-school > img {
    width: 40px;
    height: 40px;
    flex: 0 0 40px;
    object-fit: contain;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: #fff;
    padding: 3px;
}
.topbar-school > div { min-width: 0; }
.topbar-school small {
    display: flex;
    min-width: 0;
    gap: 7px;
    margin-top: 1px;
    color: var(--muted);
    font-size: 11px;
}
.topbar-school small strong,
.topbar-school small span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar-school small span::before { content: "·"; margin-right: 7px; }
.menu-toggle {
    display: none;
    min-width: 42px;
    min-height: 42px;
    border-radius: 12px;
    background: #edf3f9;
}
.user-box { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.avatar, .profile-avatar {
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    overflow: hidden;
    border-radius: 50%;
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 800;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

.content {
    width: min(1600px, 100%);
    padding: 28px;
}
.welcome {
    padding: 24px;
    border-radius: 20px;
    background: linear-gradient(135deg, #ffffff, #eef7ff);
    border: 1px solid var(--line);
    box-shadow: 0 12px 30px rgba(15,76,129,.07);
}
.welcome h2 { margin: 0 0 4px; font-size: 26px; }
.welcome p { margin: 0; color: var(--muted); }
.stats {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 14px;
    margin: 22px 0;
}
.stat, .panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: 0 8px 26px rgba(15,76,129,.06);
}
.stat { padding: 18px; }
.stat span { display: block; color: var(--muted); font-size: 12px; font-weight: 700; }
.stat strong { display: block; margin-top: 8px; color: var(--primary); font-size: 30px; letter-spacing: -.03em; }
.panel { padding: 20px; }
.panel-head {
    display: flex;
    align-items: center;
    gap: 15px;
    padding-bottom: 15px;
    margin-bottom: 15px;
    border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 19px; }
.panel-subtitle { margin: 4px 0 0; }
.toolbar { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar input, .toolbar select { width: auto; min-width: 160px; margin: 0; }
.table-wrap { overflow: auto; border-radius: 12px; }
.data-table {
    width: 100%;
    min-width: 860px;
    border-collapse: separate;
    border-spacing: 0;
}
.data-table th, .data-table td {
    padding: 12px 10px;
    border-bottom: 1px solid #e9eff6;
    text-align: left;
    vertical-align: top;
}
.data-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #f7fafc;
    color: #526579;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .03em;
}
.data-table tbody tr:hover { background: #f9fcff; }
.actions { white-space: nowrap; }
.activity-item { display: flex; gap: 10px; padding: 12px 0; border-bottom: 1px solid #eef2f6; }
.activity-dot { flex: 0 0 auto; width: 9px; height: 9px; margin-top: 7px; border-radius: 50%; background: #3b82b6; box-shadow: 0 0 0 4px #dbeafe; }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.report-summary { display: flex; flex-wrap: wrap; gap: 18px; margin-bottom: 18px; }
.report-cards {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 18px;
}
.report-card {
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: linear-gradient(135deg, #fff, #f7fbff);
}
.report-card span {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}
.report-card strong {
    display: block;
    margin-top: 5px;
    color: var(--primary);
    font-size: 22px;
}
.settings { display: grid; grid-template-columns: 90px 1fr; gap: 24px; max-width: 580px; }
.settings small { display: block; margin: 6px 0 14px; }
.school-profile-form {
    display: grid;
    grid-template-columns: minmax(220px, 290px) minmax(0, 1fr);
    gap: 24px;
}
.school-logo-grid { display: grid; align-content: start; gap: 14px; }
.school-logo-card {
    display: grid;
    gap: 9px;
    padding: 16px;
    margin: 0;
    border: 1px solid var(--line);
    border-radius: 15px;
    background: #f8fbfe;
    font-weight: 800;
}
.school-logo-card img {
    width: 96px;
    height: 96px;
    justify-self: center;
    object-fit: contain;
    border-radius: 16px;
    border: 1px solid #d7e3ee;
    background: #fff;
    padding: 6px;
}
.school-logo-card input { width: 100%; padding: 9px; background: #fff; }
.school-logo-card small { font-weight: 500; }
.school-logo-card .school-logo-reset {
    display: flex;
    align-items: center;
    gap: 7px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 650;
}
.school-logo-card .school-logo-reset input { width: 17px; height: 17px; padding: 0; accent-color: var(--primary); }
.school-profile-fields { align-content: start; }
.school-profile-actions {
    grid-column: 2;
    display: flex;
    justify-content: flex-end;
    padding-top: 4px;
}
.access-note {
    margin: 0 0 16px;
    padding: 12px 14px;
    border: 1px solid #b8d7ee;
    border-radius: 12px;
    background: #edf7ff;
    color: #254f72;
}
.role-badge, .status-badge {
    display: inline-flex;
    align-items: center;
    min-height: 27px;
    padding: 4px 9px;
    border-radius: 999px;
    background: #edf3f9;
    color: #24415f;
    font-size: 12px;
    font-weight: 800;
}
.status-badge.active { background: #dcfce7; color: #166534; }
.status-badge.inactive { background: #fee2e2; color: #991b1b; }
.permission-table { min-width: 760px; }
.permission-table th:not(:first-child), .permission-table td:not(:first-child) { width: 92px; text-align: center; }
.permission-cell input {
    width: 19px;
    height: 19px;
    accent-color: var(--primary);
    cursor: pointer;
}
.permission-cell input:disabled { cursor: not-allowed; opacity: .45; }
.user-table { min-width: 1050px; }
#savePermissionsBtn:disabled { cursor: not-allowed; opacity: .55; transform: none; }

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(15,32,51,.52);
    backdrop-filter: blur(4px);
}
.modal {
    width: min(820px, 100%);
    max-height: min(90vh, 920px);
    overflow: auto;
    background: #fff;
    border-radius: 20px;
    padding: 22px;
    box-shadow: 0 24px 80px rgba(15,32,51,.28);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 15px; }
.modal-head h2 { margin: 0; }
.close { min-width: 40px; min-height: 40px; border-radius: 12px; background: #f2f5f8; color: var(--muted); font-size: 24px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 15px; }
.form-grid label { display: block; margin: 10px 0; font-weight: 700; }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid input, .form-grid select, .form-grid textarea { width: 100%; margin-top: 6px; }
.form-grid textarea { min-height: 92px; resize: vertical; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.toast {
    position: fixed;
    right: 22px;
    bottom: calc(22px + env(safe-area-inset-bottom));
    z-index: 90;
    max-width: min(420px, calc(100vw - 32px));
    padding: 13px 16px;
    border-radius: 13px;
    background: #102a43;
    color: #fff;
    box-shadow: 0 10px 30px rgba(0,0,0,.22);
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--ok); }

@media (max-width: 1200px) {
    .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
    .sidebar {
        position: fixed;
        z-index: 50;
        inset: 0 auto 0 0;
        width: min(300px, 86vw);
        transform: translateX(-100%);
        transition: transform .2s ease;
    }
    .sidebar.open { transform: none; }
    .menu-toggle { display: inline-grid; place-items: center; }
    .topbar { padding: 0 15px; }
    .content { padding: 16px; }
    .panel-head { align-items: flex-start; flex-direction: column; }
    .toolbar { width: 100%; margin-left: 0; }
    .toolbar input, .toolbar select, .toolbar .btn { flex: 1 1 150px; }
    .settings { grid-template-columns: 1fr; }
    .school-profile-form { grid-template-columns: 1fr; }
    .school-logo-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .school-profile-actions { grid-column: 1; }
    .profile-avatar { margin: auto; }
    .report-cards { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
    body { font-size: 13px; }
    .login-screen { padding: 14px; }
    .login-card { padding: 24px; border-radius: 20px; }
    .app { display: block; }
    .topbar { min-height: 64px; }
    .topbar h1 { font-size: 18px; }
    .topbar-school { gap: 7px; }
    .topbar-school > img { width: 34px; height: 34px; flex-basis: 34px; }
    .topbar-school small span { display: none; }
    .user-box > div:nth-child(2) { display: none; }
    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .stat { padding: 14px; }
    .stat strong { font-size: 24px; }
    .panel { padding: 14px; border-radius: 14px; }
    .form-grid { grid-template-columns: 1fr; }
    .school-logo-grid { grid-template-columns: 1fr; }
    .modal-backdrop { padding: 10px; align-items: end; }
    .modal {
        width: 100%;
        max-height: 92vh;
        border-radius: 20px 20px 0 0;
        padding: 18px;
    }
    .modal-actions { position: sticky; bottom: 0; padding-top: 12px; background: #fff; }
}
@media (max-width: 420px) {
    .stats { grid-template-columns: 1fr; }
    .toolbar input, .toolbar select, .toolbar .btn { flex-basis: 100%; }
    .btn { width: 100%; }
    .user-box .btn { width: auto; }
    .actions .btn { width: auto; margin-bottom: 4px; }
}
