:root {
  --ink: #0f172a;
  --muted: #64748b;
  --card: #ffffff;
  --line: #e7ecf2;
  --accent: #2563eb;
  --accent-soft: #eaf1ff;
  --green: #16a34a;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --radius: 16px;
  --bg: #eaf0f6;
  --bg-top: #f5f8fc;
  --field: #f8fafc;
  --hover: #f1f5f9;
  color-scheme: light;
}

/* 夜間模式 */
:root[data-theme="dark"] {
  --ink: #e6ebf3;
  --muted: #94a3b8;
  --card: #111a2e;
  --line: #24324f;
  --accent: #4f8cff;
  --accent-soft: #1a2b4f;
  --green: #22c55e;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45), 0 1px 2px rgba(0, 0, 0, 0.3);
  --bg: #0b1226;
  --bg-top: #070c1c;
  --field: #0c1427;
  --hover: #1a2440;
  color-scheme: dark;
}
:root[data-theme="dark"] .pill,
:root[data-theme="dark"] .s-ready { background: rgba(245, 158, 11, .16); color: #fbbf24; }
:root[data-theme="dark"] .s-done { background: rgba(34, 197, 94, .16); color: #4ade80; }
:root[data-theme="dark"] .s-short { background: rgba(239, 68, 68, .16); color: #f87171; }
:root[data-theme="dark"] .order[data-type="銷貨"] .chip { background: #c2410c; }

/* 日／夜切換鈕 */
#theme-toggle {
  margin-left: auto; width: 36px; height: 36px; flex: none;
  display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--field); color: var(--muted); cursor: pointer;
  transition: color .2s, border-color .2s;
}
#theme-toggle:hover { color: var(--accent); border-color: var(--accent); }
/* 音樂／音效開關 */
.icon-btn {
  width: 36px; height: 36px; flex: none; display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--field); color: var(--muted); cursor: pointer;
  transition: color .2s, border-color .2s;
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }
.icon-btn svg { width: 18px; height: 18px; fill: currentColor; }
.icon-btn .on, .icon-btn[aria-pressed="true"] .off { display: none; }
.icon-btn[aria-pressed="true"] .on { display: block; }
.icon-btn[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
#music-toggle { margin-left: auto; }
#music-toggle + #sfx-toggle, #sfx-toggle + #theme-toggle { margin-left: -4px; }
#theme-toggle svg { width: 18px; height: 18px; }
/* 按鈕圖示顯示目前模式：時鐘＝自動（台灣時間）、太陽＝固定日間、月亮＝固定夜間 */
#theme-toggle svg { display: none; }
#theme-toggle[data-mode="auto"] .i-auto,
#theme-toggle[data-mode="light"] .i-sun,
#theme-toggle[data-mode="dark"] .i-moon { display: block; }
#theme-toggle[data-mode="light"], #theme-toggle[data-mode="dark"] { color: var(--accent); border-color: var(--accent); }
#tw-clock { margin: 2px 0 0; color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
#tw-clock:empty { display: none; }

/* 天氣列 */
#wx-row {
  display: flex; align-items: center; gap: 8px;
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line);
}
#wx-text { flex: 1; min-width: 0; font-size: 12px; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#wx-mode {
  flex: none; height: 28px; padding: 0 6px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--field); color: var(--ink);
  font-family: inherit; font-size: 12px; cursor: pointer;
}
#hud, .stat, #panel, #orders, #hint { transition-property: background-color, color, box-shadow, transform, opacity; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Inter", "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#scene { position: fixed; inset: 0; }
#scene canvas { display: block; touch-action: none; }

svg { fill: currentColor; }

/* Loading */
#loading {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: linear-gradient(var(--bg-top), var(--bg));
  color: var(--muted);
  transition: opacity .6s ease;
}
#loading.done { opacity: 0; pointer-events: none; }
#loading.error p { color: #b3261e; }
.loader-box {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--accent); box-shadow: 0 8px 20px rgba(37, 99, 235, .35);
  animation: spin 1.2s ease-in-out infinite;
}
@keyframes spin { 50% { transform: rotate(180deg) scale(.85); } 100% { transform: rotate(360deg); } }

/* HUD */
#hud {
  position: fixed; top: 16px; left: 16px; z-index: 10;
  padding: 14px; border-radius: var(--radius);
  background: var(--card); box-shadow: var(--shadow);
  width: min(320px, calc(100vw - 32px));
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.logo {
  width: 38px; height: 38px; border-radius: 10px; flex: none;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  box-shadow: 0 6px 14px rgba(37, 99, 235, .3);
}
.logo svg { width: 22px; height: 22px; }
#hud h1 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: .02em; }
#wh-meta { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

