:root {
    --bg: #f2f5ea;
    --surface: #ffffff;
    --ink: #1f2933;
    --brand: #1d6f42;
    --accent: #f0a202;
    --border: #d7e1d4;
    --danger: #b42318;
}

* { box-sizing: border-box; }
html, body {
    max-width: 100%;
    overflow-x: hidden;
}
body {
    margin: 0;
    font-family: "Segoe UI", Tahoma, sans-serif;
    background: linear-gradient(135deg, #eef5dc, #dcefe4 60%, #f8f8ef);
    color: var(--ink);
}

.topbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 14px 22px;
    background: var(--brand);
    color: #fff;
}
.topbar .brand { font-weight: 700; font-size: 1.15rem; }
.topbar nav { display: flex; gap: 12px; flex-wrap: wrap; }
.topbar a { color: #fff; text-decoration: none; padding: 4px 8px; border-radius: 8px; }
.topbar a:hover { background: rgba(255,255,255,0.16); }
.nav-dropdown {
    position: relative;
    display: inline-flex;
    align-items: center;
}
.dropdown-toggle {
    color: #fff;
    text-decoration: none;
    padding: 4px 8px;
    border-radius: 8px;
    border: none;
    background: transparent;
    font: inherit;
    cursor: pointer;
}
.dropdown-toggle:hover {
    background: rgba(255,255,255,0.16);
}
.dropdown-toggle::after {
    content: " ▼";
    font-size: 0.72rem;
}
.dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 2px);
    left: 0;
    min-width: 210px;
    background: #fff;
    border: 1px solid #c6d2c2;
    border-radius: 10px;
    box-shadow: 0 10px 24px rgba(31, 41, 51, 0.14);
    padding: 6px;
    z-index: 50;
}
.dropdown-menu a {
    display: block;
    color: var(--ink);
    border-radius: 8px;
    padding: 8px 10px;
}
.dropdown-menu a:hover {
    background: #eef5ea;
}
.nav-dropdown.open .dropdown-menu,
.nav-dropdown:focus-within .dropdown-menu {
    display: block;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

h1, h2 { margin-top: 0; }
.panel, .auth-card, .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 16px;
    box-shadow: 0 8px 22px rgba(31, 41, 51, 0.05);
}
.panel { margin-bottom: 16px; }
.auth-card { max-width: 460px; margin: 30px auto; }

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
}
.card h3 { margin-bottom: 6px; }
.card p { font-size: 1.6rem; font-weight: 700; margin: 0; color: var(--brand); }

.grid-form {
    display: grid;
    gap: 10px;
    grid-template-columns: 1fr;
}
.grid-form.grid-3 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
label { display: grid; gap: 6px; font-size: 0.95rem; }
input, select, button, textarea {
    font: inherit;
    padding: 10px;
    border: 1px solid #c6d2c2;
    border-radius: 10px;
}
textarea {
    resize: vertical;
}
button {
    background: var(--brand);
    color: #fff;
    border: none;
    cursor: pointer;
}
button:hover { filter: brightness(1.05); }
button.danger { background: var(--danger); }
.action-btn {
    display: inline-block;
    font: inherit;
    padding: 10px 14px;
    border-radius: 10px;
    color: #fff;
    text-decoration: none;
    border: none;
    background: var(--brand);
}
.action-btn:hover { filter: brightness(1.05); }
.tab-link {
    display: inline-block;
    font: inherit;
    padding: 10px 14px;
    border-radius: 10px;
    text-decoration: none;
    border: 1px solid #c6d2c2;
    background: #fff;
    color: var(--ink);
}
.tab-link.active-tab {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
}
.tab-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    overflow: visible;
    padding-bottom: 0;
}
.tab-row .tab-link {
    white-space: nowrap;
    flex: 0 1 auto;
}

