*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0d0d0d;
  --s1: #181818;
  --s2: #222;
  --s3: #2c2c2c;
  --border: #333;
  --accent: #c084fc;
  --accent2: #818cf8;
  --text: #f0f0f0;
  --muted: #777;
  --r: 8px;
}
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); height: 100vh; display: flex; flex-direction: column; overflow: hidden; }

/* ── Header ── */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; background: var(--s1); border-bottom: 1px solid var(--border);
  flex-shrink: 0; gap: 12px; height: 52px;
}
.logo { font-size: 17px; font-weight: 800; letter-spacing: -0.3px; background: linear-gradient(135deg, var(--accent), var(--accent2)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.brand { display: flex; align-items: center; gap: 8px; }
.help-btn {
  width: 18px; height: 18px; border-radius: 50%; flex-shrink: 0;
  background: var(--s2); border: 1px solid var(--border); color: var(--muted);
  font-size: 10px; font-weight: 700; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center; padding: 0;
}
.help-btn:hover { border-color: var(--accent); color: var(--accent); }
.hgroup { display: flex; align-items: center; gap: 8px; }
.ratio-group { display: flex; gap: 3px; background: var(--s2); border-radius: 6px; padding: 3px; }
.ratio-btn { background: none; border: none; color: var(--muted); font-size: 11px; font-weight: 700; padding: 4px 9px; border-radius: 4px; cursor: pointer; }
.ratio-btn.active { background: var(--accent); color: #fff; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 13px; border-radius: 6px; font-size: 12px; font-weight: 600; cursor: pointer; border: none; transition: opacity .15s; }
.btn-ghost { background: var(--s2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--s3); }
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff; }
.btn-primary:hover { opacity: .85; }
.btn:disabled { opacity: .35; cursor: not-allowed; }
label.btn { cursor: pointer; }
select { background: var(--s2); color: var(--text); border: 1px solid var(--border); border-radius: 5px; padding: 5px 8px; font-size: 11px; cursor: pointer; }

/* ── App shell ── */
.app { display: grid; grid-template-columns: 240px 1fr 220px; flex: 1; min-height: 0; }

/* ── Sidebar ── */
.sidebar { display: flex; flex-direction: column; background: var(--s1); border-right: 1px solid var(--border); min-height: 0; overflow: hidden; }
.right-sidebar { display: flex; flex-direction: column; background: var(--s1); border-left: 1px solid var(--border); min-height: 0; overflow: hidden; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); flex-shrink: 0; }
.badge { background: var(--accent); color: #fff; font-size: 9px; padding: 1px 6px; border-radius: 99px; }
.icon-btn { background: none; border: none; color: var(--muted); cursor: pointer; padding: 3px; border-radius: 4px; display: flex; align-items: center; }
.icon-btn:hover { color: var(--text); background: var(--s3); }

/* Library */
.lib-panel { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
.lib-grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 72px; gap: 4px; padding: 8px; overflow-y: auto; flex: 1; min-height: 0; align-content: start; }
.lib-grid::-webkit-scrollbar { width: 3px; }
.lib-grid::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.lib-photo {
  aspect-ratio: 1; border-radius: 4px; overflow: hidden; cursor: pointer;
  background: var(--s2); border: 2px solid transparent; position: relative;
  transition: border-color .15s;
}
.lib-photo:hover { border-color: var(--accent); }
.lib-photo.selected { border-color: var(--accent2); }
.lib-photo img { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.lib-photo-del {
  position: absolute; top: 3px; right: 3px; background: rgba(0,0,0,.7);
  border: none; color: #fff; border-radius: 3px; width: 18px; height: 18px;
  font-size: 10px; cursor: pointer; display: none; align-items: center; justify-content: center;
}
.lib-photo:hover .lib-photo-del { display: flex; }
.lib-photo-usage {
  position: absolute; top: 4px; right: 4px; width: 8px; height: 8px;
  border-radius: 50%; background: #4ade80; box-shadow: 0 0 0 2px rgba(0,0,0,.65);
  pointer-events: none; display: none;
}
.lib-photo.in-use .lib-photo-usage { display: block; }
/* The delete button occupies the same corner on hover, so yield to it. */
.lib-photo:hover .lib-photo-usage { display: none; }
.lib-drop {
  border: 2px dashed var(--border); border-radius: 6px; padding: 12px 10px;
  text-align: center; cursor: pointer; color: var(--muted); font-size: 11px;
  margin: 8px; transition: all .2s; flex-shrink: 0;
}
.lib-drop:hover, .lib-drop.over { border-color: var(--accent); color: var(--accent); background: rgba(192,132,252,.05); }

/* Photo preview */
.photo-preview { flex-shrink: 0; border-top: 1px solid var(--border); padding: 8px; display: none; flex-direction: column; gap: 5px; }
.photo-preview.visible { display: flex; }
.pp-wrap { height: 150px; border-radius: 6px; overflow: hidden; background: var(--s2); display: flex; align-items: center; justify-content: center; }
.pp-wrap img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.pp-name { font-size: 10px; color: var(--muted); text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Slides panel */
.slides-panel { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.slide-strip { display: flex; flex-direction: column; gap: 6px; padding: 8px; overflow-y: auto; flex: 1; min-height: 0; }
.slide-strip::-webkit-scrollbar { width: 3px; }
.slide-strip::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.slide-thumb-item {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px;
  background: var(--s2); border-radius: 6px; border: 2px solid transparent;
  cursor: pointer; transition: all .15s; position: relative;
}
.slide-thumb-item.active { border-color: var(--accent); }
.slide-thumb-item:hover { background: var(--s3); }
.slide-thumb-canvas { width: 44px; height: 44px; border-radius: 3px; object-fit: contain; background: var(--bg); flex-shrink: 0; display: block; }
.slide-label { font-size: 11px; color: var(--muted); flex: 1; }
.slide-del { background: none; border: none; color: var(--muted); cursor: pointer; padding: 3px; border-radius: 3px; opacity: 0; }
.slide-thumb-item:hover .slide-del { opacity: 1; }
.slide-del:hover { color: #f87171; }
.add-slide-btn { margin: 6px 8px 8px; padding: 8px; border: 2px dashed var(--border); border-radius: 6px; background: none; color: var(--muted); font-size: 11px; font-weight: 600; cursor: pointer; text-align: center; transition: all .2s; flex-shrink: 0; }
.add-slide-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ── Main ── */
main { display: flex; flex-direction: column; min-height: 0; overflow: hidden; position: relative; }

/* Layout picker */
.layout-picker { display: flex; background: var(--s1); border-bottom: 1px solid var(--border); flex-shrink: 0; align-items: stretch; overflow: hidden; }
.layout-picker-label { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); flex-shrink: 0; padding: 0 10px 0 16px; display: flex; align-items: center; }
.layout-opts-scroll { display: flex; gap: 6px; padding: 8px 6px; overflow-x: auto; flex: 1; align-items: center; }
.layout-opts-scroll::-webkit-scrollbar { height: 3px; }
.layout-opts-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.ls-label { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); white-space: nowrap; }
.layout-opt {
  flex-shrink: 0; background: var(--s2); border: 2px solid var(--border);
  border-radius: 6px; padding: 5px 7px; cursor: pointer; color: var(--muted);
  transition: all .15s; display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.layout-opt:hover { border-color: var(--accent2); color: var(--text); }
.layout-opt.active { border-color: var(--accent); color: var(--accent); background: rgba(192,132,252,.08); }
.layout-opt svg { display: block; }
.layout-opt-label { font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; }
.layout-picker-label { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); flex-shrink: 0; margin-right: 4px; }

/* Editor area */
.editor-area { flex: 1; display: flex; align-items: center; justify-content: center; overflow: auto; padding: 24px; position: relative; }
.editor-wrap { position: relative; }

/* The grid editor */
.grid-editor {
  position: relative; overflow: hidden;
  box-shadow: 0 12px 48px rgba(0,0,0,.7);
  background: #000;
}

/* Cells */
.cell {
  position: absolute; overflow: hidden; border-radius: 3px;
  background: var(--s2); cursor: pointer; transition: box-shadow .15s;
}
.cell.drag-target { box-shadow: inset 0 0 0 3px var(--accent); }
.cell.has-photo { cursor: grab; }
.cell.has-photo:active { cursor: grabbing; }
.cell-bg { position: absolute; inset: 0; background-size: cover; background-position: 50% 50%; pointer-events: none; }
.cell-empty {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 6px; color: var(--muted);
  font-size: 11px; font-weight: 500; pointer-events: none;
}
.cell-empty svg { opacity: .4; }
.cell-hint { font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--accent2); opacity: .85; }
.cell-overlay {
  position: absolute; inset: 0; display: flex; align-items: flex-end; justify-content: flex-end;
  padding: 6px; opacity: 0; transition: opacity .15s; pointer-events: none;
}
.cell.has-photo:hover .cell-overlay { opacity: 1; pointer-events: all; }
.cell-remove {
  background: rgba(0,0,0,.75); border: none; color: #fff; border-radius: 4px;
  width: 22px; height: 22px; font-size: 11px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

input[type="range"] { cursor: pointer; }

/* Span UI */
.btn-xs { padding: 4px 8px; font-size: 11px; }
.preset-bar { display: flex; background: var(--s1); border-bottom: 1px solid var(--border); flex-shrink: 0; align-items: stretch; overflow: hidden; }
.preset-opts { display: flex; gap: 6px; padding: 8px 6px; overflow-x: auto; flex: 1; align-items: center; }
.preset-opts::-webkit-scrollbar { height: 3px; }
.preset-opts::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.preset-opt svg { display: block; }
.save-state { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); min-width: 46px; text-align: right; transition: opacity .3s; }
.save-state.warn { color: #fbbf24; }
/* Settings, now in the slides panel so the layout picker gets the full width */
.panel-settings { display: flex; flex-direction: column; gap: 7px; padding: 9px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.ps-row { display: flex; align-items: center; gap: 8px; }
.ps-row .ls-label { width: 30px; flex-shrink: 0; }
.ps-row input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); }
.ps-row #gapVal { font-size: 11px; color: var(--muted); min-width: 20px; text-align: right; }
.ps-row input[type="color"] { width: 28px; height: 22px; border: none; border-radius: 4px; cursor: pointer; background: none; padding: 0; }

/* A warning about the composition itself, floating over the render rather than
   taking permanent space in a toolbar. */
.warn-toast {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  display: none; flex-direction: column; gap: 5px; z-index: 40;
  max-width: min(520px, calc(100% - 48px));
}
.warn-toast.visible { display: flex; }
.wt-row {
  display: flex; align-items: center; gap: 8px;
  background: rgba(28,22,8,.94); border: 1px solid #6b4f12; border-left: 3px solid #fbbf24;
  border-radius: 6px; padding: 7px 12px; box-shadow: 0 6px 20px rgba(0,0,0,.5);
}
.wt-icon { color: #fbbf24; font-size: 12px; flex-shrink: 0; }
.wt-title { font-size: 11px; font-weight: 700; color: #fbbf24; white-space: nowrap; }
.wt-msg { font-size: 11px; color: #d8cdb4; }

#zoomItem.disabled { opacity: .35; pointer-events: none; }
#zoomVal { font-size: 11px; color: var(--muted); min-width: 34px; }
/* amber once the source is being upscaled past the export resolution */
#zoomVal.soft { color: #fbbf24; }
.span-actions { display: flex; align-items: center; gap: 8px; padding: 0 14px; border-left: 1px solid var(--border); flex-shrink: 0; }

/* Cells that belong to a span, and the selected cell */
.cell.in-span::after {
  content: '⛓'; position: absolute; top: 4px; left: 4px; font-size: 9px;
  color: rgba(255,255,255,.75); text-shadow: 0 1px 3px rgba(0,0,0,.9);
  pointer-events: none;
}
.cell.selected { box-shadow: inset 0 0 0 2px var(--accent2); }

/* Where the export cuts a span */
.seam-marker {
  position: absolute; top: 0; bottom: 0; width: 0;
  border-left: 1px dashed rgba(255,255,255,.55);
  pointer-events: none; z-index: 50;
}
.seam-tag {
  position: absolute; bottom: 4px; font-size: 9px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .5px;
  color: rgba(255,255,255,.55); text-shadow: 0 1px 3px rgba(0,0,0,.9);
  pointer-events: none; z-index: 50;
}
/* Which slide of the strip is the one selected in the slide list */
.seam-tag.current { color: var(--accent); }

/* A whole chained run is one row now, so a simple left accent is enough to
   read as a chain -- no more bracket start/end caps to reconcile. */
.slide-thumb-item.in-span::before {
  content: ''; position: absolute; left: -1px; top: 4px; bottom: 4px; width: 3px;
  border-radius: 3px; background: var(--accent2); pointer-events: none;
}
.slide-thumb-item.in-span { padding-left: 12px; }
.span-chip { display: inline-block; margin-left: 5px; font-size: 9px; font-weight: 700; color: var(--accent2); }
/* Individual slide numbers inside a chain row's label, each independently
   clickable to activate just that member. */
.slide-num { cursor: pointer; }
.slide-num:hover { text-decoration: underline; }
.slide-num.sn-active { color: var(--accent); font-weight: 700; }

/* Reordering */
.slide-thumb-item { cursor: grab; }
.slide-thumb-item:active { cursor: grabbing; }
.slide-thumb-item.dragging { opacity: .35; }
.slide-thumb-item.drop-before::after, .slide-thumb-item.drop-after::after {
  content: ''; position: absolute; left: 0; right: 0; height: 2px;
  background: var(--accent); border-radius: 2px; z-index: 6; pointer-events: none;
}
.slide-thumb-item.drop-before::after { top: -4px; }
.slide-thumb-item.drop-after::after { bottom: -4px; }
.slide-strip.drop-end { box-shadow: inset 0 -3px 0 -1px var(--accent); }

/* Empty state */
.empty-state { display: flex; flex-direction: column; align-items: center; gap: 12px; color: var(--muted); text-align: center; }
.empty-state h2 { font-size: 16px; font-weight: 700; color: var(--text); }
.empty-state p { font-size: 12px; }

/* Progress */
.progress-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.8); display: none; align-items: center; justify-content: center; z-index: 999; }
.progress-box { background: var(--s1); border: 1px solid var(--border); border-radius: 12px; padding: 28px 36px; text-align: center; min-width: 260px; }
.progress-box h3 { margin-bottom: 14px; font-size: 14px; }
.pb-track { background: var(--s2); border-radius: 99px; height: 5px; overflow: hidden; margin-bottom: 8px; }
.pb-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent2)); width: 0%; transition: width .15s; }
.pb-label { font-size: 11px; color: var(--muted); }

/* Help */
.help-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.8); display: none; align-items: center; justify-content: center; z-index: 999; }
.help-overlay.visible { display: flex; }
.help-box {
  background: var(--s1); border: 1px solid var(--border); border-radius: 12px;
  width: min(560px, calc(100vw - 48px)); max-height: min(640px, calc(100vh - 48px));
  display: flex; flex-direction: column; overflow: hidden;
}
.help-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.help-head h3 { font-size: 14px; }
.help-body { padding: 4px 20px 20px; overflow-y: auto; }
.help-body section { padding-top: 16px; }
.help-body h4 {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .8px;
  color: var(--accent2); margin-bottom: 6px;
}
.help-body p { font-size: 12px; line-height: 1.55; color: var(--text); }
.help-body::-webkit-scrollbar { width: 3px; }
.help-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
