:root {
  --bg: #111;
  --panel: #161616;
  --line: #2a2a2a;
  --text: #eceae6;
  --muted: #9a958c;
  --accent: #c4a574;
  --paper: #f4f1ea;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #111;
  color: var(--text);
  font: 15px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow-x: hidden;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

body {
  padding-top: env(safe-area-inset-top);
}

button, select, input, .file-btn { font: inherit; }

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  min-height: 0;
  background: #111;
  overflow: hidden;
}

.top {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  align-items: center;
  min-width: 0;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: #111;
  z-index: 20;
}

.top-spacer { flex: 1 1 auto; min-width: 8px; }

.brand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1px;
  min-width: 0;
  flex: 0 1 auto;
  line-height: 1.15;
}

.brand-name {
  font-weight: 600;
  letter-spacing: 0.04em;
  font-size: 14px;
  white-space: nowrap;
}

.brand-by {
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--muted);
  white-space: nowrap;
}

.tier, .unlock-btn, button, select, .file-btn {
  background: #1c1c1c;
  color: var(--text);
  border: 1px solid #333;
  border-radius: 999px;
  padding: 7px 12px;
}

button { cursor: pointer; }
button:hover, .file-btn:hover, .tier:hover { border-color: var(--accent); }

.tier {
  color: var(--accent);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 11px;
  padding: 6px 10px;
  flex: 0 0 auto;
}

.unlock-btn {
  color: var(--accent);
  border-color: #5c4d34;
  background: transparent;
  flex: 0 0 auto;
}

#exportBtn {
  background: var(--paper);
  color: #161616;
  border-color: var(--paper);
  font-weight: 600;
  flex: 0 0 auto;
}

.file-btn { position: relative; overflow: hidden; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.mobile-only { display: none; }

.stage-wrap {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: #111;
}

.stage {
  position: relative;
  flex: 1;
  margin: 16px;
  background: #111;
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.histogram {
  display: none;
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 6;
  width: min(236px, calc(100% - 24px));
  padding: 8px;
  background: #111;
  border: 1px solid #3a3a3a;
}

@media (min-width: 769px) {
  .stage.has-image .histogram { display: block; }
}

.hist-head { display: flex; gap: 6px; margin-bottom: 6px; }
.hist-head button { flex: 1; min-width: 0; padding: 6px 8px; font-size: 12px; border-radius: 8px; }

#clipToggle { border-color: #e23b3b; color: #ffb4b4; font-weight: 600; }
#clipToggle[aria-pressed="true"] { background: #e23b3b; color: #140404; border-color: #e23b3b; }
#stopsToggle[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
#clipToggle kbd { font: inherit; opacity: 0.8; }

#histCanvas {
  display: block;
  width: 100%;
  height: 64px;
  background: #111;
}

.stops-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
}
.stops-legend span { display: inline-flex; align-items: center; gap: 4px; }
.stops-legend i { width: 8px; height: 8px; display: inline-block; }

.frame {
  position: relative;
  display: none;
  max-width: 100%;
  max-height: 100%;
  background: #111;
}

.stage.has-image .frame { display: block; max-height: calc(100% - 28px); }

.frame img {
  display: block;
  max-width: 100%;
  max-height: calc(100dvh - 180px);
  width: auto;
  height: auto;
  background: #111;
}

#beforeImg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  object-fit: fill;
}

#divider {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 28px;
  margin-left: -14px;
  cursor: ew-resize;
  pointer-events: auto;
  touch-action: none;
  z-index: 4;
}

#divider::after {
  content: "";
  position: absolute;
  left: 13px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #f2f2f2;
}

#split {
  position: absolute;
  left: 12px;
  bottom: 8px;
  width: calc(100% - 24px);
  height: 28px;
  margin: 0;
  display: none;
  accent-color: #bdb7ad;
}

.stage.has-image #split { display: block; }

.drop {
  color: var(--muted);
  text-align: center;
  max-width: min(420px, 100%);
  padding: 24px 16px;
}

.drop p { margin: 0 0 12px; }

.stage.has-image .drop { display: none; }

.meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  padding: 0 16px 12px;
  color: var(--muted);
  font-size: 12px;
}

.meta span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.controls {
  border-left: 1px solid var(--line);
  background: var(--panel);
  overflow: auto;
  min-width: 0;
  min-height: 0;
  padding: 8px 16px calc(28px + env(safe-area-inset-bottom));
}

.panel { display: block; margin: 0 0 8px; }
.panel h2, .tweaks h2, .signature span {
  margin: 14px 0 8px;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.positioning, .inspect-copy, .modal-lead {
  color: var(--muted);
  font-size: 13px;
  margin: 0 0 12px;
}

.signature, .exports, .row-actions, .panel-tools, .install-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.signature { margin-bottom: 10px; }
.signature button.active { border-color: var(--accent); color: var(--accent); }

.pro-mark {
  display: inline-block;
  margin-left: 6px;
  color: var(--accent);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

body[data-tier="pro"] .pro-mark { display: none; }

.field, .print-space, .check, .sr-backend, .sharpen-row label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  margin: 8px 0;
  min-width: 0;
}

.field select, .print-space select, .sr-backend select, .sharpen-row select, .point input, #licenseKey {
  width: 100%;
  max-width: 100%;
  background: #111;
  color: var(--text);
  border: 1px solid #333;
  border-radius: 8px;
  padding: 8px 10px;
}

