:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --fg: #1f2328;
  --muted: #6b7280;
  --border: #d9dce1;
  --accent: #2f6fdb;
  --accent-bg: #e6eefc;
  --hover: #f0f2f5;
  --head: #4f81bd;
  --head-fg: #ffffff;
  --view-bg: #cfd3d8;
  font-size: 13px;
}
:root[data-theme="dark"] {
  --bg: #15171b;
  --panel: #1e2126;
  --fg: #e6e8eb;
  --muted: #9aa3ad;
  --border: #30353c;
  --accent: #6ea0ff;
  --accent-bg: #253552;
  --hover: #262a31;
  --head: #2f4f7a;
  --view-bg: #0f1114;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b;
    --panel: #1e2126;
    --fg: #e6e8eb;
    --muted: #9aa3ad;
    --border: #30353c;
    --accent: #6ea0ff;
    --accent-bg: #253552;
    --hover: #262a31;
    --head: #2f4f7a;
    --view-bg: #0f1114;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--fg);
  background: var(--bg);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) 24px;
  grid-template-columns: minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

/* header */
#top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  min-height: 42px;
  min-width: 0;
  padding: 4px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 15px; letter-spacing: 0.2px; color: var(--accent); white-space: nowrap; }
.brand b { color: var(--fg); font-weight: 600; }
.group { display: flex; align-items: center; gap: 4px; min-width: 0; }
.spacer { flex: 1; }
/* small flex-basis so the header shrinks these two before wrapping to a second row */
.jobname { font-weight: 600; flex: 1 1 120px; max-width: 28vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#pageSel { flex: 1 1 140px; max-width: 260px; }
button, select, input[type="search"] {
  font: inherit;
  color: var(--fg);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 9px;
  height: 28px;
}
button { cursor: pointer; }
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: 0.45; cursor: default; }
button.on { background: var(--accent-bg); border-color: var(--accent); }
select { max-width: 260px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--accent-bg); color: var(--accent); }

/* main */
#main {
  --lw: 320px;
  --rw: 330px;
  display: grid;
  grid-template-columns: 56px var(--lw) minmax(0, 1fr) var(--rw);
  min-height: 0;
  min-width: 0;
}
#main.wide { --lw: 560px; }
#main.no-left { --lw: 0px; }
#main.no-right { --rw: 0px; }
#main.no-left #left, #main.no-right #right { display: none; }
#rail { grid-column: 1; }
#left { grid-column: 2; }
#stage { grid-column: 3; }
#right { grid-column: 4; }
[hidden] { display: none !important; }

/* icon rail */
#rail { display: flex; flex-direction: column; align-items: stretch; gap: 2px; padding: 6px 4px; background: var(--panel); border-right: 1px solid var(--border); }
#rail .spacer { flex: 1; }
#rail button { position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; height: auto; padding: 7px 0 5px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); }
#rail button span { font-size: 10px; }
#rail button:hover:not(:disabled) { background: var(--hover); color: var(--fg); }
#rail button.on { color: var(--accent); background: var(--accent-bg); }
#rail button[data-tab].on::before { content: ""; position: absolute; left: -4px; top: 8px; bottom: 8px; width: 3px; border-radius: 2px; background: var(--accent); }
#rail .count { position: absolute; top: 3px; right: 5px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--accent); color: #fff; font-size: 10px; line-height: 16px; font-weight: 600; }
.panel-title { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 0 10px; height: 34px; border-bottom: 1px solid var(--border); }
.panel-title b { font-size: 13px; }
button.ghost { border-color: transparent; background: transparent; color: var(--muted); }

/* stage: drawing + floating tool dock */
#stage { position: relative; display: grid; min-width: 0; min-height: 0; }
#dock {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 6px;
  max-width: calc(100% - 24px);
  overflow-x: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 22px rgba(0,0,0,0.18);
  z-index: 5;
}
#dock .group + .group { border-left: 1px solid var(--border); padding-left: 6px; margin-left: 2px; }
#dock button { border-color: transparent; background: transparent; }
#dock button:hover:not(:disabled) { background: var(--hover); }
#dock button.on { background: var(--accent-bg); border-color: var(--accent); color: var(--accent); }