#search {
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px; height: 40px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--field);
}
#search:focus-within { border-color: var(--accent); background: var(--card); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-icon { width: 18px; height: 18px; color: var(--muted); flex: none; }
#search input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  font: inherit; font-size: 14px; color: var(--ink);
}
kbd {
  font: 500 12px/1 "Inter", system-ui, sans-serif; color: var(--muted);
  padding: 4px 7px; border-radius: 6px; border: 1px solid var(--line); background: var(--card);
}
#search-msg { margin: 8px 2px 0; font-size: 12px; color: var(--muted); }
#search-msg:empty { display: none; }

/* Stats */
#stats {
  position: fixed; top: 16px; left: 352px; z-index: 10;
  display: flex; gap: 12px;
  transition: opacity .3s, transform .3s;
}
body.panel-open #stats { opacity: 0; transform: translateY(-8px); pointer-events: none; }
.stat {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px 12px 12px; border-radius: var(--radius);
  background: var(--card); box-shadow: var(--shadow); min-width: 150px;
}
.stat-icon {
  width: 40px; height: 40px; border-radius: 12px; flex: none;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.stat-icon svg { width: 22px; height: 22px; }
.stat p { margin: 0; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 22px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 11px; color: var(--muted); }
.ring {
  --p: 0;
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--accent-soft) 0);
  -webkit-mask: radial-gradient(circle, transparent 12px, #000 13px);
          mask: radial-gradient(circle, transparent 12px, #000 13px);
}

#hint {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 10;
  margin: 0; padding: 8px 14px; border-radius: 999px;
  background: var(--card); color: var(--muted); font-size: 12px; white-space: nowrap;
  box-shadow: var(--shadow); pointer-events: none; transition: opacity .3s;
}

/* Tooltip */
#tooltip {
  position: fixed; z-index: 20; pointer-events: none;
  padding: 6px 10px; border-radius: 8px; font-size: 12px; font-weight: 600;
  background: var(--accent); color: #fff;
  box-shadow: 0 6px 16px rgba(37, 99, 235, .35);
  transform: translate(14px, 14px); white-space: nowrap;
}

/* Panel */
#panel {
  position: fixed; top: 16px; right: 16px; z-index: 30;
  max-height: calc(100vh - 32px);
  width: min(380px, calc(100vw - 32px));
  padding: 18px; border-radius: var(--radius);
  background: var(--card); box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  transform: translateX(calc(100% + 32px));
  transition: transform .45s cubic-bezier(.2, .8, .2, 1);
}
#panel.open { transform: none; }
.panel-head { display: flex; align-items: center; gap: 12px; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.panel-title { flex: 1; min-width: 0; }
#panel h2 { margin: 0; font-size: 18px; font-weight: 700; }
#panel-shelf { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.badge {
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap;
}
#panel-close {
  width: 30px; height: 30px; border: 0; border-radius: 8px; flex: none;
  background: transparent; color: var(--muted); font-size: 22px; line-height: 1; cursor: pointer;
}
#panel-close:hover { background: var(--hover); color: var(--ink); }

