/* Rooms x periods matrix (the default "当天总览" layout) and the phone bottom sheet.
   The matrix never scrolls sideways: its density follows the width of its own container, not the device. */

.matrix-panel { background: var(--paper-bg); border: 1px solid var(--line-border); border-radius: var(--radius); overflow: visible; }
/* `clip` rounds the corners without turning the panel into a scroll container, so the sticky header keeps working. */
@supports (overflow: clip) { .matrix-panel { overflow: clip; } }

.matrix { position: relative; container-type: inline-size; container-name: matrix; padding: 0 0 2px; }
.matrix-table { --m-room-w: 40px; --m-row-h: 38px; width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed; }
.matrix-table thead th {
  position: sticky; top: var(--app-header-offset, 0px); z-index: 3;
  padding: 5px 1px 4px; background: var(--paper-bg); border-bottom: 1px solid var(--line-border);
  color: var(--muted-text); font-size: 0.6875rem; font-weight: 700; line-height: 1.2; text-align: center;
  transition: top 0.2s ease;
}
.matrix-table thead th small { display: block; font-size: 0.625rem; font-weight: 500; opacity: 0.85; }
.matrix-table .m-corner { width: var(--m-room-w); padding-left: 8px; text-align: left; }
.m-slot-word { display: none; }

.matrix-table tbody th.m-room { padding: 0; background: var(--paper-bg); border-bottom: 1px solid var(--line-border); font-weight: 700; text-align: left; vertical-align: middle; }
.m-room-btn { display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 1px; width: 100%; min-height: var(--m-row-h); padding: 2px 2px 2px 8px; border: 0; border-radius: 0; background: transparent; color: var(--ink-text); font-size: 0.75rem; font-weight: 700; text-align: left; }
.m-room-btn:hover { color: var(--blue-text); }
.m-room-name, .m-room-seats { display: none; }
.m-room-seats { color: var(--muted-text); font-size: 0.6875rem; font-weight: 500; }

.m-td { height: var(--m-row-h); padding: 2px; border-bottom: 1px solid var(--line-border); vertical-align: middle; }
.m-cell {
  position: relative; display: flex; flex-direction: column; justify-content: center; gap: 1px;
  width: 100%; height: 100%; min-height: calc(var(--m-row-h) - 4px); margin: 0; padding: 2px 4px;
  border: 0; border-radius: 6px; text-align: left; font: inherit; line-height: 1.2; overflow: hidden;
}
button.m-cell { cursor: pointer; }
button.m-cell:hover { box-shadow: inset 0 0 0 1.5px var(--blue-border); }
button.m-cell:focus-visible { outline-offset: -2px; }
.m-free, .m-unknown { cursor: pointer; }
.m-free { background: var(--cell-free-bg); color: var(--cell-free-fg); }
.m-unknown { background: var(--cell-unknown-bg); color: var(--cell-unknown-fg); background-image: repeating-linear-gradient(135deg, transparent 0 5px, var(--cell-unknown-line) 5px 6px); }
.m-course { background: var(--cell-course-bg); color: var(--cell-course-fg); }
.m-exam { background: var(--cell-exam-bg); color: var(--cell-exam-fg); }
.m-makeup { background: var(--cell-makeup-bg); color: var(--cell-makeup-fg); }
.m-software { background: var(--cell-software-bg); color: var(--cell-software-fg); }

.m-name { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; overflow-wrap: anywhere; line-break: anywhere; font-size: 0.6875rem; font-weight: 650; line-height: 1.2; }
.m-meta, .m-time { display: none; }
.m-more { position: absolute; top: 1px; right: 2px; padding: 0 4px; border-radius: 7px; background: var(--badge-bg); color: var(--badge-fg); font-size: 0.5625rem; font-weight: 700; line-height: 1.5; }
.m-cell[data-offgrid]::after { content: ''; position: absolute; right: 3px; bottom: 3px; width: 5px; height: 5px; border-radius: 50%; background: currentColor; opacity: 0.7; }
.m-live { display: none; }
.m-cell.is-dim { opacity: 0.32; }
.m-cell.is-hit { box-shadow: 0 0 0 2px var(--blue-border); }
.m-more { z-index: 1; }

/* ---- "Happening now": live cells are solid, the current column is washed, its header is solid ---- */
.matrix-table thead th.is-now { background: var(--cell-live-bg); color: var(--cell-live-fg); }
.matrix-table thead th.is-now small { color: inherit; opacity: 0.85; }
.m-cell.is-live { background: var(--cell-live-bg); color: var(--cell-live-fg); }
button.m-cell.is-live:hover { box-shadow: inset 0 0 0 2px var(--cell-live-fg); }
.m-cell.is-live .m-more { background: var(--cell-live-fg); color: var(--cell-live-bg); }
.m-cell.is-live::before { content: ''; display: none; position: absolute; top: 6px; right: 6px; width: 6px; height: 6px; border-radius: 50%; background: currentColor; animation: live-blink 1.8s ease-in-out infinite; }
.m-cell.is-live .m-live:not(:empty) { display: none; font-size: 0.6875rem; font-weight: 700; letter-spacing: 0.02em; }
/* A translucent wash over the whole current column (the current period on the day matrix, today on the week matrices).
   Placed and sized by live-status.js, so spanned cells and every density tier line up. */
