/* ============================================================
   Layout — app shell. A strict CSS grid so panels NEVER overlap
   the canvas. The stage gets whatever space is left over.
   ============================================================ */

#app {
  display: grid;
  height: 100vh;
  grid-template-columns: var(--rail-w) var(--panel-left-w) 1fr var(--panel-right-w);
  grid-template-rows: var(--topbar-h) 1fr var(--statusbar-h);
  grid-template-areas:
    "topbar topbar topbar topbar"
    "rail   left   stage  right"
    "status status status status";
  background: var(--bg);
  transition: grid-template-columns var(--t-med) var(--ease);
}

/* Collapsed panels: column shrinks to 0, content slides away. No overlap. */
#app.left-collapsed  { grid-template-columns: var(--rail-w) 0 1fr var(--panel-right-w); }
#app.right-collapsed { grid-template-columns: var(--rail-w) var(--panel-left-w) 1fr 0; }
#app.left-collapsed.right-collapsed { grid-template-columns: var(--rail-w) 0 1fr 0; }

/* ---------- Topbar ---------- */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: 0 var(--s-4);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #5b8cff);
  display: grid; place-items: center;
  box-shadow: 0 4px 12px var(--accent-glow);
}
.brand-mark svg { width: 18px; height: 18px; color: #fff; }
.brand-name { font-weight: 700; font-size: var(--fs-lg); letter-spacing: -0.02em; }
.brand-name span { color: var(--accent); }
.topbar .doc-name {
  font-size: var(--fs-sm); color: var(--ink-2);
  padding: 5px 10px; border-radius: var(--r-sm);
  border: 1px solid transparent;
}
.topbar .doc-name:hover { border-color: var(--line); background: var(--surface-2); }

/* ---------- Left rail (tool tabs) ---------- */
.rail {
  grid-area: rail;
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  align-items: center;
  padding: var(--s-3) 0;
  gap: 6px;
  z-index: 20;
}
.rail-btn {
  width: 42px; height: 42px;
  border: none; background: transparent;
  border-radius: var(--r-sm);
  color: var(--ink-3);
  display: grid; place-items: center;
  position: relative;
  transition: all var(--t-fast);
}
.rail-btn svg { width: 21px; height: 21px; stroke-width: 1.8; }
.rail-btn:hover { background: var(--surface-2); color: var(--ink); }
.rail-btn.on { background: var(--accent-soft); color: var(--accent); }
.rail-btn.on::before {
  content: ""; position: absolute; left: -9px; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 22px; border-radius: 99px; background: var(--accent);
}
.rail-btn[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute; left: calc(100% + 10px); top: 50%;
  transform: translateY(-50%);
  background: var(--ink); color: #fff;
  padding: 5px 9px; border-radius: 6px;
  font-size: var(--fs-xs); white-space: nowrap;
  box-shadow: var(--sh-2); z-index: 100; pointer-events: none;
}
.rail-spacer { flex: 1; }

/* ---------- Side panels ---------- */
.panel {
  background: var(--surface);
  display: flex; flex-direction: column;
  min-width: 0;
  overflow: hidden;
  z-index: 15;
}
.panel.left  { grid-area: left;  border-right: 1px solid var(--line); }
.panel.right { grid-area: right; border-left: 1px solid var(--line); }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 var(--s-3) 0 var(--s-4);
  height: 46px; flex-shrink: 0;
  border-bottom: 1px solid var(--line);
}
.panel-head h3 { font-size: var(--fs-md); font-weight: 640; }
.panel-body { padding: var(--s-4); flex: 1; }

.panel-collapse {
  width: 28px; height: 28px; border: none; background: transparent;
  border-radius: 6px; color: var(--ink-3); display: grid; place-items: center;
}
.panel-collapse:hover { background: var(--surface-2); color: var(--ink); }
.panel-collapse svg { width: 17px; height: 17px; }

/* ---------- Stage ---------- */
.stage {
  grid-area: stage;
  position: relative;
  background:
    radial-gradient(circle at 1px 1px, var(--bg-grid) 1.4px, transparent 0);
  background-size: 22px 22px;
  background-color: var(--bg);
  overflow: hidden;
  min-width: 0;
}
#canvas2d, #canvas3d { position: absolute; inset: 0; width: 100%; height: 100%; }
#canvas3d { display: none; }
.stage.mode-3d #canvas2d { display: none; }
.stage.mode-3d #canvas3d { display: block; }

/* Floating stage controls live INSIDE the stage, never over panels */
.stage-tools {
  position: absolute; top: var(--s-3); left: 50%;
  transform: translateX(-50%);
  display: flex; gap: var(--s-2);
  padding: 6px; border-radius: var(--r-md);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(10px) saturate(1.4);
  border: 1px solid var(--line);
  box-shadow: var(--sh-2);
  z-index: 12;
}
.view-cube {
  position: absolute; top: var(--s-3); right: var(--s-3);
  z-index: 12;
}
.zoom-cluster {
  position: absolute; bottom: var(--s-4); right: var(--s-4);
  display: flex; flex-direction: column; gap: 4px;
  padding: 5px; border-radius: var(--r-md);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line); box-shadow: var(--sh-2);
  z-index: 12;
}
.scale-readout {
  position: absolute; bottom: var(--s-4); left: var(--s-4);
  z-index: 12;
  display: flex; align-items: center; gap: 10px;
  padding: 7px 12px; border-radius: var(--r-pill);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line); box-shadow: var(--sh-1);
  font-size: var(--fs-xs); color: var(--ink-2); font-family: var(--mono);
}

/* ---------- Status bar ---------- */
.statusbar {
  grid-area: status;
  display: flex; align-items: center; gap: var(--s-4);
  padding: 0 var(--s-4);
  background: var(--surface);
  border-top: 1px solid var(--line);
  font-size: var(--fs-xs); color: var(--ink-3);
  z-index: 20;
}
.statusbar .sb-item { display: flex; align-items: center; gap: 6px; }
.statusbar .sb-item b { color: var(--ink-2); font-weight: 600; }
.statusbar .grow { flex: 1; }

/* ---------- Mobile bottom sheet toggle (hidden on desktop) ---------- */
.mobile-bar { display: none; }
.scrim { display: none; }
