:root{
  --bg: oklch(0.16 0.006 80);
  --panel: oklch(0.20 0.006 80);
  --panel-2: oklch(0.235 0.006 80);
  --line: oklch(0.30 0.006 80);
  --line-soft: oklch(0.26 0.006 80);
  --fg: oklch(0.96 0.004 80);
  --fg-2: oklch(0.80 0.005 80);
  --fg-3: oklch(0.62 0.006 80);
  --accent: oklch(0.78 0.13 65);
  --accent-2: oklch(0.85 0.10 65);
  --danger: oklch(0.72 0.15 30);
  --radius: 6px;
  --gap: 14px;
}
*{ box-sizing: border-box; }
html, body { height: 100%; }
body{
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 13px/1.4 "Geist", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
.mono { font-family: "Geist Mono", ui-monospace, monospace; }
button { font: inherit; color: inherit; }

/* App shell */
.app{
  display: grid;
  grid-template-columns: 1fr 340px;
  grid-template-rows: 44px 1fr;
  height: 100vh;
}
header{
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand{
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.brand-mark{
  width: 22px; height: 22px;
  border-radius: 4px;
  background:
    linear-gradient(180deg, transparent 0%, transparent 50%, oklch(0.96 0.004 80) 50%, oklch(0.96 0.004 80) 100%),
    linear-gradient(90deg, oklch(0.20 0.006 80), oklch(0.20 0.006 80));
  border: 1px solid var(--line);
}
.brand-sub{
  color: var(--fg-3);
  font-weight: 400;
  margin-left: 4px;
  font-size: 12px;
}
.header-actions{
  margin-left: auto;
  display: flex;
  gap: 8px;
}
.btn{
  height: 30px;
  padding: 0 12px;
  border-radius: 5px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  transition: background .12s, border-color .12s, transform .05s;
}
.btn:hover{ background: oklch(0.27 0.006 80); border-color: oklch(0.38 0.006 80); }
.btn:active{ transform: translateY(1px); }
.btn[disabled]{ opacity: .4; cursor: not-allowed; }
.btn.primary{
  background: var(--accent);
  color: oklch(0.16 0.006 80);
  border-color: transparent;
  font-weight: 500;
}
.btn.primary:hover{ background: var(--accent-2); }
.btn .kbd{
  color: var(--fg-3);
  font-size: 11px;
  margin-left: 4px;
  font-family: "Geist Mono", monospace;
}
.btn.primary .kbd{ color: oklch(0.16 0.006 80 / 0.55); }
.icon{ width: 14px; height: 14px; }

/* Canvas area */
.stage{
  position: relative;
  background:
    linear-gradient(45deg, oklch(0.18 0.006 80) 25%, transparent 25%) -8px 0/16px 16px,
    linear-gradient(-45deg, oklch(0.18 0.006 80) 25%, transparent 25%) -8px 0/16px 16px,
    linear-gradient(45deg, transparent 75%, oklch(0.18 0.006 80) 75%) 0 8px/16px 16px,
    linear-gradient(-45deg, transparent 75%, oklch(0.18 0.006 80) 75%) 0 8px/16px 16px,
    oklch(0.14 0.006 80);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.canvas-wrap{
  position: relative;
  display: inline-block;
  max-width: calc(100% - 48px);
  max-height: calc(100% - 48px);
  line-height: 0;
}
.canvas-wrap canvas{
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 44px - 48px);
  box-shadow: 0 20px 60px rgba(0,0,0,.5), 0 0 0 1px oklch(0.10 0.006 80);
  image-rendering: pixelated;
}
.canvas-wrap canvas.fit-smooth{ image-rendering: auto; }

.crop-overlay{
  position: absolute;
  inset: 0;
  overflow: hidden;
  cursor: crosshair;
  user-select: none;
  touch-action: none;
}
.crop-rect{
  position: absolute;
  border: 1px solid oklch(0.96 0.004 80);
  box-shadow: 0 0 0 99999px oklch(0.08 0.006 80 / 0.55);
  pointer-events: none;
}
.crop-rect::before,
.crop-rect::after{
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(to right, oklch(0.40 0.006 80 / 0.5) 1px, transparent 1px) 33.33% 0/33.33% 100%,
    linear-gradient(to right, oklch(0.40 0.006 80 / 0.5) 1px, transparent 1px) 66.66% 0/33.33% 100%,
    linear-gradient(to bottom, oklch(0.40 0.006 80 / 0.5) 1px, transparent 1px) 0 33.33%/100% 33.33%,
    linear-gradient(to bottom, oklch(0.40 0.006 80 / 0.5) 1px, transparent 1px) 0 66.66%/100% 33.33%;
}
.crop-rect.empty{ box-shadow: 0 0 0 99999px oklch(0.08 0.006 80 / 0.55); border-color: transparent; }
.crop-empty-msg{
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  color: var(--fg-2);
  font-size: 12.5px;
  background: oklch(0.16 0.006 80 / 0.85);
  padding: 6px 10px;
  border-radius: 4px;
  pointer-events: none;
  letter-spacing: 0.01em;
}

.perspective-overlay{
  position: absolute;
  inset: 0;
  pointer-events: none;
  user-select: none;
  touch-action: none;
}
.perspective-svg{
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.quad-handle{
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: oklch(0.96 0.004 80);
  border: 1.5px solid oklch(0.16 0.006 80);
  box-shadow: 0 0 0 1px oklch(0.40 0.006 80), 0 2px 6px rgba(0,0,0,.5);
  transform: translate(-50%, -50%);
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
}
.quad-handle:hover{ background: var(--accent); }
.quad-handle:active{ cursor: grabbing; background: var(--accent); }
.quad-handle.active{
  background: var(--accent);
  box-shadow: 0 0 0 2px oklch(0.16 0.006 80), 0 0 0 4px var(--accent), 0 2px 8px rgba(0,0,0,.55);
}

.stage-processing{
  position: absolute;
  inset: 0;
  background: oklch(0.10 0.006 80 / 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  pointer-events: auto;
}
.stage-processing-inner{
  background: oklch(0.20 0.006 80);
  color: var(--fg);
  border: 1px solid var(--line);
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 12.5px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}

.aspect-select{
  height: 26px;
  background: transparent;
  color: var(--fg);
  border: 0;
  border-radius: 4px;
  padding: 0 6px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--fg-2) 50%), linear-gradient(-45deg, transparent 50%, var(--fg-2) 50%);
  background-position: calc(100% - 9px) 50%, calc(100% - 5px) 50%;
  background-size: 4px 4px;
  background-repeat: no-repeat;
  padding-right: 18px;
}
.aspect-select:hover{ background-color: oklch(0.30 0.006 80 / 0.5); }
.aspect-select option{ background: var(--panel); color: var(--fg); }

.empty{
  position: absolute;
  inset: 24px;
  border: 1.5px dashed var(--line);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--fg-2);
  background: oklch(0.16 0.006 80 / 0.7);
  transition: border-color .15s, background .15s;
}
.empty.drag{
  border-color: var(--accent);
  background: oklch(0.78 0.13 65 / 0.08);
}
.empty h2{
  margin: 0;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: -0.01em;
}
.empty p{
  margin: 0;
  color: var(--fg-3);
  font-size: 12.5px;
}
.empty .glyph{
  width: 56px; height: 56px;
  border-radius: 8px;
  border: 1px solid var(--line);
  display: grid;
  place-items: center;
  background: var(--panel);
  color: var(--fg-2);
}
.empty .actions{
  display: flex; gap: 8px; margin-top: 4px;
}
.empty .hint{
  position: absolute;
  bottom: 18px;
  color: var(--fg-3);
  font-size: 11.5px;
}

.stage-overlay{
  position: absolute;
  top: 12px; left: 12px;
  display: flex; gap: 6px;
  pointer-events: none;
}
.chip{
  background: oklch(0.16 0.006 80 / 0.7);
  backdrop-filter: blur(8px);
  border: 1px solid oklch(0.40 0.006 80 / 0.4);
  color: var(--fg-2);
  font-size: 11.5px;
  padding: 4px 8px;
  border-radius: 4px;
  pointer-events: auto;
  font-family: "Geist Mono", monospace;
}
.stage-controls{
  position: absolute;
  bottom: 12px; left: 50%;
  transform: translateX(-50%);
  display: flex; gap: 4px;
  background: oklch(0.16 0.006 80 / 0.75);
  backdrop-filter: blur(8px);
  border: 1px solid oklch(0.40 0.006 80 / 0.4);
  padding: 4px;
  border-radius: 7px;
}
.toggle-btn{
  height: 26px;
  padding: 0 10px;
  background: transparent;
  border: 0;
  color: var(--fg-2);
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.toggle-btn:hover{ background: oklch(0.30 0.006 80 / 0.5); color: var(--fg); }
.toggle-btn.on{ background: oklch(0.30 0.006 80); color: var(--fg); }
.toggle-btn.held{ background: var(--accent); color: oklch(0.16 0.006 80); }
.toggle-btn[disabled]{
  opacity: .45;
  cursor: not-allowed;
}
.toggle-btn[disabled]:hover{ background: transparent; color: var(--fg-2); }
.toggle-btn.on[disabled]{ background: oklch(0.30 0.006 80 / 0.55); }

/* Sidebar */
aside{
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex; flex-direction: column;
}
aside::-webkit-scrollbar{ width: 8px; }
aside::-webkit-scrollbar-thumb{ background: var(--line); border-radius: 4px; }

section.panel{
  border-bottom: 1px solid var(--line);
}
.panel-head{
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 10px;
}
.panel-head .title{
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-2);
  font-weight: 500;
}
.panel-head .meta{
  color: var(--fg-3);
  font-size: 11px;
}
.panel-body{
  padding: 4px 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.reset-link{
  background: none;
  border: 0;
  color: var(--fg-3);
  cursor: pointer;
  font-size: 11px;
  padding: 2px 4px;
  border-radius: 3px;
}
.reset-link:hover{ color: var(--fg); background: var(--panel-2); }

/* Sliders */
.row{
  display: grid;
  grid-template-columns: 64px 1fr 78px;
  align-items: center;
  gap: 10px;
}
.row .label{
  color: var(--fg-2);
  font-size: 12px;
  display: flex; align-items: center; gap: 6px;
}
.row .label .swatch{
  width: 9px; height: 9px; border-radius: 2px;
  border: 1px solid oklch(0 0 0 / 0.3);
}
.row input[type=number]{
  height: 24px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--fg);
  border-radius: 4px;
  text-align: right;
  padding: 0 6px;
  font-family: "Geist Mono", monospace;
  font-size: 11.5px;
  width: 100%;
  -moz-appearance: textfield;
  appearance: textfield;
}
.row input[type=number]:focus{ outline: 1px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.row input[type=number]::-webkit-outer-spin-button,
.row input[type=number]::-webkit-inner-spin-button{ -webkit-appearance: none; margin: 0; display: none; }

.slider-track{
  position: relative;
  height: 18px;
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
  outline: 0;
  border-radius: 4px;
}
.slider-track:focus-visible{
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}
.slider-track .rail{
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--rail-from, oklch(0.30 0.006 80)) 0%, var(--rail-to, oklch(0.55 0.006 80)) 100%);
  position: relative;
}
.slider-track .rail.signed::before{
  content:"";
  position: absolute;
  left: 50%; top: -3px; bottom: -3px;
  width: 1px;
  background: oklch(0.45 0.006 80);
}
.slider-track .thumb{
  position: absolute;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--fg);
  border: 2px solid oklch(0.16 0.006 80);
  box-shadow: 0 0 0 1px oklch(0.40 0.006 80), 0 1px 4px rgba(0,0,0,.4);
  transform: translateX(-50%);
  pointer-events: none;
}

