:root {
  --bg: #f4f4f1;
  --panel: #ffffff;
  --ink: #1d1d1b;
  --muted: #6b6b66;
  --line: #d8d8d2;
  --accent: #2f5d8a;
  --accent-ink: #ffffff;
  --danger: #a33;
  --ok: #2e7d32;
  --warn: #b26a00;
  --wall: #333;
  --table: #ffffff;
  --table-line: #444;
  --men: #eaf1f8;
  --women: #f8ecf0;
  --fixture: #e9e6dc;
  --select: #ff9800;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; font: 14px/1.4 var(--font); color: var(--ink); background: var(--bg); }
body { display: flex; flex-direction: column; }
button { font: inherit; padding: 5px 10px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); cursor: pointer; }
button:hover { border-color: #999; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.mode { border-radius: 0; }
button.mode.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.modes { display: inline-flex; }
.modes button:first-child { border-radius: 6px 0 0 6px; }
.modes button:last-child { border-radius: 0 6px 6px 0; }
input, select { font: inherit; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); }
.grow { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; font-size: 12px; }
.sep { display: inline-block; width: 1px; height: 22px; background: var(--line); margin: 0 4px; vertical-align: middle; }
.only-mobile { display: none; }

#topbar { display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
#topbar .brand { font-weight: 600; font-size: 16px; }
#topbar .field { display: inline-flex; align-items: center; gap: 6px; }
.status { font-size: 12px; color: var(--muted); min-width: 60px; text-align: right; }
.status.dirty { color: var(--warn); }
.status.error { color: var(--danger); font-weight: 600; }

#layoutTools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; padding: 5px 10px; background: #fff8e6; border-bottom: 1px solid #f0dca0; font-size: 13px; }
#layoutTools .hint { color: var(--muted); margin-right: 6px; }
#layoutTools .toggle { display: inline-flex; align-items: center; gap: 4px; margin-left: 6px; }
#syncBar { position: fixed; top: 6px; left: 50%; transform: translateX(-50%); z-index: 40; width: min(92vw, 640px); background: #c62828; color: #fff; padding: 6px 12px; font-size: 13px; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.3); }
#syncBar.conflict { background: #6a1b9a; }
#syncBar.offline-only { background: #ef6c00; }
.sync-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
#syncTitle { font-weight: 800; font-size: 14px; letter-spacing: .3px; }
.sync-sub { opacity: .95; }
#syncBar button { background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.5); padding: 2px 8px; font-size: 12px; }
#syncBar button.danger { color: #fff; background: rgba(0,0,0,.25); }
#syncList { margin: 6px 0 0; padding: 0 0 0 18px; max-height: 30vh; overflow: auto; font-size: 12px; column-width: 260px; }
#syncList li { margin: 1px 0; }
#placingBar { display: flex; gap: 10px; align-items: center; padding: 6px 10px; background: #e6f2ff; border-bottom: 1px solid #b7d3f0; font-size: 13px; }

main { flex: 1; display: flex; min-height: 0; }
#canvasWrap { flex: 1; overflow: auto; padding: 8px; }
#chart { width: 100%; height: auto; max-height: calc(100vh - 70px); display: block; background: #fff; border: 1px solid var(--line); touch-action: none; font-family: var(--font); }

