:root {
  --bg: #0b0b0c;
  --panel: #151517;
  --panel-2: #1b1b1e;
  --line: #2a2a2c;
  --accent: #8ecae6;
  --accent-soft: #5d8aa8;
  --accent-tint: #16232b;
  --ink: #f2efea;
  --ink-mute: #8a8782;
  --focus: #bfe4f5;
  --radius: 6px;
  --sidebar-w: 310px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* Accessible focus ring everywhere */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  background: var(--accent);
  color: #10131a;
  padding: 10px 16px;
  border-radius: 0 0 var(--radius) 0;
  font-weight: 600;
  font-size: 13px;
}
.skip-link:focus { left: 0; }

.app { display: flex; min-height: 100vh; }

/* ---------- Mobile top bar ---------- */
.topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar__brand { display: flex; flex-direction: column; line-height: 1.15; }
.topbar__eyebrow {
  font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--accent); font-weight: 600;
}
.topbar__title {
  font-family: 'Playfair Display', Georgia, serif;
  font-style: italic; font-weight: 600; font-size: 18px;
}
.topbar__toggle {
  background: var(--panel-2); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 14px; font-size: 13px; font-weight: 600; cursor: pointer;
  font-family: inherit;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 28px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  overflow-y: auto;
  max-height: 100vh;
  position: sticky;
  top: 0;
}

.brand__eyebrow {
  font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--accent); margin: 0 0 6px; font-weight: 600;
}
.brand__title {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 600; font-style: italic;
  font-size: 26px; margin: 0 0 8px; line-height: 1.1;
}
.brand__sub { font-size: 12.5px; color: var(--ink-mute); margin: 0; line-height: 1.5; }

.control-group__label {
  display: block;
  font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-mute); margin-bottom: 10px; font-weight: 600;
}

.tpl-section-label {
  font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--accent-soft); margin: 14px 0 8px; font-weight: 600;
}
.tpl-section-label:first-child { margin-top: 0; }

/* ---------- Option buttons (format / export / template) ---------- */
.format-list, .template-list { display: flex; flex-direction: column; gap: 6px; }

.option-btn {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 10px 12px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, background 0.15s;
}
.option-btn:hover { border-color: var(--accent-soft); }
.option-btn[aria-pressed="true"],
.option-btn[aria-checked="true"] {
  border-color: var(--accent);
  background: var(--accent-tint);
}

.fmt-shape {
  flex-shrink: 0;
  border: 1.5px solid var(--ink-mute);
  border-radius: 2px;
}
.option-btn[aria-checked="true"] .fmt-shape { border-color: var(--accent); }

.opt-meta { display: flex; flex-direction: column; }
.opt-name { font-weight: 500; }
.opt-sub { font-size: 11px; color: var(--ink-mute); }

.tpl-icon { width: 28px; height: 28px; flex-shrink: 0; display: grid; gap: 2px; }
.tpl-icon .ic-cell { background: var(--ink-mute); border-radius: 1px; }
.option-btn[aria-checked="true"] .tpl-icon .ic-cell { background: var(--accent); }

/* ---------- Range sliders ---------- */
input[type="range"] { width: 100%; accent-color: var(--accent); cursor: pointer; }
.range-row {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--ink-mute); margin-top: 6px;
}
.range-row output { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- Swatches ---------- */
.swatches { display: flex; gap: 8px; }
.swatch {
  width: 34px; height: 34px; border-radius: 50%; cursor: pointer;
  border: 2px solid var(--line); padding: 0;
}
.swatch[aria-checked="true"] { border-color: var(--accent); }

/* ---------- Export button ---------- */
.export-btn {
  margin-top: 6px;
  background: var(--accent);
  color: #10131a;
  border: none;
  padding: 13px 16px;
  border-radius: var(--radius);
  font-weight: 600; font-size: 13px; letter-spacing: 0.03em;
  cursor: pointer; font-family: inherit;
  transition: filter 0.15s;
}
.export-btn:hover:not(:disabled) { filter: brightness(1.08); }
.export-btn:disabled { opacity: 0.65; cursor: progress; }

.hint { font-size: 11.5px; color: var(--ink-mute); line-height: 1.5; margin: 0; }

/* ---------- Stage / preview ---------- */
.stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: radial-gradient(ellipse at center, #141416 0%, #0b0b0c 70%);
}
.light-table {
  padding: 14px;
  border-radius: 4px;
  background: var(--panel);
  border: 1px solid var(--accent-soft);
  box-shadow: 0 0 0 1px #000, 0 30px 80px rgba(0, 0, 0, 0.6), inset 0 0 40px rgba(0, 0, 0, 0.4);
}
.grid-preview { display: grid; width: min(430px, 78vw); }
.frame-wrap { display: inline-block; line-height: 0; }

.cell {
  position: relative; overflow: hidden;
  background: #f2efea; cursor: pointer;
}
.cell:focus-visible { outline-offset: -2px; }
.cell img, .cell video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover; display: block;
}
.cell.dragover { outline: 2px dashed var(--accent); outline-offset: -4px; }

.cell__placeholder {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 4px;
  background: #1b1b1e; color: var(--ink-mute);
  transition: background 0.15s;
}
.cell:hover .cell__placeholder { background: #202024; }
.cell__plus { font-family: 'Playfair Display', Georgia, serif; font-size: 20px; color: var(--accent-soft); }
.cell__idx { font-size: 10px; letter-spacing: 0.08em; }

.cell__btn {
  position: absolute;
  border: none;
  background: rgba(11, 11, 12, 0.72);
  color: #fff;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; z-index: 2; padding: 0;
  font-size: 12px;
  line-height: 1;
}
.cell__mute { bottom: 6px; left: 6px; width: 28px; height: 28px; font-size: 13px; }
.cell__remove { top: 6px; right: 6px; width: 24px; height: 24px; font-size: 14px; }

/* On devices with hover, keep remove hidden until hover; on touch always show */
@media (hover: hover) {
  .cell__remove { opacity: 0; transition: opacity 0.12s; }
  .cell:hover .cell__remove,
  .cell:focus-within .cell__remove { opacity: 1; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13px;
  max-width: min(420px, 90vw);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  z-index: 50;
}
.toast--error { border-color: #b5544e; }

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: toast-in 0.18s ease-out; }
  @keyframes toast-in {
    from { opacity: 0; transform: translate(-50%, 8px); }
    to { opacity: 1; transform: translate(-50%, 0); }
  }
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .app { flex-direction: column; }
  .topbar { display: flex; }
  .sidebar {
    width: 100%;
    max-height: none;
    position: static;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  .sidebar .brand { display: none; }
  .sidebar[hidden] { display: none; }
  .stage { padding: 24px 16px 40px; }
  .grid-preview { width: min(430px, 92vw); }
}
