/**
 * Layout Styles
 * Grid layout, sections, header, and responsive structure
 */

main {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  display: grid;
  /* minmax(0,1fr) is required: an auto row grows with content and breaks
     percentage heights + overflow scrolling on the aside. */
  grid-template-columns: var(--aside-width) 1fr;
  grid-template-rows: minmax(0, 1fr);
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
}

/*
 * Control pane — one screen tall.
 *
 * Absolute layout (not flex-height-chain) so scrolling always works when the
 * settings body grows (e.g. after adding scene devices):
 *   .control-pane-body  = scroll viewport between top padding and footer
 *   .download-actions   = pinned footer, never scrolls away
 */
.control-pane {
  --pane-pad-top: 24px;
  --pane-pad-x: 28px;
  --pane-pad-left: 32px;
  --pane-footer-h: 92px;
  --pane-pad-bottom: 16px;

  position: relative;
  box-sizing: border-box;
  border-right: 1px solid var(--border);
  min-width: 0;
  min-height: 0;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  padding: 0;
}

.control-pane-body {
  position: absolute;
  top: var(--pane-pad-top);
  left: var(--pane-pad-left);
  right: var(--pane-pad-x);
  bottom: calc(var(--pane-footer-h) + var(--pane-pad-bottom));
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-right: 6px;
  scrollbar-gutter: stable;
  /* Children must be allowed to shrink/overflow this box, not expand it. */
  min-height: 0;
  max-height: none;
}

.control-pane-body > * {
  flex: 0 0 auto;
}

/* Pinned footer — fixed to the pane bottom, outside the scrollport. */
.control-pane > .download-actions {
  position: absolute;
  left: var(--pane-pad-left);
  right: var(--pane-pad-x);
  bottom: var(--pane-pad-bottom);
  height: var(--pane-footer-h);
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 10px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  z-index: 3;
  background: linear-gradient(
    180deg,
    transparent 0%,
    var(--glass-bg) 28%,
    var(--glass-bg) 100%
  );
}

.control-pane > .download-actions > .dropdown {
  flex: 1 1 0;
  min-width: 0;
}

.control-pane > .download-actions .btn-primary,
.control-pane > .download-actions .btn-ghost {
  width: 100%;
}

/* Resize handle: a thin column-resize strip on the right edge of the
   control-pane. Hover/drag state is shown via body[data-resizing] and
   :hover. Hidden on mobile (max-width: 960px) where the layout stacks. */
.resize-handle {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  background: transparent;
  z-index: 10;
  border-radius: 3px;
  transition: background 0.15s ease;
  touch-action: none;
}

.resize-handle:hover,
body[data-resizing="true"] .resize-handle {
  background: var(--accent);
  opacity: 0.4;
}

body[data-resizing="true"] {
  cursor: col-resize !important;
  user-select: none;
}

.workspace {
  position: relative;
  padding: 28px 48px 48px 48px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  background: var(--bg-workspace);
}

/* Header bar — brand left, view-mode segment center, theme/lang right */
.workspace-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text-primary);
}

.brand:hover .brand-name {
  color: var(--accent);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent-gradient);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  box-shadow: var(--shadow-raise);
}

.brand-name {
  display: block;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
  transition: color 0.2s ease;
}

.brand-sub {
  display: block;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-faint);
  letter-spacing: 0.02em;
}

.workspace-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.canvas-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 24px;
}

.canvas-wrapper {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--surface-elevated);
  border-radius: var(--radius-xl);
  /* Frame shape drives the stage aspect so the preview matches export
     framing. --preview-aspect is width/height (1, 1.777…, 0.5625…). */
  --preview-aspect: 1;
  --preview-max: clamp(200px, 80vh, 80vh);
  width: min(100%, calc(var(--preview-max) * var(--preview-aspect)));
  max-width: 100%;
  min-width: 0;
  height: auto;
  max-height: var(--preview-max);
  aspect-ratio: var(--preview-aspect);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

/* Explicit export shapes — JS also sets --preview-aspect from the ratio. */
.canvas-wrapper[data-frame-shape="16:9"] {
  --preview-aspect: 1.7777777778;
  aspect-ratio: 16 / 9;
}
.canvas-wrapper[data-frame-shape="9:16"] {
  --preview-aspect: 0.5625;
  aspect-ratio: 9 / 16;
}
.canvas-wrapper[data-frame-shape="preview"] {
  --preview-aspect: 1;
  aspect-ratio: 1 / 1;
}
.canvas-wrapper[data-frame-shape="4:5"] {
  --preview-aspect: 0.8;
  aspect-ratio: 4 / 5;
}
.canvas-wrapper[data-frame-shape="4:3"] {
  --preview-aspect: 1.3333333333;
  aspect-ratio: 4 / 3;
}

