:root {
  --bg: #0b0b0d;
  --bg2: #141418;
  --bg3: #1d1d23;
  --line: #2a2a32;
  --text: #f4f4f5;
  --muted: #9a9aa6;
  --accent: #4f8cff;
  --accent2: #7aa7ff;
  --danger: #ef4444;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none; }
body { font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }

.screen { position: fixed; inset: 0; display: none; flex-direction: column; background: var(--bg); }
.screen.active { display: flex; }

.bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: calc(var(--safe-t) + 8px) 12px 8px; min-height: 52px; flex: none; }
.bar .actions { display: flex; gap: 4px; align-items: center; }
.bar .title { font-weight: 600; font-size: 17px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; letter-spacing: .2px; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 12px; color: var(--text); }
.icon-btn:active { background: var(--bg3); }
.icon-btn.accent { color: var(--accent2); }
.icon-btn:disabled { opacity: .3; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 18px; border-radius: 12px; background: var(--bg3); font-weight: 600; border: 1px solid var(--line); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 13px; border-radius: 10px; }
.btn:active { filter: brightness(1.2); }
.btn[disabled] { opacity: .4; }

/* ---------- Biblioteca ---------- */
.grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 3px; padding: 3px 3px calc(var(--safe-b) + 12px); align-content: start; -webkit-overflow-scrolling: touch; }
.grid .cell { position: relative; aspect-ratio: 1; background: var(--bg3) center/cover no-repeat; border-radius: 4px; overflow: hidden; }
.grid .cell .tag { position: absolute; left: 6px; bottom: 6px; font-size: 10px; font-weight: 700; padding: 2px 5px; border-radius: 5px; background: rgba(0,0,0,.6); letter-spacing: .5px; }
.grid .cell .edited { position: absolute; right: 6px; top: 6px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 24px; text-align: center; }
.empty h1 { margin: 0; font-size: 28px; }
.empty p { margin: 0; color: var(--muted); max-width: 320px; }
.empty .btn { width: 100%; max-width: 320px; }
.empty .hint { font-size: 13px; margin-top: 8px; }
.empty-logo { width: 84px; height: 84px; border-radius: 22px; }

/* ---------- Editor ---------- */
#stage { flex: 1; position: relative; display: flex; align-items: center; justify-content: center; min-height: 0; overflow: hidden; touch-action: none; }
#frame { position: relative; }
#gl, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#overlay { pointer-events: none; }
#frame.crop #overlay { pointer-events: auto; touch-action: none; }
.badge { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,.65); padding: 4px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; }

#panel { flex: none; height: 232px; overflow-y: auto; background: var(--bg2); border-top: 1px solid var(--line); padding: 10px 14px; -webkit-overflow-scrolling: touch; }
#tools { flex: none; display: flex; overflow-x: auto; gap: 2px; padding: 6px 6px calc(var(--safe-b) + 6px); background: var(--bg2); border-top: 1px solid var(--line); scrollbar-width: none; }
#tools::-webkit-scrollbar { display: none; }
#tools button { flex: none; min-width: 64px; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 6px 4px; border-radius: 10px; color: var(--muted); font-size: 11px; }
#tools button.on { color: var(--text); background: var(--bg3); }
#tools button.on svg { color: var(--accent2); }

.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; gap: 8px; }
.panel-head h3 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 600; }
.panel-head .row { display: flex; gap: 6px; }

