:root {
    --bg: #f4f6f8;
    --card: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #e2e5e9;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --green: #16a34a;
    --green-bg: #ecfdf3;
    --red: #dc2626;
    --red-bg: #fef2f2;
}

* { box-sizing: border-box; }

@keyframes cardIn {
    from { opacity: 0; transform: translateY(14px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes ctaPulse {
    0%, 100% { box-shadow: 0 4px 14px -4px rgba(37, 99, 235, 0.45); }
    50% { box-shadow: 0 6px 22px -2px rgba(37, 99, 235, 0.7); }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}

body {
    margin: 0;
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    row-gap: 8px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    padding: 14px 16px;
}

.brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text);
    text-decoration: none;
}

.topbar-right {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px 14px;
}

.user-name { color: var(--muted); font-size: 0.9rem; }

.inline-form { display: inline; }

.link-button {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 0;
}

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 24px 16px 60px;
}

.topnav {
    display: flex;
    gap: 18px;
    margin-right: 8px;
}

.topnav a {
    color: var(--muted);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 600;
}

.topnav a:hover { color: var(--primary); }

.topnav-link {
    color: var(--muted);
    text-decoration: none;
    font-size: 0.85rem;
}

.topnav-link:hover { color: var(--primary); }

.group-teacher {
    color: var(--muted);
    font-size: 0.95rem;
    margin: -12px 0 18px;
}

.group-teacher strong { color: var(--text); }

.muted-note { color: var(--muted); margin-top: -12px; margin-bottom: 20px; }

.messages { list-style: none; padding: 0; margin: 0 0 20px; }

.message {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 8px;
    font-size: 0.95rem;
}

.message-success { background: var(--green-bg); color: var(--green); border: 1px solid #bbf7d0; }
.message-error { background: var(--red-bg); color: var(--red); border: 1px solid #fecaca; }

h1 { font-size: 1.4rem; margin-bottom: 20px; }
h2 { font-size: 1.1rem; margin: 24px 0 12px; }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
}

.grade-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 12px;
}

.grade-tile {
    display: block;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px 10px;
    text-align: center;
    font-weight: 600;
    font-size: 1.1rem;
    text-decoration: none;
    color: var(--text);
}

.grade-tile:hover { border-color: var(--primary); color: var(--primary); }

.search-box {
    width: 100%;
    padding: 12px 14px;
    font-size: 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 12px;
}

.student-list { list-style: none; padding: 0; margin: 0; }

.student-list li a {
    display: block;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    text-decoration: none;
    color: var(--text);
    background: var(--card);
}

.student-list li a:hover { border-color: var(--primary); }

.student-list .empty { color: var(--muted); padding: 8px 0; }

.back-link {
    display: inline-block;
    margin-bottom: 16px;
    color: var(--primary);
    text-decoration: none;
    font-size: 0.9rem;
}

.debt-badges { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; }

.badge {
    padding: 10px 16px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.95rem;
}

.badge-debt { background: var(--red-bg); color: var(--red); }
.badge-clear { background: var(--green-bg); color: var(--green); }

.form-row { margin-bottom: 14px; }

.form-row label {
    display: block;
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 4px;
}

.form-row .static-value {
    padding: 10px 12px;
    background: #f0f2f5;
    border-radius: 8px;
    font-weight: 600;
}

.form-row select,
.form-row input[type="number"],
.form-row input[type="text"],
.form-row input[type="password"],
.form-row input[type="date"],
.form-row textarea {
    width: 100%;
    padding: 10px 12px;
    font-size: 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: inherit;
}

.form-row input[type="checkbox"] {
    width: auto;
    margin-right: 8px;
}

.field-help { color: var(--muted); font-size: 0.8rem; margin: 4px 0 0; }
.field-error { color: var(--red); font-size: 0.85rem; margin: 4px 0 0; }

.pay-button {
    width: 100%;
    padding: 14px;
    font-size: 1.05rem;
    font-weight: 700;
    color: #fff;
    background: var(--primary);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    margin-top: 4px;
}

.pay-button:hover { background: var(--primary-dark); }

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 1000;
}

.modal-overlay[hidden] { display: none; }

