/* ============================================================
   Responsive — tablet & mobile. Panels become bottom sheets so
   the canvas always owns the full viewport on small screens.
   ============================================================ */

/* ---- Tablet: narrow the panels ---- */
@media (max-width: 1180px) {
  :root {
    --panel-left-w: 224px;
    --panel-right-w: 256px;
  }
}

/* ---- Small laptop: auto-collapse left, keep right ---- */
@media (max-width: 1000px) {
  :root { --panel-right-w: 240px; }
  #app { grid-template-columns: var(--rail-w) 0 1fr var(--panel-right-w); }
}

/* ============================================================
   Mobile (<=760px): single column. Panels slide up as sheets.
   ============================================================ */
@media (max-width: 760px) {
  body { overflow: hidden; }

  #app {
    grid-template-columns: 1fr;
    grid-template-rows: var(--topbar-h) 1fr auto;
    grid-template-areas:
      "topbar"
      "stage"
      "mobilebar";
  }

  .rail { display: none; }
  .statusbar { display: none; }

  /* Panels become full-width bottom sheets */
  .panel {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    top: auto;
    height: 70vh;
    max-height: 70vh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border: 1px solid var(--line);
    box-shadow: var(--sh-pop);
    transform: translateY(100%);
    transition: transform var(--t-med) var(--ease);
    z-index: 120;
  }
  .panel.left  { grid-area: unset; }
  .panel.right { grid-area: unset; }
  .panel.sheet-open { transform: translateY(0); }

  .panel-head {
    height: 52px;
    position: relative;
  }
  .panel-head::before {
    content: ""; position: absolute; top: 8px; left: 50%;
    transform: translateX(-50%);
    width: 40px; height: 4px; border-radius: 99px; background: var(--line-strong);
  }

  /* Scrim behind sheets */
  .scrim {
    display: block; position: fixed; inset: 0;
    background: rgba(15,22,40,0.4); backdrop-filter: blur(2px);
    opacity: 0; pointer-events: none;
    transition: opacity var(--t-med);
    z-index: 110;
  }
  .scrim.on { opacity: 1; pointer-events: auto; }

  /* Mobile bottom action bar */
  .mobile-bar {
    grid-area: mobilebar;
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 62px;
    padding: 0 8px env(safe-area-inset-bottom, 0);
    background: var(--surface);
    border-top: 1px solid var(--line);
    z-index: 100;
  }
  .mob-btn {
    display: flex; flex-direction: column; align-items: center; gap: 3px;
    border: none; background: transparent; color: var(--ink-3);
    font-size: 10px; font-weight: 600; padding: 6px 12px;
    border-radius: var(--r-sm); min-width: 56px;
  }
  .mob-btn svg { width: 22px; height: 22px; stroke-width: 1.8; }
  .mob-btn.on { color: var(--accent); }
  .mob-btn.primary {
    background: var(--accent); color: #fff;
    width: 52px; height: 52px; border-radius: 50%;
    justify-content: center; margin-top: -18px;
    box-shadow: 0 6px 18px var(--accent-glow);
  }
  .mob-btn.primary svg { width: 24px; height: 24px; }

  /* Stage floating tools shrink + reposition */
  .stage-tools { top: 8px; padding: 4px; gap: 4px; }
  .stage-tools .btn { height: 32px; }
  .view-cube { top: 8px; right: 8px; }
  .view-cube .seg button { padding: 5px 8px; font-size: 11px; }
  .zoom-cluster { bottom: 12px; right: 12px; }
  .scale-readout { bottom: 12px; left: 12px; font-size: 10px; padding: 5px 9px; }

  .tile-grid { grid-template-columns: repeat(3, 1fr); }
  .template-grid { grid-template-columns: repeat(2, 1fr); }

  .topbar { gap: 8px; padding: 0 12px; }
  .topbar .doc-name { display: none; }
  .topbar .desktop-only { display: none; }
}

@media (max-width: 420px) {
  .tile-grid { grid-template-columns: repeat(2, 1fr); }
}

/* Touch: larger hit targets */
@media (hover: none) and (pointer: coarse) {
  .btn { height: 40px; }
  .rail-btn { width: 46px; height: 46px; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
