*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --surface: #ffffff;
  --surface-dim: #f4f4f5;
  --border: #dcdde1;
  --text: #1a1a1b;
  --text-muted: #6b6b76;
  --accent: hsl(215, 20%, 46%);
  --accent-hover: hsl(215, 24%, 36%);
  --accent-light: hsl(215, 25%, 94%);
  --accent-text: #ffffff;
  --danger: hsl(0, 65%, 52%);
  --danger-light: hsl(0, 65%, 96%);
  --frame-bg: #e0e0e0;
  --radius: 6px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--surface-dim);
}

body {
  font-family: var(--font);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

body.resize-col { cursor: col-resize !important; }
body.resize-row { cursor: row-resize !important; }
body.panning    { cursor: grabbing !important; }
body.swapping   { cursor: move !important; }
body.resize-col *, body.resize-row *, body.panning *, body.swapping * {
  user-select: none !important;
  -webkit-user-select: none !important;
}

[hidden] { display: none !important; }


/* ── Upload Screen ── */

#upload-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 24px 16px;
  overflow-y: auto;
}

.upload-wrap {
  text-align: center;
  max-width: 480px;
  width: 100%;
  margin: auto;
}

.upload-wrap h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.4px;
  margin-bottom: 6px;
}

.upload-wrap > p {
  color: var(--text-muted);
  font-size: 15px;
  margin-bottom: 24px;
  line-height: 1.4;
}

.restore-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 20px;
  text-align: left;
  gap: 12px;
}

.restore-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.restore-text strong {
  font-size: 13px;
  color: var(--text);
}

.restore-text span {
  font-size: 12px;
  color: var(--text-muted);
}

.secondary-btn {
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  white-space: nowrap;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s;
}

.secondary-btn:hover {
  background: var(--accent-hover);
}

#drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 44px 20px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  background: var(--surface);
}

#drop-zone:hover,
#drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-light);
}

#drop-zone:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.drop-icon {
  width: 44px;
  height: 44px;
  margin: 0 auto 12px;
  color: var(--accent);
}

.drop-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}

.drop-subtitle {
  font-size: 14px;
  color: var(--text-muted);
}

.drop-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 14px;
  line-height: 1.4;
}

.upload-actions {
  margin-top: 16px;
}

.text-link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  text-decoration: underline;
  padding: 10px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.text-link-btn:hover {
  color: var(--accent-hover);
}

.text-link-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}


/* ── Desktop Editor Screen ── */

#editor-screen {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#sidebar {
  grid-column: 1;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
}

.mobile-tabs {
  display: none;
}

.sidebar-content-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* On desktop, show all sections simultaneously in clean vertical order */
@media (min-width: 801px) {
  .sidebar-tab-content {
    display: block !important;
  }
}

.sidebar-section h2 {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.section-header h2 { margin-bottom: 0; }

.section-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 10px;
  line-height: 1.4;
}

#add-more-btn {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: none;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  min-height: 36px;
}

#add-more-btn:hover {
  background: var(--accent);
  color: var(--accent-text);
}

#add-more-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}


/* ── Thumbnails & Reorder ── */

#thumbnails {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.thumbnail {
  position: relative;
  width: 58px;
  height: 58px;
  border-radius: 4px;
  overflow: hidden;
  border: 2px solid var(--border);
  flex-shrink: 0;
  cursor: grab;
  user-select: none;
  transition: transform 0.12s, border-color 0.12s, opacity 0.12s;
  background: #f0f0f0;
}

.thumbnail:active {
  cursor: grabbing;
}

.thumbnail.dragging {
  opacity: 0.35;
  border-color: var(--accent);
  transform: scale(0.92);
}

.thumbnail.drag-target {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.thumbnail.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.thumbnail .thumb-order-badge {
  position: absolute;
  bottom: 2px;
  left: 2px;
  background: rgba(0, 0, 0, 0.7);
  color: #ffffff;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 2px;
  pointer-events: none;
  line-height: 1.2;
}

.thumbnail .thumb-actions {
  position: absolute;
  top: 2px;
  right: 2px;
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.12s;
}

.thumbnail:hover .thumb-actions,
.thumbnail:focus-within .thumb-actions,
.thumbnail.selected .thumb-actions {
  opacity: 1;
}

.thumbnail .remove-thumb,
.thumbnail .move-thumb-btn {
  width: 20px;
  height: 20px;
  border-radius: 2px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  border: none;
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}

.thumbnail .remove-thumb:hover {
  background: var(--danger);
}

.thumbnail .move-thumb-btn:hover {
  background: var(--accent);
}

.thumbnail .remove-thumb:focus-visible,
.thumbnail .move-thumb-btn:focus-visible {
  outline: 2px solid white;
  opacity: 1;
}


/* ── Layout Presets ── */

.preset-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.preset-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, color 0.12s;
  text-align: center;
  min-height: 52px;
}

