/* Rooms × periods matrix (the default "当天总览" layout), the week matrices and the matrix legend.
   Migrated into `@layer components` in Phase 3c together with the 58 legacy rules it used to out-rank; the shared panel
   chrome it competed with now lives in css/components.css (loaded just before this file, same layer). See docs/CSS-ARCHITECTURE.md.

   Geometry: every period column is at least --m-col-min wide, so on a phone the grid scrolls inside its own box
   (.matrix-scroller) instead of widening the page; the room column (left) and the period header (top) stay put.
   Density follows the width of the matrix itself (container queries), never the device width. */
@layer components {
  .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 (the scroller below is one). */
  @supports (overflow: clip) { .matrix-panel { overflow: clip; } }

  .matrix {
    --m-cols: 6;                      /* day matrix: six periods. The week matrices override it. */
    --m-room-w: 52px;                 /* the sticky left column */
    --m-col-min: 92px;                /* every period / weekday column is at least this wide */
    --m-row-h: 44px;
    --m-head-h: 40px;
    position: relative; container-type: inline-size; container-name: matrix; padding: 0 0 2px;
  }
  .matrix.matrix-week-grid { --m-cols: 7; }

  /* The period header is its own strip ABOVE the sideways scroller and sticks to the page stage. It has to be a
     separate element: anything that scrolls sideways is a scroll container, and `overflow-x: auto` cannot be paired
     with `overflow-y: visible` (the used value of the other axis becomes `auto`), so a header living inside the
     scroller could only ever stick to that box — never to the stage. ui/matrix-scroll.js mirrors scrollLeft into it. */
  .matrix-head {
    position: sticky; top: 0; z-index: 5;
    overflow: hidden;                                  /* crops the track when the grid is scrolled sideways */
    background: var(--paper-bg);
  }
  .matrix-head-track { min-width: calc(var(--m-room-w) + var(--m-cols) * var(--m-col-min)); }

  /* No max-height: the grid is exactly as tall as its rows, so the stage is the only thing that scrolls vertically.
     `overflow-y: hidden` is the used value `overflow-x: auto` forces on the other axis anyway, and the box is never
     shorter than its content, so nothing is ever clipped. */
  .matrix-scroller {
    position: relative; overflow-x: auto; overflow-y: hidden;
    overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch;
    scroll-padding-left: var(--m-room-w);
  }
  .matrix-canvas { position: relative; min-width: calc(var(--m-room-w) + var(--m-cols) * var(--m-col-min)); }

  .matrix-table { width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed; }
  .matrix-table col.m-col-room { width: var(--m-room-w); }

  .matrix-table thead th {
    position: sticky; top: 0; 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;
  }
  .matrix-table thead th small { display: block; font-size: 0.625rem; font-weight: 500; opacity: 0.85; }
  .matrix-table .m-corner { left: 0; z-index: 4; width: var(--m-room-w); padding-left: 0; text-align: center; }
  .m-slot-word { display: none; }

  .matrix-table tbody th.m-room,
  .matrix-table tbody th.m-slotrow { position: sticky; left: 0; z-index: 2; background: var(--paper-bg); border-bottom: 1px solid var(--line-border); font-weight: 700; text-align: center; vertical-align: middle; }
  .matrix-table tbody th.m-room { padding: 0; }
  .m-room-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; width: 100%; min-height: var(--m-row-h); padding: 2px; border: 0; border-radius: 0; background: transparent; color: var(--ink-text); font-size: var(--font-size-caption); font-weight: 700; text-align: center; }
  .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; }

  /* free periods: the absence of ink — a dashed outline, never a fill, plus a non-colour marker. */
  .m-free { flex-direction: row; align-items: center; justify-content: center; border: 1px dashed var(--cell-unknown-line); background: transparent; color: var(--cell-free-fg); }
  .m-free:hover { background: var(--surface-subtle); }
  .m-free .m-free-s, .m-free .m-free-l { font-size: 0.6875rem; font-weight: 600; line-height: 1; }
  .m-free .m-free-s::before { content: ''; display: inline-block; width: 7px; height: 7px; margin-right: 4px; border: 1.5px solid currentColor; border-radius: 50%; vertical-align: -1px; }
  .m-free .m-free-l { display: none; }

  /* incomplete coverage: hatched, with a glyph and the real explanation, so it never relies on colour alone. */
  .m-unknown {
    flex-direction: row; align-items: center; justify-content: center; gap: 6px;
    border: 1px solid var(--cell-unknown-line); color: var(--cell-unknown-fg);
    background-color: var(--cell-unknown-bg);
    background-image: repeating-linear-gradient(135deg, var(--cell-unknown-line) 0 1px, transparent 1px 7px);
  }
  .m-unknown .m-glyph { flex: none; width: 14px; height: 14px; }
  .m-unknown .m-name { font-weight: 600; font-size: 0.6875rem; -webkit-line-clamp: 2; }

  .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; z-index: 1; 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); }
  /* A session that does not line up with the period boundaries always shows its exact time, at every density. */
  .m-cell[data-offgrid] .m-time { display: block; }

  /* ---- "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).
     It lives inside .matrix-canvas, so it scrolls with the grid; it is placed and sized by ui/live-status.js. */
  .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; } }

  /* "Back to now": shown (by ui/matrix-scroll.js) only while today's column is off screen. */
  .m-now-btn { position: absolute; right: 10px; bottom: 10px; z-index: 5; display: inline-flex; align-items: center; gap: 5px; min-height: 34px; padding: 0 12px; border: 1px solid var(--line-border); border-radius: var(--radius-pill); background: var(--paper-bg); color: var(--blue-text); font-size: var(--font-size-caption); font-weight: 650; box-shadow: 0 2px 10px color-mix(in srgb, var(--c-000000-bg) 18%, transparent); cursor: pointer; }
  .m-now-btn:hover { border-color: var(--blue-border); }
  .m-now-btn[hidden] { display: none; }
  .m-now-btn::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: var(--live-border); }

  /* 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: var(--font-size-label); 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. */
  @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: var(--font-size-caption); 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: var(--font-size-label); font-weight: 600; }
    .legend-btn::before { content: ''; width: 10px; height: 10px; border-radius: 3px; background: var(--cell-live-bg); }
    .matrix { --m-row-h: 40px; }
  }
  @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 { --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: var(--font-size-caption); }
    .m-free .m-free-s { display: none; }
    .m-free .m-free-l { display: inline; }
    .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 { --m-room-w: 104px; --m-row-h: 54px; --m-head-h: 46px; }
    .m-cell { padding: 4px 7px; }
    .m-cell.is-live { padding-right: 18px; }
    .m-name { font-size: var(--font-size-label); -webkit-line-clamp: 2; }
    .m-meta { font-size: var(--font-size-caption); }
    .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: var(--font-size-caption); padding: 8px 2px 6px; }
    .matrix-table thead th small { font-size: 0.6875rem; }
    .m-room-btn { font-size: var(--font-size-body-sm); }
  }
  @container matrix (min-width: 1100px) {
    .matrix { --m-row-h: 58px; }
    .m-name { font-size: var(--font-size-body-sm); }
    .m-meta { font-size: var(--font-size-label); }
    .m-time { font-size: var(--font-size-caption); }
  }

  /* Short landscape: the date and the tools 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: height is the scarce axis, so a day row is one line (name · teacher). */
  @media (max-height: 480px) and (orientation: landscape) {
    .matrix { --m-row-h: 36px; }
    .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[data-offgrid] .m-time { display: block; } /* …except when the period boundaries would lie about the time */
    .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: var(--font-size-caption); }
    .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: center; font-size: var(--font-size-label); }
    .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-row-h: 56px; }
  .matrix-room-week tbody th.m-slotrow { display: table-cell; padding: 2px 2px 2px 6px; text-align: left; }
  .m-slotrow .m-slot-num { display: block; font-size: var(--font-size-caption); }
  .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: var(--font-size-label); 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: the week grid trades font size for an extra line. */
  @container matrix (max-width: 359px) {
    .matrix-table.matrix-room-week { --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-row-h: 66px; }
    .matrix-table.matrix-heat { --m-row-h: 46px; }
    .matrix-room-week .m-name { -webkit-line-clamp: 2; }
    .w-count { font-size: var(--font-size-body-sm); }
    .w-s { height: 6px; }
  }
  @container matrix (min-width: 768px) {
    .matrix-table.matrix-room-week { --m-row-h: 84px; }
    .matrix-table.matrix-heat { --m-row-h: 52px; }
    .matrix-room-week .m-slotrow .m-slot-num { font-size: var(--font-size-body-sm); }
    .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; }
  }

  /* The inline legend becomes a footer strip on wide screens. */
  @media (min-width: 901px) and (min-height: 481px) { .m-legend { border-top: 1px solid var(--line-border); } }

}
