:root {
  --bg: #f6f6f8;
  --surface: #ffffff;
  --text: #1b1b1f;
  --sub: #5b5b66;
  --line: #d0d0d8;
  --head: #eceef3;
  --accent: #1d4ed8;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --conflict-bg: #f6d5d2;
  --conflict-line: #b3261e;
  --focus: #1d4ed8;
  --c0: #c0392b; --c0t: #fff;
  --c1: #b45309; --c1t: #fff;
  --c2: #4d7c0f; --c2t: #fff;
  --c3: #0f766e; --c3t: #fff;
  --c4: #1d4ed8; --c4t: #fff;
  --c5: #6d28d9; --c5t: #fff;
  --c6: #be185d; --c6t: #fff;
  --c7: #475569; --c7t: #fff;
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131316;
    --surface: #1d1d22;
    --text: #f2f2f5;
    --sub: #a6a6b2;
    --line: #3a3a44;
    --head: #26262c;
    --accent: #8ab4ff;
    --accent-text: #10151f;
    --danger: #f2b8b5;
    --conflict-bg: #4a2422;
    --conflict-line: #f2b8b5;
    --focus: #8ab4ff;
    --c0: #f28b82; --c0t: #1a1a1a;
    --c1: #f5b26b; --c1t: #1a1a1a;
    --c2: #a3d66b; --c2t: #1a1a1a;
    --c3: #5ec8be; --c3t: #1a1a1a;
    --c4: #8ab4ff; --c4t: #1a1a1a;
    --c5: #c4a7f5; --c5t: #1a1a1a;
    --c6: #f49ac1; --c6t: #1a1a1a;
    --c7: #b4bfce; --c7t: #1a1a1a;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 8px; }
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.top { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.tablebar, .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
main { padding: 12px 16px 24px; max-width: 1400px; margin: 0 auto; }
.toolbar { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.label { color: var(--sub); font-size: 13px; }
.toolbar label { display: inline-flex; align-items: center; gap: 6px; color: var(--sub); font-size: 13px; }

button, select, input, textarea { font: inherit; color: var(--text); }
button, select, input[type="text"], input[type="number"], input[type="time"], input[type="date"], textarea {
  background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; min-height: 36px;
}
textarea { width: 100%; min-height: 72px; resize: vertical; }
button { cursor: pointer; }
button:hover { background: var(--head); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: not-allowed; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.banner { background: var(--head); border-bottom: 1px solid var(--line); padding: 10px 16px; }
.banner p { margin: 0 0 8px; }
.banner[hidden] { display: none; }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.side { display: flex; flex-direction: column; gap: 16px; }
.side > section { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 12px; }
.gridwrap { min-width: 0; }
.print-title { display: none; }
.hint { color: var(--sub); font-size: 13px; margin: 8px 0 0; }

/* 課表格 */
.grid { display: grid; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; position: relative; }
.grid > * { min-width: 0; }
.gh { background: var(--head); font-weight: 600; text-align: center; padding: 8px 2px; border-bottom: 1px solid var(--line); border-left: 1px solid var(--line); font-size: 14px; }
.gh.corner { border-left: 0; }
.gp { padding: 4px 4px; text-align: center; border-top: 1px solid var(--line); display: flex; flex-direction: column; justify-content: center; line-height: 1.25; }
.gp b { font-size: 15px; }
.gp small { color: var(--sub); font-size: 10.5px; }
.empty { border: 0; border-top: 1px solid var(--line); border-left: 1px solid var(--line); border-radius: 0; background: transparent; min-height: 48px; padding: 0; color: transparent; }
.empty:hover, .empty:focus-visible { background: var(--head); color: var(--sub); }
.run { margin: 2px; border-radius: 6px; padding: 4px 5px; text-align: left; overflow: hidden; border: 0; min-height: 0; font-size: 13px; line-height: 1.3; overflow-wrap: anywhere; display: block; }
.run:hover { filter: brightness(1.08); background: inherit; }
.run .n { font-weight: 700; display: block; }
.run .s { font-size: 11.5px; display: block; opacity: .95; }
.run.conflict {
  background: repeating-linear-gradient(135deg, var(--conflict-bg) 0 8px, transparent 8px 16px), var(--conflict-bg);
  outline: 2px solid var(--conflict-line); outline-offset: -2px; color: var(--text);
}
.run.conflict .tag { display: block; font-weight: 700; color: var(--conflict-line); }
.run[data-color="0"] { background: var(--c0); color: var(--c0t); }
.run[data-color="1"] { background: var(--c1); color: var(--c1t); }
.run[data-color="2"] { background: var(--c2); color: var(--c2t); }
.run[data-color="3"] { background: var(--c3); color: var(--c3t); }
.run[data-color="4"] { background: var(--c4); color: var(--c4t); }
.run[data-color="5"] { background: var(--c5); color: var(--c5t); }
.run[data-color="6"] { background: var(--c6); color: var(--c6t); }
.run[data-color="7"] { background: var(--c7); color: var(--c7t); }
.run.conflict[data-color] { background: repeating-linear-gradient(135deg, var(--conflict-bg) 0 8px, transparent 8px 16px), var(--conflict-bg); color: var(--text); }
.grid.compact .run { font-size: 11px; padding: 3px; }
.grid.compact .run .s { font-size: 10px; }
.grid.compact .gh { font-size: 12.5px; }

/* 側欄 */
table.stat { border-collapse: collapse; width: 100%; font-size: 14px; }
table.stat th, table.stat td { text-align: left; padding: 3px 0; font-weight: 400; }
table.stat td { text-align: right; font-variant-numeric: tabular-nums; }
table.stat th { color: var(--sub); }
.stat-h { margin: 10px 0 2px; font-size: 13px; color: var(--sub); }
.none { color: var(--sub); margin: 0; font-size: 14px; }
.conflict-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.conflict-list li { border-left: 4px solid var(--conflict-line); padding: 2px 0 2px 8px; font-size: 14px; }
.courselist { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.courselist button, .courselist .item { display: grid; grid-template-columns: 12px 1fr; gap: 8px; width: 100%; text-align: left; padding: 6px 8px; align-items: start; }
.courselist .item { border: 1px solid var(--line); border-radius: 6px; }
.dot { width: 12px; height: 12px; border-radius: 3px; margin-top: 5px; }
.dot[data-color="0"] { background: var(--c0); } .dot[data-color="1"] { background: var(--c1); }
.dot[data-color="2"] { background: var(--c2); } .dot[data-color="3"] { background: var(--c3); }
.dot[data-color="4"] { background: var(--c4); } .dot[data-color="5"] { background: var(--c5); }
.dot[data-color="6"] { background: var(--c6); } .dot[data-color="7"] { background: var(--c7); }
.courselist small { display: block; color: var(--sub); font-size: 12.5px; }
.foot { padding: 12px 16px 24px; color: var(--sub); font-size: 13px; }

/* 對話框 */
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px); }
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog form, dialog .dbody { padding: 16px; display: grid; gap: 12px; }
dialog h2 { font-size: 17px; margin: 0; }
.field { display: grid; gap: 4px; font-size: 13px; color: var(--sub); }
.field input, .field select { color: var(--text); font-size: 15px; width: 100%; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.actions .left { margin-right: auto; }
.err { color: var(--danger); margin: 0; font-size: 14px; min-height: 1.4em; }
fieldset { border: 1px solid var(--line); border-radius: 8px; margin: 0; padding: 8px 10px 10px; }
legend { font-size: 13px; color: var(--sub); padding: 0 4px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { position: relative; }
.swatch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.swatch span { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 8px; font-weight: 700; border: 3px solid transparent; }
.swatch input:checked + span { border-color: var(--text); }
.swatch input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.swatch span[data-color="0"] { background: var(--c0); color: var(--c0t); } .swatch span[data-color="1"] { background: var(--c1); color: var(--c1t); }
.swatch span[data-color="2"] { background: var(--c2); color: var(--c2t); } .swatch span[data-color="3"] { background: var(--c3); color: var(--c3t); }
.swatch span[data-color="4"] { background: var(--c4); color: var(--c4t); } .swatch span[data-color="5"] { background: var(--c5); color: var(--c5t); }
.swatch span[data-color="6"] { background: var(--c6); color: var(--c6t); } .swatch span[data-color="7"] { background: var(--c7); color: var(--c7t); }
.slot { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: grid; gap: 8px; }
.slot .head { display: flex; gap: 8px; align-items: center; }
.slot .head select { width: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.chip span { display: block; min-width: 40px; text-align: center; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; font-size: 14px; }
.chip input:checked + span { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.chip input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.ptable { width: 100%; border-collapse: collapse; }
.ptable th { font-size: 12.5px; color: var(--sub); text-align: left; font-weight: 400; padding: 2px 4px; }
.ptable td { padding: 3px 4px; }
.ptable input { width: 100%; min-width: 0; }
.scroll { overflow: auto; max-height: 50vh; }
.preview-img { max-width: 100%; height: auto; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.urlbox { width: 100%; }

.toast { position: fixed; left: 16px; right: 16px; bottom: 16px; max-width: 420px; background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10; }
.toast.show { opacity: 1; }

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 520px) {
  main { padding: 10px 12px 20px; }
  .top { padding: 10px 12px; }
  .two { grid-template-columns: 1fr; }
  .gp b { font-size: 13px; }
  .gp small { display: none; }
  .gh { font-size: 12.5px; }
  .tablebar select { flex: 1 1 100%; }
}
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }

/* 列印:A4 橫向一頁,只印課表 */
@page { size: A4 landscape; margin: 10mm; }
@media print {
  :root { --bg: #fff; --surface: #fff; --text: #000; --sub: #333; --line: #888; --head: #e8e8e8;
    --c0: #c0392b; --c0t: #fff; --c1: #b45309; --c1t: #fff; --c2: #4d7c0f; --c2t: #fff; --c3: #0f766e; --c3t: #fff;
    --c4: #1d4ed8; --c4t: #fff; --c5: #6d28d9; --c5t: #fff; --c6: #be185d; --c6t: #fff; --c7: #475569; --c7t: #fff;
    --conflict-bg: #f6d5d2; --conflict-line: #b3261e; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { font-size: 11pt; }
  .top, .banner, .toolbar, .side, .foot, .hint, .toast, dialog { display: none !important; }
  main { padding: 0; max-width: none; }
  .layout { display: block; }
  .print-title { display: block; font-size: 16pt; margin: 0 0 4mm; }
  .grid { border-radius: 0; height: 165mm; grid-template-rows: auto repeat(var(--rows), minmax(0, 1fr)) !important; }
  .gp { padding: 1px 2px; }
  .run { margin: 1px; padding: 1px 3px; }
  .empty { min-height: 0; }
  .grid.compact .run, .run { font-size: 9pt; }
  .run .s { font-size: 8pt; }
  .gp small { display: block; font-size: 7.5pt; }
}
[hidden] { display: none !important; }
