:root {
  --bg: #0b0a09;
  --panel: #131210;
  --line: #262420;
  --text: #e9e4dc;
  --dim: #8d877d;
  --accent: #ff4a1c;
  --track: #2e2b27;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex; flex-direction: column; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.bar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line); background: var(--bg);
  flex: 0 0 auto;
}
.title { font-weight: 600; letter-spacing: .02em; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#meta { font-weight: 400; color: var(--dim); margin-left: 8px; font-variant-numeric: tabular-nums; }
.right { display: flex; gap: 8px; align-items: center; }
.btn {
  border: 1px solid var(--line); background: #1c1a17; border-radius: 8px;
  padding: 7px 13px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #24211d; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #140600; font-weight: 600; }
.btn.primary:hover { background: #ff6338; }
.btn.ghost { background: transparent; }
.btn.icon { padding: 6px 8px; display: inline-flex; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.big { padding: 12px 22px; font-size: 15px; }
.fmt {
  background: #1c1a17; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px;
}
body:not(.has-image) .needs-img { opacity: .35; pointer-events: none; }
body.busy { cursor: progress; }
body.busy #export { opacity: .6; }

/* ---------- layout ---------- */
main { flex: 1; display: flex; min-height: 0; }
#stage {
  flex: 1; position: relative; min-width: 0; display: flex; align-items: center; justify-content: center;
  padding: 24px; background:
    radial-gradient(ellipse at center, #15130f 0%, var(--bg) 70%);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; overflow: hidden;
}
#out { width: 100%; height: 100%; object-fit: contain; display: none; cursor: zoom-out; }
body.has-image #out { display: block; }
#empty { text-align: center; color: var(--dim); }
body.has-image #empty { display: none; }
.hint {
  position: absolute; bottom: 10px; left: 0; right: 0; text-align: center; color: var(--dim);
  font-size: 11px; pointer-events: none; opacity: .7;
}
body:not(.has-image) .hint { display: none; }
body.comparing .hint { color: var(--accent); opacity: 1; }
body.comparing .hint::before { content: "Original · "; }
.dropzone {
  position: absolute; inset: 12px; border: 2px dashed var(--accent); border-radius: 14px;
  display: none; align-items: center; justify-content: center; font-size: 18px; color: var(--accent);
  background: rgba(11,10,9,.75); pointer-events: none;
}
body.drag .dropzone { display: flex; }

#panel {
  width: 320px; flex: 0 0 auto; border-left: 1px solid var(--line); background: var(--panel);
  overflow-y: auto; overscroll-behavior: contain; padding-bottom: max(20px, env(safe-area-inset-bottom));
}
.presets {
  display: flex; gap: 6px; padding: 12px 14px; overflow-x: auto; scrollbar-width: none;
  border-bottom: 1px solid var(--line);
}
.phead { position: sticky; top: 0; z-index: 4; background: var(--panel); }

/* ---------- looks (presets) ---------- */
.lookgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 10px; padding-bottom: 6px; }
.look {
  border: 0; background: transparent; padding: 0; cursor: pointer; color: var(--dim); text-align: left;
  display: flex; flex-direction: column; gap: 5px; font-size: 11.5px;
}
.lk-img {
  display: block; aspect-ratio: 1; border-radius: 6px; overflow: hidden; background: #1c1a17;
  box-shadow: 0 0 0 1px var(--line);
}
.lk-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.lk-img .ph { display: block; width: 100%; height: 100%; background: linear-gradient(90deg, #1c1a17, #26231f, #1c1a17); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
.look:hover { color: var(--text); }
.look.on { color: var(--text); }
.look.on .lk-img { box-shadow: 0 0 0 2px var(--accent); }
body:not(.has-image) .lk-img { background: #1c1a17; }
.gnote { color: var(--dim); font-size: 11px; margin: 10px 0 0; }
.look { position: relative; }
.lk-dice, .lk-x {
  position: absolute; top: 5px; border: 0; border-radius: 6px; background: rgba(0,0,0,.62); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; width: 24px; height: 24px; padding: 0; cursor: pointer;
  opacity: 0; transition: opacity .12s;
}
.lk-dice { right: 5px; }
.lk-x { left: 5px; font-size: 15px; line-height: 1; }
.look:hover .lk-dice, .look:hover .lk-x, .look:focus-visible .lk-dice { opacity: 1; }
@media (hover: none) { .lk-dice, .lk-x { opacity: .9; } }
.look.mine .lk-name::before { content: "★ "; color: var(--accent); }
.saveform { display: flex; gap: 6px; margin: 0 0 12px; }
.saveform[hidden] { display: none; }
.saveform input {
  flex: 1; min-width: 0; background: #1c1a17; border: 1px solid var(--line); border-radius: 7px; padding: 6px 9px; font-size: 16px;
}
@media (min-width: 761px) { .saveform input { font-size: 13px; } }

/* ---------- tabs ---------- */
.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}
.tab {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 3px;
  border: 0; background: transparent; color: var(--dim); padding: 7px 2px 6px; border-radius: 9px; cursor: pointer;
  font-size: 10.5px; letter-spacing: .02em; white-space: nowrap;
}
.tab:hover { color: var(--text); background: #1c1a17; }
.tab.on { color: var(--text); background: #26231f; box-shadow: inset 0 0 0 1px #34302a; }
.tab .dot {
  position: absolute; top: 6px; right: calc(50% - 16px); width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent); display: none;
}
.tab.changed .dot { display: block; }
.group { display: none; border-bottom: 0; padding-top: 12px; }
.group.active { display: block; animation: fadein .16s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }
.ghead { display: flex; align-items: center; justify-content: space-between; margin: 0 0 10px; }
.ghead h2 { margin: 0; }
.gact { display: inline-flex; align-items: center; gap: 4px; }
.groll { opacity: .8; padding: 4px; }
.presets::-webkit-scrollbar { display: none; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 5px 11px;
  cursor: pointer; white-space: nowrap; color: var(--dim);
}
.chip:hover { color: var(--text); }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.group { padding: 10px 14px 6px; border-bottom: 1px solid var(--line); }
.group h2 {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .14em; color: var(--dim);
  margin: 4px 0 8px; font-weight: 600;
}
.row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 2px 8px; margin: 0 0 10px; }
.row .lbl { color: #bdb6ab; cursor: default; user-select: none; -webkit-user-select: none; }
.row.changed .lbl { color: var(--text); }
.row output { color: var(--dim); font-variant-numeric: tabular-nums; font-size: 12px; cursor: pointer; }
.row.changed output { color: var(--accent); }
.row input[type=range] { grid-column: 1 / -1; }
.row.select select {
  background: #1c1a17; border: 1px solid var(--line); border-radius: 7px; padding: 4px 6px; max-width: 170px;
}

input[type=range] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; margin: 0;
  --t: 0;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 3px; border-radius: 3px;
  background: linear-gradient(to right, var(--text) 0, var(--text) calc(var(--t) * 100%), var(--track) calc(var(--t) * 100%));
}
input[type=range]::-moz-range-track { height: 3px; border-radius: 3px; background: var(--track); }
input[type=range]::-moz-range-progress { height: 3px; background: var(--text); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 15px; height: 15px; border-radius: 50%; background: var(--text);
  margin-top: -6px; border: 0; box-shadow: 0 0 0 3px var(--panel);
}
input[type=range]::-moz-range-thumb { width: 15px; height: 15px; border-radius: 50%; background: var(--text); border: 0; }

.row.color .sw { display: flex; gap: 6px; grid-column: 1 / -1; margin-top: 4px; flex-wrap: wrap; }
.swatch {
  width: 22px; height: 22px; border-radius: 50%; border: 0; cursor: pointer; padding: 0;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.12);
}
.swatch.on { box-shadow: 0 0 0 2px var(--panel), 0 0 0 3.5px var(--text); }
.swatch.custom {
  position: relative; overflow: hidden;
  background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red);
}
.swatch.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.foot { padding: 14px; color: var(--dim); font-size: 11.5px; }
.foot p { margin: 10px 0 0; }

/* ---------- light leak placement ---------- */
.placebar { display: flex; gap: 6px; margin: -2px 0 12px; }
.h-place { display: none; }
body.placing .h-place { display: inline; color: var(--accent); opacity: 1; }
body.placing .h-compare { display: none; }
body.placing .hint { opacity: 1; }
body.placing #out { cursor: crosshair; }
#leakmark {
  position: absolute; left: 0; top: 0; width: 0; height: 0; pointer-events: none; display: none; z-index: 3;
}
body.placing-leak #leakmark { display: block; }
#maskmark {
  position: absolute; left: 0; top: 0; pointer-events: none; display: none; z-index: 3; overflow: visible;
  filter: drop-shadow(0 0 1px rgba(0,0,0,.9));
}
body.placing-mask #maskmark, body.mask-flash #maskmark { display: block; }
#leakmark::before {
  content: ""; position: absolute; left: -14px; top: -14px; width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.6), inset 0 0 0 1px rgba(0,0,0,.6);
}
#leakmark i {
  position: absolute; left: 0; top: -1px; width: 34px; height: 2px; background: #fff; transform-origin: 0 50%;
  box-shadow: 0 0 0 1px rgba(0,0,0,.5);
}

