:root {
  --bg: #f5f6f8;
  --panel: #fff;
  --line: #e3e5e8;
  --text: #1f2329;
  --muted: #8a9099;
  --primary: #1677ff;
  --danger: #d4380d;
  --warn: #d48806;
  --ok: #389e0d;
  --radius: 6px;
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", system-ui, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 18px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }
.grow { flex: 1; }
.row { display: flex; align-items: center; gap: 8px; }
.wrap { flex-wrap: wrap; }

/* ---- 按鈕、表單 ---- */
button, .btn {
  font: inherit; border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: 5px 12px; border-radius: var(--radius); cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 4px;
}
button:hover, .btn:hover { border-color: var(--primary); color: var(--primary); text-decoration: none; }
button.primary, .btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { filter: brightness(1.08); color: #fff; }
button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; }
button.small { padding: 2px 8px; font-size: 12px; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.on { background: #e6f0ff; border-color: var(--primary); color: var(--primary); }
input, select, textarea {
  font: inherit; border: 1px solid var(--line); border-radius: var(--radius); padding: 5px 8px; background: #fff; color: var(--text); min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px #1677ff22; }
textarea { resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 4px; }
label.field > span { font-size: 12px; color: var(--muted); }
label.field > input, label.field > select, label.field > textarea { width: 100%; }
.side.right .scroll { overflow-x: hidden; }
.floor-card .info .row { flex-wrap: nowrap; }
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.form .full { grid-column: 1 / -1; }

/* ---- 版面 ---- */
.topbar {
  height: 52px; background: #fff; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; padding: 0 16px;
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 8px; }
.brand .logo { width: 26px; height: 26px; border-radius: 6px; background: var(--primary); color: #fff; display: grid; place-items: center; font-size: 13px; }
.page { max-width: 1280px; margin: 0 auto; padding: 20px 16px 60px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
.card-body { padding: 16px; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.tabs button { border: none; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 8px 14px; }
.tabs button.active { border-bottom-color: var(--primary); color: var(--primary); font-weight: 600; }

/* ---- 表格 ---- */
table.grid { width: 100%; border-collapse: collapse; background: #fff; }
table.grid th, table.grid td { border-bottom: 1px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: middle; }
table.grid th { background: #fafafa; font-weight: 600; font-size: 13px; position: sticky; top: 0; z-index: 1; }
table.grid tr:hover td { background: #fafcff; }
table.grid td.num, table.grid th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid tr.group td { background: #f5f7fa; font-weight: 600; }
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.table-wrap table.grid tr:last-child td { border-bottom: none; }

.tag { display: inline-block; padding: 0 6px; border-radius: 4px; font-size: 12px; line-height: 20px; color: #fff; white-space: nowrap; }
.pill { display: inline-block; padding: 0 8px; border-radius: 10px; font-size: 12px; line-height: 20px; background: #f0f0f0; white-space: nowrap; }
.pill.ok { background: #f6ffed; color: var(--ok); }
.pill.warn { background: #fff7e6; color: var(--warn); }
.pill.bad { background: #fff2e8; color: var(--danger); }

/* ---- 專案卡片 ---- */
.projects { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.project-card { padding: 16px; display: flex; flex-direction: column; gap: 6px; cursor: pointer; transition: box-shadow .15s; }
.project-card:hover { box-shadow: 0 4px 14px #0000000f; border-color: #c9d8f5; }
.project-card h2 { font-size: 15px; }

/* ---- 樓層 ---- */
.floors { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.floor-card { overflow: hidden; display: flex; flex-direction: column; }
.floor-card .thumb { height: 140px; background: #f0f2f5 center/contain no-repeat; display: grid; place-items: center; color: var(--muted); cursor: pointer; border-bottom: 1px solid var(--line); }
.floor-card .info { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }

/* ---- 對話框 ---- */
.modal-mask { position: fixed; inset: 0; background: #0006; display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: #fff; border-radius: 10px; width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 10px 40px #0003; }
.modal.wide { width: min(1100px, 100%); }
.modal header { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.modal .body { padding: 16px 18px; overflow: auto; }
.modal footer { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; align-items: center; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
#toast div { background: #1f2329e6; color: #fff; padding: 8px 16px; border-radius: 6px; font-size: 13px; }
#toast div.err { background: #d4380de6; }

.empty { padding: 40px; text-align: center; color: var(--muted); }

/* ---- 登入 ---- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 32px 28px; display: flex; flex-direction: column; gap: 16px; text-align: center; }
.login .err { color: var(--danger); font-size: 13px; }

/* ---- 平面圖編輯 ---- */
body.editor { height: 100vh; overflow: hidden; display: flex; flex-direction: column; }
.editor-main { flex: 1; display: flex; min-height: 0; }
.side { width: 270px; background: #fff; border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side.right { border-right: none; border-left: 1px solid var(--line); width: 300px; }
.side .sec { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.side .sec h3 { font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 6px; }
.side .scroll { overflow: auto; flex: 1; min-height: 0; }
.layers { display: flex; flex-direction: column; gap: 1px; max-height: 40vh; overflow: auto; }
.layer { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 5px; cursor: pointer; }
.layer:hover { background: #f5f7fa; }
.layer.active { background: #e6f0ff; }
.layer .dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.layer .eye { border: none; background: none; padding: 0 2px; cursor: pointer; color: var(--muted); font-size: 15px; line-height: 1; }
.layer .eye.off { opacity: .35; }
.layer .cnt { margin-left: auto; font-size: 12px; color: var(--muted); }
.lib-item { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border: 1px solid var(--line); border-radius: 6px; margin-bottom: 6px; cursor: grab; background: #fff; user-select: none; }
.lib-item:hover { border-color: var(--primary); }
.lib-item.armed { border-color: var(--primary); background: #e6f0ff; }
.lib-item .ico { width: 30px; height: 30px; border-radius: 50%; color: #fff; display: grid; place-items: center; font-size: 10px; font-weight: 700; flex: none; }
.lib-item .nm { font-size: 13px; line-height: 1.3; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.lib-item .qty { font-size: 12px; white-space: nowrap; }
.canvas-wrap { flex: 1; position: relative; overflow: hidden; background: #e9ebef; }
.canvas-wrap svg { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; }
.canvas-wrap.placing svg { cursor: crosshair; }
.canvas-wrap.linking svg { cursor: alias; }
.canvas-hint { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: #1f2329d9; color: #fff; padding: 6px 14px; border-radius: 6px; font-size: 13px; pointer-events: none; }
.zoombar { position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 4px; }
.zoombar button { width: 34px; height: 34px; justify-content: center; padding: 0; background: #fffe; }
.dev { cursor: pointer; }
.dev text { font-size: 11px; font-weight: 600; paint-order: stroke; stroke: #fff; stroke-width: 3px; fill: #1f2329; pointer-events: none; }
.dev .lbl { fill: #fff; stroke: none; font-size: 9px; font-weight: 700; }
.dev.dim { opacity: .35; }
.dev.sel .ring { stroke: #1677ff; stroke-width: 3; }
.dev.over .ring { stroke: var(--danger); stroke-width: 3; stroke-dasharray: 4 2; }
.link { stroke-width: 2; fill: none; stroke-dasharray: 6 4; pointer-events: none; }
.link.dim { opacity: .3; }
.props { padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.props .head { display: flex; align-items: center; gap: 8px; }
.props .cap { background: #fafafa; border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; font-size: 12px; display: flex; flex-direction: column; gap: 4px; }
.children { font-size: 12px; max-height: 160px; overflow: auto; display: flex; flex-wrap: wrap; gap: 4px; }
.children a { background: #f0f2f5; border-radius: 4px; padding: 1px 6px; }

@media print {
  .topbar, .side, .zoombar, .canvas-hint { display: none !important; }
  body.editor { height: auto; }
  .canvas-wrap { background: #fff; }
}
@media (max-width: 900px) {
  .side { width: 220px; }
  .side.right { position: absolute; right: 0; top: 52px; bottom: 0; z-index: 5; box-shadow: -4px 0 12px #0001; }
  .form { grid-template-columns: 1fr; }
}