.canvas-wrapper[data-view-mode="3d"] canvas {
  display: none;
}

.canvas-wrapper[data-view-mode="2d"] model-viewer {
  display: none;
}

/* Scene mode: the single canvas + single viewer are hidden, the scene grid
   takes over the wrapper. */
.canvas-wrapper[data-view-mode="scene"] canvas,
.canvas-wrapper[data-view-mode="scene"] #mockup-model-viewer {
  display: none;
}

.canvas-wrapper[data-view-mode="2d"] .scene-grid,
.canvas-wrapper[data-view-mode="3d"] .scene-grid {
  display: none;
}

/* Frame-shape rules above hardcode aspect-ratio for recording. Scene mode
   must win and follow the grid geometry via --preview-aspect (set by JS). */
.canvas-wrapper[data-view-mode="scene"] {
  width: min(100%, calc(var(--preview-max) * var(--preview-aspect)));
  aspect-ratio: var(--preview-aspect);
}

/* Freeform stage — devices float on the canvas. Cells are invisible hit/box
   helpers only; no frames, no rounded chrome around devices. */
.scene-grid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 0;
  touch-action: none;
}

.scene-cell {
  position: absolute;
  left: calc(var(--x, 0.5) * 100%);
  top: calc(var(--y, 0.5) * 100%);
  width: calc(var(--w, 0.2) * 100%);
  height: calc(var(--h, 0.54) * 100%);
  transform: translate(-50%, -50%);
  cursor: move;
  min-width: 0;
  min-height: 0;
  background: transparent;
  border: none;
  outline: none;
  box-shadow: none;
}

.scene-cell.is-dragging {
  cursor: grabbing;
  z-index: 2;
}

.scene-cell.is-orbiting {
  cursor: all-scroll;
  z-index: 2;
}

body[data-scene-alt="true"] .scene-cell {
  cursor: all-scroll;
}

/* Selection = tiny index badge, not a rectangular frame around empty space. */
.scene-cell-badge {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-30%, -30%);
  transition: opacity 0.12s ease;
}

.scene-cell.is-selected .scene-cell-badge {
  opacity: 1;
}

.scene-cell model-viewer {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  background: transparent;
  border: none;
  border-radius: 0;
  outline: none;
  box-shadow: none;
  --poster-color: transparent;
}

.canvas-wrapper[data-drag-active="true"] {
  box-shadow: inset 0 0 0 2px var(--accent-ring), 0 32px 80px var(--accent-ring);
  background: var(--accent-soft);
}

canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
}

model-viewer {
  width: 100%;
  height: 100%;
  background: transparent;
}

/* Responsive Layout */
@media (max-width: 960px) {
  body {
    height: auto;
    min-height: 100vh;
    overflow: auto;
  }
  main {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(50dvh, 100dvh);
    height: auto;
  }
  .control-pane {
    --pane-footer-h: 88px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    height: auto;
    min-height: calc(280px + var(--pane-footer-h));
    max-height: none;
  }
  .control-pane-body {
    position: relative;
    top: auto;
    left: auto;
    right: auto;
    bottom: auto;
    max-height: min(50dvh, 480px);
    margin: 20px 24px 0;
  }
  .control-pane > .download-actions {
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    height: auto;
    margin: 12px 24px 16px;
  }
  .control-pane button,
  .control-pane label.btn-primary {
    font-size: 13px;
  }

  .resize-handle {
    display: none;
  }

  .workspace {
    padding: 20px 24px 24px 24px;
  }

  .workspace-bar {
    align-items: flex-start;
  }

  .brand-sub {
    display: none;
  }

  .canvas-stage {
    margin-top: 16px;
  }

  .canvas-wrapper {
    display: flex;
    /* Reuse the same --preview-max for both the width cap and max-height so
       height = width / aspect never collides with a tighter max-height and
       warps the frame shape (previously the width cap used 80vh while
       max-height was 70dvh, breaking 1:1 / 9:16 / 4:5 in landscape). */
    --preview-max: 70dvh;
    width: min(100%, calc(var(--preview-max) * var(--preview-aspect, 1)));
    height: auto;
    max-height: var(--preview-max);
    aspect-ratio: var(--preview-aspect, 1);
  }
}