/* 設計代幣：顏色、字級、圓角、控制項高度都從這裡取，不要在各處另寫色值
   字級 4 級（12／13／14／16）、圓角 3 級（4／6／8）、控制項高度 32px（窄畫面與觸控 40px） */
:root {
  --bg: #f3f5f8; --panel: #fff; --line: #d9dee5; --line-strong: #9aa7b8; --text: #1b2330; --muted: #5f6b7c;
  --head: #14233a; --head-text: #c5d0e0; --th: #eef1f5;
  --accent: #0b5cad; --accent-soft: #e6f0fb; --accent-ink: #0b4a8a; --sel: #5bb0ff;
  --ok: #12803a; --bad: #c22525; --wait: #9a5b00; --wait-soft: #fff8e6; --wait-line: #f0d9a0;
  --probe-red: #d92626; --probe-black: #111;
  --fs-xs: 12px; --fs-s: 13px; --fs: 14px; --fs-l: 16px;
  --r-s: 4px; --r: 6px; --r-l: 8px;
  --ctl: 32px;
}
@media (max-width: 767px), (pointer: coarse) { :root { --ctl: 40px; } kbd { display: none; } }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { font: var(--fs)/1.5 "Segoe UI", "Microsoft JhengHei", "Noto Sans TC", sans-serif; color: var(--text); background: var(--bg);
  display: flex; flex-direction: column; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
header { display: flex; align-items: center; gap: 24px; padding: 0 16px; background: var(--head); color: #fff; flex-wrap: wrap; }
h1 { font-size: var(--fs-l); margin: 8px 0; }
nav { display: flex; gap: 2px; }
nav button { background: none; border: 0; border-radius: 0; color: var(--head-text); padding: 12px 16px; min-height: 0; border-bottom: 3px solid transparent; }
nav button:hover { color: #fff; }
nav button[aria-selected="true"] { color: #fff; border-bottom-color: var(--sel); }
nav button:focus-visible { outline-color: #fff; outline-offset: -4px; }
main { flex: 1; min-height: 0; }
.tab { display: none; height: 100%; }
.tab.active.split { display: flex; }
.tab.active.page { display: block; padding: 16px 24px; overflow: auto; max-width: 1100px; }
.side { width: 270px; flex: none; background: var(--panel); border-right: 1px solid var(--line); padding: 12px; overflow: auto; }
.side.wide { width: 330px; }
.work { flex: 1; min-width: 0; padding: 12px 16px; overflow: auto; }
h2 { font-size: var(--fs-l); margin: 0 0 8px; }
h3 { font-size: var(--fs-s); margin: 16px 0 4px; color: var(--muted); }

/* 控制項：同一個高度 */
button, select, textarea, input[type=number], input[type=search], input[type=text], .filebtn {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); }
button, .filebtn { min-height: var(--ctl); padding: 0 12px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
select, input[type=number], input[type=search], input[type=text] { min-height: var(--ctl); padding: 0 8px; }
textarea { width: 100%; resize: vertical; padding: 6px 8px; }
input[type=number] { width: 84px; }
button:hover, .filebtn:hover { border-color: var(--line-strong); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
/* 破壞性動作：紅字紅框，放在工具列最右邊（.tgroup.end），按下都會先確認 */
button.danger { color: var(--bad); border-color: #e7b3b3; }
button.danger:hover { border-color: var(--bad); }
kbd { font: var(--fs-xs) Consolas, monospace; color: var(--muted); }
label { cursor: pointer; }
input[type=checkbox], input[type=radio] { width: 16px; height: 16px; margin: 0 2px 0 0; accent-color: var(--accent); vertical-align: -3px; }
.side label { display: block; margin: 8px 0; }
#gen-run { width: 100%; margin-top: 8px; justify-content: center; }

/* 工具列：控制項分組，組與組之間一條細線；.end 推到最右邊 */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 8px; }
.tgroup { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.tgroup + .tgroup { padding-left: 12px; border-left: 1px solid var(--line); }
.tgroup.end { margin-left: auto; padding-left: 0; border-left: 0; }
.tgroup:not(:has(> :not([hidden]))) { display: none; }
.toolbar label { display: inline-flex; align-items: center; gap: 4px; min-height: var(--ctl); }
.sep { width: 1px; height: 22px; background: var(--line); }
.hint, .muted { color: var(--muted); font-size: var(--fs-s); }
.hint { margin: 0 0 8px; }
.counts { margin-top: 4px; font-variant-numeric: tabular-nums; }

/* 常駐提示列：圖面正上方，寫「現在該做什麼」 */
.statusline { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; padding: 6px 12px; font-size: var(--fs-s);
  background: var(--accent-soft); color: var(--accent-ink); border: 1px solid var(--line); border-bottom: 0; border-radius: var(--r) var(--r) 0 0; }
.statusline b { font-weight: 600; }
.statusline:not([hidden]) + .canvas-wrap { border-top-left-radius: 0; border-top-right-radius: 0; }
.empty { color: var(--muted); text-align: center; padding: 48px 16px; margin: 0; }

.canvas-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); overflow: auto; max-height: 72vh; }
.canvas-wrap svg { display: block; font-family: "Segoe UI", "Microsoft JhengHei", "Noto Sans TC", sans-serif; user-select: none; touch-action: none; }
.part { cursor: pointer; }
.part.sel .hit { stroke: var(--sel); stroke-width: 1.5; stroke-dasharray: 4 3; fill: rgba(91,176,255,.08); }
.term { cursor: crosshair; }
.term:hover { stroke: var(--accent); stroke-width: 3; }
.term.pending { fill: #ffcf33; stroke: #b07a00; stroke-width: 3; }
.wire { cursor: pointer; }
.wire .halo { stroke: rgba(91,176,255,.45); }
.wire.mark-bad .halo { stroke: rgba(194,37,37,.45); }
.wire.mark-ok .halo { stroke: rgba(18,128,58,.45); }
.wire.pre path { stroke-dasharray: 7 4; }

/* 元件面板 */
.pal { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; margin-bottom: 4px; padding: 4px 8px; }
.pal svg { flex: none; }
.pal span { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.pal b { font-weight: 600; font-size: var(--fs-s); }
.pal i, .pal em { font-style: normal; font-size: var(--fs-xs); color: var(--muted); }
.pal em { color: var(--accent); }
#pal-search { width: 100%; }

/* 表格與標籤 */
table { border-collapse: collapse; width: 100%; background: var(--panel); font-size: var(--fs-s); }
th, td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
th { background: var(--th); font-weight: 600; }
td { font-variant-numeric: tabular-nums; }
tr.add td { background: var(--wait-soft); }
table.narrow { width: auto; min-width: 320px; }
.cols { display: grid; grid-template-columns: minmax(280px, 1fr) minmax(320px, 1.3fr); gap: 16px; }
.tag { display: inline-block; font-size: var(--fs-xs); padding: 0 6px; border-radius: var(--r-s); border: 1px solid; white-space: nowrap; }
.tag.ok { color: var(--ok); }
.tag.wait { color: var(--wait); }
.chip { display: inline-block; background: var(--accent-soft); color: var(--accent-ink); border-radius: var(--r-s); padding: 0 8px; margin: 0 4px 4px 0; font-size: var(--fs-s); }
/* 真的需要注意的事才用 .warn（官方沒寫、還沒做、要抽查）；空狀態用 .empty */
.warn { color: var(--wait); background: var(--wait-soft); border: 1px solid var(--wait-line); border-radius: var(--r); padding: 6px 10px; margin: 8px 0; font-size: var(--fs-s); }
.calc { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 12px; margin: 8px 0 0; }
.calc ol { margin: 0; padding-left: 20px; }
.calc li { margin-bottom: 4px; }
.calc p { margin: 8px 0 0; }
.checks { margin: 0; padding-left: 0; list-style: none; }
.checks li { margin-bottom: 8px; }
.sw { display: inline-block; width: 12px; height: 12px; border: 1px solid #555; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.row { display: flex !important; align-items: center; gap: 8px; }

/* 術科 */
#ex-list button { display: block; width: 100%; text-align: left; margin-bottom: 8px; padding: 6px 12px; }
#ex-list button.on { border-color: var(--accent); background: var(--accent-soft); }
#ex-list small { display: block; color: var(--muted); font-size: var(--fs-xs); }
#ex-list button.offbtn small { color: var(--wait); }
#ex-list button.offbtn.ready small { color: var(--ok); }
#ex-list h3 { margin-top: 16px; }
.result { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 12px; margin-bottom: 8px; min-height: 40px; }
.result ul { margin: 4px 0 0; padding-left: 20px; }
.good { color: var(--ok); } .bad { color: var(--bad); }
.runbox { border-color: var(--accent); display: flex; gap: 8px 12px; align-items: center; flex-wrap: wrap; }
.runbox[hidden] { display: none; }
.clock { font-variant-numeric: tabular-nums; }
.official { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 6px 12px; margin-bottom: 8px; font-size: var(--fs-s); }
.official summary { cursor: pointer; font-weight: 600; }
.official > div { max-height: 30vh; overflow: auto; margin-top: 6px; }
.official p { margin: 2px 0; }
.ex-notes { margin-top: 6px; }
.ex-notes summary { cursor: pointer; color: var(--accent); }
.ex-notes ol { margin: 6px 0 0; padding-left: 1.4em; }
.ex-notes li { margin-bottom: 4px; }
.srcbox { font-size: var(--fs-s); background: var(--bg); border-radius: var(--r); padding: 8px; margin: 8px 0; }
/* 放在側欄最下面，當作頁尾聲明 */
.srcbox.foot { font-size: var(--fs-xs); color: var(--muted); background: none; border-top: 1px solid var(--line); border-radius: 0; padding: 12px 2px 0; margin-top: 16px; }
.offq { border: 1px dashed var(--line); border-radius: var(--r); padding: 4px 8px; margin-bottom: 4px; font-size: var(--fs-s); background: #fafbfc; }
.offq small { display: block; color: var(--wait); font-size: var(--fs-xs); }

/* 檢視切換、屬性列 */
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: var(--r) 0 0 var(--r); margin-left: 0; }
.seg button:last-child { border-radius: 0 var(--r) var(--r) 0; }
.seg button.on { background: var(--accent); border-color: var(--accent); color: #fff; position: relative; }
.viewctl { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.props { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 4px 12px; margin-bottom: 8px; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; min-height: 40px; }
.props input[type=number] { width: 70px; }

/* 顯示出處的圖例 */
.src-legend { font-size: var(--fs-xs); color: var(--muted); display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.src-legend i { display: inline-block; width: 14px; height: 6px; border-radius: 3px; margin-left: 4px; }
.src-legend .s-pan { background: rgba(255,140,0,.75); }
.src-legend .s-jia { background: rgba(220,40,40,.7); }
.src-legend .s-q { background: repeating-linear-gradient(90deg, rgba(110,110,110,.7) 0 3px, transparent 3px 6px); }

/* 送電模擬 */
.simhost { margin-bottom: 8px; }
.simbar { display: flex; align-items: center; gap: 8px 12px; flex-wrap: wrap; }
/* 送電開關是狀態切換，不是頁面主要按鈕：藍框；送電中變紅底（帶電） */
.sim-toggle { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.sim-toggle.live { background: var(--bad); border-color: var(--bad); color: #fff; }
.sim-body { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-start; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 12px; margin-top: 6px; }
.sim-log { margin: 0; padding-left: 18px; flex: 1 1 240px; font-size: var(--fs-s); max-height: 96px; overflow: auto; }
.sim-log li.bad { color: var(--bad); font-weight: 600; }
.sim-log li:not(:first-child) { opacity: .65; }
.tank { display: flex; align-items: center; gap: 8px; font-size: var(--fs-s); max-width: 230px; }
.tank-box { width: 34px; height: 70px; border: 2px solid #445; border-top: 0; border-radius: 0 0 var(--r-s) var(--r-s); position: relative; flex: none; background: #f4f7fa; }
.tank-water { position: absolute; left: 0; right: 0; bottom: 0; background: #4aa3ee; transition: height .25s linear; }
svg.simming .term { pointer-events: none; opacity: .5; }
svg.simming .wire { cursor: default; }
svg.simming.probing .term { pointer-events: auto; opacity: 1; }
.smoke .sm { opacity: 0; animation: smoke 1.6s ease-out infinite; }
.smoke .s2 { animation-delay: .5s; }
.smoke .s3 { animation-delay: 1s; }
.smoke .fl { animation: flame .25s steps(2) infinite; }
@keyframes smoke { 0% { transform: translate(0, 0) scale(.6); opacity: .75; } 100% { transform: translate(8px, -46px) scale(2.4); opacity: 0; } }
@keyframes flame { 0% { fill: #ff7a1a; r: 6px; } 100% { fill: #ffd21a; r: 4px; } }
.spark { animation: spark .18s steps(2) infinite; }
@keyframes spark { 0% { opacity: 1; } 100% { opacity: .15; } }
.part.shake { animation: shake .09s linear infinite; }
@keyframes shake { 0% { translate: -1px 0; } 50% { translate: 1px 0; } 100% { translate: -1px 0; } }

/* Toast：只給「已完成、已儲存、做不到的原因」這類一次性回饋；停留時間依字數（symbols.js） */
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); max-width: calc(100vw - 32px); background: var(--head); color: #fff; padding: 8px 16px;
  border-radius: var(--r-l); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 80; }
#toast.show { opacity: 1; }

/* 三用電表：浮在右下角，量測時不用離開線路圖 */
.meter { position: fixed; right: 16px; bottom: 16px; z-index: 30; display: flex; flex-direction: column; gap: 6px; align-items: flex-start;
  width: calc(262px * var(--mz, 1)); max-height: calc(100vh - 80px); overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-l);
  padding: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.28); touch-action: pan-x pan-y; /* 雙指捏合交給電表自己縮放 */ }
.meter[hidden] { display: none; }
.meter-svg { flex: 0 0 auto; width: calc(244px * var(--mz, 1)); height: auto; user-select: none; }
.meter-svg .m-range { cursor: pointer; }
.meter-svg .lcd { letter-spacing: 1px; }
.meter-svg .needle { transition: all .35s ease-out; }
.meter-side { flex: none; min-width: 0; font-size: var(--fs-s); }
.meter-probes { line-height: 1.7; margin-bottom: 6px; }
.meter-note { min-height: 34px; margin-bottom: 6px; }
.meter-btns { display: flex; gap: 6px; flex-wrap: wrap; }
.meter-help { display: none; }
.m-red { color: var(--probe-red); font-weight: 700; }
.m-blk { color: var(--probe-black); font-weight: 700; }
.meter-model { display: flex; margin-left: 24px; }
.meter-model button { flex: 1; padding: 0 8px; font-size: var(--fs-s); justify-content: center; }
.meter-model button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.m-resize { position: absolute; left: 4px; top: 4px; width: 22px; height: 22px; display: flex; align-items: center; justify-content: center; cursor: nwse-resize; color: #555; border-radius: var(--r-s); touch-action: none; z-index: 1; }
.m-resize:hover { background: var(--th); color: var(--accent); }
.meter.mini { width: auto; }
.meter.mini .meter-svg, .meter.mini .meter-side, .meter.mini .m-resize { display: none; }
.meter.mini .meter-model { margin-left: 0; }
.probe-mark { pointer-events: none; }

/* Modal（PLC 程式、人機介面、評審表、自動評分） */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 60; display: flex; align-items: center; justify-content: center; padding: 12px; }
.modal { background: var(--panel); border-radius: var(--r-l); padding: 16px; width: min(680px, 100%); max-height: 92vh; overflow: auto; box-shadow: 0 8px 24px rgba(0,0,0,.3); }
.modal h3 { margin: 0 0 8px; font-size: var(--fs-l); color: var(--text); }
.plc-cols { display: flex; gap: 12px; flex-wrap: wrap; }
.plc-cols textarea { flex: 1 1 260px; min-height: 300px; font: var(--fs)/1.5 Consolas, monospace; text-transform: uppercase; }
.plc-help { flex: 0 0 200px; font-size: var(--fs-xs); line-height: 1.5; background: var(--bg); border-radius: var(--r); padding: 8px; }
.plc-err { margin: 8px 0; font-size: var(--fs-s); color: var(--bad); min-height: 20px; }
.plc-btns { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.plc-brand { display: block; margin-bottom: 8px; }
.hmi-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-bottom: 6px; }
.confirm-modal { width: min(420px, 100%); }
.confirm-modal .bad { color: var(--bad); margin: 0 0 12px; }
.confirm-btns { display: flex; gap: 8px; justify-content: flex-end; }
.sc-modal { width: min(1100px, 96vw); max-height: 92vh; overflow: auto; }
.sc-wrap { overflow-x: auto; }
.sc-t { border-collapse: collapse; width: 100%; font-size: var(--fs-s); }
.sc-t th, .sc-t td { border: 1px solid var(--line); padding: 4px 6px; vertical-align: top; }
.sc-t th { background: var(--th); position: sticky; top: 0; }
.sc-t .sc-h td { background: #e2e8f0; font-weight: 600; }
.sc-t .sc-op { min-width: 14em; }
.sc-t .sc-c { white-space: nowrap; }
.ag-tbl { border-collapse: collapse; width: 100%; margin: 4px 0 12px; font-size: var(--fs-s); }
.ag-tbl td { padding: 4px 6px; border: 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.ag-tbl td:first-child { width: 1.5em; } .ag-tbl td:nth-child(2) { width: 4em; white-space: nowrap; }

/* 側欄 Drawer 的開關按鈕與遮罩：只在窄畫面出現（app.js） */
.side-btn { display: none; }
.side-back { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 65; }

@media (max-width: 1023px) {
  .side { width: 230px; } .side.wide { width: 270px; }
  .cols { grid-template-columns: 1fr; }
}
/* 手機（直放或橫放）：側欄收成 Drawer；app.js 用同一個條件 */
@media (max-width: 767px), (orientation: landscape) and (max-height: 500px) {
  .side, .side.wide { position: fixed; top: 0; bottom: 0; left: 0; width: min(330px, 88vw); z-index: 70; border-right: 0;
    transform: translateX(-100%); visibility: hidden; transition: transform .2s ease-out, visibility 0s linear .2s; box-shadow: 4px 0 16px rgba(0,0,0,.2); }
  .side.open { transform: none; visibility: visible; transition: transform .2s ease-out; }
  .side-btn { display: inline-flex; margin-bottom: 8px; }
}
/* 手機橫放：標題列壓成一行，圖框可以佔滿整個螢幕高度（捲到圖面就看到全圖） */
@media (orientation: landscape) and (max-height: 500px) {
  header { flex-wrap: nowrap; gap: 16px; }
  h1 { font-size: var(--fs); margin: 0; white-space: nowrap; }
  nav { overflow-x: auto; }
  nav button { padding: 6px 12px; white-space: nowrap; }
  /* 扣掉標題列與提示列：提示列＋圖框剛好一個螢幕高 */
  .canvas-wrap { max-height: calc(100vh - 84px); max-height: calc(100dvh - 84px); }
}
/* 手機直放：工具列分組改成一組一段 */
@media (max-width: 767px) {
  header { gap: 0 16px; }
  h1 { margin: 8px 0 0; }
  nav { width: 100%; overflow-x: auto; }
  nav button { padding: 8px 12px; white-space: nowrap; }
  .work { padding: 12px 16px; }
  .tab.active.page { padding: 16px; }
  .tgroup + .tgroup { padding-left: 0; border-left: 0; }
  .toolbar { gap: 8px; }
  .canvas-wrap { max-height: 64vh; }
  .plc-help { flex: 1 1 100%; }
  /* 三用電表改成貼底的橫條：左邊電表、右邊讀值與按鈕，上面還看得到線路圖 */
  .meter { left: 8px; right: 8px; bottom: 8px; width: auto; flex-direction: row; flex-wrap: wrap; max-height: 50vh; }
  .meter-model { flex-basis: 100%; }
  .meter-svg { width: calc(150px * var(--mz, 1)); }
  .meter-side { flex: 1 1 120px; }
  .meter.mini { left: auto; width: auto; }
  .work:has(.meter:not([hidden]):not(.mini)) { padding-bottom: 52vh; } /* 圖面能捲到電表上方 */
  .cols { grid-template-columns: minmax(0, 1fr); }
  table.narrow { min-width: 0; }
  .empty { padding: 24px 0; }
}

/* 減少動態：關掉裝飾性的轉場；故障的冒煙、火花、震動改成靜止標示（元件仍畫成燒毀或異常狀態） */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; }
  .smoke .sm, .smoke .fl, .spark, .part.shake { animation: none !important; }
  .smoke .sm { opacity: .6; }
}
