/* Shared diagram actions sit above the drawing. */
.stage-heading > .diagram-topbar { display:flex; align-items:center; justify-content:space-between; flex:1 0 100%; gap:12px; padding:0; border:0; background:transparent; }
.diagram-view-actions { display:flex; align-items:center; gap:18px; }
.diagram-view-actions .text-button { display:inline-flex; align-items:center; gap:6px; font-size:12px; min-height:40px; }
/* Mobile workflow, scoped away from the desktop workspace. */
.mobile-wall-interaction { display:none; }
@media(max-width:1220px) {
  main { padding:14px 0 0; }
  .stage { border:0; border-radius:0;  }
  .site-header { padding:15px 16px; }
  .stage-heading { padding:13px 14px; gap:10px; flex-wrap:wrap; }
  .stage-heading h1 { font-size:17px; line-height:1.25; letter-spacing:-.35px; max-width:none; margin:0; }
  .stage-heading > div:first-child { flex:1; }
  .stage-heading .stage-actions { width:auto; flex:none; align-self:flex-start; margin:0; }
  .stage-summary { display:none; }
  .unit-toggle button { font-size:10px; padding:6px 8px; }
  .stage > .canvas-viewport { width:100%; height:auto; aspect-ratio:173 / 139; padding:0; overflow:hidden; }
  .stage-heading > .diagram-topbar { padding:0; }
  .diagram-view-actions { gap:16px; }
  .mobile-wall-interaction { display:block; padding:0 12px 12px; background:var(--card); }
  #mobile-select-hint { font-size:11px; color:var(--muted); text-align:center; line-height:1.5; padding:12px 0; margin:0; }
  .mobile-scale-note { padding:10px 12px; margin:12px 0 0; background:#f4eee4; border-left:2px solid #a18b66; border-radius:0 4px 4px 0; color:#67583e; font-size:12px; line-height:1.6; }
  #mobile-selection-bar { display:flex; align-items:center; gap:9px; margin:12px 0; padding:12px 10px; background:#edf1e9; border:1px solid #c8d3c2; border-radius:7px; min-height:67px; }
  .mobile-selection-copy { flex:1; min-width:0; }
  .mobile-selection-copy strong { display:block; font-size:13px; font-weight:500; overflow-wrap:anywhere; }
  .mobile-selection-copy span { display:block; font-size:11px; line-height:1.5; color:#59645d; margin-top:4px; }
  #mobile-measurements-open { font-size:12px; padding:10px 12px; min-height:42px; background:var(--card); }
  #mobile-deselect { width:30px; height:42px; flex:none; border:0; padding:0; background:transparent; font-size:23px; color:#59645d; }
  .mobile-setup-actions { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:8px; }
  .mobile-setup-actions button { min-height:46px; padding:10px 3px; border:1px solid #bfc7c2; border-radius:6px; background:var(--card); font-size:12px; font-weight:500; }
  .mobile-setup-actions button:active { background:var(--soft); }
  .canvas-caption { padding:5px 12px; }
  .canvas-caption > #wall-caption { display:none; }
  .canvas-actions { width:100%; justify-content:space-between; gap:7px; }
  .canvas-actions .history-actions { gap:2px; padding-right:9px; }
  .canvas-actions .history-actions button { min-width:34px; min-height:42px; }
  .canvas-actions > .text-button { min-height:42px; font-size:11px; padding:7px 0; }
  .stage #wall-svg { touch-action:pan-y; }
  /* Arm touch editing on the SVG and its HTML host before the next gesture.
     Setting this only after pointerdown is too late for browser scroll intent. */
  .stage #wall-svg[data-editing="true"],.stage #canvas-viewport[data-editing="true"] { touch-action:none; }
  #wall-svg [data-frame]:not(.frame-selected) .frame-hit,
  #wall-svg [data-object]:not(.fixture-selected) .fixture-hit { touch-action:pan-y; }
  #wall-svg .frame-selected .frame-hit,#wall-svg .fixture-selected .fixture-hit { touch-action:none; }
  #wall-svg .resize-hit { touch-action:none; cursor:nwse-resize; }
  #wall-svg .frame-resize-controls,#wall-svg .resize-handle { touch-action:none; }
  .mobile-sheet-heading { display:flex; align-items:center; justify-content:space-between; gap:15px; padding:0 0 18px; margin:0 0 20px; border-bottom:1px solid var(--line); }
  .mobile-sheet-heading h2 { font-size:25px; font-weight:400; letter-spacing:-.6px; margin:0; }
  .mobile-sheet-heading .eyebrow { margin:0 0 7px; font-size:9px; letter-spacing:1px; }
  .mobile-sheet-heading .button { min-height:44px; font-size:12px; padding:10px 13px; flex:none; }
  /* A viewport host and a separately positioned sheet prevent keyboard
     resizing from moving the field under a user's finger. */
  #mobile-measurements,#mobile-setup { position:fixed; inset:0 auto auto 0; top:var(--sheet-viewport-top,0px); width:100%; max-width:none; height:var(--sheet-viewport-height,100dvh); max-height:none; margin:0; padding:0; border:0; border-radius:0; background:#28312866; overflow:hidden; box-shadow:none; }
  #mobile-setup { --sheet-top:14vh; }
  .mobile-sheet-panel { position:absolute; inset:var(--sheet-top,14vh) 0 0; display:flex; flex-direction:column; min-height:0; padding:0 16px max(16px,env(safe-area-inset-bottom)); border-radius:18px 18px 0 0; background:var(--paper); box-shadow:0 -8px 40px #28312820; overflow:hidden; }
  .sheet-dismiss-area { position:absolute; inset:0 0 auto; height:var(--sheet-top,14vh); width:100%; padding:0; border:0; border-radius:0; background:transparent; }
  .sheet-grip { flex:none; display:flex; align-items:center; justify-content:center; width:100%; height:32px; padding:0; border:0; border-radius:0; background:transparent; touch-action:none; cursor:grab; }
  .sheet-grip span { width:36px; height:4px; border-radius:4px; background:#a9b5ad; }
  .sheet-grip:active { cursor:grabbing; }
  .mobile-sheet-heading { touch-action:none; user-select:none; }
  .sheet-close { display:grid; place-items:center; flex:none; width:44px; height:44px; padding:0; border:0; border-radius:50%; background:transparent; color:#526057; }
  .sheet-close:active { background:#e0e6df; }

  #mobile-measurements-body,#mobile-setup-body { flex:1; min-height:0; overflow-y:auto; overscroll-behavior:contain; -webkit-overflow-scrolling:touch; scroll-padding:16px 0 24px; }
  .mobile-sheet-panel > .mobile-sheet-heading { flex:none; position:static; margin:0 0 16px; padding:0 0 16px; background:var(--paper); }
  #mobile-measurements-title,#mobile-setup-title { outline:none; }
  #mobile-measurements .inspector { display:block!important; padding:0; background:transparent; border:0; }
  #mobile-measurements .inspector-heading { margin:0 0 16px; padding:0; border:0; }
  #mobile-measurements .inspector h2 { font-size:19px; letter-spacing:-.3px; }
  #mobile-measurements .selection-mode { margin:0 0 2px; }
  #mobile-measurements #selection-fields { padding:18px 15px; }
  #mobile-measurements input,#mobile-measurements select { font-size:17px; scroll-margin-top:20px; }
  #mobile-measurements input[type=checkbox] { min-height:0; }
  #mobile-measurements .selection-actions .button { min-height:44px; }
  #mobile-setup .controls { display:block!important; position:static; max-height:none; overflow:visible; background:transparent; border:0; }
  #mobile-setup .panel { padding:0; }
  #mobile-setup .tabs { position:sticky; top:0; z-index:2; background:var(--paper); margin-bottom:16px; padding-top:0; }
  /* Only setup sheets need to recover space from their extra navigation/header.
     Measurements retains its existing keyboard layout. */
  #mobile-setup .tabs { position:static; }
  #mobile-setup .editor-group { padding:20px 18px; }
  #mobile-measurements::backdrop,#mobile-setup::backdrop { background:transparent; backdrop-filter:none; }
  #toast { bottom:max(20px,env(safe-area-inset-bottom)); }
}
@media(prefers-reduced-motion:reduce) {
  #mobile-measurements,#mobile-setup { transition:none; scroll-behavior:auto; }
}
@media print {
  .mobile-wall-interaction,#mobile-measurements,#mobile-setup { display:none!important; }
}

/* History belongs with the existing unit controls, without a toolbar band. */
.stage-heading .stage-actions { display:flex; align-items:center; gap:10px; }
.stage-heading .stage-actions .history-actions { display:flex; align-items:center; gap:2px; }
.stage-heading .stage-actions .history-actions button { width:30px; min-width:30px; min-height:34px; padding:0; font-size:20px; }
@media(max-width:1220px) {
  .stage-heading .stage-actions { gap:6px; }
  .stage-heading .stage-actions .history-actions button { width:28px; min-width:28px; }
  .stage-heading > .diagram-topbar { justify-content:flex-end; }
}

/* Reuse the board-and-batten mobile stage header arrangement. */
.header-unit-controls { display:contents; }
@media(max-width:1220px) {
  .stage-heading { padding:17px 16px; gap:14px; }
  .stage-heading > div:first-child { flex:1 0 100%; }
  .stage-heading > .stage-actions { display:flex; flex:1 0 100%; width:100%; justify-content:space-between; align-items:center; gap:8px; margin:0; }
  .header-unit-controls { display:flex; align-items:center; gap:3px; }
  .stage-heading .mobile-header-button { display:inline-flex; align-items:center; gap:6px; min-height:40px; padding:8px 9px; font-size:10px; font-weight:400; border:1px solid var(--line); border-radius:24px; background:var(--card); white-space:nowrap; }
  .stage-heading .stage-actions .history-actions { gap:0; }
  .stage-heading .stage-actions .history-actions button { width:24px; min-width:24px; min-height:40px; font-size:18px; }
}

@media(max-width:1220px) { .stage-heading #reset { width:auto; flex:none; } }

@media(max-width:1220px) {
  .stage-heading .unit-toggle { display:flex; flex-shrink:0; padding:3px; background:#fafbfa; border-radius:7px; }
  .stage-heading .unit-toggle button { min-height:44px; padding:7px 9px; border:0; border-radius:5px; font-size:11px; font-weight:500; color:var(--muted); }
  .stage-heading .unit-toggle button.active { background:var(--soft); color:#424c3d; box-shadow:none; }
  .stage-heading .stage-actions .history-actions button { min-height:44px; font-size:20px; }
}

@media(max-width:1220px) {
  .stage-heading > div:first-child { flex:1; min-width:0; }
  .stage-heading > .header-unit-controls { display:flex; flex:none; align-items:center; gap:3px; }
  .stage-heading > .header-unit-controls .history-actions { display:flex; align-items:center; gap:0; }
  .stage-heading > .header-unit-controls .history-actions button { width:24px; min-width:24px; min-height:44px; padding:0; font-size:20px; }
}

/* Use the existing outlined header button treatment for named history actions. */
.stage-heading .stage-actions .history-actions { display:flex; align-items:center; gap:4px; }
.stage-heading .stage-actions .history-actions button {
  display:inline-flex; align-items:center; justify-content:center; gap:4px;
  width:auto; min-width:0; min-height:40px; padding:8px 7px;
  border:1px solid var(--line); border-radius:24px; background:var(--card);
  font:500 10px Arial,Helvetica,sans-serif; line-height:normal;
}
@media(max-width:1220px) {
  .stage-heading .mobile-header-button { font:500 10px Arial,Helvetica,sans-serif; line-height:normal; }
}

@media(max-width:360px) { .stage-heading .history-actions svg { display:none; } }

@media(max-width:1220px) {
  .stage-heading #expand-diagram {
    display:inline-flex; align-items:center; justify-content:center; gap:6px;
    flex:0 0 auto; width:auto; min-width:73.53px; height:40px; min-height:40px;
    padding:8px 9px; font:500 10px Arial,Helvetica,sans-serif; line-height:normal;
    border:1px solid #c5ceca; border-radius:24px; background:#fafbfa;
    color:#282e2d; white-space:nowrap;
  }
  .stage-heading #expand-diagram svg { flex:none; width:14px; height:14px; }
}

.stage-heading #undo,.stage-heading #redo {
  display:inline-flex; align-items:center; justify-content:center;
  flex:0 0 40px; width:40px; min-width:40px; height:40px; min-height:40px;
  padding:0; border:1px solid #c5ceca; border-radius:24px;
  background:#fafbfa; color:#282e2d; font-size:0; line-height:1;
}
.stage-heading #undo svg,.stage-heading #redo svg { display:block; flex:none; width:16px; height:16px; }

@media(min-width:1221px) {
  .stage-heading #expand-diagram {
    display:inline-flex; align-items:center; justify-content:center; gap:6px;
    flex:0 0 auto; width:auto; min-width:73.53px; height:40px; min-height:40px;
    padding:8px 9px; font:500 10px Arial,Helvetica,sans-serif; line-height:normal;
    border:1px solid #c5ceca; border-radius:24px; background:#fafbfa;
    color:#282e2d; white-space:nowrap;
  }
  .stage-heading #expand-diagram svg { flex:none; width:14px; height:14px; }
}
.canvas-caption:has(.canvas-actions:empty) { display:none; }

@media(min-width:1221px) {
  .stage { position:relative; }
  .stage > #expand-diagram {
    position:absolute; right:20px; z-index:2;
    display:inline-flex; align-items:center; justify-content:center; gap:6px;
    height:40px; min-height:40px; padding:8px 9px;
    border:1px solid #c5ceca; border-radius:24px; background:#fafbfa;
    color:#282e2d; font:500 10px Arial,Helvetica,sans-serif; line-height:normal;
    white-space:nowrap;
  }
  .stage > #expand-diagram svg { flex:none; width:14px; height:14px; }
}
@media(max-width:360px) {
  .stage-heading #undo,.stage-heading #redo { flex-basis:34px; width:34px; min-width:34px; }
  .stage-heading > .stage-actions { gap:6px; }
  .stage-heading #reset { padding-left:6px; padding-right:6px; font-size:9px; }
}

@media(min-width:1221px) {
  .stage-heading > .desktop-title-actions { display:flex; align-items:center; gap:12px; min-width:0; flex:1; }
  .desktop-title-actions h1 { margin:0; min-width:0; }
  .desktop-title-actions #expand-diagram { position:static; flex:none; }
}

@media(min-width:1221px) {
  .stage > #expand-diagram { top:auto; bottom:16px; right:16px; }
}

/* Use the existing stacked stage header when the desktop has three columns. */
@media(min-width:1221px) {
  .stage-heading { flex-wrap:wrap; gap:14px; }
  .stage-heading > div:first-child { flex:1 0 100%; }
  .stage-heading > .stage-actions { width:100%; justify-content:space-between; }
}
@media(max-width:1220px) {
  .stage-heading #expand-diagram,.stage-heading #reset { flex:0 0 108px; width:108px; min-width:108px; height:40px; min-height:40px; padding:8px 9px; font:500 10px Arial,Helvetica,sans-serif; line-height:normal; }
}
@media(max-width:360px) {
  .stage-heading #expand-diagram,.stage-heading #reset { flex-basis:90px; width:90px; min-width:90px; padding:8px 4px; }
  .stage-heading #expand-diagram { white-space:normal; }
}
#diagram-dialog .canvas-viewport { display:block; height:100%; }
.diagram-pan-canvas { display:grid; place-items:center; }
#diagram-dialog .diagram-sheet { margin:0; }
#diagram-dialog[data-mode=view] .canvas-viewport:active { cursor:grabbing; }
#diagram-dialog .canvas-viewport { scrollbar-width:none; user-select:none; }
#diagram-dialog .canvas-viewport::-webkit-scrollbar { display:none; }
#diagram-dialog[data-mode=view] #wall-svg { pointer-events:none; }

/* Compact unit switch beside the title, centred on the same horizontal axis. */
@media(max-width:1220px) {
  .stage-heading { align-items:center; }
  .stage-heading > div:first-child,.stage-heading > .header-unit-controls { align-self:center; }
  .stage-heading > .header-unit-controls > .unit-toggle button { min-height:28px; height:28px; padding:6px 8px; font-size:10px; line-height:normal; }
}

@media(max-width:1220px) {
  .stage-heading { padding-top:4px; }
  .stage-heading h1 { font-size:20px; line-height:1.25; font-weight:500; }
}

@media(max-width:1220px) {
  .stage { border-top:1px solid transparent; }
  .stage-heading { padding:17px 16px 23px; column-gap:12px; row-gap:20px; border-bottom:1px solid transparent; }
  .stage-heading h1 { font-size:17px; line-height:1.3; font-weight:400; letter-spacing:-.4px; }
  .stage-heading > .header-unit-controls > .unit-toggle button { padding:6px 5px; }
  .mobile-wall-interaction { display:flex; flex-direction:column; padding:16px 12px 16px; }
  #mobile-scale-note { order:0; }
  #mobile-selection-bar { order:1; margin:8px 0 12px; }
  .mobile-setup-actions { order:2; }
  #mobile-select-hint { order:3; padding:10px 4px 0; text-align:left; font-size:11px; line-height:1.6; }
}
/* Vaul owns modal lifecycle, scroll locking, dismissal and animation. */
.pencil-sheet-overlay { position:fixed; inset:0; z-index:100; background:rgba(40,49,40,.4); }
.pencil-sheet#mobile-measurements,.pencil-sheet#mobile-setup { position:fixed; inset:auto 0 0; z-index:101; height:86dvh; max-height:calc(100dvh - 12px); width:100%; max-width:none; padding:0; margin:0; border:0; border-radius:18px 18px 0 0; background:var(--paper); display:flex; flex-direction:column; outline:none; overflow:hidden; }
.pencil-sheet > .sheet-grip { margin:0; padding:0; flex:none; width:100%; height:32px; border:0; background:transparent; }
.pencil-sheet > .sheet-grip > span { width:36px; height:4px; background:#a9b5ad; border-radius:4px; }
.pencil-sheet-mount { flex:1; min-height:0; display:flex; }
.pencil-sheet-mount > .mobile-sheet-panel { position:relative; inset:auto; width:100%; height:auto; flex:1; border-radius:0; box-shadow:none; }
.pencil-sheet .mobile-sheet-heading { touch-action:auto; }

@media(max-width:1220px) {
  #mobile-selection-bar { padding:4px 6px 4px 12px; min-height:54px; gap:8px; background:var(--paper); border:1px solid var(--line); border-radius:6px; }
  .mobile-selection-copy strong { white-space:nowrap; overflow:hidden; text-overflow:ellipsis; overflow-wrap:normal; font-size:13px; }
  #mobile-selection-size { display:none; }
  #mobile-measurements-open { display:flex; align-items:center; justify-content:center; gap:6px; min-height:44px; padding:8px 10px; border:0; border-radius:4px; background:transparent; font-size:13px; flex:none; }
  #mobile-deselect { width:40px; height:44px; }
}

@media(max-width:1220px) {
  #mobile-measurements-open { border:1px solid var(--line); border-radius:20px; background:var(--card); padding:8px 14px; font-size:13px; }
  #mobile-measurements-open:active { background:var(--soft); }
  #mobile-selection-bar { gap:10px; }
}

@media(max-width:1220px) {
  #mobile-measurements-open { border:0; border-radius:0; background:transparent; }
}

@media(max-width:1220px) {
  #mobile-measurements-open { white-space:nowrap; padding:8px 6px; font-weight:500; }
  #mobile-selection-bar { gap:6px; }
}

@media(max-width:1220px) {
  #mobile-selection-bar { padding:0; min-height:0; border:0; background:transparent; gap:8px; }
  #mobile-measurements-open { display:flex; align-items:center; justify-content:space-between; flex:1; min-width:0; gap:12px; text-align:left; padding:12px; min-height:68px; border:1px solid var(--line); border-radius:7px; background:var(--card); font-weight:400; }
  #mobile-measurements-open .mobile-selection-copy { flex:1; min-width:0; }
  #mobile-measurements-open .mobile-selection-copy strong { font-size:14px; }
  #mobile-selection-size { display:block; margin-top:3px; font-size:12px; line-height:1.5; color:var(--muted); white-space:normal; }
  #mobile-measurements-open svg { flex:none; color:#59645d; }
  #mobile-deselect { width:40px; height:44px; }
  #mobile-measurements-open:active { background:var(--soft); }
}

@media(max-width:1220px) { #mobile-measurements-open { background:transparent; } }

@media(max-width:1220px) {
  .mobile-setup-actions { display:flex; gap:0; padding:0; overflow:hidden; border:1px solid var(--line); border-radius:6px; background:transparent; }
  .mobile-setup-actions button { flex:1; min-width:0; min-height:48px; margin:0; padding:12px 5px; border:0; border-radius:0; background:transparent; font-size:13px; color:var(--ink); }
  .mobile-setup-actions button + button { border-left:1px solid var(--line); }
  .mobile-setup-actions button:active { background:var(--soft); }
}

@media(max-width:1220px) {
  .mobile-setup-actions { background:#e2e6e3; border-color:#aeb8b1; }
  .mobile-setup-actions button { color:#282e2d; }
  .mobile-setup-actions button + button { border-left-color:#aeb8b1; }
  .mobile-setup-actions button:active { background:#d5ddd6; }
}

@media(max-width:1220px) {
  .mobile-setup-actions { background:transparent; border-color:#6f7973; }
  .mobile-setup-actions button + button { border-left-color:#6f7973; }
  .mobile-setup-actions button:active { background:var(--soft); }
}

@media(max-width:1220px) { #mobile-measurements-open { border-color:#6f7973; } }

@media(min-width:1221px) {
  .stage-heading { display:grid; grid-template-columns:minmax(0,1fr) auto; align-items:center; gap:14px 12px; }
  .stage-heading > div:first-child { min-width:0; }
  .stage-heading > .header-unit-controls { justify-self:end; }
  .stage-heading > .stage-actions { grid-column:1 / -1; display:grid; grid-template-columns:1fr auto 1fr; width:100%; margin:0; gap:12px; align-items:center; }
  .stage-heading .stage-actions > #expand-diagram { justify-self:start; }
  .stage-heading .stage-actions > .history-actions { justify-self:center; }
  .stage-heading .stage-actions > #reset { justify-self:end; }
}

/* A fading-out backdrop must not swallow the next setup-button tap. */
.pencil-sheet-overlay[data-state="closed"] { pointer-events:none!important; }

@media(max-width:1220px) {
  #mobile-measurements-open { min-height:60px; padding:8px 12px; }
}

/* Tablet and narrow-window workflow: the wall stays above its sheet controls. */
@media(min-width:721px) and (max-width:1220px) {
 .stage { max-width:980px; width:100%; margin:0 auto; }
 .stage > .canvas-viewport { max-width:800px; margin:0 auto; }
 .mobile-wall-interaction { max-width:800px; width:100%; margin:0 auto; }
 .stage-heading h1 { font-size:22px; }
 .pencil-sheet#mobile-measurements,.pencil-sheet#mobile-setup { width:min(680px,100%); left:50%; right:auto; translate:-50% 0; }
 .installation { max-width:980px; width:calc(100% - 32px); margin:18px auto 0; }
}

@media(min-width:721px) and (max-width:1220px) and (max-height:600px) {
 .site-header { padding:6px 16px; }
 main { padding-top:4px; }
 .stage-heading { padding:6px 14px; row-gap:6px; }
 .stage-heading .stage-actions { margin-top:4px; }
 .stage-heading h1 { font-size:18px; }
 .mobile-wall-interaction { padding-top:4px; padding-bottom:4px; }
 #mobile-selection-bar { margin:4px 0 6px; }
}
