/* ============ asciiGen — dark editor theme ============ */
:root {
  --bg: #09090b;
  --bg-stage: #060607;
  --panel: #101013;
  --panel-2: #16161a;
  --panel-3: #1d1d22;
  --line: #232329;
  --line-2: #2e2e36;
  --text: #ededf0;
  --text-dim: #9b9ba4;
  --text-faint: #62626b;
  --accent: #2f6bff;
  --accent-hover: #4a7dff;
  --accent-soft: rgba(47, 107, 255, 0.16);
  --danger: #ff5f56;
  --radius: 10px;
  --radius-s: 7px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 13px;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
.hidden { display: none !important; }

#app { display: flex; flex-direction: column; height: 100vh; }

/* ============ Top bar ============ */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  height: 48px; padding: 0 12px 0 16px; flex: 0 0 auto;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  z-index: 30;
}
.logo { display: flex; align-items: center; gap: 8px; user-select: none; }
.logo-mark { font-family: var(--font-mono); font-size: 13px; color: var(--accent); letter-spacing: -1px; }
.logo-text { font-weight: 600; font-size: 14px; letter-spacing: -0.2px; }
.tb-right { display: flex; align-items: center; gap: 6px; }
.tb-sep { width: 1px; height: 20px; background: var(--line-2); margin: 0 6px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: var(--radius-s); color: var(--text-dim);
  transition: background .12s, color .12s;
}
.icon-btn svg { width: 16px; height: 16px; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; pointer-events: none; }

.ghost-btn, .primary-btn, .stage-btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 32px; padding: 0 13px; border-radius: var(--radius-s);
  font-size: 12.5px; font-weight: 500; white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s;
}
.ghost-btn svg, .primary-btn svg, .stage-btn svg { width: 14px; height: 14px; }
.ghost-btn { border: 1px solid var(--line-2); color: var(--text); background: transparent; }
.ghost-btn:hover { background: var(--panel-2); border-color: #3a3a44; }
.primary-btn { background: var(--accent); color: #fff; }
.primary-btn:hover { background: var(--accent-hover); }
.primary-btn:disabled { opacity: .5; pointer-events: none; }

/* ============ Workspace ============ */
#workspace { display: flex; flex: 1; min-height: 0; }

/* ---- icon rail ---- */
#rail {
  width: 58px; flex: 0 0 auto; display: flex; flex-direction: column; align-items: center;
  gap: 2px; padding: 10px 0; border-right: 1px solid var(--line); background: var(--bg);
}
.rail-btn {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  width: 50px; padding: 8px 0 7px; border-radius: var(--radius-s);
  color: var(--text-faint); text-decoration: none;
  transition: color .12s, background .12s;
}
.rail-btn svg { width: 19px; height: 19px; }
.rail-btn span { font-size: 9.5px; font-weight: 500; letter-spacing: .1px; }
.rail-btn:hover { color: var(--text-dim); background: var(--panel); }
.rail-btn.active { color: var(--accent); }
.rail-btn.active:hover { background: none; }

/* ---- settings panel ---- */
#panel {
  width: 248px; flex: 0 0 auto; overflow-y: auto; overflow-x: hidden;
  border-right: 1px solid var(--line); background: var(--bg);
  padding: 14px 14px 40px; scrollbar-width: thin; scrollbar-color: #2c2c34 transparent;
}
#panel::-webkit-scrollbar { width: 8px; }
#panel::-webkit-scrollbar-thumb { background: #26262e; border-radius: 4px; border: 2px solid var(--bg); }

.sec { margin-bottom: 22px; }
.sec-title {
  font-size: 10.5px; font-weight: 600; letter-spacing: .8px; text-transform: uppercase;
  color: var(--text-faint); margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;
}
.hint { font-size: 10.5px; color: var(--text-faint); line-height: 1.45; margin-top: 8px; }

/* segmented control */
.seg {
  display: flex; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-s);
  padding: 2px; gap: 2px;
}
.seg button {
  flex: 1; height: 24px; border-radius: 5px; font-size: 11.5px; font-weight: 500; color: var(--text-dim);
  transition: background .12s, color .12s;
}
.seg button:hover { color: var(--text); }
.seg button.active { background: var(--panel-3); color: var(--text); }

/* drop zone */
.dropzone {
  margin-top: 10px; border: 1px dashed var(--line-2); border-radius: var(--radius);
  padding: 18px 10px; text-align: center; color: var(--text-dim); font-size: 12px;
  cursor: pointer; transition: border-color .15s, background .15s;
}
.dropzone .dz-icon { font-size: 16px; margin-bottom: 5px; opacity: .7; }
.dropzone b { color: var(--accent); font-weight: 500; cursor: pointer; }
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }

/* source thumbs */
.thumb-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; margin-top: 10px; }
.thumb {
  aspect-ratio: 1; border-radius: 6px; overflow: hidden; position: relative;
  border: 1.5px solid transparent; background: var(--panel-2); padding: 0;
  transition: border-color .12s;
}
.thumb canvas, .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb:hover { border-color: var(--line-2); }
.thumb.active { border-color: var(--accent); }
.thumb.thumb-label { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--text-faint); font-size: 8.5px; }
.thumb.thumb-label svg { width: 14px; height: 14px; }
.thumb.thumb-label:hover { color: var(--text-dim); }

/* control rows */
.ctl { margin-bottom: 12px; }
.ctl-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.ctl-label { font-size: 12px; color: var(--text-dim); display: flex; align-items: center; gap: 6px; }
.ctl-val { font-size: 11px; color: var(--text-faint); font-variant-numeric: tabular-nums; }
.ctl-val .unit { margin-left: 1px; opacity: .7; }

/* range slider */
input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 3px; border-radius: 2px;
  background: linear-gradient(to right, var(--accent) 0%, var(--accent) var(--fill, 50%), #2a2a32 var(--fill, 50%), #2a2a32 100%);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 13px; height: 13px; border-radius: 50%; background: #fff;
  margin-top: -5px; box-shadow: 0 1px 4px rgba(0,0,0,.5); transition: transform .1s;
}
input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.12); }
input[type="range"]::-moz-range-track { height: 3px; border-radius: 2px; background: #2a2a32; }
input[type="range"]::-moz-range-progress { height: 3px; border-radius: 2px; background: var(--accent); }
input[type="range"]::-moz-range-thumb { width: 13px; height: 13px; border: 0; border-radius: 50%; background: #fff; }

/* select */
.sel-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.sel-row .ctl-label { flex: 0 0 auto; }
select {
  appearance: none; -webkit-appearance: none;
  background: var(--panel-2) url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%239b9ba4' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 9px center;
  border: 1px solid var(--line-2); color: var(--text); border-radius: var(--radius-s);
  font-size: 12px; font-family: inherit; height: 28px; padding: 0 26px 0 10px; cursor: pointer;
  max-width: 150px; flex: 1;
}
select:hover { border-color: #3a3a44; }
select:focus { outline: none; border-color: var(--accent); }

/* toggle switch */
.switch { position: relative; width: 32px; height: 18px; flex: 0 0 auto; cursor: pointer; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch .knob {
  position: absolute; inset: 0; border-radius: 10px; background: #2b2b33; transition: background .15s;
}
.switch .knob::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%;
  background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0,0,0,.4);
}
.switch input:checked + .knob { background: var(--accent); }
.switch input:checked + .knob::after { transform: translateX(14px); }
.tog-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }

/* style grid */
.style-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.style-cell {
  border: 1.5px solid var(--line); border-radius: var(--radius-s); overflow: hidden;
  background: #000; padding: 0; display: flex; flex-direction: column; transition: border-color .12s;
}
.style-cell canvas { width: 100%; height: 44px; display: block; background: #000; }
.style-cell .sc-name {
  font-size: 9.5px; color: var(--text-dim); padding: 4px 2px 5px; text-align: center;
  background: var(--panel); border-top: 1px solid var(--line); width: 100%;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.style-cell:hover { border-color: var(--line-2); }
.style-cell.active { border-color: var(--accent); }
.style-cell.active .sc-name { color: var(--text); }

/* preset chip grid (color presets, tone presets) */
.chip-grid { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  height: 24px; padding: 0 10px; border-radius: 6px; border: 1px solid var(--line-2);
  font-size: 11px; color: var(--text-dim); background: var(--panel);
  transition: border-color .12s, color .12s, background .12s;
}
.chip:hover { color: var(--text); border-color: #3a3a44; }
.chip.active { border-color: var(--accent); color: var(--text); background: var(--accent-soft); }

/* color input row */
.color-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.color-well {
  display: flex; align-items: center; gap: 8px; border: 1px solid var(--line-2); border-radius: var(--radius-s);
  padding: 3px 8px 3px 3px; cursor: pointer; background: var(--panel);
}
.color-well:hover { border-color: #3a3a44; }
.color-well .swatch { width: 20px; height: 20px; border-radius: 5px; border: 1px solid rgba(255,255,255,.12); }
.color-well .cv { font-family: var(--font-mono); font-size: 10.5px; color: var(--text-dim); }
.color-well input[type="color"] { position: absolute; opacity: 0; width: 0; height: 0; }

/* text input */
input[type="text"] {
  width: 100%; background: var(--panel-2); border: 1px solid var(--line-2); border-radius: var(--radius-s);
  color: var(--text); font-family: var(--font-mono); font-size: 12px; height: 28px; padding: 0 9px;
}
input[type="text"]:focus { outline: none; border-color: var(--accent); }

/* effect list */
.fx-item { border: 1px solid var(--line); border-radius: var(--radius-s); margin-bottom: 7px; background: var(--panel); overflow: hidden; }
.fx-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; }
.fx-name { font-size: 12px; color: var(--text); display: flex; align-items: center; gap: 7px; }
.fx-badge {
  font-size: 8.5px; text-transform: uppercase; letter-spacing: .5px; color: var(--accent);
  border: 1px solid rgba(47,107,255,.4); border-radius: 4px; padding: 1px 4px;
}
.fx-body { padding: 2px 10px 10px; border-top: 1px solid var(--line); }
.fx-item.off .fx-name { color: var(--text-dim); }

/* curve editor */
#curve-canvas {
  width: 100%; height: 140px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius-s); cursor: crosshair; display: block; touch-action: none;
}

/* ============ Stage ============ */
#stage {
  flex: 1; min-width: 0; position: relative; display: flex; align-items: center; justify-content: center;
  background: var(--bg-stage); padding: 28px 28px 74px; overflow: hidden;
}
#canvas-wrap {
  position: relative; width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
}
#out {
  max-width: 100%; max-height: 100%; border-radius: 4px;
  box-shadow: 0 8px 60px rgba(0,0,0,.6);
  background:
    repeating-conic-gradient(#1a1a1f 0% 25%, #101014 0% 50%) 0 0 / 18px 18px;
}
#stage-actions {
  position: absolute; bottom: 22px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 8px; z-index: 10;
}
.stage-btn {
  background: rgba(18, 18, 22, .88); border: 1px solid var(--line-2); backdrop-filter: blur(10px);
  color: var(--text); box-shadow: 0 4px 20px rgba(0,0,0,.4);
}
.stage-btn:hover { background: rgba(30, 30, 36, .92); border-color: #3d3d48; }
.crop-pop-anchor { position: relative; }
.pop {
  position: absolute; bottom: 42px; left: 0; min-width: 130px;
  background: var(--panel-2); border: 1px solid var(--line-2); border-radius: var(--radius);
  padding: 5px; box-shadow: 0 10px 40px rgba(0,0,0,.55); z-index: 40;
}
.pop button {
  display: flex; width: 100%; align-items: center; justify-content: space-between;
  padding: 7px 10px; border-radius: 6px; font-size: 12px; color: var(--text-dim);
}
.pop button:hover { background: var(--panel-3); color: var(--text); }
.pop button.active { color: var(--accent); }

/* ============ Dialog ============ */
.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .6); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.dialog {
  width: 380px; max-width: calc(100vw - 40px); background: var(--panel);
  border: 1px solid var(--line-2); border-radius: 14px; padding: 18px;
  box-shadow: 0 24px 80px rgba(0,0,0,.6);
}
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.dialog-head h2 { font-size: 15px; font-weight: 600; }
.dialog .seg { margin-bottom: 16px; }
#export-body { min-height: 150px; }
#export-body textarea {
  width: 100%; height: 150px; background: #000; color: #cfcfd6; border: 1px solid var(--line-2);
  border-radius: var(--radius-s); font-family: var(--font-mono); font-size: 7px; line-height: 1.15;
  padding: 8px; white-space: pre; overflow: auto; resize: none;
}
.dialog-foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.export-note { font-size: 11px; color: var(--text-faint); line-height: 1.5; margin-top: 10px; }
.export-progress { font-size: 12px; color: var(--accent); margin-top: 10px; display: none; }

/* ============ Toast ============ */
#toast {
  position: fixed; bottom: 26px; right: 26px; z-index: 200;
  background: var(--panel-3); border: 1px solid var(--line-2); color: var(--text);
  border-radius: var(--radius-s); padding: 10px 16px; font-size: 12.5px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
  animation: toast-in .18s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* narrow screens */
@media (max-width: 860px) {
  .rail-btn span { display: none; }
  #rail { width: 46px; }
  #panel { width: 216px; }
  .ghost-btn { padding: 0 9px; }
}