/* ---------- per-row re-roll ---------- */
.rv { display: inline-flex; align-items: center; gap: 6px; }
.roll {
  border: 0; background: transparent; color: var(--dim); padding: 2px; border-radius: 5px; cursor: pointer; display: inline-flex;
  opacity: .6;
}
.roll:hover { color: var(--text); opacity: 1; background: #24211d; }
.roll.spin svg { animation: spin .45s ease; }
@keyframes spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }
.row { touch-action: manipulation; }

/* ---------- film strip, undo/redo ---------- */
#view { flex: 1; display: flex; min-width: 0; min-height: 0; }
#strip {
  flex: 0 0 96px; display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 10px 0 14px; overflow-y: auto; overflow-x: hidden; border-right: 1px solid var(--line);
  background:
    radial-gradient(circle, var(--bg) 2.4px, transparent 2.9px) 6px 4px / 10px 14px repeat-y,
    radial-gradient(circle, var(--bg) 2.4px, transparent 2.9px) calc(100% - 6px) 4px / 10px 14px repeat-y,
    #1a1815;
  scrollbar-width: none;
}
#strip::-webkit-scrollbar { display: none; }
body:not(.has-image) #strip { display: none; }
.histbtns { display: flex; gap: 2px; }
.histbtns .btn { padding: 5px 7px; }
.histbtns .btn:disabled { opacity: .3; cursor: default; }
#frames { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.frame {
  position: relative; width: 68px; flex: 0 0 auto; border-radius: 3px; overflow: hidden; cursor: pointer;
  background: #000; box-shadow: 0 0 0 1px #000; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  touch-action: pan-y;
}
.frame img, .frame .ph { display: block; width: 68px; height: 68px; object-fit: cover; pointer-events: none; }
.frame .ph { background: linear-gradient(90deg, #222, #2c2a26, #222); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }
.frame .n {
  position: absolute; left: 0; right: 0; bottom: 0; font-size: 10px; text-align: center; padding: 1px 0;
  background: rgba(0,0,0,.55); color: #f1c48a; letter-spacing: .06em; font-variant-numeric: tabular-nums;
}
.frame.on { box-shadow: 0 0 0 2px var(--accent); }
.frame .x {
  position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; border-radius: 50%; border: 0; padding: 0;
  background: rgba(0,0,0,.7); color: #fff; font-size: 13px; line-height: 18px; cursor: pointer; opacity: 0;
}
.frame:hover .x { opacity: 1; }
@media (hover: none) { .frame .x { opacity: .85; } }
.frame.add {
  border: 1px dashed #4a463f; background: transparent; color: var(--dim); height: 68px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; box-shadow: none; font: inherit;
}
.frame.add span { font-size: 20px; line-height: 1; }
.frame.add small { font-size: 9.5px; letter-spacing: .04em; }
.frame.add:hover { color: var(--text); border-color: var(--dim); }
#peek {
  position: absolute; inset: 24px; width: calc(100% - 48px); height: calc(100% - 48px); object-fit: contain;
  display: none; pointer-events: none; z-index: 2;
}
body.peeking #peek { display: block; }
.h-peek { display: none; }
body.peeking .h-peek, body.comparing .h-peek:not(:empty) { display: inline; color: var(--accent); }
body.peeking .h-compare, body.peeking .h-place { display: none; }
body.peeking .hint { opacity: 1; }
body.peeking .hint::before { content: none; }

/* ---------- export sheet (phones) ---------- */
#sheet {
  position: fixed; inset: 0; background: rgba(0,0,0,.88); z-index: 20;
  display: flex; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
#sheet[hidden] { display: none; }
.sheet-inner { display: flex; flex-direction: column; align-items: center; gap: 12px; max-height: 100%; width: 100%; }
#sheet-img { max-width: 100%; max-height: 70vh; object-fit: contain; -webkit-touch-callout: default; user-select: auto; }
.sheet-tip { margin: 0; color: var(--dim); }
.sheet-btns { display: flex; gap: 8px; }

#toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #2a2723; border: 1px solid var(--line); padding: 9px 14px; border-radius: 9px; z-index: 30;
}
#toast[hidden] { display: none; }
.err { color: var(--accent); max-width: 380px; }