.modal-box {
    background: var(--card);
    border-radius: 14px;
    padding: 24px;
    max-width: 420px;
    width: 100%;
    box-shadow: 0 20px 45px -12px rgba(0, 0, 0, 0.35);
}

.modal-box h3 { margin: 0 0 10px; font-size: 1.1rem; }
.modal-box p { margin: 0 0 18px; color: var(--text); line-height: 1.5; }

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.modal-actions .pay-button,
.modal-actions .secondary-button {
    width: 100%;
}

.modal-actions .link-button {
    align-self: center;
    padding: 6px;
}

.cta-button {
    display: inline-block;
    width: auto;
    padding: 12px 22px;
    font-size: 0.95rem;
    text-decoration: none;
    text-align: center;
    margin-bottom: 20px;
    animation: ctaPulse 2.6s ease-in-out infinite;
    transition: transform 0.16s ease;
}

.cta-button:hover { transform: translateY(-2px) scale(1.02); }

.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 12px;
    margin: 0 -16px;
    padding: 0 16px;
}

table.payments-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card);
    border-radius: 12px;
    overflow: hidden;
}

.table-scroll table.payments-table { white-space: nowrap; }

table.payments-table th,
table.payments-table td {
    text-align: left;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}

table.payments-table th { color: var(--muted); font-weight: 600; background: #fafbfc; }

table.payments-table tbody tr {
    animation: cardIn 0.4s ease both;
    animation-delay: calc(var(--i, 0) * 45ms);
    transition: background-color 0.15s ease;
}

table.payments-table tbody tr:hover { background-color: #f8fafc; }

.pager {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 16px 0;
    font-size: 0.9rem;
    color: var(--muted);
}

.pager a { color: var(--primary); text-decoration: none; font-weight: 600; }

table.payments-table td.cell-wrap { white-space: normal; min-width: 260px; max-width: 460px; }
table.payments-table td.cell-nowrap { white-space: nowrap; }

.action-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 600;
    background: #eef2ff;
    color: var(--primary-dark);
}

.action-delete, .action-login_failed, .action-denied, .action-not_found { background: var(--red-bg); color: var(--red); }
.action-create, .action-login { background: var(--green-bg); color: var(--green); }
.action-view { background: #f3f4f6; color: var(--muted); }

.empty-note { color: var(--muted); padding: 12px 0; }

.login-card {
    max-width: 360px;
    margin: 80px auto;
}

.login-card h1 { text-align: center; }

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    margin-bottom: 8px;
}

.stat-card {
    position: relative;
    overflow: hidden;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 18px 20px;
    animation: cardIn 0.5s cubic-bezier(.22,1,.36,1) both;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card-link {
    display: block;
    text-decoration: none;
    color: inherit;
    cursor: pointer;
}

.stat-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary), #8fb0ff);
}

.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 14px 28px -14px rgba(37, 99, 235, 0.35);
}