.m-now { position: absolute; top: 0; bottom: 0; z-index: 2; pointer-events: none; background: var(--live-band-bg); box-shadow: inset 1.5px 0 0 color-mix(in srgb, var(--live-border) 55%, transparent), inset -1.5px 0 0 color-mix(in srgb, var(--live-border) 55%, transparent); }
.m-now[hidden] { display: none; }
@media (prefers-reduced-motion: reduce) { .m-cell.is-live::before { animation: none; } }

/* Layout switch: a quiet segmented control instead of three loud buttons. */
.schedule-layout-switch { gap: 2px; padding: 3px; border: 1px solid var(--line-border); border-radius: 10px; background: var(--bg-bg); }
.schedule-layout-switch button { min-height: 30px; padding: 0 12px; border: 0; border-radius: 7px; background: transparent; color: var(--muted-text); font-size: 0.8125rem; font-weight: 600; }
.schedule-layout-switch button:hover { color: var(--ink-text); }
.schedule-layout-switch button[aria-pressed="true"] { background: var(--paper-bg); color: var(--ink-text); border: 0; box-shadow: 0 1px 3px color-mix(in srgb, var(--ink-text) 18%, transparent), 0 0 0 1px var(--line-border); }
.legend-btn, .tools-note { display: none; }
.legend-list { display: grid; gap: 12px; margin: 0; padding: 0; list-style: none; }
.legend-list li { display: flex; align-items: flex-start; gap: 12px; }
.legend-list .m-sw { flex: none; width: 20px; height: 20px; margin-top: 2px; border-radius: 5px; }
.legend-list p { margin: 2px 0 0; }

/* Legend swatches reuse the cell colours. */
.m-sw.m-live-sw { background: var(--cell-live-bg); border-color: var(--cell-live-bg); }
.m-sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--line-border); }
.m-sw.m-unknown { background-image: repeating-linear-gradient(135deg, transparent 0 3px, var(--cell-unknown-line) 3px 4px); }

/* Phones and small tablets: one slim row above the grid (layout switch + legend button), no description, no inline
   legend. The page title (day strip, app bar) already says which day it is. Week / free-room / duty stay one tap away
   in the bottom navigation. */