.check { flex-direction: row; align-items: center; color: var(--text); font-size: 14px; }

.sr-row, .sr-choices { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sr-row { margin: 4px 0 8px; }
.sr-row > span, .sr-backend { color: var(--muted); font-size: 12px; }
.sr-choices button.active { border-color: var(--accent); color: var(--accent); }
.sr-backend { flex-direction: row; align-items: center; }
.sr-backend select { flex: 1; }

button.auto-grade {
  width: 100%;
  margin: 2px 0 10px;
  border-color: var(--accent);
  color: var(--accent);
  border-radius: 10px;
}

.tweaks {
  margin: 8px 0 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.export-tip {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}
.exports { margin-top: 8px; }
.exports button { flex: 1 1 140px; text-align: left; border-radius: 10px; }
.batch-btn { margin-top: 8px; border-radius: 10px; }

.sharpen-row { display: flex; gap: 8px; margin-top: 8px; }
.sharpen-row label { flex: 1; margin: 0; }

.group { margin: 14px 0 18px; }

.slider {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 2px 8px;
  margin: 8px 0;
}

.slider label { color: var(--text); min-width: 0; }
.slider .value { color: var(--muted); font-variant-numeric: tabular-nums; }
.slider input[type="range"] { grid-column: 1 / -1; width: 100%; accent-color: #bdb7ad; }

.points { display: flex; flex-direction: column; gap: 6px; }
.point { display: flex; gap: 6px; min-width: 0; }
.point input { width: 72px; }
.point button, .points > button { border-radius: 8px; }

.tabbar {
  display: none;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  background: #141414;
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar button {
  min-width: 0;
  min-height: 48px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 2px;
  font-size: 11px;
  letter-spacing: 0.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabbar button.active { color: var(--paper); }

.install-sheet {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 30;
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.install-sheet[hidden] { display: none; }

.install-card, .modal-card {
  width: min(440px, 100%);
  background: #161616;
  border: 1px solid #333;
  border-radius: 16px;
  padding: 16px 16px 14px;
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.45);
}

.install-card { pointer-events: auto; }
.install-card p, .install-card ol { margin: 0 0 8px; color: var(--text); }
.install-card ol { padding-left: 18px; color: var(--muted); }
#installTitle { font-weight: 600; }

.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.62);
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  place-items: center;
}

.modal:not([hidden]) { display: grid; }

.modal-card {
  max-height: min(90dvh, 760px);
  overflow: auto;
  border-radius: 18px;
}

.modal-card h2 { margin: 0 0 8px; font-size: 22px; font-weight: 600; letter-spacing: 0; text-transform: none; color: var(--paper); }
.pro-list { margin: 0 0 16px; padding-left: 18px; }
.pro-list li { margin: 6px 0; }

.gumroad {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 44px;
  margin-bottom: 14px;
  border-radius: 999px;
  background: var(--paper);
  color: #161616;
  text-decoration: none;
  font-weight: 600;
}

#unlockFeedback { min-height: 1.2em; margin: 8px 0 0; }
#unlockFeedback[data-state="ok"] { color: #d5c4a1; }
#unlockFeedback[data-state="bad"] { color: #ffb4b4; }

.text-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  padding: 0;
  border-radius: 0;
}

#unlockClose { float: right; }

body.modal-open { overflow: hidden; }

@media (display-mode: standalone) {
  .install-sheet { display: none !important; }
}

@media (max-width: 768px) {
  .app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
  }

  .top { position: sticky; top: 0; flex: 0 0 auto; padding: 10px 12px; }
  .desktop-only { display: none !important; }
  #unlockBtn { display: none !important; }
  .mobile-only { display: inline-flex; }

  .stage-wrap { flex: 1 1 auto; min-height: 0; }
  .stage { margin: 8px 8px 0; }
  .frame, .frame img { max-height: 100%; }
  .meta { padding: 6px 12px 8px; }

  .controls {
    flex: 0 0 auto;
    max-height: 38vh;
    border-left: 0;
    border-top: 1px solid var(--line);
    padding: 4px 12px 12px;
  }

  .panel { display: none; }
  .panel.active { display: block; }

  .histogram { display: none; position: static; width: 100%; margin-top: 8px; }
  body[data-tab="inspect"].has-image .histogram { display: block; }

  .slider input[type="range"] { height: 44px; }
  #split { height: 44px; }

  .tabbar { display: grid; flex: 0 0 auto; }

  .install-sheet { bottom: calc(64px + env(safe-area-inset-bottom)); }

  .signature button, .exports button, .file-btn, #exportBtn, .tier {
    min-height: 44px;
  }
}