.stat-card-warn { border-color: #fecaca; background: var(--red-bg); }
.stat-card-warn::before { background: linear-gradient(90deg, var(--red), #f59e0b); }
.stat-card-warn:hover { box-shadow: 0 14px 28px -14px rgba(220, 38, 38, 0.35); }

.stat-grid > .stat-card:nth-child(1) { animation-delay: .02s; }
.stat-grid > .stat-card:nth-child(2) { animation-delay: .08s; }
.stat-grid > .stat-card:nth-child(3) { animation-delay: .14s; }
.stat-grid > .stat-card:nth-child(4) { animation-delay: .20s; }
.stat-grid > .stat-card:nth-child(5) { animation-delay: .26s; }

.stat-icon {
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 9px;
    background: rgba(37, 99, 235, 0.1);
    color: var(--primary);
    margin-bottom: 10px;
}

.stat-icon svg { width: 18px; height: 18px; }

.stat-card-warn .stat-icon { background: rgba(220, 38, 38, 0.1); color: var(--red); }

.stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-card-warn .stat-value { color: var(--red); }
.stat-label { color: var(--muted); font-size: 0.85rem; margin-top: 4px; }

.quick-links {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin: 20px 0 28px;
}

.quick-link {
    padding: 10px 16px;
    border-radius: 8px;
    background: var(--card);
    border: 1px solid var(--border);
    text-decoration: none;
    color: var(--text);
    font-weight: 600;
    font-size: 0.9rem;
    animation: cardIn 0.45s cubic-bezier(.22,1,.36,1) both;
    transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease, color 0.16s ease;
}

.quick-link:hover {
    border-color: var(--primary);
    color: var(--primary);
    transform: translateY(-2px);
    box-shadow: 0 10px 20px -12px rgba(37, 99, 235, 0.4);
}

.quick-links > .quick-link:nth-child(1) { animation-delay: .30s; }
.quick-links > .quick-link:nth-child(2) { animation-delay: .34s; }
.quick-links > .quick-link:nth-child(3) { animation-delay: .38s; }
.quick-links > .quick-link:nth-child(4) { animation-delay: .42s; }
.quick-links > .quick-link:nth-child(5) { animation-delay: .46s; }
.quick-links > .quick-link:nth-child(6) { animation-delay: .50s; }

.list-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.list-header h1 { margin: 0; }

.add-button {
    width: auto;
    padding: 10px 18px;
    font-size: 0.9rem;
    text-decoration: none;
    display: inline-block;
}

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 18px;
}

.filter-bar input[type="text"],
.filter-bar select {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
}

.secondary-button {
    padding: 9px 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--card);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
}

.secondary-button:hover { border-color: var(--primary); color: var(--primary); }

.row-actions {
    display: flex;
    gap: 12px;
    white-space: nowrap;
}

.row-actions a {
    color: var(--primary);
    text-decoration: none;
    font-size: 0.85rem;
    padding: 4px 0;
}

.total-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px 18px;
    margin-bottom: 16px;
    font-size: 0.95rem;
    color: var(--muted);
}

.total-banner strong {
    font-size: 1.2rem;
    color: var(--text);
}

.link-button.danger { color: var(--red); }

.row-actions { align-items: center; }

.action-btn,
.row-actions a.action-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid transparent;
    font-family: inherit;
    font-size: 0.82rem;
    font-weight: 600;
    line-height: 1.2;
    cursor: pointer;
    text-decoration: none;
    transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