/* ---- families panel ---- */
#panel { width: 400px; border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; padding: 8px; gap: 6px; }
.panel-head { display: flex; align-items: center; gap: 8px; }
.filters { display: flex; gap: 8px; font-size: 12px; flex-wrap: wrap; }
.filters label { display: inline-flex; gap: 3px; align-items: center; }
.legend { line-height: 1.2; }
#rosterList { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; border: 1px solid var(--line); border-radius: 6px; user-select: none; -webkit-user-select: none; }
#rosterList li { padding: 2px 4px; border-bottom: 1px solid #eee; }
#rosterList li.locked { background: #dcefd9; }
#rosterList li.locked-short { background: #fff3c4; }
#rosterList li.hl { background: #fff1d6; }
#rosterList li.dragging { opacity: .5; }
.fam-top { display: flex; align-items: center; gap: 3px; white-space: nowrap; }
.fam-top .drag { cursor: grab; color: #aaa; touch-action: none; padding: 0 3px; font-size: 15px; }
.fam-top .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; font-weight: 600; cursor: pointer; }
.fam-top button { padding: 1px 5px; font-size: 12px; line-height: 1.4; }
.fam-top .grp { display: inline-flex; align-items: center; gap: 1px; font-size: 12px; margin-left: 3px; border-radius: 5px; padding: 0 2px; }
.fam-top .grp.men { background: #e3edf8; }
.fam-top .grp.women { background: #fbe4ec; }
.fam-top .grp .n { font-weight: 600; color: var(--ink); min-width: 2.4em; text-align: center; }
.fam-top .grp button { background: transparent; border-color: transparent; }
.fam-top .grp button:hover { border-color: #999; background: #fff; }
.fam-top .assignbtn, li.member .assignbtn { padding: 0 5px; }
.notebtn svg { vertical-align: -2px; }
.legend .lg { display: inline-block; padding: 0 5px; border-radius: 4px; }
.legend .lg.men { background: #e3edf8; }
.legend .lg.women { background: #fbe4ec; }
li.member .mn.men { border-left: 3px solid #9cbbe0; padding-left: 5px; }
li.member .mn.women { border-left: 3px solid #eaa7c0; padding-left: 5px; }
.toggle-btn.active { background: #e3edf8; border-color: var(--accent); }
.fam-top .grp .n.short { color: var(--warn); }
.fam-top .grp .n.over { color: var(--danger); }
.fam-top .grp .n.ok { color: var(--ok); }
.fam-top .grp button { padding: 0 4px; font-size: 12px; }
.fam-top .notebtn.has { color: var(--warn); border-color: var(--warn); }
.fam-top .x, .member .x { border: 0; padding: 0 4px; color: var(--muted); background: none; }
.fam-top .caret { border: 0; background: none; padding: 0 2px; color: #888; font-size: 12px; width: 16px; }
ul.members { list-style: none; margin: 4px 0 2px 20px; padding: 0 0 0 6px; border-left: 2px solid #e4e4de; }
li.member { display: flex; align-items: center; gap: 4px; padding: 2px 0; font-size: 13px; }
li.member .mn { flex: 1; cursor: pointer; }
li.member .drag { cursor: grab; color: #bbb; touch-action: none; padding: 0 3px; }
li.member button { padding: 1px 6px; font-size: 12px; }
li.member-add { display: flex; gap: 3px; padding: 3px 0; }
li.member-add input { flex: 1; min-width: 0; padding: 2px 6px; font-size: 13px; }
li.member-add select { padding: 2px 4px; font-size: 12px; }
li.member-add button { padding: 1px 6px; font-size: 12px; }
.fam-top .x:hover { color: var(--danger); }
.lockbtn.on { background: #dff0df; border-color: #9c9; }
#rosterAdd { display: flex; gap: 4px; }
#rosterAdd input { flex: 1; min-width: 0; }

#dragGhost { position: fixed; z-index: 50; pointer-events: none; padding: 4px 10px; background: #fff7dc; border: 1px solid var(--select); border-radius: 6px; box-shadow: 0 4px 14px rgba(0,0,0,.2); font-weight: 600; transform: translate(-50%, -140%); white-space: nowrap; }

/* ---- SVG ---- */
.room-main rect.floor { fill: #fdfdfb; }
.room-side rect.floor { fill: #fbf6f8; }
.room-faint rect.floor { fill: #f5f5f2; }
.room-faint { opacity: .7; }
.wall { stroke: var(--wall); stroke-width: 6; fill: none; stroke-linecap: square; }
.gap { stroke: #fff; stroke-width: 7; }
.door-leaf { stroke: var(--wall); stroke-width: 1.2; fill: none; }
.door-arc { stroke: #777; stroke-width: .8; fill: none; stroke-dasharray: 2 2; }
.window { stroke: #5a7aa0; stroke-width: 1.4; }
.opening { stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.bay { stroke: var(--wall); stroke-width: 6; fill: #fdfdfb; }
.room-name { font-size: 9px; fill: #8a8a84; pointer-events: none; }

.fixture rect { fill: var(--fixture); stroke: var(--table-line); stroke-width: 1; }
.fixture.aron rect { fill: #e2d9c3; stroke-width: 1.5; }
.fixture.bimah rect { fill: #e8e2cf; stroke-width: 1.5; }
.fixture.label rect { fill: none; stroke: none; }
.fixture text { font-size: 9px; text-anchor: middle; dominant-baseline: middle; pointer-events: none; }
.fixture.aron text, .fixture.bimah text { font-size: 11px; font-weight: 600; }
.fixture.label text { font-size: 12px; fill: #777; }

.table rect.top { fill: var(--table); stroke: var(--table-line); stroke-width: 1.2; }
.table.men rect.top { fill: var(--men); }
.table.women rect.top { fill: var(--women); }
.seat rect { fill: transparent; stroke: var(--table-line); stroke-width: .7; }
.seat.filled rect { fill: #fff; }
.seat text { text-anchor: middle; dominant-baseline: middle; pointer-events: none; fill: #111; }
.seat text.first { fill: #333; }
.seat.hl rect { fill: #ffe2a8; }
.seat.drop rect { fill: #cfe7ff; stroke: var(--accent); stroke-width: 1.6; }
.seat.drag-src rect { fill: #eee; }
.mode-seat .seat { cursor: pointer; }
.mode-seat.placing .seat { cursor: copy; }
.mode-seat .seat.filled { cursor: grab; }
.mode-seat .seat:hover rect { fill: #fff6dc; }
.mode-layout .draggable { cursor: grab; }
.mode-layout .draggable.selected > rect:first-of-type, .mode-layout .draggable.selected rect.top { stroke: var(--select); stroke-width: 2; }
.table-label { font-size: 5px; fill: #888; text-anchor: middle; pointer-events: none; }
.dim line { stroke: #7a7a74; stroke-width: .8; }
.dim text { font-size: 7px; fill: #55554f; }
.gapline line { stroke: #c0392b; stroke-width: .8; stroke-dasharray: 3 2; }
.gapline text { font-size: 6px; fill: #c0392b; font-weight: 600; paint-order: stroke; stroke: #fff; stroke-width: 2; }
.gapline.wall line { stroke: #2f5d8a; }
.gapline.wall text { fill: #2f5d8a; }
.scalebar line { stroke: #999; stroke-width: 1; }
.scalebar text { font-size: 7px; fill: #999; }

/* ---- dialogs ---- */
dialog { border: 1px solid var(--line); border-radius: 10px; padding: 16px; width: min(92vw, 420px); box-shadow: 0 10px 40px rgba(0,0,0,.2); }
dialog::backdrop { background: rgba(0,0,0,.3); }
dialog h3 { margin: 0 0 10px; }
dialog input { width: 100%; margin-bottom: 8px; font-size: 16px; }
dialog .actions { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
dialog .actions.wrap { flex-wrap: wrap; }
#chartsList, #historyList { list-style: none; padding: 0; margin: 0 0 8px; max-height: 45vh; overflow: auto; border: 1px solid var(--line); border-radius: 6px; }
#chartsList li, #historyList li { padding: 6px 10px; border-bottom: 1px solid #eee; display: flex; gap: 8px; align-items: center; cursor: pointer; }
#chartsList li.active { background: #eef3f9; font-weight: 600; }
#historyList li .ts { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }

/* ---- mobile ---- */
@media (max-width: 900px) {
  .only-mobile { display: inline-block; }
  #panel { position: fixed; right: 0; top: 0; bottom: 0; width: min(96vw, 400px); transform: translateX(100%); transition: transform .2s; z-index: 5; box-shadow: -6px 0 20px rgba(0,0,0,.15); }
  #panel.open { transform: none; }
  #topbar .brand span { display: none; }
  #chart { max-height: none; }
}

/* ---- print ---- */
.print-only { display: none; }
@media print {
  @page { size: letter landscape; margin: 0.35in; }
  body { background: #fff; display: block; }
  #topbar, #layoutTools, #placingBar, #syncBar, #panel, dialog, #dragGhost { display: none !important; }
  main { display: block; }
  #canvasWrap { padding: 0; overflow: visible; }
  .print-only { display: block; }
  #printTitle { font-size: 18px; font-weight: 700; text-align: center; margin: 0 0 4px; }
  #chart { border: 0; width: 100%; height: auto; max-height: 7.3in; }
  .mode-seat .seat:hover rect { fill: transparent; }
}
