:root {
  color-scheme: dark;
  --bg: #000000;
  --panel: #1c1c1e;
  --canvas: #0a0a0a;
  --frame: #2c2c2e;
  --frame-empty: #1c1c1e;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(120, 180, 255, 0.6);
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.35);
  --accent-glow: rgba(10, 132, 255, 0.15);
  --danger: #ff453a;
  --white-soft: rgba(255, 255, 255, 0.55);
  --white-muted: rgba(255, 255, 255, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100dvh;
}

body {
  font-family: -apple-system, "SF Pro Display", "SF Pro Text", "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: #f5f5f7;
  display: grid;
  place-items: center;
  padding: clamp(12px, 3vw, 28px);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.studio {
  position: relative;
  z-index: 1;
  width: min(1360px, 100%);
}

.layout-shell {
  border-radius: 22px;
  border: 1px solid var(--white-muted);
  background: var(--panel);
  box-shadow:
    0 0 0 0.5px rgba(255, 255, 255, 0.04),
    0 32px 80px rgba(0, 0, 0, 0.65);
  padding: clamp(8px, 1vw, 12px);
}

.layout {
  --cols: 12;
  --rows: 8;
  width: 100%;
  aspect-ratio: 3 / 2;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: clamp(6px, 0.6vw, 8px);
  position: relative;
  border-radius: 16px;
  background: var(--canvas);
  padding: 6px;
}

/* ── Frame tiles ── */

.frame-preview,
.frame-tile {
  min-width: 0;
  min-height: 0;
  border-radius: 14px;
  position: relative;
}

.frame-preview {
  z-index: 1;
  pointer-events: none;
  border: 2px dashed var(--accent);
  background: var(--accent-glow);
}

.frame-preview.invalid {
  border-color: var(--danger);
  background: rgba(255, 69, 58, 0.1);
}

.frame-tile {
  z-index: 2;
  overflow: hidden;
  border: 1px solid transparent;
  background: var(--frame-empty);
  cursor: grab;
  animation: tile-enter 260ms cubic-bezier(0.2, 0.6, 0.35, 1) both;
  animation-delay: calc(var(--frame-order, 0) * 30ms);
  transition: box-shadow 200ms ease, border-color 200ms ease, opacity 200ms ease, transform 200ms ease;
}

.frame-tile:active {
  cursor: grabbing;
}

.frame-tile:hover,
.frame-tile.selected {
  border-color: var(--accent);
  box-shadow:
    0 0 0 1.5px var(--accent),
    0 0 20px var(--accent-glow);
}

.frame-tile.drop-target {
  border-color: var(--accent);
  box-shadow:
    0 0 0 1.5px var(--accent),
    0 0 28px var(--accent-glow);
}

.frame-tile.pending-delete {
  animation: none;
  opacity: 0.3;
  transform: scale(0.92);
  border-color: var(--accent-soft);
  box-shadow: 0 0 0 1px var(--accent-soft);
}

.frame-tile.pending-delete .corner-handle {
  border-color: var(--accent-soft);
}

.frame-tile.pending-delete .edge-handle::before {
  background: var(--accent-soft);
}

.frame-tile.pending-delete .center-plus {
  opacity: 0;
}

.frame-tile.empty {
  background: var(--frame-empty);
}

.frame-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* ── Plus button (Apple squircle style) ── */

.center-plus {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: clamp(32px, 3.8vw, 44px);
  aspect-ratio: 1;
  border-radius: 12px;
  border: 0;
  background: rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(20px) saturate(1.5);
  -webkit-backdrop-filter: blur(20px) saturate(1.5);
  cursor: pointer;
  z-index: 4;
  transition: background 180ms ease, transform 180ms ease, opacity 180ms ease;
}

.center-plus:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translate(-50%, -50%) scale(1.08);
}

.center-plus:active {
  transform: translate(-50%, -50%) scale(0.94);
  background: rgba(255, 255, 255, 0.25);
}

.center-plus::before,
.center-plus::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  border-radius: 1px;
  background: #fff;
  transform: translate(-50%, -50%);
}

.center-plus::before {
  width: 44%;
  height: 1.5px;
}

.center-plus::after {
  width: 1.5px;
  height: 44%;
}