.inline-form {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.students-list-add-form {
    margin-top: 12px;
    display: grid;
    grid-template-columns: repeat(5, minmax(120px, 1fr));
    gap: 8px;
    align-items: end;
}

.students-list-add-form > * {
    min-width: 0;
}

.students-list-add-form label {
    min-width: 0;
}

.students-list-add-form input,
.students-list-add-form select {
    width: 100%;
    min-width: 0;
}

.students-list-add-form button {
    width: 100%;
    height: 42px;
}

.students-list-panel {
    border: 0;
    box-shadow: none;
    background: transparent;
    padding: 0;
}

.table-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.table-actions form {
    margin: 0;
}

.table-actions .action-btn,
.table-actions button {
    min-width: 0;
    white-space: nowrap;
}

.reassign-cell {
    position: relative;
    min-width: 74px;
}

.reassign-trigger {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 10px;
    font-size: 1.1rem;
    line-height: 1;
}

.reassign-panel {
    position: absolute;
    top: 44px;
    right: 0;
    width: min(310px, calc(100vw - 48px));
    z-index: 30;
    background: #fff;
    border: 1px solid #dce6d8;
    border-radius: 12px;
    box-shadow: 0 12px 24px rgba(31, 41, 51, 0.14);
    padding: 10px;
}

.reassign-form {
    display: grid;
    gap: 8px;
}

.reassign-form select,
.reassign-form input,
.reassign-form button {
    width: 100%;
}

.note-cell {
    max-width: 300px;
    white-space: pre-line;
    font-size: 0.88rem;
    color: #385064;
}

.inline-edit-form {
    display: none;
}

.inline-edit-input {
    width: 100%;
    min-width: 110px;
    padding: 8px 9px;
}

.inline-edit-textarea {
    width: 100%;
    min-height: 34px;
    height: 34px;
    line-height: 20px;
    padding: 6px 4px;
    overflow-y: hidden;
    overflow-x: auto;
    white-space: nowrap;
    resize: none;
}

.inline-edit-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 2px;
}

.clear-note-btn,
.edit-row-btn,
.clear-row-btn {
    padding: 4px 8px;
    border-radius: 8px;
    font-size: 0.78rem;
    line-height: 1.2;
    background: #e9efe5;
    color: #2b3a33;
}

.delete-row-cell {
    width: 130px;
}

.lookup-status {
    display: block;
    min-height: 0;
    color: #375244;
    font-size: 0.78rem;
}

.lookup-status::before {
    display: inline-block;
    width: 1em;
    margin-right: 4px;
    text-align: center;
}

.lookup-status[data-state="saving"]::before {
    content: "...";
    color: #375244;
}

.lookup-status[data-state="saved"]::before {
    content: "\2714";
    color: #1d6f42;
}

.lookup-status[data-state="error"]::before {
    content: "\26A0";
    color: #b42318;
}

.lookup-status[data-state="idle"]::before,
.lookup-status:not([data-state])::before {
    content: "";
}

.students-list-table th,
.students-list-table td {
    vertical-align: top;
    padding: 6px 8px;
}

.students-list-panel .table-wrap {
    overflow-x: auto;
}

.students-list-table {
    table-layout: auto;
    min-width: 1120px;
}

.students-list-table th:nth-child(5),
.students-list-table td[data-label="Họ tên"] {
    min-width: 220px;
}

.students-list-table th:nth-child(6),
.students-list-table td[data-label="Email"] {
    min-width: 260px;
}

.students-list-table .inline-edit-input,
.students-list-table .inline-edit-textarea {
    border: 0;
    border-bottom: 1px solid #b9c9b2;
    border-radius: 0;
    background: transparent;
    padding: 4px 4px;
}

.students-list-table .inline-edit-input:focus,
.students-list-table .inline-edit-textarea:focus {
    outline: none;
    box-shadow: none;
    border-bottom-color: var(--brand);
}

.students-list-table .avatar-thumb {
    width: 38px;
    height: 38px;
    border-radius: 8px;
}

.students-list-table tbody tr {
    height: auto;
}