.preset-btn:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.preset-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.preset-btn.active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent-hover);
  font-weight: 700;
  box-shadow: inset 0 0 0 1px var(--accent);
}

.preset-icon {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 32px;
  height: 22px;
  background: #ebebed;
  padding: 2px;
  border-radius: 2px;
}

.preset-icon-row {
  display: flex;
  gap: 2px;
  flex: 1;
}

.preset-icon-cell {
  background: var(--accent);
  flex: 1;
  border-radius: 1px;
  opacity: 0.6;
}

.preset-btn.active .preset-icon-cell {
  opacity: 1;
}


/* ── Ratio Buttons ── */

#ratio-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.ratio-btn {
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, color 0.12s;
  min-height: 40px;
}

.ratio-btn:hover {
  border-color: var(--accent);
}

.ratio-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.ratio-btn[aria-checked="true"] {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}


/* ── Custom Ratio ── */

#custom-ratio {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

#custom-ratio input[type="number"] {
  width: 60px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  text-align: center;
  font-family: var(--font);
  min-height: 40px;
}

#custom-ratio input[type="number"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

#custom-ratio .ratio-sep {
  font-weight: 600;
  color: var(--text-muted);
  font-size: 15px;
}


/* ── Range & Color Controls ── */

.control-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.control-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  min-height: 36px;
  cursor: pointer;
}

.control-row output,
.control-row .val {
  font-size: 13px;
  color: var(--text-muted);
  min-width: 40px;
  text-align: right;
}

.control-row input[type="color"] {
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  padding: 2px;
  background: var(--surface);
}

.control-row .color-label {
  font-size: 13px;
  color: var(--text-muted);
  font-family: 'Consolas', 'Monaco', monospace;
}


/* ── Project Controls ── */

.project-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.project-btn-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.project-btn {
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.project-btn:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.project-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.reset-btn {
  padding: 10px;
  border: 1px solid transparent;
  background: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  border-radius: var(--radius);
  text-align: center;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.12s, background 0.12s;
}

.reset-btn:hover {
  color: var(--danger);
  background: var(--danger-light);
}

.reset-btn:focus-visible {
  outline: 2px solid var(--danger);
  outline-offset: 1px;
}


/* ── Export ── */

.export-section {
  padding: 14px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.export-btn-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.export-btn {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  transition: background 0.12s, border-color 0.12s;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.export-btn:hover {
  border-color: var(--accent);
}

.export-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.export-btn.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.export-btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}


/* ── Canvas Area ── */

#canvas-area {
  grid-column: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: hidden;
  position: relative;
  min-height: 0;
  flex: 1;
}

#collage-container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  width: 100%;
  min-height: 0;
}

#collage-frame {
  position: relative;
  overflow: hidden;
  border-radius: 4px;
  box-shadow:
    0 1px 3px rgba(0, 0, 0, 0.08),
    0 4px 16px rgba(0, 0, 0, 0.06);
  flex-shrink: 0;
}

#collage-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 10px;
  text-align: center;
  flex-shrink: 0;
  line-height: 1.4;
}


/* ── Collage Cells ── */

.collage-cell {
  position: absolute;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  transition: box-shadow 0.12s, outline 0.12s;
}

.collage-cell:active {
  cursor: grabbing;
}

.collage-cell.cell-drop-target {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
  box-shadow: inset 0 0 0 9999px rgba(80, 105, 135, 0.28);
}

.collage-cell.cell-selected {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}

.collage-cell img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.collage-cell .cell-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: white;
  border: none;
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s, background 0.12s;
  padding: 0;
  z-index: 4;
}

.collage-cell:hover .cell-remove,
.collage-cell:focus-within .cell-remove,
.collage-cell.cell-selected .cell-remove {
  opacity: 1;
}

.collage-cell .cell-remove:hover {
  background: var(--danger);
}

.collage-cell .cell-remove:focus-visible {
  opacity: 1;
  outline: 2px solid white;
  outline-offset: 1px;
}