@media (max-width: 900px), (max-height: 480px) and (orientation: landscape) {
  .matrix-panel { position: relative; display: flex; flex-wrap: wrap; align-items: center; }
  .matrix-panel > * { flex: 1 0 100%; min-width: 0; }
  .matrix-panel > .panel-head { flex: 1 1 0; padding: 8px 6px 8px 12px; gap: 0; border-bottom: 0; }
  .matrix-panel > .panel-head h2 { overflow: hidden; font-size: 0.9375rem; white-space: nowrap; text-overflow: ellipsis; }
  .matrix-panel .panel-head p, .matrix-panel .panel-actions { display: none; }
  .matrix-panel > .panel-head.m-head-quiet { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .matrix-panel .software-reminder-bar.empty { display: none; }
  .matrix-panel > .schedule-tools { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; padding: 8px 12px 8px 6px; border-bottom: 0; }
  .matrix-panel:has(> .panel-head.m-head-quiet) > .schedule-tools { flex: 1 0 100%; justify-content: space-between; padding-left: 12px; }
  .matrix-panel > .legend { display: none; }
  .matrix-panel .schedule-layout-switch { width: auto; flex: none; }   /* beats the legacy full-width switch at <= 380px */
  .matrix-panel .schedule-layout-switch button { flex: none; }
  .tools-note { display: inline; color: var(--muted-text); font-size: 0.75rem; font-weight: 600; white-space: nowrap; }
  .legend-btn { display: inline-flex; align-items: center; gap: 6px; min-height: 38px; padding: 0 10px; border: 1px solid var(--line-border); border-radius: 10px; background: var(--paper-bg); color: var(--muted-text); font-size: 0.8125rem; font-weight: 600; }
  .legend-btn::before { content: ''; width: 10px; height: 10px; border-radius: 3px; background: var(--cell-live-bg); }
  .matrix-table { --m-row-h: 38px; }
}
@media (max-width: 359px) { .tools-note { display: none; } }

/* Density follows the matrix's own width. */
@container matrix (min-width: 480px) {
  .m-cell.is-live::before { display: block; }
  .m-cell.is-live { padding-right: 16px; } /* room for the dot */
  .m-cell.is-live .m-live:not(:empty) { display: block; }
  .matrix-table { --m-room-w: 76px; --m-row-h: 48px; }
  .m-room-id { display: none; }
  .m-room-name, .m-room-seats { display: block; }
  .m-slot-word { display: inline; }
  .m-name { font-size: 0.75rem; }
  .m-meta { display: block; overflow: hidden; font-size: 0.6875rem; line-height: 1.2; opacity: 0.85; white-space: nowrap; text-overflow: ellipsis; }
}
@container matrix (min-width: 768px) {
  .matrix-table { --m-room-w: 104px; --m-row-h: 58px; }
  .m-cell { padding: 4px 7px; }
  .m-cell.is-live { padding-right: 18px; }
  .m-name { font-size: 0.8125rem; -webkit-line-clamp: 2; }
  .m-meta { font-size: 0.75rem; }
  .m-time { display: block; overflow: hidden; font-size: 0.6875rem; opacity: 0.75; white-space: nowrap; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
  .matrix-table thead th { font-size: 0.75rem; padding: 8px 2px 6px; }
  .matrix-table thead th small { font-size: 0.6875rem; }
  .m-room-btn { font-size: 0.875rem; padding-left: 12px; }
}
@container matrix (min-width: 1100px) {
  .matrix-table { --m-row-h: 66px; }
  .m-name { font-size: 0.875rem; }
  .m-meta { font-size: 0.8125rem; }
  .m-time { font-size: 0.75rem; }
}

/* Short landscape: the date, the layout switch and the legend button share one line above the grid. */
@media (max-height: 480px) and (orientation: landscape) {
  .matrix-panel > .panel-head.m-head-quiet { position: static; width: auto; height: auto; overflow: visible; clip: auto; padding: 2px 6px 2px 12px; }
  .matrix-panel > .schedule-tools, .matrix-panel:has(> .panel-head.m-head-quiet) > .schedule-tools { flex: 0 0 auto; margin-left: auto; padding: 4px 10px 4px 6px; }
  .matrix-panel > .matrix { border-top: 1px solid var(--line-border); }
}

/* Short landscape screens: height is the scarce axis, so a day row is one line (name · teacher) and the time lives in the sheet. */
@media (max-height: 480px) and (orientation: landscape) {
  .matrix-table { --m-row-h: 34px; }
  .m-time, .m-room-seats, .m-cell.is-live .m-live:not(:empty) { display: none; } /* one-line rows: the solid cell and dot already say "now" */
  .m-cell { flex-direction: row; align-items: center; gap: 6px; padding: 1px 6px; }
  .m-name { flex: 0 1 auto; -webkit-line-clamp: 1; font-size: 0.75rem; }
  .m-meta { flex: 1 1 0; min-width: 0; }
  .m-cell.is-live .m-meta { display: none; } /* the live dot already takes the room a teacher name would need */
  .m-room-btn { flex-direction: row; align-items: center; justify-content: flex-start; padding-left: 10px; font-size: 0.8125rem; }
  .m-more { top: 50%; margin-top: -8px; }
}

/* ---- Week matrices (rooms or periods down, weekdays across). Declared after the tiers above so they win. ---- */
.matrix-table thead th.m-day { padding: 5px 0 4px; }
.m-day-name { display: block; }
.matrix-table thead th.m-day.is-focus { background: var(--softblue-bg); }
.matrix-table thead th.m-day.is-now { background: var(--cell-live-bg); color: var(--cell-live-fg); }
.m-mark { display: inline-block; width: 5px; height: 5px; margin-left: 3px; border-radius: 50%; background: var(--badge-alert-bg); vertical-align: middle; }

.matrix-table.matrix-room-week { --m-room-w: 44px; --m-row-h: 56px; }
.matrix-room-week .m-corner { padding-left: 0; }
.matrix-room-week tbody th.m-slotrow { display: table-cell; padding: 2px 2px 2px 6px; background: var(--paper-bg); border-bottom: 1px solid var(--line-border); font-weight: 700; line-height: 1.2; text-align: left; vertical-align: middle; }
.m-slotrow .m-slot-num { display: block; font-size: 0.75rem; }
.m-slotrow small { display: block; color: var(--muted-text); font-size: 0.625rem; font-weight: 500; }
.matrix-room-week .m-name { -webkit-line-clamp: 3; }
.matrix-room-week .m-cell { padding: 3px 3px; }

.matrix-table.matrix-heat { --m-row-h: 40px; }
.w-cell {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  width: 100%; height: 100%; min-height: calc(var(--m-row-h) - 4px); margin: 0; padding: 3px 4px;
  border: 0; border-radius: 6px; font: inherit; cursor: pointer;
}
.w-cell:hover { box-shadow: inset 0 0 0 1.5px var(--blue-border); }
.w-busy { background: var(--cell-course-bg); color: var(--cell-course-fg); }
.w-free { background: var(--cell-free-bg); color: var(--cell-free-fg); }
.w-unknown { background: var(--cell-unknown-bg); color: var(--cell-unknown-fg); background-image: repeating-linear-gradient(135deg, transparent 0 5px, var(--cell-unknown-line) 5px 6px); }
/* the selected weekday is marked in the column header only; boxing every cell as well made the grid noisy */
.w-cell.is-dim { opacity: 0.32; }
.w-cell.is-hit { box-shadow: 0 0 0 2px var(--blue-border); }
.w-count { font-size: 0.8125rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.w-stripes { display: flex; gap: 1px; width: 100%; max-width: 64px; }
.w-s { flex: 1 1 0; height: 5px; border-radius: 1px; background: currentColor; opacity: 0.22; }
.w-s-course { background: var(--cell-course-fg); opacity: 1; }
.w-s-exam { background: var(--cell-exam-fg); opacity: 1; }
.w-s-makeup { background: var(--cell-makeup-fg); opacity: 1; }
.w-s-software { background: var(--cell-software-fg); opacity: 1; }
.w-busy .w-s:not([class*="w-s-"]) { background: var(--cell-course-fg); opacity: 0.2; }

/* 320-360px phones: seven weekday columns are ~32px wide, so the week grid trades font size for an extra line. */
@container matrix (max-width: 359px) {
  .matrix-table.matrix-room-week { --m-room-w: 40px; --m-row-h: 58px; }
  .matrix-room-week .m-cell { padding: 2px; }
  .matrix-room-week .m-name { font-size: 0.625rem; -webkit-line-clamp: 4; }
}

@container matrix (min-width: 480px) {
  .matrix-table.matrix-room-week { --m-room-w: 64px; --m-row-h: 66px; }
  .matrix-table.matrix-heat { --m-row-h: 46px; }
  .matrix-room-week .m-name { -webkit-line-clamp: 2; }
  .w-count { font-size: 0.875rem; }
  .w-s { height: 6px; }
}
@container matrix (min-width: 768px) {
  .matrix-table.matrix-room-week { --m-room-w: 84px; --m-row-h: 84px; }
  .matrix-table.matrix-heat { --m-row-h: 52px; }
  .matrix-room-week .m-slotrow .m-slot-num { font-size: 0.875rem; }
  .matrix-room-week .m-slotrow small { font-size: 0.6875rem; }
  .w-stripes { max-width: 96px; }
  .w-s { height: 7px; }
}
@container matrix (min-width: 1100px) {
  .matrix-table.matrix-room-week { --m-row-h: 96px; }
  .matrix-table.matrix-heat { --m-row-h: 58px; }
}
@media (max-height: 480px) and (orientation: landscape) {
  .matrix-table.matrix-room-week { --m-row-h: 46px; }
  .matrix-room-week .m-cell { flex-direction: column; align-items: stretch; gap: 1px; padding: 2px 4px; }
  .matrix-room-week .m-name { -webkit-line-clamp: 2; font-size: 0.6875rem; }
  .matrix-room-week .m-time { display: block; }
  .matrix-table.matrix-heat { --m-row-h: 36px; }
}

@media (prefers-reduced-motion: reduce) { .matrix-table thead th { transition: none; } }
@media print {
  .matrix-panel { overflow: visible; }
  .matrix-table thead th { position: static; }
  .m-cell { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---- Bottom sheet: the shared detail dialog slides up from the bottom edge on phones. ---- */
@media (max-width: 600px) {
  dialog#detailDialog {
    width: 100%; max-width: 100%; max-height: 88dvh;
    margin: auto 0 0; border-radius: 18px 18px 0 0; border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  dialog#detailDialog.wide { max-height: 94dvh; }
  dialog#detailDialog .dialog-head::before { content: ''; position: absolute; top: 6px; left: 50%; width: 36px; height: 4px; margin-left: -18px; border-radius: 2px; background: var(--line-border); }
  dialog#detailDialog .dialog-head { padding-top: 18px; }
  dialog#detailDialog .dialog-body { max-height: calc(88dvh - 74px); }
}
@media (max-width: 600px) and (prefers-reduced-motion: no-preference) {
  dialog#detailDialog[open] { animation: sheet-up 0.2s ease-out; }
  @keyframes sheet-up { from { transform: translateY(32px); opacity: 0; } to { transform: none; opacity: 1; } }
}

.cell-list { display: grid; gap: 8px; }
.cell-list .event { margin: 0; }
