: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); }

.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::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); }

@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; }
}