/* ---------- phones ---------- */
@media (max-width: 760px) {
  body { overflow: hidden; height: 100vh; height: 100dvh; }
  main { flex-direction: column; }
  #view { flex: 0 0 auto; flex-direction: column-reverse; }
  #stage { flex: 0 0 auto; height: 42vh; height: 42dvh; padding: 8px; }
  #peek { inset: 10px; width: calc(100% - 20px); height: calc(100% - 20px); }
  #strip {
    flex: 0 0 auto; flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-top: 1px solid var(--line);
    padding: 10px 10px; gap: 8px;
    background:
      radial-gradient(circle, var(--bg) 2.2px, transparent 2.7px) 4px 4px / 12px 10px repeat-x,
      radial-gradient(circle, var(--bg) 2.2px, transparent 2.7px) 4px calc(100% - 4px) / 12px 10px repeat-x,
      #1a1815;
  }
  .histbtns { flex-direction: column; }
  #frames { flex-direction: row; gap: 8px; }
  .frame { width: 46px; touch-action: pan-x; }
  .frame img, .frame .ph { width: 46px; height: 46px; }
  .frame.add { height: 46px; }
  .frame.add small { font-size: 8px; }
  .frame .n { font-size: 9px; }
  .histbtns { flex-direction: row; }
  #panel { width: auto; border-left: 0; border-top: 1px solid var(--line); flex: 1; min-height: 0; overflow-y: auto; }
  .title #meta { display: none; }
  .bar { gap: 6px; padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .right { gap: 6px; }
  .fmt { max-width: 86px; }
  .hint { bottom: 2px; }
  .row { margin-bottom: 12px; }
  .tabs {
    display: flex; overflow-x: auto; scrollbar-width: none; gap: 2px; padding: 6px 8px;
    scroll-snap-type: x proximity;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { flex: 0 0 auto; min-width: 58px; padding: 7px 6px 6px; scroll-snap-align: start; }
  .presets { padding: 10px 12px; }
  input[type=range] { height: 28px; }
  input[type=range]::-webkit-slider-thumb { width: 20px; height: 20px; margin-top: -8.5px; }
}
@media (max-width: 420px) { .title { visibility: hidden; } #vary { padding: 7px 9px; } }

/* macOS shell: a little more air under the title bar */
.mac .bar { padding-top: 8px; }