/* notes panel */
#notesTools { display: flex; gap: 6px; padding: 6px; border-bottom: 1px solid var(--border); }
#notesTools input { flex: 1; min-width: 0; }
#notesList { flex: 1; padding: 6px; }
.note { padding: 7px 9px; margin: 0 0 6px; border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 6px; cursor: pointer; }
.note:hover { background: var(--hover); }
.note.resolved { border-left-color: var(--border); color: var(--muted); }
.note .meta { display: flex; gap: 6px; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.note .meta .ref { font-family: Consolas, monospace; color: var(--fg); }
.note .meta .grow { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note .txt { white-space: pre-wrap; word-break: break-word; }
.note .rep { font-size: 11px; color: var(--muted); margin-top: 3px; }
aside { background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
#left { border-right: 1px solid var(--border); }
#right { border-left: 1px solid var(--border); }
.scroll { overflow: auto; min-height: 0; }
.half { flex: 1 1 50%; border-bottom: 1px solid var(--border); }
#treeTools { display: flex; gap: 6px; padding: 6px; border-bottom: 1px solid var(--border); align-items: center; }
#treeTools input[type="search"] { flex: 1; min-width: 0; }
#treeTools label { white-space: nowrap; color: var(--muted); }
#tree { flex: 1; }

/* canvas */
#view {
  position: relative;
  overflow: hidden;
  background: var(--view-bg);
  cursor: grab;
  touch-action: none;
}
#view:active { cursor: grabbing; }
#view canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.ct-base { background: transparent; }
.ct-overlay { pointer-events: none; }

/* tree */
ul.tree { list-style: none; margin: 0; padding: 0; }
ul.tree li { margin: 0; }
.row {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding-right: 8px;
  cursor: pointer;
  white-space: nowrap;
  border-left: 3px solid transparent;
}
.row:hover { background: var(--hover); }
.row.sel { background: var(--accent-bg); border-left-color: var(--accent); }
.row.empty { color: var(--muted); }
.caret { width: 14px; color: var(--muted); display: inline-block; transition: transform 0.1s; text-align: center; }
.caret.open { transform: rotate(90deg); }
.caret.none { visibility: hidden; }
.sw { width: 10px; height: 10px; border-radius: 2px; flex: none; border: 1px solid rgba(0,0,0,0.2); }
.ty { color: var(--muted); font-size: 10px; width: 10px; }
.code { color: var(--muted); font-family: Consolas, monospace; font-size: 12px; }
.name { overflow: hidden; text-overflow: ellipsis; flex: 1; }
.qty { font-variant-numeric: tabular-nums; text-align: right; }
.unit { color: var(--muted); width: 26px; }

/* tables */
.panel-head { padding: 6px 8px; font-weight: 600; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.muted { color: var(--muted); font-weight: normal; }
.hint { padding: 12px; color: var(--muted); }
table.grid { border-collapse: collapse; width: 100%; font-size: 12px; }
table.grid th { background: var(--head); color: var(--head-fg); font-weight: 600; text-align: left; padding: 4px 6px; position: sticky; top: 29px; }
table.grid td { padding: 3px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: var(--hover); }
table.grid tr.sel { background: var(--accent-bg); }
table.grid tr.other { color: var(--muted); }
table.grid tr.empty { color: var(--muted); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.mono { font-family: Consolas, monospace; }
#summary table.grid th { top: 29px; }
#summary table.grid { width: auto; min-width: 100%; }
#summary td:nth-child(2) { max-width: 150px; overflow: hidden; text-overflow: ellipsis; }

dl.detail { margin: 0; padding: 6px 8px; display: grid; grid-template-columns: 84px 1fr; gap: 3px 8px; font-size: 12px; }
dl.detail dt { color: var(--muted); }
dl.detail dd { margin: 0; word-break: break-word; }

/* status */
#status {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 10px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border-top: 1px solid var(--border);
  white-space: nowrap;
}
#status .grow { flex: 1; color: var(--fg); overflow: hidden; text-overflow: ellipsis; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translate(-50%, 20px);
  background: #1f2328;
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s;
  max-width: 70vw;
}
#toast.show { opacity: 0.95; transform: translate(-50%, 0); }

/* tools / dialogs (phase 2) */
button.icon { width: 28px; padding: 0; }
#detailTools { display: flex; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
#detailWrap { display: flex; flex-direction: column; min-height: 0; }
#detailWrap #detail { flex: 1; }
#stScale { cursor: pointer; }
#stScale:hover { color: var(--accent); }
body.dirty #btnSave { border-color: var(--accent); color: var(--accent); }
dialog {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--fg);
  padding: 16px 18px;
  min-width: 340px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}
dialog::backdrop { background: rgba(0,0,0,0.35); }
dialog h3 { margin: 0 0 10px; font-size: 14px; }
dialog .field { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 8px; margin: 6px 0; }
dialog .field > span { color: var(--muted); }
dialog .field3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
dialog .field3 .field { grid-template-columns: 1fr; gap: 2px; }
dialog input, dialog select { font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--border); border-radius: 5px; padding: 4px 8px; height: 28px; min-width: 0; }
dialog input[type="color"] { padding: 2px; width: 48px; }
dialog .or { text-align: center; color: var(--muted); margin: 2px 0; }
dialog menu { display: flex; flex-direction: row-reverse; gap: 6px; margin: 14px 0 0; padding: 0; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
dialog .panel-head { margin: 8px -4px 4px; }
.joblist button { display: block; width: 100%; text-align: left; margin: 2px 0; height: auto; padding: 5px 8px; }
.joblist .muted { float: right; }
.linkrow { display: grid; grid-template-columns: 50px 1fr auto auto auto; gap: 6px; align-items: center; margin: 3px 0; font-size: 12px; }
.linkrow code { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply { padding: 4px 8px; margin: 2px 0 2px 110px; background: var(--bg); border-radius: 5px; font-size: 12px; }
.reply b { color: var(--muted); font-weight: normal; margin-right: 6px; }
button.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.1); }

@media (max-width: 1100px) {
  #main { --lw: 260px; --rw: 280px; }
  #main.wide { --lw: 440px; }
}
@media (max-width: 800px) {
  #main, #main.wide { --lw: 0px; --rw: 0px; }
  #left, #right { display: none; }
}