.collage-cell .cell-grip {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: white;
  border: none;
  font-size: 13px;
  font-weight: bold;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  opacity: 0;
  transition: opacity 0.12s, background 0.12s;
  padding: 0;
  z-index: 4;
}

.collage-cell:hover .cell-grip,
.collage-cell:focus-within .cell-grip,
.collage-cell.cell-selected .cell-grip {
  opacity: 1;
}

.collage-cell .cell-grip:hover {
  background: var(--accent);
}

.collage-cell .cell-grip:active {
  cursor: grabbing;
}

.collage-cell .cell-grip:focus-visible {
  opacity: 1;
  outline: 2px solid white;
  outline-offset: 1px;
}


/* ── Resize Handles ── */

.resize-handle {
  position: absolute;
  z-index: 5;
  background: transparent;
  touch-action: none;
}

.resize-handle::after {
  content: '';
  position: absolute;
  background: transparent;
  border-radius: 2px;
  transition: background 0.1s;
}

.resize-handle.vertical {
  cursor: col-resize;
}

.resize-handle.vertical::after {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 4px;
  height: 32px;
}

.resize-handle.horizontal {
  cursor: row-resize;
}

.resize-handle.horizontal::after {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 32px;
  height: 4px;
}

.resize-handle:hover::after,
.resize-handle:focus-visible::after {
  background: rgba(0, 0, 0, 0.35);
}


/* ── Tablet State (801px to 1100px) ── */

@media (min-width: 801px) and (max-width: 1100px) {
  #editor-screen {
    grid-template-columns: 260px 1fr;
  }
  #sidebar {
    padding: 0;
  }
  .sidebar-content-scroll {
    padding: 16px 14px;
    gap: 16px;
  }
}


/* ── Mobile State (<= 800px): Phones & Small Tablets ── */

@media (max-width: 800px) {
  #editor-screen {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  /* Canvas preview is prominent on top */
  #canvas-area {
    order: 1;
    flex: 1 1 auto;
    min-height: 200px;
    max-height: 54dvh;
    padding: 10px 12px;
    background: var(--surface-dim);
  }

  #collage-hint {
    margin-top: 6px;
    font-size: 11px;
  }

  /* Sidebar transforms into bottom control drawer */
  #sidebar {
    order: 2;
    flex: 0 0 auto;
    height: 46dvh;
    border-right: none;
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  }

  /* Segmented tabs at the top of bottom drawer */
  .mobile-tabs {
    display: flex;
    background: #fbfbfb;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  .mobile-tab-btn {
    flex: 1;
    min-height: 46px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    padding: 4px;
    transition: color 0.12s, border-bottom-color 0.12s, background 0.12s;
  }

  .mobile-tab-btn svg {
    color: inherit;
  }

  .mobile-tab-btn.active {
    color: var(--accent);
    background: var(--surface);
    border-bottom-color: var(--accent);
  }

  .mobile-tab-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
  }

  /* Content area scrolls inside selected tab */
  .sidebar-content-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px;
    -webkit-overflow-scrolling: touch;
    gap: 0;
  }

  .sidebar-tab-content {
    display: none;
  }

  .sidebar-tab-content.active {
    display: block;
  }

  /* Mobile thumbnails always show actions */
  .thumbnail {
    width: 60px;
    height: 60px;
  }

  .thumbnail .thumb-actions {
    opacity: 1;
  }

  .thumbnail .remove-thumb,
  .thumbnail .move-thumb-btn {
    width: 22px;
    height: 22px;
    font-size: 13px;
  }

  /* Sticky Export Bar at bottom of drawer */
  .export-section {
    padding: 8px 14px max(10px, env(safe-area-inset-bottom, 10px));
    border-top: 1px solid var(--border);
    background: var(--surface);
  }

  .export-btn-group {
    flex-direction: row;
    gap: 8px;
  }

  .export-btn {
    flex: 1;
    min-height: 44px;
    font-size: 13px;
    padding: 8px;
  }
}


/* ── Small Mobile Screens (<= 480px) ── */

@media (max-width: 480px) {
  .upload-wrap h1 { font-size: 24px; }
  .upload-wrap > p { font-size: 13px; }
  #drop-zone { padding: 32px 16px; }

  .mobile-tab-btn span {
    font-size: 10px;
  }

  .preset-grid {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .preset-btn {
    min-height: 48px;
    padding: 6px 4px;
    font-size: 10px;
  }
}
