:root {
  color-scheme: light;
  --page: #f9f9f9; --surface: #ffffff; --ink: #191919; --ink2: #666666; --grid: #e7e7e7; --axis: #c2c2c2;
  --work: #f47230; --shunt: #257de0; --scd: #00b894; --other: #9a9a9a;
  --work-tint: #fbd9c6; --shunt-tint: #d8e9fd; --ok: #006c10; --critical: #b52a2b; --warn-bg: #fff3d6;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0b0b0b; --surface: #191919; --ink: #efefef; --ink2: #a6a6a6; --grid: #303030; --axis: #474747;
    --work: #e76624; --shunt: #338ced; --scd: #00a888; --other: #7d7d7d;
    --work-tint: #5c2f18; --shunt-tint: #16385c; --ok: #53c556; --critical: #f08585; --warn-bg: #3a2e10;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0 auto; max-width: 1500px; padding: 16px; background: var(--page); color: var(--ink); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", sans-serif; }
h1 { font-size: 20px; margin: 0; } h2 { font-size: 17px; margin: 0; } h3 { font-size: 15px; margin: 0 0 8px; }
header { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; margin-bottom: 12px; }
#source { margin: 0; color: var(--ink2); font-size: 13px; flex: 1 1 200px; }
.actions { display: flex; gap: 8px; }
button, .button, select, input[type=number] { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--axis); border-radius: 6px; padding: 6px 12px; min-height: 38px; }
button, .button { cursor: pointer; }
.button.primary { background: #1b64be; border-color: #1b64be; color: #fff; }
.button input { position: absolute; opacity: 0; width: 1px; height: 1px; }
:focus-visible, .button:focus-within { outline: 2px solid var(--shunt); outline-offset: 2px; }
#message { margin: 0 0 12px; padding: 8px 12px; border-radius: 6px; background: var(--warn-bg); }
#empty { border: 2px dashed var(--axis); border-radius: 10px; padding: 48px 24px; text-align: center; background: var(--surface); }
body.drag #empty, body.drag main { outline: 3px solid var(--shunt); outline-offset: 4px; }
#empty p { color: var(--ink2); margin: 8px auto 0; max-width: 560px; }

#filters { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: end; margin-bottom: 14px; }
#filters label { display: grid; gap: 2px; font-size: 13px; color: var(--ink2); }
#filters input[type=number] { width: 112px; }
#filters .check { display: flex; align-items: center; gap: 6px; min-height: 38px; color: var(--ink); }
nav { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.tabs { display: flex; margin-right: 8px; }
.tabs button { border-radius: 0; margin-left: -1px; min-width: 48px; }
.tabs button:first-child { border-radius: 6px 0 0 6px; } .tabs button:last-child { border-radius: 0 6px 6px 0; }
.tabs [aria-pressed=true] { background: var(--ink); color: var(--surface); }

.legend { display: flex; flex-wrap: wrap; gap: 4px 18px; list-style: none; padding: 0; margin: 0 0 12px; font-size: 13px; color: var(--ink2); }
.legend li { display: flex; align-items: center; gap: 6px; }
.k { width: 20px; height: 10px; border-radius: 2px; }
.k.work { background: var(--work); } .k.scd { background: var(--scd); } .k.other { background: var(--other); }
.k.shunt { height: 0; border-top: 3px solid var(--shunt); border-radius: 0; } .k.transit { height: 0; border-top: 2px dotted var(--work); border-radius: 0; }

.panel { background: var(--surface); border: 1px solid var(--grid); border-radius: 10px; padding: 14px; margin-bottom: 14px; }
.panel > p { margin: 0 0 8px; color: var(--ink2); font-size: 13px; }
.scroll { overflow-x: auto; }
.gaps { display: flex; flex-wrap: wrap; gap: 8px; }
.gap { text-align: left; line-height: 1.35; }
.gap, .cell { display: flex; flex-direction: column; justify-content: flex-start; }
.gap small { color: var(--ink2); }
.gap > span { display: flex; gap: 6px; align-items: center; }
.gap b { font-weight: 600; }
.tag { display: inline-block; font-size: 12px; line-height: 18px; padding: 0 6px; border-radius: 4px; border: 1px solid var(--axis); white-space: nowrap; }
.tag.free { color: var(--ok); border-color: currentColor; font-weight: 600; }
.tag.pass { background: var(--shunt-tint); border-color: transparent; }
.tag.busy { background: var(--work-tint); border-color: transparent; }
.tag.none { color: var(--ink2); border-style: dashed; }
.tag.scd { border-color: var(--scd); box-shadow: inset 4px 0 var(--scd); padding-left: 9px; }
.tag.alert { color: var(--critical); border-color: currentColor; }

.matrix { border-collapse: separate; border-spacing: 2px; font-size: 12px; }
.matrix th { font-weight: 400; color: var(--ink2); padding: 0 4px; }
.matrix thead th { min-width: 26px; text-align: center; padding: 0; line-height: 1.2; }
.matrix thead th.weekend { color: var(--ink); font-weight: 700; }
.matrix tbody th { position: sticky; left: 0; z-index: 1; background: var(--surface); text-align: left; white-space: nowrap; }
.matrix tbody th button { min-height: 0; padding: 0 4px; border: 0; background: none; font-size: 12px; }
.matrix tbody th.station button { font-weight: 600; }
.matrix tr.on th button { text-decoration: underline; text-decoration-thickness: 2px; }
.matrix td { padding: 0; }
.matrix td button { display: block; width: 26px; min-height: 22px; height: 22px; padding: 0; border-radius: 3px; font-size: 11px; line-height: 1; border: 1px solid var(--grid); background: var(--surface); }
.matrix td .busy { background: var(--work-tint); border-color: transparent; }
.matrix td .pass { background: var(--shunt-tint); border-color: transparent; }
.matrix td .has-scd { box-shadow: inset 0 0 0 2px var(--scd); font-weight: 700; }
.matrix td .none { border-style: dashed; }
.matrix td.count { text-align: right; padding-left: 6px; font-variant-numeric: tabular-nums; }

.calendar { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.calendar > span { text-align: center; color: var(--ink2); font-size: 13px; }
.cell { width: 100%; text-align: left; padding: 6px 8px; min-height: 104px; vertical-align: top; }
.cell.none { opacity: .55; }
.cell .top { display: flex; flex-wrap: wrap; gap: 2px 6px; align-items: center; margin-bottom: 4px; }
.cell .num { font-weight: 600; }
.cell small { display: block; font-size: 12px; color: var(--ink2); overflow-wrap: anywhere; }

/* 路線圖：里程換算成百分比定位 */
.plot { position: relative; }
.lane { position: relative; height: 14px; }
.lane + .lane { margin-top: 2px; }
.mini .lane { height: 7px; }
.bar { position: absolute; top: 1px; bottom: 1px; min-width: 4px; border-radius: 2px; }
.bar.work { background: var(--work); } .bar.scd { background: var(--scd); } .bar.other { background: var(--other); }
.seg { position: absolute; top: 50%; border-top: 2px dotted var(--work); transform: translateY(-50%); }
.seg.shunt { border-top: 3px solid var(--shunt); }
.head { position: absolute; top: 50%; width: 0; height: 0; border: 6px solid transparent; transform: translate(-100%, -50%); border-left: 9px solid var(--c); border-right: 0; }
.head.left { transform: translate(0, -50%); border-left: 0; border-right: 9px solid var(--c); }
.mini .head { border-width: 4px 0 4px 6px; } .mini .head.left { border-width: 4px 6px 4px 0; }
.dot { position: absolute; top: 50%; width: 8px; height: 8px; border-radius: 50%; background: var(--c); transform: translate(-50%, -50%); box-shadow: 0 0 0 2px var(--surface); }
.mini .dot { width: 5px; height: 5px; box-shadow: none; }
.c-shunt { --c: var(--shunt); } .c-work { --c: var(--work); }
.gridlines { position: absolute; inset: 0; pointer-events: none; }
.gridlines i { position: absolute; top: 0; bottom: 0; background: var(--grid); min-width: 1px; opacity: .6; }
.axis { position: relative; height: 34px; font-size: 11px; color: var(--ink2); border-bottom: 1px solid var(--axis); }
.axis span { position: absolute; bottom: 2px; transform: translateX(-50%); text-align: center; line-height: 1.2; white-space: nowrap; }

.wide { min-width: 760px; }
.week-row { display: grid; grid-template-columns: 148px 1fr; gap: 12px; border-top: 1px solid var(--grid); padding: 8px 0; }
.week-row > button { align-self: start; text-align: left; border: 0; background: none; padding: 0 4px; line-height: 1.6; }
.week-row .tag { margin-right: 4px; }
.lane-label { position: relative; font-size: 12px; line-height: 16px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.order { border-top: 1px solid var(--grid); }
.order summary { cursor: pointer; padding: 6px 0 8px; list-style: none; position: relative; }
.order summary::-webkit-details-marker { display: none; }
.order summary:hover { background: var(--page); }
.order .line { display: flex; flex-wrap: wrap; gap: 0 10px; font-size: 13px; position: relative; }
.order .line b { font-weight: 600; } .order .line span:not(.tag) { color: var(--ink2); }
.order dl { display: grid; grid-template-columns: max-content 1fr; gap: 2px 14px; margin: 0 0 8px; font-size: 13px; }
.order dt { color: var(--ink2); } .order dd { margin: 0; overflow-wrap: anywhere; }
.order pre { white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.5 inherit; font-family: inherit; background: var(--page); padding: 8px; border-radius: 6px; margin: 0 0 10px; }
.group { margin-top: 14px; }
.alerts { margin: 0; padding-left: 20px; font-size: 14px; }
.stats { display: flex; flex-wrap: wrap; gap: 4px 22px; margin: 0 0 4px; }
.stats b { color: var(--ink); font-size: 22px; font-weight: 600; margin-right: 4px; }

table.list { border-collapse: collapse; width: 100%; min-width: 720px; font-size: 13px; }
.list th { text-align: left; color: var(--ink2); font-weight: 400; } .list th, .list td { padding: 6px 10px 6px 0; border-top: 1px solid var(--grid); vertical-align: top; }
.list button { min-height: 0; padding: 2px 8px; white-space: nowrap; }
.rules { color: var(--ink2); font-size: 14px; margin: 8px 0 32px; }

@media (max-width: 720px) {
  body { padding: 10px; }
  .calendar { gap: 3px; }
  .cell { min-height: 72px; padding: 4px; }
  .cell small { display: none; }
  .cell .tag { white-space: normal; font-size: 10px; padding: 0 3px; line-height: 15px; } .cell .tag.scd { padding-left: 6px; }
  .week-row { grid-template-columns: 1fr; gap: 4px; }
  .axis span.far { display: none; }
}