.action-btn svg { width: 14px; height: 14px; flex-shrink: 0; }
.action-btn:hover { transform: translateY(-1px); }
.action-btn:active { transform: translateY(0); }
.action-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.action-btn-edit,
.row-actions a.action-btn-edit {
    color: var(--primary-dark);
    background: #eff6ff;
    border-color: #bfdbfe;
}
.action-btn-edit:hover { background: #dbeafe; border-color: #93c5fd; box-shadow: 0 6px 14px -8px rgba(37, 99, 235, 0.6); }

.action-btn-delete {
    color: var(--red);
    background: var(--red-bg);
    border-color: #fecaca;
}
.action-btn-delete:hover { background: #fee2e2; border-color: #fca5a5; box-shadow: 0 6px 14px -8px rgba(220, 38, 38, 0.6); }

.cancel-link {
    display: block;
    text-align: center;
    margin-top: 12px;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.9rem;
}
.cancel-link:hover { color: var(--text); }

.confirm-box { text-align: center; max-width: 440px; animation: cardIn 0.2s ease both; }
.confirm-icon {
    width: 52px;
    height: 52px;
    margin: 0 auto 14px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}
.confirm-icon svg { width: 24px; height: 24px; }
.confirm-danger .confirm-icon { background: var(--red-bg); color: var(--red); }
.confirm-edit .confirm-icon { background: #eff6ff; color: var(--primary); }
.confirm-box h3 { font-size: 1.15rem; }
.confirm-box .confirm-text { color: var(--muted); margin-bottom: 14px; }

.confirm-changes {
    list-style: none;
    margin: 0 0 18px;
    padding: 10px 14px;
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 10px;
    text-align: left;
    font-size: 0.92rem;
}
.confirm-changes li { padding: 4px 0; }
.confirm-changes li span { color: var(--muted); }
.confirm-changes s { color: var(--red); margin-right: 2px; }
.confirm-changes strong { color: var(--green); }

.pay-button.danger-button { background: var(--red); }
.pay-button.danger-button:hover { background: #b91c1c; }

.total-banner-big {
    padding: 18px 20px;
}

.total-banner-big strong { font-size: 1.5rem; }

.debt-overview { margin-bottom: 24px; }

.debt-period { margin-top: 0; }
.debt-period strong { color: var(--text); }

.filter-reset { text-decoration: none; color: var(--text); display: inline-block; }

.debt-summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px 16px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 18px 20px;
    margin-bottom: 16px;
}

.debt-summary-item { display: flex; flex-direction: column; gap: 4px; }
.debt-summary-item span { color: var(--muted); font-size: 0.85rem; }
.debt-summary-item strong { font-size: 1.3rem; }
.debt-summary-paid strong { color: var(--green); }
.debt-summary-debt strong { color: var(--red); }

.debt-summary-bar {
    grid-column: 1 / -1;
    height: 10px;
    background: var(--red-bg);
    border-radius: 99px;
    overflow: hidden;
}

.debt-summary-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #22c55e, var(--green));
    border-radius: 99px;
    transition: width 0.6s cubic-bezier(.22,1,.36,1);
}

.debt-summary-foot {
    grid-column: 1 / -1;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 6px;
    color: var(--muted);
    font-size: 0.85rem;
}

.stat-sub { color: var(--green); font-size: 0.8rem; font-weight: 600; margin-top: 6px; }

.h1-sub { color: var(--muted); font-weight: 500; font-size: 0.9em; }

a.student-link { color: var(--primary); text-decoration: none; font-weight: 600; }
a.student-link:hover { text-decoration: underline; }

.status-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 99px;
    font-size: 0.78rem;
    font-weight: 600;
    white-space: nowrap;
}
.status-paid { background: var(--green-bg); color: var(--green); }
.status-unpaid { background: var(--red-bg); color: var(--red); }
.status-partial { background: #fffbeb; color: #b45309; }
.status-neutral { background: #f3f4f6; color: var(--muted); }

.debt-section-paid { color: var(--green); font-weight: 700; }
td.paid-cell { color: var(--green); font-weight: 600; }
.count-ok { color: var(--green); font-weight: 700; }
.count-bad { color: var(--red); font-weight: 700; }

@media (max-width: 640px) {
    .debt-summary { grid-template-columns: 1fr; }
}

.debt-section-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    scroll-margin-top: 16px;
    animation: cardIn 0.4s ease both;
}

.debt-section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.debt-section-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.05rem;
    font-weight: 700;
}

.debt-section-icon {
    width: 32px;
    height: 32px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(37, 99, 235, 0.1);
    color: var(--primary);
    flex-shrink: 0;
}

.debt-section-icon svg { width: 17px; height: 17px; }

.debt-section-meta {
    color: var(--muted);
    font-size: 0.85rem;
}

.debt-section-total { font-weight: 700; }
.debt-section-total.has-debt { color: var(--red); }
.debt-section-total.no-debt { color: var(--green); }

.debt-cell {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 150px;
}

.debt-cell-amount { font-weight: 700; }
.debt-cell-amount.has-debt { color: var(--red); }
.debt-cell-amount.no-debt { color: var(--green); font-weight: 600; }

.debt-bar {
    height: 6px;
    background: #f0f2f5;
    border-radius: 99px;
    overflow: hidden;
}

.debt-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #f59e0b, var(--red));
    border-radius: 99px;
    transition: width 0.6s cubic-bezier(.22,1,.36,1);
}

@media (max-width: 640px) {
    .container { padding: 16px 16px 48px; }

    .topbar { padding: 12px 16px; }

    .topnav { gap: 14px; margin-right: 0; }

    .user-name { display: none; }

    h1 { font-size: 1.2rem; margin-bottom: 16px; }
    h2 { font-size: 1rem; }

    .login-card { margin: 40px auto; }

    .list-header {
        flex-direction: column;
        align-items: stretch;
    }

    .add-button,
    .cta-button {
        width: 100%;
        text-align: center;
    }

    .filter-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-bar input[type="text"],
    .filter-bar select,
    .filter-bar .secondary-button {
        width: 100%;
    }

    .stat-grid { grid-template-columns: 1fr 1fr; }

    .grade-grid { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); }

    .debt-badges { flex-direction: column; }
    .badge { text-align: center; }

    .quick-links { gap: 8px; }
    .quick-link { flex: 1 1 calc(50% - 8px); text-align: center; }

    table.payments-table th,
    table.payments-table td {
        padding: 8px 10px;
        font-size: 0.82rem;
    }

    .row-actions { gap: 16px; }
}

