:root {
  --text-xs: clamp(0.75rem, 0.7rem + 0.2vw, 0.8125rem);
  --text-sm: clamp(0.8125rem, 0.78rem + 0.2vw, 0.875rem);
  --text-base: clamp(0.9375rem, 0.9rem + 0.2vw, 1rem);
  --text-lg: clamp(1.0625rem, 1rem + 0.4vw, 1.25rem);

  --navy: #1f3a5f;
  --navy-600: #2b4a73;
  --navy-50: #eef2f7;
  --ink: #1d232b;
  --muted: #5b6573;
  --line: #dde1e6;
  --surface: #ffffff;
  --bg: #f5f3ef;
  --cedar: #b5652b;
  --upper: #c98f5a;
  --lower: #ebcb9f;
  --slat: #a8703f;
  --heater: #2f3337;
  --clear: #c0392b;
  --intake: #2e86c1;
  --exhaust: #e67e22;
  --ok: #2f7d4f;
  --warn: #b7791f;
  --bad: #c0392b;
  --radius: 8px;
  --font: 'General Sans', 'Helvetica Neue', Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: var(--text-base);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

button, input, select, textarea { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.brand-logo { width: 42px; height: 42px; object-fit: contain; }
.brand-title { font-weight: 600; font-size: var(--text-lg); color: var(--navy); line-height: 1.1; }
.brand-sub { font-size: var(--text-xs); color: var(--muted); }
.project-fields { display: flex; gap: 12px; flex: 1; min-width: 0; }
.project-fields label { display: flex; flex-direction: column; font-size: var(--text-xs); color: var(--muted); gap: 2px; flex: 1; min-width: 120px; }
.project-fields label.wide { flex: 2; }
.top-actions { display: flex; gap: 8px; flex-shrink: 0; }

input[type=text], input[type=number], select, textarea, output {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  background: #fff;
  font-size: var(--text-sm);
  width: 100%;
}
output { display: block; background: var(--navy-50); border-color: transparent; color: var(--navy); font-weight: 500; }
input[type=text]:focus, input[type=number]:focus, select:focus, textarea:focus { outline: 2px solid var(--navy); outline-offset: -1px; border-color: var(--navy); }
textarea { resize: vertical; line-height: 1.45; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  padding: 7px 14px;
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background 150ms ease, border-color 150ms ease;
  white-space: nowrap;
}
.btn:hover { background: var(--navy-50); border-color: #c5cdd8; }
.btn.primary { background: var(--navy); color: #fff; border-color: var(--navy); }
.btn.primary:hover { background: var(--navy-600); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: var(--text-xs); width: 100%; }
.btn.danger { color: var(--bad); }
.btn.danger:hover { background: #fbeaea; border-color: #efc2bd; }
.btn:focus-visible { outline: 2px solid var(--navy); outline-offset: 2px; }

/* Tabs */
.tabs {
  display: flex; gap: 4px; align-items: flex-end;
  padding: 10px 20px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.tab {
  border: 1px solid transparent; border-bottom: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px 8px 0 0;
  font-size: var(--text-sm);
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab.active { background: var(--surface); border-color: var(--line); color: var(--navy); font-weight: 600; margin-bottom: -1px; padding-bottom: 9px; }
.tab-actions { margin-left: auto; display: flex; gap: 6px; padding-bottom: 6px; }
.tab-actions .btn { padding: 5px 10px; font-size: var(--text-xs); }

/* Workspace */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 290px minmax(0, 1fr) 290px;
  min-height: 0;
}
.panel {
  background: var(--surface);
  overflow-y: auto;
  padding: 4px 16px 24px;
}
.panel.left { border-right: 1px solid var(--line); }
.panel.right { border-left: 1px solid var(--line); }
.panel section { padding: 14px 0; border-bottom: 1px solid var(--line); }
.panel section:last-child { border-bottom: none; }
.panel h2 { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.08em; color: var(--navy); margin: 0 0 10px; font-weight: 600; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }
.grid2 label, .sel-grid label { display: flex; flex-direction: column; gap: 3px; font-size: var(--text-xs); color: var(--muted); }
.span2 { grid-column: span 2; }
.custom { margin-top: 10px; padding: 10px; background: var(--navy-50); border-radius: 6px; }
.hint { font-size: var(--text-xs); color: var(--muted); margin: 8px 0 0; line-height: 1.45; }
.checks-inline { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 10px; font-size: var(--text-xs); color: var(--muted); }
.checks-inline span { width: 100%; }
.checks-inline label { display: flex; gap: 4px; align-items: center; color: var(--ink); cursor: pointer; }

.add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.btn.add { display: flex; align-items: center; gap: 8px; text-align: left; padding: 8px 10px; }
.sw { display: inline-block; width: 16px; height: 12px; border-radius: 2px; flex-shrink: 0; }
.sw.upper { background: var(--upper); border: 1px solid var(--slat); }
.sw.lower { background: var(--lower); border: 1px solid var(--slat); }
.sw.intake { background: var(--intake); }
.sw.exhaust { background: var(--exhaust); }
.sw.heater { background: var(--heater); border-radius: 50%; width: 12px; }
.sw.clear { background: #fbeaea; border: 1px dashed var(--clear); }
.sw.label { width: 16px; height: 16px; font-size: 11px; font-weight: 700; color: var(--navy); background: var(--navy-50); text-align: center; line-height: 16px; }

/* Stage */
.stage { display: flex; flex-direction: column; min-width: 0; padding: 14px 20px; gap: 10px; }
.stage-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.layout-name {
  border: 1px solid transparent !important; background: transparent !important;
  font-size: var(--text-lg) !important; font-weight: 600; color: var(--navy);
  padding: 2px 6px !important; margin-left: -6px; width: min(460px, 100%) !important;
}
.layout-name:hover { border-color: var(--line) !important; }
.stage-sub { font-size: var(--text-xs); color: var(--muted); margin-top: 2px; }
.stage-tools { display: flex; align-items: center; gap: 14px; flex-shrink: 0; }
.stage-head { flex-wrap: wrap; }
.item-list { display: flex; flex-direction: column; gap: 4px; }
.item-list button { text-align: left; white-space: normal; display: flex; gap: 8px; align-items: center; }
.item-list button.active { background: var(--navy-50); border-color: var(--navy); }
#heaterSource a { color: var(--navy); }
.print-warning { color: #8f241a; border-left: 3px solid #c0392b; padding-left: 8px; }
.view-toggles { display: flex; gap: 14px; font-size: var(--text-xs); color: var(--muted); flex-shrink: 0; padding-top: 6px; }
.view-toggles label { display: flex; gap: 5px; align-items: center; cursor: pointer; }
.remove-selected { padding: 6px 10px; font-size: var(--text-xs); }
.remove-selected:disabled { opacity: 0.45; cursor: not-allowed; }
.remove-help { margin-top: 10px; padding: 9px 10px; border-radius: 6px; background: #f7f8fa; color: var(--muted); font-size: var(--text-xs); line-height: 1.45; }
.canvas-wrap {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-height: 420px;
  position: relative;
  overflow: hidden;
}
#plan { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
.view-mode { display:flex; gap:8px; align-items:center; flex-wrap:wrap; }
.view-mode .btn { min-height:44px; }
.view-mode .hint { margin:0; }
#threePanel { flex:1; min-height:420px; display:flex; flex-direction:column; gap:8px; }
.three-toolbar { display:flex; flex-wrap:wrap; gap:8px; align-items:center; }
.three-toolbar .btn { min-height:44px; padding:6px 10px; }
.three-toolbar label { display:flex; align-items:center; gap:4px; font-size:var(--text-xs); min-height:44px; }
#threeViewport { position:relative; flex:1; min-height:380px; height:480px; border:1px solid var(--line); border-radius:var(--radius); overflow:hidden; background:#f5f2ec; }
#threeViewport canvas { display:block; width:100%; height:100%; touch-action:none; }
#threeStatus { position:absolute; inset:16px; font-size:var(--text-sm); color:var(--ink); z-index:1; pointer-events:none; }
.three-caption { margin:0; }
#threeSaveStatus { min-height:2.9em; margin:0; }
#threeDownload { text-decoration:none; }
#cOpeningsPanel > label, #cOpeningEditor > label { margin-top:8px; }
#cOpeningMessage, #cDeckMessage { color:var(--navy); }
#cOpeningEditor { margin-top:8px; padding-top:8px; border-top:1px solid var(--line); }
#cSummary { color:var(--navy); }
#hAngleWrap label { display:flex; flex-direction:column; gap:4px; font-size:var(--text-xs); color:var(--muted); }
#hTurn { margin-top:8px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: var(--text-xs); color: var(--muted); }
.legend span { display: flex; align-items: center; gap: 6px; }

/* SVG element styles */
#plan .item { cursor: move; }
#plan .wallitem { cursor: ew-resize; }
#plan .wallitem.vert { cursor: ns-resize; }
#plan .handle { cursor: pointer; }
#plan .handle.h { cursor: ew-resize; }
#plan .handle.v { cursor: ns-resize; }

/* Selection panel */
.sel-empty { font-size: var(--text-sm); color: var(--muted); line-height: 1.5; }
.sel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }
.sel-type { font-weight: 600; font-size: var(--text-sm); margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
.sel-actions { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.sel-actions .btn { flex: 1; padding: 6px 8px; font-size: var(--text-xs); }

.checks { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.checks li { font-size: var(--text-xs); line-height: 1.45; padding: 8px 10px 8px 28px; border-radius: 6px; position: relative; }
.checks li::before { content: ''; position: absolute; left: 10px; top: 12px; width: 8px; height: 8px; border-radius: 50%; }
.checks li.ok { background: #edf6f0; color: #1f5135; }
.checks li.ok::before { background: var(--ok); }
.checks li.warn { background: #fdf5e6; color: #6b4711; }
.checks li.warn::before { background: var(--warn); }
.checks li.bad { background: #fbeaea; color: #7a1f16; }
.checks li.bad::before { background: var(--bad); }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(20, 28, 38, 0.45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal[hidden] { display: none; }
.modal-card { background: #fff; border-radius: 10px; padding: 22px; width: min(640px, 100%); box-shadow: 0 20px 50px rgba(0,0,0,0.2); }
.modal-card.small { width: min(420px, 100%); }
.modal-card h2 { margin: 0 0 8px; font-size: var(--text-lg); color: var(--navy); }
.modal-card p { font-size: var(--text-sm); color: var(--muted); line-height: 1.5; margin: 0 0 12px; }
.modal-card textarea { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 14px; }
.code-msg { margin-right: auto; font-size: var(--text-xs); color: var(--ok); }
.code-msg.err { color: var(--bad); }
.save-reminder { padding: 8px 20px; background: #f7f3ec; color: #51483e; font-size: 12px; border-bottom: 1px solid var(--line); }
.file-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.file-actions a { text-decoration: none; }
.file-actions [hidden] { display: none; }
.template-add { margin-top: 10px; width: 100%; }
.outline-tools { border-top: 1px solid var(--line); padding-top: 12px; display: grid; gap: 8px; }
#shapeEditor { display: grid; gap: 10px; margin-top: 8px; }
.angle-tools { display: flex; flex-wrap: wrap; gap: 6px; }
.clearance-notice { padding: 8px 12px; margin: 0; font-size: var(--text-xs); background: #fff4de; color: #654213; border-radius: 5px; }
.outline-handle { cursor: move; }
.view-toggles { flex-wrap: wrap; }
#shapeEdit[aria-pressed="true"] { background: var(--navy); color: white; }
.outline-tools input, .outline-tools select { min-width: 0; }
.wallitem.free { cursor: move; }
.bench-wall-tools { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 12px; display: grid; gap: 8px; }
#tClearance { font-size: 12px; padding: 6px 9px; }
.bench-toolbar { display:flex; gap:8px; flex-wrap:wrap; padding: 10px 0; }
.bench-toolbar .btn { font-size:12px; }
.corner-picker { display:grid; grid-template-columns:1fr 1fr; gap:10px; margin:12px 0; }
.corner-picker label { display:flex; align-items:center; gap:6px; font-size:12px; }
#wallBenchSection, #adaptiveSection { border-top:1px solid var(--line); margin-top:16px; padding-top:16px; }
#wallBenchMsg { color:var(--navy); margin-top:12px; }
#cornerModal label { margin-top:12px; }
#cornerMsg, #roomActionStatus { color:var(--navy); }
#roomActionStatus { margin:0 0 8px; font-size:12px; }
.modal-card { max-height: 90vh; overflow-y: auto; }
.modal-card summary { cursor: pointer; font-size: 14px; margin: 12px 0; }
.modal-actions { flex-wrap: wrap; }
.modal-card .code-msg { overflow-wrap: anywhere; }
.radio { display: flex; gap: 8px; align-items: center; font-size: var(--text-sm); padding: 4px 0; cursor: pointer; }

#printArea { display: none; }

/* Responsive */
@media (max-width: 1180px) {
  .workspace { grid-template-columns: 260px minmax(0, 1fr); }
  .panel.right { grid-column: 1 / -1; border-left: none; border-top: 1px solid var(--line); display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 20px; }
  .panel.right section { border-bottom: none; }
}
@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .project-fields { order: 3; width: 100%; flex-basis: 100%; }
  .workspace { display: flex; flex-direction: column; }
  .panel { overflow: visible; }
  .stage { order: -1; padding: 12px; }
  .panel.left { border-right: none; border-bottom: 1px solid var(--line); }
  .panel.right { grid-template-columns: 1fr; }
  .canvas-wrap { min-height: 360px; }
  .stage-head { flex-direction: column; }
  .stage-tools { width: 100%; justify-content: space-between; flex-wrap: wrap; }
}

/* Print */
@media print {
  @page { size: letter portrait; margin: 0.5in; }
  body { background: #fff; display: block; }
  .topbar, .tabs, .workspace, .modal, .save-reminder { display: none !important; }
  #printArea { display: block; }
  .print-page { page-break-after: always; break-after: page; font-family: var(--font); color: #1d232b; }
  .print-page:last-child { page-break-after: auto; break-after: auto; }
  .ph { display: flex; align-items: center; gap: 12px; border-bottom: 1px solid #d5d9df; padding-bottom: 10px; margin-bottom: 12px; }
  .ph img { width: 54px; height: 54px; object-fit: contain; }
  .ph h1 { font-size: 18pt; margin: 0; color: var(--navy); }
  .ph .sub { font-size: 9.5pt; color: #5b6573; margin-top: 2px; }
  .pplan svg { width: 100%; height: auto; max-height: 5.2in; display: block; }
  .plegend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 8.5pt; color: #333; margin: 8px 0 10px; }
  .plegend span { display: flex; align-items: center; gap: 5px; }
  .plegend i { display: inline-block; width: 14px; height: 9px; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .pnotes h3 { font-size: 10.5pt; color: var(--navy); margin: 8px 0 4px; }
  .pnotes p, .pnotes li { font-size: 9.5pt; line-height: 1.45; margin: 0 0 4px; }
  .pnotes ul { margin: 0; padding-left: 16px; }
  .pnotes .manual-print { font-size: 7pt; overflow-wrap: anywhere; margin-top: 6px; }
  .pfoot { font-size: 8pt; color: #5b6573; border-top: 1px solid #d5d9df; padding-top: 6px; margin-top: 10px; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* Compact address control and online status row. */
.project-address { display: flex; flex-direction: column; flex: 2; min-width: 0; gap: 2px; }
.project-fields .project-address > label { flex: none; min-width: 0; }
.address-control { display: flex; align-items: center; gap: 4px; min-width: 0; }
.address-control input { flex: 1; min-width: 0; }
.address-control .icon-button { display: inline-flex; align-items: center; justify-content: center; flex: 0 0 30px; width: 30px; height: 30px; padding: 0; border-color: transparent; }
.icon-button svg { display: block; pointer-events: none; }
.cloud-bar { flex-wrap: wrap; }
#cloudStatus { flex: 1 1 220px; min-width: 0; overflow-wrap: anywhere; }
.cloud-bar .btn { width: auto; flex: 0 0 auto; }


.cloud-bar { display:flex; align-items:center; gap:12px; }
.cloud-bar.error { color:var(--bad); }

.choice-bar{display:flex;align-items:center;gap:10px;margin:0 18px;color:var(--muted)}
.choice-bar:not(:has(:not([hidden]))){display:none}
#lockedStatus{font-size:.85rem}
