:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #5d6878;
  --border: #dde2e8;
  --accent: #b3261e;
  --accent-text: #ffffff;
  --today: #b3261e;
  --weekend-bg: #eef0f3;
  --outside-bg: #f6f7f9;

  --hoc-bg: #e3edfd;   --hoc-bar: #2563eb;   --hoc-fg: #173a7a;
  --thi-bg: #fde4e2;   --thi-bar: #d92d20;   --thi-fg: #8a1610;
  --nghi-bg: #e2f5e6;  --nghi-bar: #3b9a57;  --nghi-fg: #1d5530;
  --le-bg: #fdf0d9;    --le-bar: #e08a00;    --le-fg: #6e4200;
  --hd-bg: #efe7fb;    --hd-bar: #7c4dce;    --hd-fg: #452577;

  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161c;
    --surface: #1b2129;
    --text: #e6eaf0;
    --muted: #9aa5b4;
    --border: #2d3540;
    --accent: #e5534b;
    --accent-text: #ffffff;
    --today: #ff7b72;
    --weekend-bg: #171c22;
    --outside-bg: #14181e;

    --hoc-bg: #1a2c4a;   --hoc-bar: #5b8def;   --hoc-fg: #cfe0ff;
    --thi-bg: #4a1d1b;   --thi-bar: #ff6b61;   --thi-fg: #ffd9d6;
    --nghi-bg: #183626;  --nghi-bar: #4fbf72;  --nghi-fg: #c9f0d5;
    --le-bg: #3d2d12;    --le-bar: #f0a530;    --le-fg: #ffe4b8;
    --hd-bg: #2e2347;    --hd-bar: #a07ef0;    --hd-fg: #e5d9ff;
  }
}

:root[data-theme="dark"] {
  --bg: #12161c;
  --surface: #1b2129;
  --text: #e6eaf0;
  --muted: #9aa5b4;
  --border: #2d3540;
  --accent: #e5534b;
  --accent-text: #ffffff;
  --today: #ff7b72;
  --weekend-bg: #171c22;
  --outside-bg: #14181e;

  --hoc-bg: #1a2c4a;   --hoc-bar: #5b8def;   --hoc-fg: #cfe0ff;
  --thi-bg: #4a1d1b;   --thi-bar: #ff6b61;   --thi-fg: #ffd9d6;
  --nghi-bg: #183626;  --nghi-bar: #4fbf72;  --nghi-fg: #c9f0d5;
  --le-bg: #3d2d12;    --le-bar: #f0a530;    --le-fg: #ffe4b8;
  --hd-bg: #2e2347;    --hd-bar: #a07ef0;    --hd-fg: #e5d9ff;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
}

.wrap { max-width: 1280px; margin: 0 auto; padding: 0 16px; }

.site-header { background: var(--surface); border-bottom: 1px solid var(--border); padding: 14px 0 12px; }
.head-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.theme-btn { flex: 0 0 auto; white-space: nowrap; }
.site-header h1 { margin: 0; font-size: 1.35rem; letter-spacing: -0.01em; }
.subtitle { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }

/* ---- Thanh điều hướng ---- */
.month-nav {
  display: flex; align-items: center; gap: 8px;
  margin: 16px 0 10px; flex-wrap: wrap;
}
.month-nav h2 { margin: 0 8px; font-size: 1.4rem; min-width: 9ch; text-align: center; flex: 1 1 auto; }
.btn {
  font: inherit; font-size: 0.95rem;
  padding: 7px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:focus-visible { outline: 2px solid var(--hoc-bar); outline-offset: 2px; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn-primary:hover:not(:disabled) { border-color: var(--accent); filter: brightness(1.08); }

/* ---- Chú thích ---- */
.legend { list-style: none; margin: 0 0 14px; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 0.88rem; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.sw { width: 14px; height: 14px; border-radius: 3px; display: inline-block; border-left: 4px solid; }

/* ---- Màu theo loại ---- */
.t-hoc      { background: var(--hoc-bg);  border-color: var(--hoc-bar);  color: var(--hoc-fg); }
.t-thi      { background: var(--thi-bg);  border-color: var(--thi-bar);  color: var(--thi-fg); font-weight: 700; }
.t-nghi     { background: var(--nghi-bg); border-color: var(--nghi-bar); color: var(--nghi-fg); }
.t-le       { background: var(--le-bg);   border-color: var(--le-bar);   color: var(--le-fg); }
.t-hoatdong { background: var(--hd-bg);   border-color: var(--hd-bar);   color: var(--hd-fg); }

/* ---- Lưới tháng ---- */
.calendar {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 1px; background: var(--border);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
}
.calendar .dow {
  background: var(--surface); padding: 6px 8px;
  font-weight: 600; font-size: 0.85rem; color: var(--muted); text-align: center;
}
.cell {
  background: var(--surface); min-height: 150px; padding: 8px;
  display: flex; flex-direction: column; gap: 6px; min-width: 0;
}
.cell .num { font-weight: 600; font-size: 1rem; line-height: 1; padding: 2px 2px 4px; }
.cell.weekend { background: var(--weekend-bg); }
.cell.weekend .note { color: var(--muted); font-size: 0.86rem; padding: 0 2px; }
.cell.weekend .note.holiday { color: var(--le-fg); background: var(--le-bg); border-left: 4px solid var(--le-bar); border-radius: 4px; padding: 6px 8px; line-height: 1.5; }
.cell.outside { background: var(--outside-bg); }
.cell.outside .num { color: var(--muted); opacity: 0.45; }
.cell.today { box-shadow: inset 0 0 0 2px var(--today); }
.cell.today .num { color: var(--today); }
.cell.today .num::after { content: " · Hôm nay"; font-size: 0.75rem; font-weight: 600; }

.slot {
  border-left: 4px solid; border-radius: 4px;
  padding: 6px 8px; font-size: 0.86rem; line-height: 1.5;
  overflow-wrap: anywhere;
}
.slot b { font-weight: 700; }
.slot.full { flex: 1 1 auto; }

/* ---- Danh sách (điện thoại) ---- */
.day-list { display: none; }
.row {
  display: flex; gap: 10px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; margin-bottom: 6px; scroll-margin-top: 12px;
}
.admin-link { color: var(--muted); }
.row .d { flex: 0 0 92px; font-weight: 600; font-size: 0.9rem; line-height: 1.25; }
.row .d small { display: block; font-weight: 400; color: var(--muted); }
.row .slots { flex: 1 1 auto; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.row.weekend { background: transparent; border-style: dashed; color: var(--muted); padding: 5px 10px; }
.row.weekend .d { font-weight: 500; }
.row.weekend .slots { font-size: 0.85rem; }
.row.weekend.holiday .slots { color: var(--le-fg); }
.row.today { border: 2px solid var(--today); }
.row.today .d { color: var(--today); }

/* ---- Phần phụ ---- */
.extras { display: grid; grid-template-columns: minmax(260px, 1fr) 2fr; gap: 16px; margin: 20px 0 8px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.card h3 { margin: 0 0 10px; font-size: 1.05rem; }
.upcoming { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.upcoming li { border-left: 4px solid; border-radius: 4px; padding: 6px 8px; font-size: 0.9rem; }
.upcoming .when { font-weight: 700; margin-right: 4px; }
.upcoming .empty { border: 0; color: var(--muted); padding: 0; }

.table-wrap { overflow-x: auto; }
.subjects { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.subjects th, .subjects td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.subjects th { color: var(--muted); font-weight: 600; white-space: nowrap; }
.subjects td.num, .subjects th.num { text-align: right; }
.subjects td.exam { white-space: nowrap; }
.subjects tr.done td { color: var(--muted); }
.subjects tr.next td { background: var(--thi-bg); color: var(--thi-fg); font-weight: 600; }
.subjects tfoot td { font-weight: 700; border-bottom: 0; }

.site-footer { border-top: 1px solid var(--border); margin-top: 16px; padding: 14px 0 24px; font-size: 0.85rem; }
.site-footer p { margin: 2px 0; }
.muted { color: var(--muted); }


/* ---- Chuyển cách xem ---- */
.view-switch { display: inline-flex; gap: 4px; margin: 16px 0 0; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.vs-btn { font: inherit; font-size: 0.95rem; padding: 7px 14px; border: 0; border-radius: 7px; background: transparent; color: var(--muted); cursor: pointer; }
.vs-btn.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.vs-btn:focus-visible { outline: 2px solid var(--hoc-bar); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---- Xem theo môn ---- */
.subj-link { color: inherit; text-decoration: none; border-bottom: 1px dashed var(--muted); }
.subj-link:hover { color: var(--hoc-fg); border-bottom-color: var(--hoc-bar); }
.subj-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 16px 0 14px; }
.chip { font-size: 0.86rem; padding: 6px 11px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text); text-decoration: none; white-space: nowrap; }
.chip:hover { border-color: var(--muted); }
.chip.done { color: var(--muted); }
.chip.has-info::after { content: ' 👤'; font-size: 0.8em; }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.subj-detail { display: grid; grid-template-columns: minmax(260px, 1fr) 2fr; gap: 16px; align-items: start; margin-bottom: 8px; }
.subj-head { grid-column: 1 / -1; }
.subj-head h2 { margin: 0 0 8px; font-size: 1.35rem; }
.subj-meta { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 0; font-size: 0.92rem; }
.subj-meta dt { color: var(--muted); }
.subj-meta dd { margin: 0; }
.lect-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.lect-list li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 1px 10px; font-size: 0.92rem; }
.lname { font-weight: 600; }
.phone { color: var(--hoc-fg); text-decoration: none; font-variant-numeric: tabular-nums; }
.phone::before { content: '📞 '; }
.phone:hover { text-decoration: underline; }
.sess { display: grid; grid-template-columns: 120px 1fr; gap: 12px; padding: 10px 10px 10px 12px; border-left: 4px solid; border-radius: 6px; margin-bottom: 8px; line-height: 1.5; }
.sess .when b { display: block; font-size: 0.92rem; }
.sess .when small { color: inherit; opacity: 0.85; }
.sess .ltitle { font-weight: 600; }
.sess .lcode { font-weight: 400; opacity: 0.75; font-size: 0.85em; }
.sess .lline { display: flex; flex-wrap: wrap; gap: 2px 12px; font-size: 0.9rem; margin-top: 2px; }
.sess .lline .lname { font-weight: 500; }
.sess .phone { color: inherit; opacity: 0.95; }
.sess.past { opacity: 0.6; }
.sess.today { box-shadow: 0 0 0 2px var(--today); }
.small { font-size: 0.85rem; }

/* Chip đánh số, chế độ xem trong môn */
.chip-no { font-weight: 700; opacity: 0.75; font-variant-numeric: tabular-nums; }
.chip.active .chip-no { opacity: 1; }
.subj-detail.mode-week { grid-template-columns: 1fr; }
.mode-week .lect-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 8px 16px; }
.sess-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.sess-bar h3 { margin: 0; }
.mode-switch { display: inline-flex; gap: 2px; padding: 3px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.mode-btn { font: inherit; font-size: 0.85rem; padding: 5px 10px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); cursor: pointer; white-space: nowrap; }
.mode-btn.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--border); }

/* Bảng lịch tuần */
.wk + .wk { margin-top: 16px; }
.wk-title { font-weight: 600; font-size: 0.92rem; color: var(--muted); margin: 0 0 6px; }
.wk-scroll { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
.wk-table { width: 100%; min-width: 760px; border-collapse: collapse; table-layout: fixed; font-size: 0.86rem; line-height: 1.45; }
.wk-table th, .wk-table td { border: 1px solid var(--border); padding: 8px; vertical-align: top; text-align: left; }
.wk-table tr > :first-child { border-left: 0; }
.wk-table tr > :last-child { border-right: 0; }
.wk-table thead th { border-top: 0; background: var(--surface); font-weight: 500; }
.wk-table tbody tr:last-child > * { border-bottom: 0; }
.wk-table thead th small { color: var(--muted); }
.wk-table thead th.is-today { color: var(--today); }
.wk-table thead th.is-today small { color: var(--today); }
.wk-corner, .wk-slot { width: 64px; color: var(--muted); font-weight: 600; background: var(--surface); position: sticky; left: 0; z-index: 1; }
.wk-table td.mine { border-left: 4px solid; }
.wk-table td.mine .ltitle { font-weight: 600; }
.wk-table td.mine .lcode { font-weight: 400; opacity: 0.75; font-size: 0.85em; }
.wk-table td.mine .lline { display: flex; flex-wrap: wrap; gap: 0 8px; margin-top: 3px; font-size: 0.84rem; }
.wk-table td.mine .lline .lname { font-weight: 500; }
.wk-table td.mine .phone { color: inherit; }
.wk-table td.mine .nodata { opacity: 0.75; margin-top: 2px; }
.wk-table td.other { color: var(--muted); font-size: 0.8rem; background: var(--weekend-bg); }
.wk-table td.past { opacity: 0.6; }
.wk-table td.is-today { box-shadow: inset 0 0 0 2px var(--today); }

@media (max-width: 1000px) {
  .subj-detail { grid-template-columns: 1fr; }
  .lect-box { order: 2; }
  .lect-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px 16px; }
  .extras { grid-template-columns: 1fr; }
  .cell { min-height: 130px; padding: 6px; }
}

/* ---- Điện thoại: < 640px ---- */
@media (max-width: 639.98px) {
  .view-switch { display: flex; }
  .vs-btn { flex: 1 1 0; }
  .sess { grid-template-columns: 1fr; gap: 4px; }
  .subj-chips { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 6px; margin-right: -16px; padding-right: 16px; scrollbar-width: thin; }
  body { font-size: 15px; }
  .site-header h1 { font-size: 1.15rem; }
  .subtitle { font-size: 0.82rem; }
  .calendar { display: none; }
  .day-list { display: block; }
  .month-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .month-nav h2 { grid-column: 1 / -1; order: -1; margin: 0; font-size: 1.3rem; }
  .month-nav #today { grid-column: 1 / -1; }
  .legend { gap: 4px 12px; font-size: 0.8rem; }
  .subjects { font-size: 0.82rem; }
  .subjects th, .subjects td { padding: 5px 6px; }
}

/* Bảng tuần trên điện thoại: hàng = ngày, cột = Sáng / Chiều */
.wk-table.wk-narrow { min-width: 0; font-size: 0.84rem; }
.wk-narrow .wk-corner, .wk-narrow .wk-slot { width: 58px; position: static; }
.wk-narrow .wk-slot b { display: block; color: var(--text); }
.wk-narrow .wk-slot small { font-weight: 400; }
.wk-narrow .wk-slot.is-today, .wk-narrow .wk-slot.is-today b { color: var(--today); }
.wk-narrow th, .wk-narrow td { padding: 6px; overflow-wrap: anywhere; }
.wk-narrow td.mine .lline { flex-direction: column; gap: 0; }