.frame-tile:not(.empty) .center-plus {
  opacity: 0;
  pointer-events: none;
}

/* ── Corner handles (inner curved bracket) ── */

.corner-handle {
  position: absolute;
  width: 20px;
  height: 20px;
  border: 2.5px solid var(--accent);
  background: transparent;
  padding: 0;
  z-index: 4;
  opacity: 0;
  transition: opacity 200ms ease;
}

.corner-handle::before,
.corner-handle::after {
  content: none;
}

.frame-tile:hover .corner-handle,
.frame-tile.selected .corner-handle,
.frame-tile:hover .edge-handle,
.frame-tile.selected .edge-handle {
  opacity: 1;
}

/* NW */
.corner-nw {
  left: 6px;
  top: 6px;
  border-right: 0;
  border-bottom: 0;
  border-top-left-radius: 8px;
  cursor: nwse-resize;
}

/* NE */
.corner-ne {
  right: 6px;
  top: 6px;
  border-left: 0;
  border-bottom: 0;
  border-top-right-radius: 8px;
  cursor: nesw-resize;
}

/* SW */
.corner-sw {
  left: 6px;
  bottom: 6px;
  border-right: 0;
  border-top: 0;
  border-bottom-left-radius: 8px;
  cursor: nesw-resize;
}

/* SE */
.corner-se {
  right: 6px;
  bottom: 6px;
  border-left: 0;
  border-top: 0;
  border-bottom-right-radius: 8px;
  cursor: nwse-resize;
}

/* ── Edge handles ── */

.edge-handle {
  position: absolute;
  background: transparent;
  border: 0;
  padding: 0;
  z-index: 4;
  opacity: 0;
  transition: opacity 200ms ease;
}

.edge-handle::before {
  content: "";
  position: absolute;
  background: var(--accent);
  border-radius: 1px;
}

.edge-n,
.edge-s {
  left: 28px;
  right: 28px;
  height: 12px;
  cursor: ns-resize;
}

.edge-n::before,
.edge-s::before {
  left: 50%;
  transform: translateX(-50%);
  width: min(40px, 40%);
  height: 2.5px;
}

.edge-n {
  top: 0;
}

.edge-n::before {
  top: 4px;
}

.edge-s {
  bottom: 0;
}

.edge-s::before {
  bottom: 4px;
}

.edge-e,
.edge-w {
  top: 28px;
  bottom: 28px;
  width: 12px;
  cursor: ew-resize;
}

.edge-e::before,
.edge-w::before {
  top: 50%;
  transform: translateY(-50%);
  width: 2.5px;
  height: min(40px, 40%);
}

.edge-e {
  right: 0;
}

.edge-e::before {
  right: 4px;
}

.edge-w {
  left: 0;
}

.edge-w::before {
  left: 4px;
}

/* ── Processing overlay ── */

.processing {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px) saturate(1.2);
  -webkit-backdrop-filter: blur(8px) saturate(1.2);
  opacity: 1;
  transition: opacity 200ms ease;
}

.processing.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 28px;
  aspect-ratio: 1;
  border-radius: 999px;
  border: 2.5px solid rgba(255, 255, 255, 0.15);
  border-top-color: #fff;
  animation: spin 800ms linear infinite;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  border: 0;
  padding: 0;
  clip: rect(0 0 0 0);
  overflow: hidden;
}

@keyframes tile-enter {
  from {
    opacity: 0;
    transform: scale(0.96);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes copied-flash {
  0% {
    box-shadow: inset 0 0 0 0 rgba(10, 132, 255, 0);
  }

  30% {
    box-shadow: inset 0 0 0 3px rgba(10, 132, 255, 0.5);
  }

  100% {
    box-shadow: inset 0 0 0 0 rgba(10, 132, 255, 0);
  }
}

.layout.copied {
  animation: copied-flash 500ms ease-out;
}

@media (max-width: 760px) {
  body {
    padding: 8px;
  }

  .layout {
    aspect-ratio: 4 / 5;
    gap: 5px;
  }

  .frame-tile {
    border-radius: 10px;
  }

  .corner-handle {
    width: 14px;
    height: 14px;
    border-width: 2px;
  }

  .center-plus {
    border-radius: 10px;
  }
}
