:root {
    --bg: #f6f6f4;
    --surface: #ffffff;
    --border: #ddd9d2;
    --text: #22201d;
    --muted: #6d6862;
    --accent: #8a5a2b;
    --accent-dark: #6d4620;
    --green: #2f8a4c;
    --green-soft: #e3f4e8;
    --grey-soft: #ebe9e5;
    --red: #b3261e;
    --red-soft: #fbe7e6;
    --radius: 10px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
    font-size: 16px;
    line-height: 1.65;
}

a { color: var(--accent); }
a:hover { color: var(--accent-dark); }

/* ---------- chrome ---------- */

.site-header {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
    align-items: center;
    justify-content: space-between;
    padding: 0.9rem 1.25rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.brand {
    font-weight: 600;
    font-size: 1.05rem;
    text-decoration: none;
    color: var(--text);
}

.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-nav a { text-decoration: none; color: var(--muted); }
.site-nav a.current, .site-nav a:hover { color: var(--accent); }

.admin-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 1.25rem;
    background: #efece7;
    border-bottom: 1px solid var(--border);
    font-size: 0.95rem;
}

.admin-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.admin-nav a { text-decoration: none; color: var(--muted); }
.admin-nav a.current, .admin-nav a:hover { color: var(--accent); }

.logout { display: flex; align-items: center; gap: 0.75rem; }
.who { color: var(--muted); }

.container {
    max-width: 900px;
    margin: 0 auto;
    padding: 1.75rem 1.25rem 4rem;
}

.site-footer {
    padding: 1.5rem 1.25rem;
    color: var(--muted);
    font-size: 0.9rem;
    text-align: center;
}

/* ---------- typography ---------- */

h1 { font-size: 1.5rem; margin: 0 0 0.35rem; }
h2 { font-size: 1.2rem; margin: 2rem 0 0.75rem; }
h3 { font-size: 1.02rem; margin: 1.5rem 0 0.5rem; }
.lede { color: var(--muted); margin: 0 0 1.5rem; }
.muted { color: var(--muted); }
.small { font-size: 0.9rem; }