@media (max-width: 400px) {
    .stat-grid { grid-template-columns: 1fr; }
}

/* ---- Phone layout for the debt pages: tables turn into cards ---- */
@media (max-width: 640px) {
    .table-scroll:has(.stack-table) { margin: 0; padding: 0; overflow: visible; }

    table.stack-table,
    table.stack-table tbody { display: block; background: transparent; }
    table.stack-table thead { display: none; }

    table.stack-table tr {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 10px 12px;
        background: var(--card);
        border: 1px solid var(--border);
        border-radius: 12px;
        padding: 12px 14px;
        margin-bottom: 10px;
    }

    table.stack-table td {
        display: block;
        padding: 0;
        border: none;
        font-size: 0.88rem;
        font-weight: 600;
        white-space: normal;
        min-width: 0;
    }

    table.stack-table td[data-label]::before {
        content: attr(data-label);
        display: block;
        margin-bottom: 2px;
        font-size: 0.7rem;
        font-weight: 600;
        color: var(--muted);
    }

    table.stack-table td.cell-title { grid-column: 1 / -1; font-size: 1.05rem; font-weight: 700; }
    table.stack-table td.cell-title.cell-span2 { grid-column: 1 / 3; }
    table.stack-table td.cell-status { text-align: right; align-self: center; }
    table.stack-table td.cell-wide { grid-column: 1 / -1; }
    table.stack-table td.cell-title a { display: block; padding: 2px 0; }

    .debt-cell { min-width: 0; }

    .filter-bar-2col { flex-direction: row; flex-wrap: wrap; }
    .filter-bar-2col select,
    .filter-bar-2col .secondary-button {
        width: auto;
        flex: 1 1 calc(50% - 5px);
        min-width: 0;
        text-align: center;
    }
    .filter-bar-2col .filter-wide { flex: 1 1 100%; width: 100%; }

    .debt-summary { padding: 14px 16px; gap: 10px; }
    .debt-summary-item { flex-direction: row; justify-content: space-between; align-items: baseline; }
    .debt-summary-item strong { font-size: 1.1rem; }
    .debt-summary-foot { flex-direction: column; }

    .debt-overview { grid-template-columns: 1fr 1fr; gap: 10px; }
    .debt-overview .stat-card { padding: 12px 14px; }
    .debt-overview .stat-icon { width: 28px; height: 28px; margin-bottom: 6px; }
    .debt-overview .stat-value { font-size: 1.05rem; }
    .debt-overview .stat-label { font-size: 0.75rem; }

    .debt-section-card { padding: 14px; }
    .debt-section-head { flex-direction: column; align-items: flex-start; gap: 6px; }
    .total-banner-big strong { font-size: 1.25rem; }
}

