:root {
    --bg: #f4f6f8;
    --card: #ffffff;
    --ink: #1f2933;
    --muted: #6b7280;
    --line: #e2e8f0;
    --primary: #1d4ed8;
    --primary-ink: #ffffff;
    --danger: #b91c1c;
    --ok-bg: #dcfce7;
    --ok-ink: #166534;
    --off-bg: #f1f5f9;
    --off-ink: #64748b;
    --err-bg: #fee2e2;
    --err-ink: #991b1b;
    --info-bg: #dbeafe;
    --info-ink: #1e40af;
    --succ-bg: #dcfce7;
    --succ-ink: #166534;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: 'Inter', system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    color: var(--ink);
    background: var(--bg);
    line-height: 1.5;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
    display: flex; align-items: center; justify-content: space-between;
    background: #0f172a; color: #fff; padding: 0 1.25rem; height: 56px;
}
.topbar .brand a { color: #fff; font-weight: 700; }
.mainnav { display: flex; align-items: center; gap: 1rem; }
.mainnav a { color: #cbd5e1; }
.mainnav a:hover { color: #fff; text-decoration: none; }
.logout-form { margin: 0; }

.container { max-width: 1200px; margin: 1.5rem auto; padding: 0 1rem; }
.footer { text-align: center; color: var(--muted); padding: 2rem 0; font-size: .85rem; }

h1 { font-size: 1.5rem; margin: 0 0 .75rem; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; font-size: .85rem; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }

.card {
    background: var(--card); border: 1px solid var(--line); border-radius: 10px;
    padding: 1.25rem; margin-bottom: 1.25rem;
}
.card-narrow { max-width: 460px; margin-left: auto; margin-right: auto; }

label { display: block; margin-bottom: 1rem; }
label > span { display: block; font-weight: 600; margin-bottom: .35rem; font-size: .9rem; }
input[type=text], input[type=password], select {
    width: 100%; padding: .55rem .65rem; border: 1px solid var(--line);
    border-radius: 8px; font-size: 1rem; background: #fff;
}
.hint { display: block; color: var(--muted); font-size: .8rem; margin-top: .25rem; }

.form-actions { display: flex; gap: .5rem; align-items: center; margin-top: .5rem; }

.btn {
    display: inline-block; padding: .55rem .9rem; border-radius: 8px;
    border: 1px solid var(--line); background: #fff; color: var(--ink);
    cursor: pointer; font-size: .95rem;
}
.btn:hover { text-decoration: none; background: #f8fafc; }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { background: #1e40af; }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #991b1b; }
.linkbtn {
    background: none; border: none; color: var(--primary); cursor: pointer;
    padding: 0; font-size: .95rem;
}
.linkbtn:hover { text-decoration: underline; }
.inline { display: inline; margin: 0; }

/* Data tables — pretty-table design (table-layout:fixed, card, ellipsis, no wrap). */
.table {
    width: 100%;
    border-collapse: collapse;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 10px;
    overflow: hidden;                 /* clip rounding on the header too */
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
    table-layout: fixed;              /* KEY: predictable widths, no reshuffling */
    font-size: 0.9rem;
}
.table thead tr { background: #f5f5f7; }
.table th {
    padding: 10px 14px;
    text-align: left;
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .03em;
    color: var(--muted);
    border-bottom: 2px solid var(--line);
    /* Headers may wrap to a second line rather than overflow into the next column
       (fixed layout + a long label would otherwise bleed across the border). */
    white-space: normal;
    vertical-align: bottom;
    word-break: break-word;
}
.table td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
    white-space: nowrap;              /* short cells never wrap */
}
.table tbody tr { transition: background .1s; }
.table tbody tr:hover { background: #f9f9fb; }
.table tr:last-child td { border-bottom: none; }

/* Long text columns: truncate with ellipsis instead of wrapping. Put the full text in
   title="..." and set the column width via style="width:NN%" on the <th>. */
.cell-ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Secondary text under a main value (e.g. time under a date, email under a name). */
.cell-sub { display: block; font-size: .74rem; color: var(--muted); font-weight: 400; margin-top: 1px; }
/* Empty state row. */
.table-empty { text-align: center; padding: 60px 20px; color: var(--muted); font-size: 1rem; }

.row-actions { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.row-actions .danger, a.danger { color: var(--danger); }

.badge { padding: .15rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; }
.badge-ok { background: var(--ok-bg); color: var(--ok-ink); }
.badge-off { background: var(--off-bg); color: var(--off-ink); }

.stats { display: flex; gap: 1rem; flex-wrap: wrap; }
.stat {
    flex: 1; min-width: 160px; background: var(--card); border: 1px solid var(--line);
    border-radius: 10px; padding: 1.25rem; text-align: center; color: var(--ink);
}
.stat:hover { text-decoration: none; border-color: var(--primary); }
.stat-num { display: block; font-size: 2rem; font-weight: 700; }
.stat-label { display: block; color: var(--muted); margin-top: .25rem; }

.deflist { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0 0 1rem; }
.deflist dt { font-weight: 600; color: var(--muted); }
.deflist dd { margin: 0; }

.flash { padding: .7rem .9rem; border-radius: 8px; margin-bottom: 1rem; font-size: .95rem; }
.flash-error { background: var(--err-bg); color: var(--err-ink); }
.flash-success { background: var(--succ-bg); color: var(--succ-ink); }
.flash-info { background: var(--info-bg); color: var(--info-ink); }

/* Phase 2 — domain UI */
.table td.num, .table th.num { text-align: right; white-space: nowrap; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem 1rem; }
.form-grid label { margin: 0; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
label.checkbox { display: flex; flex-direction: row; align-items: center; gap: .5rem; }
label.checkbox input { width: auto; }
.table tfoot th { font-weight: 700; color: var(--ink); border-top: 2px solid var(--line); }

/* Phase 2 — teacher interface */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; align-items: start; }
@media (max-width: 720px) { .two-col { grid-template-columns: 1fr; } }
.roster-list { margin: 0; padding-left: 1.5rem; }
.roster-list li { padding: .15rem 0; }
.student-list { list-style: none; margin: 0; padding: 0; max-height: 60vh; overflow-y: auto; }
.student-list li { border-bottom: 1px solid var(--line); }
.student-list li a { display: flex; align-items: center; gap: .5rem; padding: .5rem .4rem; color: var(--ink); }
.student-list li a:hover { text-decoration: none; background: var(--off-bg); }
.student-list li.active a { background: var(--info-bg); font-weight: 600; }
.dot { display: inline-block; width: .8rem; height: .8rem; border-radius: 999px; border: 1px solid var(--line); flex: none; }
.st-white  { background: #ffffff; }
.st-green  { background: #2e7d32; }
.st-yellow { background: #f6c344; }
.st-grey   { background: #9aa0a6; }
.student-list li.st-green  a { border-left: 4px solid #2e7d32; }
.student-list li.st-yellow a { border-left: 4px solid #f6c344; }
.student-list li.st-grey   a { border-left: 4px solid #9aa0a6; color: var(--muted); }
.student-list li.st-white  a { border-left: 4px solid transparent; }
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; font-size: .9rem; }
.legend .dot { margin-left: .6rem; }
.return-grid td { vertical-align: middle; }
.paid-box[hidden] { display: none; }

/* Table tools: universal filter + click-to-sort headers */
.filter-input {
    width: 100%;
    max-width: 420px;
    padding: 9px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 0.92rem;
    font-family: inherit;
    color: var(--ink);
    margin-bottom: 14px;
    transition: border-color .15s, box-shadow .15s;
}
.filter-input::placeholder { color: var(--muted); }
.filter-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(29, 78, 216, .15);
}
.no-match { text-align: center; padding: 40px 20px; color: var(--muted); font-size: .95rem; }

.table th.sortable { cursor: pointer; user-select: none; }
.table th.sortable:hover { color: var(--ink); }
.table th.sortable::after { content: '\2195'; opacity: .35; margin-left: .35em; font-size: .8em; }
.table th.sortable[aria-sort="ascending"]::after  { content: '\25B2'; opacity: .9; }
.table th.sortable[aria-sort="descending"]::after { content: '\25BC'; opacity: .9; }
