:root {
    --bg: #0f172a;
    --bg-2: #1e293b;
    --panel: #ffffff;
    --ink: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --primary: #2563eb;
    --primary-d: #1d4ed8;
    --green: #16a34a;
    --red: #dc2626;
    --amber: #d97706;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--ink);
    background: #f1f5f9;
    font-size: 15px;
    line-height: 1.45;
}
a { color: var(--primary); text-decoration: none; }
hr { border: none; border-top: 1px solid var(--line); margin: 14px 0; }
.muted { color: var(--muted); }
.center { text-align: center; }
.hidden { display: none !important; }
.show-mobile { display: none; }
/* Wrap a wide table so it scrolls inside its card instead of off the page. */
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ---------- Auth pages ---------- */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #1e293b, #0f172a); }
.auth-card { background: var(--panel); width: 100%; max-width: 420px; padding: 32px; border-radius: 16px; box-shadow: 0 20px 40px rgba(0,0,0,.25); }
.auth-card h1 { margin: 0 0 8px; font-size: 1.5rem; }
.auth-card label { display: block; font-size: .85rem; font-weight: 600; margin: 12px 0 4px; }
.auth-card input, .auth-card select { width: 100%; }
.auth-card .remember-row { display: flex; align-items: center; gap: 8px; font-weight: 500; margin: 14px 0 4px; }
.auth-card .remember-row input { width: auto; margin: 0; }
.tabs { display: flex; gap: 6px; margin: 14px 0; }
.tab-btn { flex: 1; padding: 8px; border: 1px solid var(--line); background: #f8fafc; border-radius: 8px; cursor: pointer; font-weight: 600; }
.tab-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---------- Dropdown menu ---------- */
.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
    position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
    min-width: 190px; background: #fff; border: 1px solid var(--line);
    border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.12); padding: 6px; overflow: hidden;
}
.dropdown-menu a {
    display: flex; align-items: center; gap: 8px; padding: 9px 12px;
    border-radius: 7px; color: var(--ink); text-decoration: none; font-weight: 600; font-size: .9rem;
}
.dropdown-menu a:hover { background: #f1f5f9; }

/* ---------- Inputs ---------- */
input, select, textarea {
    font: inherit; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(37,99,235,.35); border-color: var(--primary); }
textarea { width: 100%; resize: vertical; }
label { font-size: .9rem; }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border: 1px solid var(--line); background: #fff; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 600; color: var(--ink); }
.btn:hover { background: #f8fafc; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-d); }
.btn-success { background: var(--green); border-color: var(--green); color: #fff; }
.btn-success:hover { background: #15803d; border-color: #15803d; color: #fff; }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover { background: #b91c1c; border-color: #b91c1c; color: #fff; }
.btn-sm { padding: 5px 9px; font-size: .82rem; }
.btn-xs { padding: 2px 6px; font-size: .72rem; gap: 3px; border-radius: 6px; }
.btn-block { width: 100%; justify-content: center; margin-top: 16px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Compact "currently out" pills (restroom) */
.out-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.out-chip {
    display: inline-flex; align-items: center; gap: 6px;
    background: #fef3c7; color: #92400e; border-radius: 999px;
    padding: 3px 5px 3px 10px; font-size: .82rem; line-height: 1.2;
}
.out-chip .out-room { color: #b45309; font-size: .72rem; }
.out-chip .out-time { color: #a16207; font-size: .72rem; }

/* ---------- Alerts / badges ---------- */
.alert { padding: 10px 14px; border-radius: 8px; margin: 10px 0; font-size: .9rem; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .72rem; font-weight: 700; }
.badge-test { background: #fef3c7; color: #92400e; }
.badge-impersonate { background: #ede9fe; color: #6d28d9; }
.badge-absent { background: #e2e8f0; color: #475569; }

/* Students marked absent/excused today are dimmed across daily lists. */
.att-absent { opacity: .5; }
.att-absent:hover { opacity: 1; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 230px; background: var(--bg); color: #cbd5e1; display: flex; flex-direction: column; padding: 16px 10px; position: sticky; top: 0; height: 100vh; }
.brand { font-size: 1.15rem; font-weight: 700; color: #fff; padding: 8px 12px 16px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; min-height: 0; overflow-y: auto; }
.nav-link { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; color: #cbd5e1; font-weight: 500; }
.nav-link i { width: 18px; text-align: center; }
.nav-link:hover { background: var(--bg-2); color: #fff; }
.nav-link.active { background: var(--primary); color: #fff; }
.sidebar-foot { border-top: 1px solid #334155; padding-top: 8px; }
.content { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 22px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; flex-wrap: wrap; }
.topbar-left { display: flex; align-items: center; gap: 8px; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user-chip { color: var(--muted); font-size: .9rem; text-decoration: none; }
.notify { position: relative; }
.notify-btn {
    position: relative; width: 36px; height: 36px; border: 1px solid var(--line); background: #fff;
    border-radius: 999px; cursor: pointer; color: var(--ink);
    display: inline-flex; align-items: center; justify-content: center;
}
.notify-btn:hover { background: #f8fafc; }
.notify-btn.has-alerts { color: #b45309; border-color: #fcd34d; background: #fffbeb; }
.notify-badge {
    position: absolute; top: -4px; right: -4px; min-width: 16px; height: 16px; padding: 0 4px;
    border-radius: 999px; background: var(--red); color: #fff;
    font-size: .68rem; font-weight: 700; line-height: 16px; text-align: center;
}
.notify-panel {
    position: absolute; right: 0; top: calc(100% + 8px); z-index: 40;
    width: min(340px, calc(100vw - 24px));
    background: #fff; border: 1px solid var(--line); border-radius: 12px;
    box-shadow: 0 12px 32px rgba(15,23,42,.16); overflow: hidden;
}
.notify-head {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    padding: 10px 12px; border-bottom: 1px solid var(--line);
}
.notify-list { max-height: 360px; overflow: auto; }
.notify-empty { margin: 0; padding: 16px 12px; color: var(--muted); }
.notify-item { display: flex; gap: 8px; align-items: flex-start; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.notify-item:last-child { border-bottom: none; }
.notify-main { display: flex; flex-direction: column; gap: 1px; color: var(--ink); flex: 1; min-width: 0; text-decoration: none; }
.notify-main:hover strong { text-decoration: underline; }
.notify-main strong { font-weight: 700; }
.notify-main span { font-size: .85rem; color: var(--muted); }
.notify-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.notify-dismiss { border: none; background: transparent; color: var(--muted); cursor: pointer; padding: 2px 4px; font-size: .95rem; }
.notify-dismiss:hover { color: var(--ink); }
.notify-push, .push-setup { border-top: 1px solid var(--line); padding: 10px 12px; font-size: .85rem; }
.push-setup { border-top: none; padding: 0; }
.push-setup ol, .notify-push ol { margin: 8px 0 10px; padding-left: 18px; }
.push-setup li, .notify-push li { margin: 4px 0; }
.push-setup .btn, .notify-push .btn { margin-top: 6px; }
a.user-chip:hover { color: var(--ink); text-decoration: underline; }
.nav-toggle { display: none; align-items: center; justify-content: center; width: 40px; height: 40px; border: 1px solid var(--line); background: #fff; border-radius: 8px; cursor: pointer; font-size: 1.1rem; }
.nav-backdrop { display: none; position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 60; }
.org-switch { max-width: 240px; }
.page { padding: 22px; }

/* ---------- Cards / grid ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 18px; }
.card h2 { margin: 0 0 12px; font-size: 1.15rem; }
.card h3 { margin: 0 0 10px; font-size: 1rem; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row-between { justify-content: space-between; }
.spacer { flex: 1; }
.stat { text-align: center; }
.stat .num { font-size: 2rem; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: .85rem; }

/* Compact "Today at a glance" strip: small header, stats on one line each. */
.glance { padding: 12px 16px; }
.glance h2 { font-size: 1rem; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.glance .grid-4 { gap: 8px; }
.glance .stat { display: flex; align-items: baseline; justify-content: center; gap: 6px; }
.glance .stat .num { font-size: 1.4rem; line-height: 1; }
.glance .stat .lbl { font-size: .8rem; }

/* ---------- Tables ---------- */
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); }
table.data th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
table.data tr:hover td { background: #f8fafc; }

/* ---------- Student cards ---------- */
.student-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.student-card { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #fff; }
.student-card .name { font-weight: 700; }
.pt-list { margin-top: 6px; }
.pt-list-head, .pt-row {
    display: grid;
    grid-template-columns: minmax(90px, 1fr) minmax(100px, 1.2fr) max-content minmax(44px, max-content);
    align-items: center;
    column-gap: 8px;
}
.pt-list-head {
    padding: 4px 2px;
    color: var(--muted);
    font-size: .68rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
    border-bottom: 1px solid var(--line);
}
.pt-row { min-height: 38px; padding: 4px 2px; border-bottom: 1px solid var(--line); }
.pt-row:hover { background: #f8fafc; }
.pt-name-cell { min-width: 0; }
.pt-name-cell .name { font-weight: 650; }
.pt-actions { display: flex; gap: 4px; }
.pt-row .bal { justify-self: end; text-align: center; }
.points-pill { font-weight: 700; padding: 2px 10px; border-radius: 999px; background: #eff6ff; color: var(--primary); }

/* ---------- Modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 560px; max-height: 88vh; overflow: auto; padding: 22px; }
.modal h2 { margin-top: 0; }
.modal-lg { max-width: 820px; }

/* ---------- Toast ---------- */
#toasts { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); font-size: .9rem; opacity: 0; transform: translateY(10px); transition: .2s; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }

/* ---------- Timers ---------- */
.timer-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.timer-card { border: 2px solid var(--line); border-radius: 12px; padding: 14px; text-align: center; background: #fff; }
.timer-card.running { border-color: var(--green); }
.timer-card.done { border-color: var(--red); background: #fef2f2; }
.timer-clock { font-size: 2.2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.timer-card.done .timer-clock { color: var(--red); }

/* ---------- Responsive / phone ---------- */
@media (max-width: 820px) {
    .nav-toggle { display: inline-flex; }

    /* Sidebar becomes an off-canvas drawer. */
    .sidebar {
        position: fixed; top: 0; left: -270px; height: 100vh; width: 250px;
        box-shadow: 4px 0 24px rgba(0,0,0,.35); transition: left .22s ease; z-index: 70;
    }
    body.nav-open .sidebar { left: 0; }
    body.nav-open .nav-backdrop { display: block; }

    .content { width: 100%; }
    .page { padding: 14px; }
    .topbar { padding: 10px 14px; }
    .topbar-left { gap: 10px; }
    .org-switch { max-width: 160px; }
    .user-chip { display: none; }

    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    .pt-list-head, .pt-row { grid-template-columns: minmax(64px, 1fr) minmax(72px, 1.1fr) max-content max-content; column-gap: 4px; }

    /* Any header table (.as-cards is added by JS) becomes stacked cards so
       nothing scrolls sideways and every action button stays visible. */
    table.data.as-cards, table.data.as-cards tbody { display: block; width: 100%; white-space: normal; }
    table.data.as-cards thead { display: none; }
    table.data.as-cards tr {
        display: block; background: #fff; border: 1px solid var(--line);
        border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
    }
    table.data.as-cards tr:hover td { background: transparent; }
    table.data.as-cards td {
        display: block; border: none; padding: 1px 0; word-break: break-word;
    }
    table.data.as-cards td.cell-title { font-size: 1.05rem; font-weight: 700; padding-bottom: 2px; }
    /* Name tables: show the whole name as the title ("Last, First") instead of
       hiding the first name in a small labeled field below. */
    table.data.as-cards td.cell-title.has-firstname { display: inline-block; padding-bottom: 0; }
    table.data.as-cards td.cell-title.has-firstname::after { content: ','; }
    table.data.as-cards td.cell-title.name-first::after { content: none; }
    table.data.as-cards td.cell-firstname {
        display: inline-block; font-size: 1.05rem; font-weight: 700; padding: 1px 0 2px; margin-left: 5px;
    }
    table.data.as-cards td.cell-firstname::before { content: none; }
    table.data.as-cards td.cell-firstname:empty { display: none; }
    /* Keep the name on its own line: the field after the first name starts fresh. */
    table.data.as-cards td.cell-firstname + td { display: block; }
    table.data.as-cards td[data-label] { display: inline-block; margin: 2px 18px 2px 0; font-size: .9rem; vertical-align: top; }
    table.data.as-cards td[data-label]:empty { display: none; }
    table.data.as-cards td[data-label]::before {
        content: attr(data-label); display: block; color: var(--muted);
        font-size: .62rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
    }
    table.data.as-cards td.cell-actions {
        display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px;
        padding-top: 8px; border-top: 1px dashed var(--line);
    }
    table.data.as-cards td.cell-actions::before { content: none; }

    /* Fallback: a header-less / un-enhanced table still scrolls rather than
       break the layout (the only such table is the short activity feed). */
    table.data:not(.as-cards) { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }

    /* Safety net against stray wide content forcing the page sideways.
       `clip` (not `hidden`) avoids creating a scroll container, so the sticky
       topbar keeps working. Inputs/media never exceed the viewport width. */
    .page { overflow-x: clip; }
    input, select, textarea, img, pre, code { max-width: 100%; }
    .show-mobile { display: inline; }

    /* Roomier touch targets. */
    .btn { padding: 10px 14px; }
    .btn-sm { padding: 7px 11px; }

    .modal { max-height: 90vh; }
    .student-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 480px) {
    .student-grid, .timer-grid { grid-template-columns: 1fr; }
    .auth-card { padding: 22px; border-radius: 12px; }
    .stat .num { font-size: 1.6rem; }
}