.announcement-form {
    display: grid;
    grid-template-columns: repeat(2, minmax(280px, 1fr));
    gap: 12px;
    align-items: end;
}
.announcement-form.stacked {
    grid-template-columns: 1fr;
}
.announcement-form .content-field,
.announcement-form .submit-wrap {
    grid-column: 1 / -1;
}
.announcement-form .content-field textarea {
    min-height: 120px;
}
.announcement-form input[type="file"] {
    width: 100%;
    background: #fff;
}
.announcement-form .submit-wrap {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
}

@media (max-width: 860px) {
    .announcement-form {
        grid-template-columns: 1fr;
    }
    .announcement-form .submit-wrap {
        justify-content: stretch;
    }
    .announcement-form .submit-wrap button {
        width: 100%;
    }
}

.table-wrap {
    width: 100%;
    overflow-x: hidden;
}
table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    table-layout: fixed;
}
th, td {
    padding: 10px;
    border-bottom: 1px solid #e9efe5;
    text-align: left;
    font-size: 0.94rem;
    vertical-align: top;
    overflow-wrap: anywhere;
    word-break: break-word;
}
th { background: #f4f8ee; }

.flash-wrap { margin-bottom: 10px; }
.flash {
    padding: 10px 12px;
    margin-bottom: 8px;
    border-radius: 10px;
}
.flash.success { background: #e8f8ec; color: #166534; }
.flash.error { background: #fdecec; color: #9f1239; }

.muted { color: #5f6c77; font-size: 0.92rem; }
.block { display: block; }

.result-box {
    margin-top: 10px;
    padding: 10px;
    border-radius: 10px;
    background: #f6f8f7;
    border: 1px solid #d5ddd9;
}
.result-box.ok { background: #e8f8ec; border-color: #bce4c8; color: #166534; }
.result-box.err { background: #fdecec; border-color: #f8c6c6; color: #9f1239; }
.draggable-row { cursor: grab; }
.draggable-row.dragging { opacity: 0.55; }
.avatar-preview img {
    width: 160px;
    height: 160px;
    object-fit: cover;
    border-radius: 12px;
    border: 1px solid #c6d2c2;
}
.avatar-thumb {
    width: 56px;
    height: 56px;
    object-fit: cover;
    border-radius: 10px;
    border: 1px solid #c6d2c2;
}
.scan-avatar-wrap {
    margin-top: 10px;
}
.scan-avatar-img {
    width: 140px;
    height: 140px;
    object-fit: cover;
    border-radius: 12px;
    border: 1px solid #c6d2c2;
    background: #fff;
}
.score-sheet th,
.score-sheet td {
    white-space: normal;
    border: 1px solid #cdd5d1;
    padding: 5px 6px;
    font-size: 0.86rem;
}
.score-sheet {
    border-collapse: collapse;
    border: 1px solid #cdd5d1;
    table-layout: fixed;
    width: 100%;
}
.score-sheet th {
    background: #eef2ec;
    text-align: center;
}
.score-sheet .col-mssv {
    width: 90px;
}
.score-sheet .col-name {
    width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
}
.score-sheet .col-att {
    width: 46px;
    text-align: center;
}
.score-sheet .col-score {
    width: 58px;
    text-align: center;
}
.score-sheet .col-note {
    width: 120px;
}
.score-cell {
    min-height: 20px;
    outline: none;
    text-align: center;
}
.score-cell-empty {
    min-height: 20px;
    display: block;
}
.score-cell:focus {
    box-shadow: inset 0 0 0 2px #1f2933;
}
.score-cell.dirty {
    border-color: #f0a202;
    background: #fff8e6;
}
.score-cell.saving {
    border-color: #6d8ee5;
    background: #edf3ff;
}
.score-cell.saved {
    border-color: #1d6f42;
    background: #e8f8ec;
}
.role-block {
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #fff;
    margin-bottom: 10px;
    padding: 10px;
}
.role-block summary {
    cursor: pointer;
}

.group-schedule-form {
    border: 1px solid #dce6d8;
    border-radius: 12px;
    padding: 12px;
    background: linear-gradient(180deg, #ffffff, #f7faf5);
}
.group-schedule-table th {
    background: #eef5ea;
    font-weight: 700;
}
.group-schedule-table tbody tr:nth-child(even) {
    background: #fbfdf9;
}
.group-schedule-table td:first-child {
    font-weight: 600;
    color: #214a34;
}
.schedule-select {
    min-width: 140px;
    border-radius: 10px;
    border: 1px solid #b9c9b2;
    background: #fff;
}
.schedule-save-btn {
    margin-top: 12px;
}

.learning-sessions-wrap {
    overflow-x: hidden;
}

.learning-sessions-table {
    width: 100%;
    table-layout: fixed;
}

.learning-sessions-table input,
.learning-sessions-table select {
    width: 100%;
    min-width: 0;
}

.learning-sessions-table .save-info-col {
    width: 22%;
    position: relative;
}

.learning-sessions-table details {
    background: #f8fbf6;
    border: 1px solid #dce6d8;
    border-radius: 10px;
    padding: 6px 8px;
}

.learning-sessions-table details > summary {
    cursor: pointer;
    font-weight: 600;
    color: #214a34;
}

.learning-sessions-table .save-info-list {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 35;
    width: min(560px, calc(100vw - 56px));
    max-height: 180px;
    overflow-y: auto;
    overflow-x: auto;
    background: #ffffff;
    border: 1px solid #d8e2d4;
    border-radius: 10px;
    box-shadow: 0 10px 20px rgba(31, 41, 51, 0.14);
    padding: 8px;
}

.learning-sessions-table .save-info-list label {
    grid-template-columns: auto 1fr;
    align-items: start;
    gap: 8px;
    font-size: 0.9rem;
    margin-bottom: 4px;
    white-space: nowrap;
    min-width: max-content;
}

.learning-sessions-table .save-info-list input[type="checkbox"] {
    width: 16px;
    min-width: 16px;
    height: 16px;
    margin: 2px 0 0;
    padding: 0;
    border-radius: 4px;
}

.learning-sessions-table .save-info-text {
    font-size: 0.88rem;
    line-height: 1.35;
    white-space: normal;
    word-break: break-word;
}

.learning-sessions-table .save-info-line {
    white-space: nowrap;
}

.learning-sessions-table .stt-order-select {
    min-width: 72px;
}

.session-group-checklist {
    border: 1px solid #d2d7cf;
    border-radius: 10px;
    padding: 10px;
    background: #f5f8f2;
    min-width: 320px;
}

.session-group-checklist label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 4px 0;
    font-weight: 500;
}

.lesson-filter-form {
    display: grid;
    grid-template-columns: repeat(3, minmax(220px, 1fr));
    gap: 10px 12px;
    align-items: end;
}

.lesson-setup-form {
    display: grid;
    grid-template-columns: repeat(4, minmax(180px, 1fr));
    gap: 10px 12px;
    align-items: end;
}

.lesson-col-2 {
    grid-column: span 2;
}

.lesson-form-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.lesson-form-actions button,
.lesson-form-actions .action-btn {
    min-width: 130px;
    text-align: center;
}

select.session-group-checklist {
    min-height: 190px;
}

.lesson-setup-form > label,
.lesson-filter-form > label {
    min-width: 0;
}

@media (max-width: 980px) {
    .lesson-filter-form {
        grid-template-columns: repeat(2, minmax(220px, 1fr));
    }
    .lesson-setup-form {
        grid-template-columns: repeat(2, minmax(220px, 1fr));
    }
}

@media (max-width: 680px) {
    .lesson-filter-form,
    .lesson-setup-form {
        grid-template-columns: 1fr;
    }
    .lesson-col-2 {
        grid-column: auto;
    }
}

@media (max-width: 720px) {
    .container { padding: 12px; }
    .topbar { padding: 12px; }
    .topbar nav {
        width: 100%;
        gap: 8px;
    }
    .topbar nav > a,
    .topbar nav > .nav-dropdown {
        flex: 1 1 calc(50% - 8px);
    }
    .topbar nav > a {
        text-align: center;
        padding: 8px 10px;
        border: 1px solid rgba(255,255,255,0.25);
    }
    .dropdown-toggle {
        width: 100%;
        text-align: center;
        padding: 8px 10px;
        border: 1px solid rgba(255,255,255,0.25);
    }
    .dropdown-menu {
        width: 100%;
        min-width: 0;
        position: static;
        margin-top: 6px;
        box-shadow: none;
        border-color: rgba(255,255,255,0.4);
    }
    .tab-row {
        display: grid;
        grid-template-columns: 1fr;
    }
    .tab-row .tab-link {
        width: 100%;
        text-align: left;
    }
    .inline-form {
        flex-direction: column;
        align-items: stretch;
    }
    .inline-form > * {
        width: 100%;
    }
    label input,
    label select,
    label textarea,
    .inline-form input,
    .inline-form select,
    .inline-form textarea,
    .inline-form button,
    .inline-form .action-btn {
        width: 100%;
    }
    .students-list-add-form {
        grid-template-columns: 1fr;
    }
    .table-wrap {
        border: 0;
        background: transparent;
    }
    table,
    thead,
    tbody,
    tr,
    th,
    td {
        display: block;
        width: 100%;
    }
    thead {
        display: none;
    }
    tbody tr {
        background: #fff;
        border: 1px solid #dbe5d7;
        border-radius: 12px;
        padding: 6px 10px;
        margin-bottom: 10px;
        box-shadow: 0 4px 14px rgba(31, 41, 51, 0.05);
    }
    tbody tr:last-child {
        margin-bottom: 0;
    }
    tbody td {
        border: 0;
        padding: 8px 0;
        display: flex;
        gap: 10px;
        align-items: flex-start;
        justify-content: space-between;
    }
    tbody td[data-label]::before {
        content: attr(data-label);
        flex: 0 0 42%;
        max-width: 42%;
        font-weight: 700;
        color: #375244;
    }
    tbody td > * {
        flex: 1 1 auto;
        min-width: 0;
    }
    .avatar-thumb {
        width: 64px;
        height: 64px;
    }
    .score-sheet,
    .score-sheet thead,
    .score-sheet tbody,
    .score-sheet tr,
    .score-sheet th,
    .score-sheet td {
        display: table;
        width: auto;
    }
    .score-sheet {
        width: 100%;
        table-layout: fixed;
    }
    .score-sheet thead {
        display: table-header-group;
    }
    .score-sheet tbody {
        display: table-row-group;
    }
    .score-sheet tr {
        display: table-row;
    }
    .score-sheet th,
    .score-sheet td {
        display: table-cell;
    }
    .score-sheet td::before {
        content: none;
    }

    .students-list-table,
    .students-list-table thead,
    .students-list-table tbody,
    .students-list-table tr,
    .students-list-table th,
    .students-list-table td {
        display: table;
        width: auto;
    }
    .students-list-table {
        width: 100%;
        table-layout: auto;
    }
    .students-list-table thead {
        display: table-header-group;
    }
    .students-list-table tbody {
        display: table-row-group;
    }
    .students-list-table tr {
        display: table-row;
        background: transparent;
        border: 0;
        border-radius: 0;
        padding: 0;
        margin-bottom: 0;
        box-shadow: none;
    }
    .students-list-table th,
    .students-list-table td {
        display: table-cell;
        vertical-align: top;
    }
    .students-list-table td::before {
        content: none;
    }

    .table-actions {
        display: flex;
        width: 100%;
        gap: 6px;
    }
    .table-actions .action-btn,
    .table-actions button {
        width: auto;
        padding: 8px 12px;
        border-radius: 8px;
    }
    .students-list-table .reassign-panel {
        width: min(280px, calc(100vw - 32px));
        right: auto;
        left: 0;
    }
    .learning-sessions-table .save-info-list {
        right: auto;
        left: 0;
        width: min(92vw, 520px);
    }
}