.meta {
  display: grid; grid-template-columns: auto 1fr; gap: 10px 16px;
  margin: 14px 0; padding-bottom: 14px; border-bottom: 1px solid var(--line); font-size: 13px;
}
.meta dt { color: var(--muted); }
.meta dd { margin: 0; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

#panel h3 { margin: 0 0 8px; font-size: 13px; font-weight: 600; }
.table-wrap { overflow: auto; flex: 0 1 auto; border: 1px solid var(--line); border-radius: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px; text-align: left; border-bottom: 1px solid var(--line); }
tbody tr:last-child td { border-bottom: 0; }
th { font-size: 11px; color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--field); }
td.code { font-weight: 600; white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
td.num { font-weight: 700; color: var(--accent); }

/* ERP 單據 */
#orders {
  position: fixed; left: 16px; bottom: 16px; z-index: 12;
  width: min(340px, calc(100vw - 32px)); max-height: calc(100vh - 210px);
  display: flex; flex-direction: column;
  border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow);
}
.orders-head {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 12px 14px; border: 0; background: transparent; cursor: pointer; text-align: left;
  font-family: inherit; color: var(--ink);
}
.orders-head b { font-size: 14px; }
.pill { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: #fef3c7; color: #b45309; }
#orders-count { margin-left: auto; font-size: 12px; color: var(--muted); }
.chev { width: 18px; height: 18px; color: var(--muted); transition: transform .2s; }
#orders.collapsed .chev { transform: rotate(180deg); }
#orders.collapsed .orders-body { display: none; }
.orders-body { display: flex; flex-direction: column; min-height: 0; padding: 0 14px 12px; }
.orders-actions { display: flex; gap: 8px; margin-bottom: 10px; }
.orders-actions button {
  flex: 1; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: var(--field); cursor: pointer;
  font-family: inherit; font-size: 12px; font-weight: 600; color: var(--ink);
}
.orders-actions button:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
#orders-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 0; display: flex; flex-direction: column; gap: 8px; }
.order { padding: 10px; border: 1px solid var(--line); border-radius: 12px; }
.order[data-state="done"] { opacity: .55; }
.order-top { display: flex; align-items: center; gap: 8px; }
.chip { flex: none; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 6px; color: #fff; background: var(--green); }
.order[data-type="銷貨"] .chip { background: #ea580c; }
.order[data-type="進貨"] .chip { background: var(--accent); }
.order-no { min-width: 0; font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { flex: none; margin-left: auto; font-size: 11px; font-weight: 600; white-space: nowrap; padding: 2px 8px; border-radius: 999px; background: var(--hover); color: var(--muted); }
.s-moving, .s-closing { background: var(--accent-soft); color: var(--accent); }
.s-ready { background: #fef3c7; color: #b45309; }
.s-done { background: #dcfce7; color: #15803d; }
.s-short { background: #fee2e2; color: #b91c1c; }
.order-sub { margin: 6px 0 0; font-size: 12px; color: var(--muted); line-height: 1.5; }
.order-act {
  margin-top: 8px; width: 100%; height: 30px; border: 0; border-radius: 8px; cursor: pointer;
  background: var(--accent); color: #fff; font-family: inherit; font-size: 12px; font-weight: 600;
}
#orders-sync { margin: 10px 0 0; font-size: 11px; color: var(--muted); }

/* 製程進出站（右下） */
#wip {
  position: fixed; right: 16px; bottom: 16px; z-index: 12;
  width: min(360px, calc(100vw - 32px)); max-height: calc(100vh - 210px);
  display: flex; flex-direction: column;
  border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow);
  transition: opacity .25s, transform .25s;
}
body.panel-open #wip { opacity: 0; transform: translateY(8px); pointer-events: none; }
#wip-count { margin-left: auto; font-size: 12px; color: var(--muted); }
#wip.collapsed .chev { transform: rotate(180deg); }
#wip.collapsed .orders-body { display: none; }
#wip .orders-body { overflow-y: auto; }
#wip-stations { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 10px; }
.wst {
  display: flex; flex-direction: column; align-items: center; gap: 1px; min-width: 0;
  padding: 5px 2px 4px; border: 1px solid var(--line); border-top: 3px solid var(--c); border-radius: 8px;
  background: var(--field); color: var(--ink); font-family: inherit; cursor: pointer;
}
.wst:hover { background: var(--hover); }
.wst b { font-size: 11px; color: var(--c); }
.wst-name { font-size: 9.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.wst-n { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
.wst.busy { box-shadow: inset 0 0 0 1px var(--c); }
.wst.busy .wst-n::after { content: ""; display: inline-block; width: 6px; height: 6px; margin-left: 3px; vertical-align: middle; border-radius: 50%; background: var(--c); animation: wst-pulse 1s infinite alternate; }
@keyframes wst-pulse { from { opacity: .25; } to { opacity: 1; } }
#wip-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.wip-mo { cursor: pointer; padding: 8px 10px; }
.wip-mo:hover { background: var(--hover); }
.wip-mo .chip { background: #475569; }
.wip-mo[data-state="done"] { opacity: .55; }
.w-run { background: #dcfce7; color: #15803d; }
.w-queue, .w-backlog { background: var(--accent-soft); color: var(--accent); }
.w-hold, .w-out, .w-call { background: #fef3c7; color: #b45309; }
.w-move { background: #fce7f3; color: #be185d; }
.w-htq, .w-ht { background: #fee2e2; color: #b91c1c; }
.w-done { background: var(--hover); color: var(--muted); }
.wip-route { display: grid; grid-template-columns: repeat(11, 1fr); gap: 3px; margin-top: 6px; }
.wip-route span.ht { outline: 1px dashed #b91c1c; outline-offset: 1px; }
.wip-route span.rw { background-image: repeating-linear-gradient(45deg, transparent 0 2px, rgba(234,88,12,.7) 2px 4px); }
.wip-route span { height: 5px; border-radius: 3px; background: var(--line); }
.wip-route span.ok { background: #22c55e; }
.wip-route span.cur { background: var(--accent); animation: wst-pulse .8s infinite alternate; }
.wip-h { margin: 12px 0 6px; font-size: 12px; color: var(--muted); font-weight: 600; }
#wip-log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; font-size: 11.5px; }
#wip-log li { display: flex; gap: 6px; align-items: baseline; white-space: nowrap; overflow: hidden; }
#wip-log li span { overflow: hidden; text-overflow: ellipsis; color: var(--ink); }
#wip-log time { color: var(--muted); font-variant-numeric: tabular-nums; }
.ev { flex: none; font-size: 10.5px; padding: 0 5px; border-radius: 4px; color: #fff; background: #64748b; }
.ev-進站 { background: #2563eb; }
.ev-出站 { background: #16a34a; }
.ev-移轉 { background: #db2777; }
.ev-投料 { background: #ea580c; }
.ev-入庫 { background: #475569; }
.ev-重工 { background: #ea580c; }
.ev-委外出廠, .ev-委外返廠 { background: #b91c1c; }
#wip-sync { margin: 10px 0 0; font-size: 11px; color: var(--muted); }
.orders-actions button.urgent { color: #dc2626; border-color: #fecaca; }
.orders-actions button.urgent:hover { background: #fee2e2; border-color: #dc2626; color: #dc2626; }
.wip-mo[data-urgent="1"] { border-color: #fca5a5; box-shadow: inset 3px 0 0 #dc2626; }
.wip-mo[data-urgent="1"] .chip, .agv-mo.urgent .chip { background: #dc2626; }

/* 跟車中的搬運車資訊卡（右上） */
#agv-card {
  position: fixed; top: 16px; right: 16px; z-index: 30;
  width: min(340px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow-y: auto;
  padding: 16px; border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow);
}
#agv-card[hidden] { display: none; }
#agv-close { margin-left: auto; width: 30px; height: 30px; border: 0; border-radius: 8px; background: var(--hover); color: var(--ink); font-size: 18px; cursor: pointer; }
.agv-head { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.agv-head b { font-size: 16px; }
.a-load { background: #dcfce7; color: #15803d; }
.a-busy { background: var(--accent-soft); color: var(--accent); }
.agv-route { margin: 4px 0 10px; font-size: 13px; color: var(--muted); }
.agv-mo { padding: 10px; border: 1px solid var(--line); border-radius: 12px; }
.agv-mo.urgent { border-color: #fca5a5; background: rgba(254, 226, 226, .35); }
.agv-mo .chip { background: #475569; }
.agv-meta { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 10px 0 8px; font-size: 12.5px; }
.agv-meta dt { color: var(--muted); white-space: nowrap; }
.agv-meta dd { margin: 0; font-weight: 600; min-width: 0; overflow-wrap: anywhere; }
.agv-hist { margin: 8px 0 0; font-family: inherit; font-size: 11px; color: var(--muted); white-space: pre-wrap; }
.agv-empty { margin: 0; font-size: 13px; color: var(--muted); }
.mc-prog { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.mc-prog span { display: block; height: 100%; background: #22c55e; transition: width .3s; }
.agv-hist { max-height: 160px; overflow-y: auto; }
body.agv-follow #wip { opacity: 0; transform: translateY(8px); pointer-events: none; }

@media (max-width: 920px) { #hint { display: none; } }

.stat-cash { cursor: pointer; }
.stat-cash:hover, .stat-cash:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.stat-cash .stat-icon { background: #dcfce7; color: #16a34a; }
.stat-cash.cash-up .stat-value { color: #16a34a; }
.stat-cash.cash-down .stat-value { color: #dc2626; }
.stat-cash .stat-value { transition: color .3s; }
@media (max-width: 1500px) { #stats .stat:nth-child(5) { display: none; } }
@media (max-width: 1100px) { #stats .stat:nth-child(n+3) { display: none; } }
@media (max-width: 760px) { #stats { display: none; } }

/* Phone: panel becomes bottom drawer */
@media (max-width: 640px) {
  #hud { padding: 12px; }
  .brand { margin-bottom: 10px; }
  #hud h1 { font-size: 16px; }
  kbd { display: none; }
  #panel {
    top: auto; left: 0; right: 0; bottom: 0; width: 100%;
    max-height: max(50vh, min(320px, 85vh)); border-radius: 18px 18px 0 0; padding: 16px;
    overflow-y: auto;
    transform: translateY(105%);
  }
  #panel.open { transform: none; }
  .meta { margin: 10px 0; padding-bottom: 10px; }
  .table-wrap { overflow: visible; flex: none; }
  #hint { bottom: 12px; }
  #orders { left: 12px; bottom: 12px; width: calc(100vw - 24px); max-height: 55vh; }
  body.panel-open #orders { display: none; }
  #wip { right: 12px; bottom: 66px; width: calc(100vw - 24px); max-height: 55vh; }
  body:has(#orders:not(.collapsed)) #wip { display: none; }
  #agv-card { top: auto; bottom: 12px; right: 12px; left: 12px; width: auto; max-height: 50vh; }
  body.agv-follow #orders { display: none; }
}