/* Sliders */
.slider { padding: 5px 0 7px; }
.slider .lbl { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 2px; }
.slider .lbl span:last-child { color: var(--muted); font-variant-numeric: tabular-nums; }
.slider.changed .lbl span:last-child { color: var(--accent2); }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; margin: 0; touch-action: pan-y; }
input[type=range]::-webkit-slider-runnable-track { height: 3px; border-radius: 2px; background: var(--track, #3a3a44); }
input[type=range]::-moz-range-track { height: 3px; border-radius: 2px; background: var(--track, #3a3a44); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; margin-top: -9.5px; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.6); }
input[type=range]::-moz-range-thumb { width: 22px; height: 22px; border: 0; border-radius: 50%; background: #fff; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 7px 12px; border-radius: 18px; background: var(--bg3); border: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.chip.on { background: var(--text); color: #000; border-color: var(--text); font-weight: 600; }

.toggle { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; font-size: 14px; }
.switch { width: 46px; height: 28px; border-radius: 14px; background: #3a3a44; position: relative; transition: background .15s; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(18px); }

/* Filtros */
.thumbs { display: flex; gap: 10px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.thumbs::-webkit-scrollbar { display: none; }
.thumb { flex: none; width: 82px; text-align: center; font-size: 11px; color: var(--muted); }
.thumb .img { width: 82px; height: 82px; border-radius: 10px; background: var(--bg3); overflow: hidden; border: 2px solid transparent; display: grid; place-items: center; }
.thumb canvas, .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.on .img { border-color: var(--accent); }
.thumb.on { color: var(--text); font-weight: 600; }
.thumb .name { margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thumb.add .img { border: 1px dashed var(--line); color: var(--muted); }
.tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tags span { font-size: 11px; padding: 2px 7px; border-radius: 10px; background: var(--bg3); color: var(--muted); }

/* HSL */
.bands { display: flex; justify-content: space-between; padding: 4px 0 10px; }
.band { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; outline: 2px solid transparent; }
.band.on { border-color: var(--bg2); outline-color: var(--text); }

#curveCanvas { width: 100%; max-width: min(300px, 62vw); aspect-ratio: 1; display: block; margin: 0 auto 8px; background: #0f0f13; border-radius: 8px; touch-action: none; }

.crop-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 6px; }
.crop-actions .btn { padding: 0 6px; font-size: 12px; min-height: 40px; }

/* Presets */
.presets-info { padding: 4px 16px 12px; color: var(--muted); font-size: 13px; }
.preset-list { flex: 1; overflow-y: auto; padding: 0 12px calc(var(--safe-b) + 16px); }
.preset-row { display: flex; align-items: center; gap: 12px; padding: 10px 8px; border-bottom: 1px solid var(--line); }
.preset-row .pv { width: 46px; height: 46px; border-radius: 8px; background: var(--bg3) center/cover; flex: none; }
.preset-row .nm { flex: 1; min-width: 0; }
.preset-row .nm div:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.preset-row .nm div:last-child { font-size: 12px; color: var(--muted); }
.preset-row .btn { min-height: 34px; padding: 0 10px; font-size: 12px; }
.preset-empty { text-align: center; color: var(--muted); padding: 40px 20px; }

/* Hojas / diálogos */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 20; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--bg2); border-radius: 18px 18px 0 0; padding: 16px 16px calc(var(--safe-b) + 16px); max-height: 85vh; overflow-y: auto; border-top: 1px solid var(--line); }
.sheet h2 { margin: 0 0 12px; font-size: 18px; }
.sheet .field { margin-bottom: 12px; }
.sheet .field > label { display: block; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .8px; margin-bottom: 6px; }
.sheet .stack { display: flex; flex-direction: column; gap: 8px; }
.sheet .stack .btn { width: 100%; }
.sheet input[type=text] { width: 100%; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); padding: 0 12px; font: inherit; font-size: 16px; -webkit-user-select: text; user-select: text; }
.sheet .note { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.sheet .preview-out { width: 100%; max-height: 40vh; object-fit: contain; border-radius: 8px; background: #000; margin-bottom: 10px; display: block; }

.busy { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.6); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; font-size: 14px; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 3px solid #ffffff33; border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(var(--safe-b) + 90px); transform: translateX(-50%); z-index: 50; background: #26262e; border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px); text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.4); }

@media (min-width: 900px) {
  /* En escritorio/tablet: panel a la derecha */
  #editor { display: none; }
  #editor.active { display: grid; grid-template-columns: 1fr 340px; grid-template-rows: auto 1fr auto; }
  #editor .bar { grid-column: 1 / 3; }
  #stage { grid-row: 2 / 4; grid-column: 1; }
  #panel { grid-column: 2; grid-row: 2; height: auto; border-left: 1px solid var(--line); border-top: 0; }
  #tools { grid-column: 2; grid-row: 3; flex-wrap: wrap; border-left: 1px solid var(--line); }
}
@media (max-height: 640px) { #panel { height: 190px; } }

/* Grupos de presets */
.group-head { display: flex; align-items: center; gap: 6px; padding: 16px 8px 8px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg); z-index: 1; }
.group-head .gname { flex: 1; font-weight: 700; font-size: 15px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-head .gname span { color: var(--muted); font-weight: 400; }
.brand small { font-size: 11px; font-weight: 500; color: var(--muted); align-self: flex-end; margin-bottom: 3px; }

/* Gestor de presets y grupos */
.mgr-tools { padding: 0 12px 8px; display: flex; flex-direction: column; gap: 8px; }
.mgr-tools input[type=search] { width: 100%; min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg2); color: var(--text); padding: 0 12px; font: inherit; font-size: 16px; -webkit-appearance: none; -webkit-user-select: text; user-select: text; }
.mgr-btns { display: flex; gap: 6px; }
.mgr-btns .btn { flex: 1; }
.mgr-btns .btn.on { background: var(--text); color: #000; border-color: var(--text); }
.group-head { cursor: pointer; }
.group-head .chev { width: 18px; height: 18px; color: var(--muted); transition: transform .15s; flex: none; }
.group-head.collapsed .chev { transform: rotate(-90deg); }
.group-empty { padding: 10px 8px 14px; font-size: 13px; color: var(--muted); }
.preset-row .more { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 10px; color: var(--muted); flex: none; }
.preset-row .more:active { background: var(--bg3); }
.check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--muted); flex: none; display: grid; place-items: center; }
.check.on { background: var(--accent); border-color: var(--accent); }
.check.on::after { content: ''; width: 6px; height: 11px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg) translate(-1px, -1px); }
.sel-bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; align-items: center; gap: 6px; padding: 10px 12px calc(var(--safe-b) + 10px); background: var(--bg2); border-top: 1px solid var(--line); }
.sel-bar span { flex: 1; font-size: 13px; color: var(--muted); }
.sheet .menu-sub { font-size: 13px; color: var(--muted); margin: -6px 0 12px; }
.panel-tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 8px; }
.panel-tools .btn { padding: 0 4px; }
