:root {
  --bg: #ffffff;
  --fg: #0e0e0e;
  --muted: #7a7a7a;
  --line: #e6e6e6;
  --line-strong: #0e0e0e;
  --panel: #fafafa;
  --accent: #0e0e0e;
  --danger: #c92626;
  --mono: 'SF Mono', 'Menlo', 'Monaco', 'Consolas', monospace;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e0e;
    --fg: #f2f2f2;
    --muted: #999;
    --line: #262626;
    --line-strong: #f2f2f2;
    --panel: #141414;
    --accent: #f2f2f2;
  }
}

:root[data-theme="light"] {
  --bg: #ffffff; --fg: #0e0e0e; --muted: #7a7a7a; --line: #e6e6e6;
  --line-strong: #0e0e0e; --panel: #fafafa; --accent: #0e0e0e;
}
:root[data-theme="dark"] {
  --bg: #0e0e0e; --fg: #f2f2f2; --muted: #999; --line: #262626;
  --line-strong: #f2f2f2; --panel: #141414; --accent: #f2f2f2;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--fg);
  font-family: var(--sans);
  font-size: 13px;
  height: 100%;
  overflow: hidden;
}

button { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  height: 52px;
}
.brand { display: flex; align-items: baseline; gap: 10px; font-family: var(--mono); }
.brand-mark { font-size: 18px; }
.brand-name { font-weight: 600; letter-spacing: -0.01em; }
.brand-sub { color: var(--muted); font-size: 11px; }

.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent; border: 1px solid transparent;
  padding: 6px 12px; border-radius: 999px; cursor: pointer;
  font-family: var(--mono); font-size: 12px; color: var(--muted);
}
.tab:hover { color: var(--fg); }
.tab.active { background: var(--fg); color: var(--bg); }

.screen { display: none; height: calc(100vh - 52px); }
.screen.active { display: grid; grid-template-columns: 320px 1fr; }

.panel {
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 16px 18px 24px;
  overflow-y: auto;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
  position: sticky; top: -16px; background: var(--panel); z-index: 2;
  padding: 16px 0 8px; margin-top: -16px;
}
.panel-head h2 { margin: 0; font-size: 14px; font-family: var(--mono); font-weight: 600; }
.panel-foot {
  margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line);
  position: sticky; bottom: -24px; background: var(--panel); padding-bottom: 24px;
}

.panel h3 {
  margin: 18px 0 8px; font-size: 11px; font-family: var(--mono);
  font-weight: 500; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted);
  border-top: 1px solid var(--line); padding-top: 14px;
}
.panel h3:first-of-type { border-top: none; padding-top: 0; }

.field { margin-bottom: 10px; }
.field label {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 11px; color: var(--muted); font-family: var(--mono);
  margin-bottom: 4px;
}
.field label span[data-out] {
  color: var(--fg); font-variant-numeric: tabular-nums;
}
.field input[type="text"], .field select, .field input[type="color"] {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--line);
  background: var(--bg); color: var(--fg);
  font-family: var(--mono); font-size: 12px;
  border-radius: 4px;
}
.field input[type="color"] { padding: 2px; height: 30px; }
.field input[type="range"] { width: 100%; accent-color: var(--fg); }
.field input[type="checkbox"] { accent-color: var(--fg); }
.field.range label { justify-content: space-between; }

.row { display: flex; gap: 6px; }
.btn {
  border: 1px solid var(--line-strong); background: var(--bg); color: var(--fg);
  padding: 6px 12px; border-radius: 4px; cursor: pointer;
  font-family: var(--mono); font-size: 11px; letter-spacing: 0.03em;
  transition: transform 0.05s ease;
}
.btn:hover { background: var(--fg); color: var(--bg); }
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--fg); color: var(--bg); }
.btn.primary:hover { opacity: 0.85; }
.btn.ghost { border-color: var(--line); color: var(--muted); }
.btn.ghost:hover { color: var(--fg); border-color: var(--line-strong); background: var(--bg); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger); color: white; }

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

/* stage */
.stage { padding: 18px 24px; overflow: auto; display: flex; flex-direction: column; min-width: 0; }
.stage.full { grid-column: 1 / -1; }
.stage-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.stage-head h2 { margin: 0; font-size: 14px; font-family: var(--mono); font-weight: 600; }

.tile-preview {
  flex: 1; display: flex; align-items: center; justify-content: center;
  min-height: 400px;
}
.tile-preview svg {
  width: min(60vh, 70%); height: auto; max-width: 100%;
  filter: drop-shadow(0 20px 40px rgba(0,0,0,0.08));
}

.stage-comp .comp-preview {
  flex: 1; display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  background:
    repeating-conic-gradient(#0000 0 25%, rgba(0,0,0,0.03) 0 50%) 0 0/24px 24px;
}
.comp-preview svg { max-height: 100%; max-width: 100%; height: 100%; width: auto; }

/* tile picker chips */
.tile-picker {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
  max-height: 260px; overflow-y: auto;
  padding: 4px; border: 1px solid var(--line); border-radius: 4px;
}
.tile-picker .tile-chip {
  aspect-ratio: 1; border: 2px solid transparent; border-radius: 4px;
  cursor: pointer; background: var(--bg); overflow: hidden;
  position: relative;
}
.tile-picker .tile-chip svg { width: 100%; height: 100%; display: block; }
.tile-picker .tile-chip.active { border-color: var(--fg); }
.tile-picker .tile-chip .badge {
  position: absolute; top: 2px; right: 4px;
  font-family: var(--mono); font-size: 9px; color: var(--muted);
}

/* library */
.lib-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  padding: 4px 4px 40px;
  overflow-y: auto;
}
.lib-card {
  border: 1px solid var(--line); border-radius: 4px;
  background: var(--bg); overflow: hidden;
  display: flex; flex-direction: column;
}
.lib-card .lib-thumb {
  aspect-ratio: 1; background: var(--panel);
  display: flex; align-items: center; justify-content: center;
}
.lib-card .lib-thumb svg { width: 100%; height: 100%; }
.lib-card .lib-meta {
  padding: 8px 10px; font-family: var(--mono); font-size: 10px;
  display: flex; justify-content: space-between; align-items: center;
  gap: 6px; border-top: 1px solid var(--line);
}
.lib-card .lib-meta .seed { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-card .lib-actions button {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; padding: 2px 4px;
}
.lib-card .lib-actions button:hover { color: var(--fg); }
.lib-card .lib-actions button.del:hover { color: var(--danger); }

.progress {
  margin-top: 10px; font-family: var(--mono); font-size: 10px; color: var(--muted);
  min-height: 14px;
}
.progress-bar {
  width: 200px; height: 4px; background: var(--line); border-radius: 2px; overflow: hidden;
}
.scrub { width: 0%; height: 100%; background: var(--fg); transition: width 0.05s linear; }

/* scrollbar */
.panel::-webkit-scrollbar, .tile-picker::-webkit-scrollbar, .lib-grid::-webkit-scrollbar { width: 8px; }
.panel::-webkit-scrollbar-thumb, .tile-picker::-webkit-scrollbar-thumb, .lib-grid::-webkit-scrollbar-thumb {
  background: var(--line); border-radius: 4px;
}