/* Levels histogram + handles */
.levels-graph{
  position: relative;
  height: 84px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  overflow: hidden;
}
.levels-graph canvas{
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.levels-axis{
  position: relative;
  height: 26px;
  background: linear-gradient(90deg, #000 0%, #fff 100%);
  border-radius: 3px;
  margin-top: 6px;
  cursor: pointer;
  user-select: none;
}
.levels-axis .handle{
  position: absolute;
  bottom: -6px;
  width: 0; height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 9px solid var(--c);
  transform: translateX(-50%);
  cursor: ew-resize;
  filter: drop-shadow(0 0 1px rgba(0,0,0,0.6));
}
.levels-axis .handle.shadow{ --c: #111; }
.levels-axis .handle.midtone{ --c: #888; }
.levels-axis .handle.highlight{ --c: #fff; }
.levels-axis .handle:focus-visible{
  outline: none;
  filter: drop-shadow(0 0 0 var(--accent)) drop-shadow(0 0 2px var(--accent));
}
.levels-output{
  position: relative;
  height: 10px;
  background: linear-gradient(90deg, #000 0%, #fff 100%);
  border-radius: 3px;
  margin-top: 18px;
  cursor: pointer;
  user-select: none;
}
.levels-output .handle{
  position: absolute;
  top: 50%;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--c);
  border: 1.5px solid oklch(0.20 0.006 80);
  box-shadow: 0 0 0 1px oklch(0.40 0.006 80);
  transform: translate(-50%, -50%);
  cursor: ew-resize;
}
.levels-output .handle.out-shadow{ --c: #111; }
.levels-output .handle.out-highlight{ --c: #fff; }
.levels-output .handle:focus-visible{
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.levels-values{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  margin-top: 10px;
}
.levels-values .v{
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 4px 6px;
  font-family: "Geist Mono", monospace;
  font-size: 11px;
  text-align: center;
  color: var(--fg);
}
.levels-values .v .lab{
  color: var(--fg-3);
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: block;
  margin-bottom: 2px;
}
.levels-values input{
  background: transparent;
  border: 0;
  color: var(--fg);
  width: 100%;
  text-align: center;
  font-family: inherit;
  font-size: 11px;
  padding: 0;
  -moz-appearance: textfield;
  appearance: textfield;
}
.levels-values input::-webkit-outer-spin-button,
.levels-values input::-webkit-inner-spin-button{ -webkit-appearance: none; margin: 0; display: none; }
.levels-values input:focus{ outline: 1px solid var(--accent); border-radius: 2px; }

.stepper{
  display: flex;
  align-items: center;
  gap: 2px;
}
.stepper input{ flex: 1; min-width: 0; }
.step-btn{
  background: transparent;
  border: 0;
  padding: 0;
  width: 14px;
  height: 16px;
  border-radius: 3px;
  color: var(--fg-3);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.5;
  flex: 0 0 auto;
  transition: opacity .12s, color .12s, background .12s;
}
.step-btn svg{ display: block; }
.levels-values .v:hover .step-btn{ opacity: 1; }
.step-btn:hover{ color: var(--fg); background: oklch(0.30 0.006 80); }
.step-btn:active{ color: var(--accent); background: oklch(0.34 0.006 80); }
.step-btn[disabled]{ opacity: 0.2; cursor: not-allowed; }
.step-btn[disabled]:hover{ background: transparent; color: var(--fg-3); }
.step-btn:focus-visible{ outline: 1px solid var(--accent); outline-offset: 1px; }

/* Stepper variant inside a .row: unified bordered control */
.row .stepper{
  height: 24px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  gap: 0;
  overflow: hidden;
}
.row .stepper:focus-within{
  outline: 1px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
}
.row .stepper input[type=number]{
  height: 100%;
  background: transparent;
  border: 0;
  border-radius: 0;
  text-align: right;
  padding: 0 4px;
  min-width: 0;
  width: auto;
}
.row .stepper input[type=number]:focus{ outline: 0; }
.row .step-btn{
  height: 100%;
  width: 16px;
  border-radius: 0;
  opacity: 0.55;
}
.row:hover .step-btn,
.row .stepper:focus-within .step-btn{ opacity: 1; }
.row .step-btn:hover{ background: oklch(0.30 0.006 80); color: var(--fg); }
.row .step-btn[disabled]{ opacity: 0.2; cursor: not-allowed; }

.sublabel{
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-3);
  margin-top: 4px;
}

/* Preset row */
.presets{
  display: flex; gap: 4px; flex-wrap: wrap;
}
.preset{
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--fg-2);
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  cursor: pointer;
}
.preset:hover{ color: var(--fg); border-color: oklch(0.40 0.006 80); }
.preset.active{ background: oklch(0.78 0.13 65 / 0.18); color: var(--accent-2); border-color: oklch(0.78 0.13 65 / 0.5); }

/* Toggles & misc */
.switch{
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  font-size: 12px; color: var(--fg-2);
}
.switch input{
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  overflow: hidden;
  white-space: nowrap;
}
.switch input:focus-visible + .track{
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.switch .track{
  width: 26px; height: 14px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  position: relative;
  transition: background .15s;
}
.switch .track::after{
  content: "";
  position: absolute;
  width: 10px; height: 10px;
  background: var(--fg-2);
  border-radius: 50%;
  top: 1px; left: 1px;
  transition: left .15s, background .15s;
}
.switch input:checked + .track{ background: var(--accent); border-color: transparent; }
.switch input:checked + .track::after{ left: 14px; background: oklch(0.16 0.006 80); }

.toast{
  position: fixed;
  bottom: 20px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: oklch(0.96 0.004 80);
  color: oklch(0.16 0.006 80);
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 12.5px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
  opacity: 0;
  transition: transform .2s, opacity .2s;
  pointer-events: none;
  z-index: 50;
}
.toast.show{ opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error{ background: var(--danger); color: oklch(0.16 0.006 80); }

.file-input{ display: none; }

/* Narrow / mobile viewports */
@media (max-width: 720px) {
  body{ overflow: auto; }
  .app{
    grid-template-columns: 1fr;
    grid-template-rows: 44px minmax(50vh, auto) auto;
    height: auto;
    min-height: 100vh;
  }
  aside{
    width: auto;
    border-left: 0;
    border-top: 1px solid var(--line);
    overflow-y: visible;
  }
  header{
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .brand, .header-actions{ flex-shrink: 0; }
  .brand-sub{ display: none; }
  .header-actions .btn .kbd{ display: none; }
  .header-actions{ gap: 6px; }
  .header-actions .btn{ padding: 0 9px; }
  .stage{ min-height: 50vh; }
  .canvas-wrap canvas{
    max-height: calc(100vh - 44px - 80px);
  }
  .stage-controls{
    flex-wrap: wrap;
    max-width: calc(100% - 24px);
    justify-content: center;
  }
}