/* ---------- cards, forms ---------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1.25rem;
}

.field { margin-bottom: 1rem; }
.field label { display: block; font-weight: 500; margin-bottom: 0.3rem; }
.field .hint { display: block; color: var(--muted); font-size: 0.85rem; margin-bottom: 0.3rem; }

input[type="text"], input[type="email"], input[type="password"],
input[type="number"], input[type="date"], input[type="time"],
select, textarea {
    width: 100%;
    padding: 0.55rem 0.7rem;
    font: inherit;
    color: inherit;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
}

input:focus, select:focus, textarea:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

textarea { min-height: 5rem; resize: vertical; }

.row { display: flex; flex-wrap: wrap; gap: 1rem; }
.row > * { flex: 1 1 12rem; }

/* Honeypot: hidden from people, still submitted by naive bots. */
.hp {
    position: absolute;
    left: -9999px;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

button, .button {
    display: inline-block;
    padding: 0.55rem 1.1rem;
    font: inherit;
    font-weight: 500;
    color: #fff;
    background: var(--accent);
    border: 1px solid var(--accent);
    border-radius: 8px;
    text-decoration: none;
    cursor: pointer;
}

button:hover, .button:hover { background: var(--accent-dark); color: #fff; }

button.secondary, .button.secondary {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}
button.secondary:hover { background: #f0ede9; color: var(--text); }

button.danger { background: var(--red); border-color: var(--red); }

button.link {
    background: none;
    border: none;
    color: var(--accent);
    padding: 0;
    text-decoration: underline;
}
button.link:hover { background: none; color: var(--accent-dark); }

.actions { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; }

/* ---------- messages ---------- */

.flash, .notice {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin: 0 0 1rem;
    /* Backup failures put absolute paths and raw SMTP replies in here, and
       neither offers a line-break opportunity. */
    overflow-wrap: anywhere;
}
.flash-success, .notice.success { background: var(--green-soft); color: #1c5c33; }
.flash-error, .notice.error { background: var(--red-soft); color: #8c1d18; }
.notice.info { background: #eceff5; color: #2c3b57; }

.error-text { color: var(--red); font-size: 0.9rem; margin: 0.25rem 0 0; }
input.invalid, select.invalid, textarea.invalid { border-color: var(--red); }

/* ---------- tables ---------- */

table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.6rem 0.5rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-weight: 600; color: var(--muted); font-size: 0.9rem; }
.table-wrap { overflow-x: auto; }

.badge {
    display: inline-block;
    padding: 0.1rem 0.5rem;
    border-radius: 999px;
    font-size: 0.8rem;
    background: var(--grey-soft);
    color: var(--muted);
}
.badge.ok { background: var(--green-soft); color: #1c5c33; }
.badge.bad { background: var(--red-soft); color: #8c1d18; }

.student-id {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 1.05rem;
    letter-spacing: 0.06em;
}

/* ---------- attendance grid ---------- */

.summary {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    align-items: baseline;
    margin-bottom: 1rem;
}
.summary .percent { font-size: 2.2rem; font-weight: 600; line-height: 1; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(3.4rem, 1fr));
    gap: 0.4rem;
}

.tile {
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1 / 1;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 500;
    background: var(--grey-soft);
    color: var(--muted);
}
.tile.attended { background: var(--green); color: #fff; }

.legend { display: flex; gap: 1.25rem; margin-top: 0.85rem; font-size: 0.9rem; color: var(--muted); }
.legend span { display: flex; align-items: center; gap: 0.4rem; }
.swatch { width: 0.9rem; height: 0.9rem; border-radius: 4px; background: var(--grey-soft); }
.swatch.attended { background: var(--green); }

/* ---------- misc ---------- */

.tiles-links { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1rem; }
.tiles-links a { display: block; text-decoration: none; color: inherit; }
.tiles-links .card { margin: 0; height: 100%; }
.tiles-links .card:hover { border-color: var(--accent); }

.inline-form { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.inline-form input[type="text"] { width: 7rem; }

hr { border: none; border-top: 1px solid var(--border); margin: 1.5rem 0; }

/* ---------- narrow screens ---------- */

/*
 * Below ~640px a five-column admin table cannot be read without scrolling it
 * sideways, and the action column — approve, cancel, download — is the one that
 * ends up off-screen. So each row becomes its own card and each cell carries the
 * column heading it lost, taken from the `data-label` attribute on the <td>.
 * The <thead> is then redundant, so it goes.
 */
@media (max-width: 40rem) {
    .table-wrap { overflow-x: visible; }

    .table-wrap table,
    .table-wrap tbody,
    .table-wrap tr,
    .table-wrap td {
        display: block;
        width: 100%;
    }

    /* display:block above already collapses the table roles, so holding thead
       off-screen preserves no header/cell association — it only makes a screen
       reader recite every column name before the first card. The per-cell
       data-label ::before text carries the heading instead. */
    .table-wrap thead { display: none; }

    .table-wrap tr {
        border: 1px solid var(--border);
        border-radius: 8px;
        padding: 0.6rem 0.8rem;
        margin-bottom: 0.75rem;
    }

    .table-wrap tr:last-child { margin-bottom: 0; }

    /*
     * The label sits above the value rather than beside it. A two-column grid
     * looks tidier but breaks the moment a cell holds more than one node: every
     * child — and every bare text node — becomes its own grid item, so the
     * second line of a cell lands under the heading instead of beside it.
     */
    .table-wrap td {
        display: block;
        padding: 0.35rem 0;
        border-bottom: none;
        /* Without this a long unbroken token — an absolute path in a backup
           error, a support URL — scrolls the whole document sideways, because
           the card layout has given up the table's own overflow-x. */
        overflow-wrap: anywhere;
    }

    .table-wrap td + td { border-top: 1px solid var(--border); }

    .table-wrap td[data-label]::before {
        content: attr(data-label);
        display: block;
        font-weight: 600;
        font-size: 0.8rem;
        color: var(--muted);
        line-height: 1.6;
    }

    /* Actions get the full width of the card rather than a 7rem stub. */
    .inline-form input[type="text"] { width: auto; flex: 1 1 6rem; }

    /* Comfortable thumb targets. button.link needs min-height rather than
       padding: `button.link { padding: 0 }` outscores a bare `button` selector,
       so padding here would silently lose — including on the logout button,
       which is not inside a table at all. */
    button, .button { padding: 0.7rem 1.1rem; }
    button.link { min-height: 44px; }
    .table-wrap input[type="checkbox"],
    .actions input[type="checkbox"] { width: 1.5rem; height: 1.5rem; }

    .container { padding: 1.25rem 1rem 3rem; }
    .summary .percent { font-size: 1.9rem; }
}