@media (max-width: 400px) {
    .debt-overview { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 640px) {
    table.stack-table td[data-label] { white-space: nowrap; }

    table.stack-2col tr { grid-template-columns: 1fr 1fr; }
    table.stack-2col td { order: 4; }
    table.stack-2col td.cell-title { order: 0; }
    table.stack-2col td[data-label="Hisoblangan"] { order: 1; }
    table.stack-2col td[data-label="To'langan"] { order: 2; }
    table.stack-2col td.cell-wide:not(.cell-counts) { order: 3; grid-column: 1 / -1; }
    table.stack-2col td.cell-counts { grid-column: auto; }

    .topbar { row-gap: 4px; }
    .topbar-right { gap: 4px 14px; width: 100%; justify-content: space-between; }
    .topnav { flex-wrap: wrap; gap: 4px 14px; }
}

/* ---- Class page: pupils coloured by debt ---- */
.student-list li.student-row a {
    border-left-width: 5px;
    padding: 12px 14px;
}

.student-list li.state-debt a { background: var(--red-bg); border-color: #fecaca; border-left-color: var(--red); }
.student-list li.state-ok a { background: var(--green-bg); border-color: #bbf7d0; border-left-color: var(--green); }
.student-list li.state-debt a:hover { border-color: var(--red); }
.student-list li.state-ok a:hover { border-color: var(--green); }

.sr-main { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 4px 12px; }
.sr-name { font-weight: 600; }
.sr-status { font-size: 0.9rem; font-weight: 700; }
.state-debt .sr-status { color: var(--red); }
.state-ok .sr-status { color: var(--green); }

.sr-months { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.month-chip {
    background: #fff;
    border: 1px solid #fecaca;
    color: var(--red);
    border-radius: 99px;
    padding: 2px 10px;
    font-size: 0.78rem;
    font-weight: 600;
}

@media (max-width: 640px) {
    .sr-main { flex-direction: column; align-items: flex-start; }
    .sr-name { font-size: 1rem; }
    .student-list li.student-row a { padding: 12px; }
}

/* ---- Attendance (davomat) ---- */
.att-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px 16px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 16px;
    margin-bottom: 8px;
    font-size: 0.92rem;
}
.att-bar-info { display: flex; flex-wrap: wrap; gap: 4px 18px; color: var(--muted); }
.att-bar-info strong { color: var(--text); }
.att-bar-absent strong { color: var(--red); }
.att-bar-link { color: var(--primary); text-decoration: none; font-weight: 600; }
.att-hint { color: var(--muted); font-size: 0.82rem; margin: 0 0 14px; }

.student-list li.student-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-left-width: 5px;
    border-radius: 8px;
    background: var(--card);
}
.student-list li.state-debt { background: var(--red-bg); border-color: #fecaca; border-left-color: var(--red); }
.student-list li.state-ok { background: var(--green-bg); border-color: #bbf7d0; border-left-color: var(--green); }
.student-list li.student-row a.sr-link,
.student-list li.student-row a.sr-link:hover {
    flex: 1;
    min-width: 0;
    margin: 0;
    padding: 2px 0;
    border: none;
    border-radius: 0;
    background: transparent;
}

.att-switch {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    min-width: 64px;
    padding: 4px 2px;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}
.att-switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.att-track {
    position: relative;
    width: 54px;
    height: 30px;
    border-radius: 99px;
    background: var(--green);
    transition: background-color 0.2s ease;
}
.att-track::after {
    content: '';
    position: absolute;
    top: 3px;
    left: 27px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
    transition: left 0.2s ease;
}
.att-switch input:not(:checked) + .att-track { background: var(--red); }
.att-switch input:not(:checked) + .att-track::after { left: 3px; }
.att-switch input:focus-visible + .att-track { outline: 2px solid var(--primary); outline-offset: 2px; }
.att-switch input:disabled + .att-track { opacity: 0.6; }
.att-label { font-size: 0.72rem; font-weight: 700; color: var(--green); }
.student-row.is-absent .att-label { color: var(--red); }

.toast {
    position: fixed;
    left: 50%;
    bottom: 20px;
    transform: translateX(-50%);
    max-width: calc(100% - 32px);
    background: #1f2937;
    color: #fff;
    padding: 12px 18px;
    border-radius: 10px;
    font-size: 0.9rem;
    box-shadow: 0 10px 25px -8px rgba(0, 0, 0, 0.5);
    z-index: 1100;
}
.toast[hidden] { display: none; }

.absent-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.absent-chip {
    background: var(--red-bg);
    border: 1px solid #fecaca;
    color: var(--red);
    border-radius: 99px;
    padding: 6px 14px;
    font-size: 0.88rem;
    font-weight: 600;
    text-decoration: none;
}
.att-all-here { color: var(--green); font-weight: 600; }

.att-meter { height: 6px; background: #f0f2f5; border-radius: 99px; overflow: hidden; }
.att-meter-fill { height: 100%; background: linear-gradient(90deg, #f59e0b, var(--red)); border-radius: 99px; }
.att-meter-fill.is-good { background: linear-gradient(90deg, #22c55e, var(--green)); }

@media (max-width: 640px) {
    .student-list li.student-row { padding: 10px; gap: 8px; }
    .att-bar { padding: 10px 12px; }
}

.filter-bar input[type="date"] {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
    font-family: inherit;
    background: var(--card);
}
@media (max-width: 640px) {
    .filter-bar input[type="date"] { width: 100%; }
}
.stat-grid > .stat-card:nth-child(6) { animation-delay: .32s; }

@media (max-width: 640px) {
    table.stack-2col td.cell-title.cell-span2 { grid-column: auto; }
    table.stack-2col td.cell-status { order: 0; }
}
