/* ── Edit Mode (local build, flag-gated) — @barbotaipa's concept ─────────── */

#editMode {
  display: none;
  position: fixed; inset: 0; z-index: 9500;
  background: var(--bg);
  grid-template-columns: 1fr 340px;
  grid-template-rows: 1fr auto;
  gap: 0;
}

#emCanvasWrap {
  grid-row: 1; grid-column: 1;
  position: relative; overflow: hidden;
  background: var(--bg);
}
#emCanvas { display: block; touch-action: none; }

/* Everything floating OVER the image is a HUD: an OPAQUE dark plate with FIXED
   light text, both themes, any art — the theme's --surface vars are translucent
   by design and a bright image washes straight through them (owner screenshot,
   2026-08-24; same class of bug as CLAUDE.md's opaque-plate rule for overlays). */
.em-hud, #emRail {
  background: rgba(16, 16, 21, 0.82);
  -webkit-backdrop-filter: blur(14px) saturate(0.9);
  backdrop-filter: blur(14px) saturate(0.9);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 12px;
  color: #f2f2f4;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
}
.em-hud { padding: 6px 8px; }
.em-hud .download-btn {
  background: transparent;
  color: #f2f2f4;
  border-color: rgba(255, 255, 255, 0.22);
}
.em-hud .download-btn:hover { background: rgba(255, 255, 255, 0.12); color: #fff; border-color: rgba(255, 255, 255, 0.4); }
.em-hud .active-mode { border-color: var(--accent); color: var(--accent); }
.em-hud-dim { color: #b9b9c2; }

/* tool rail floats over the canvas' right edge, like the mock */
#emRail {
  position: absolute; right: 14px; top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; gap: 6px;
  padding: 6px; z-index: 3;
}
.em-tool {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  width: 44px; padding: 7px 0 5px;
  background: transparent; border: 0; border-radius: 9px;
  color: #f2f2f4; cursor: pointer; font: inherit;
}
.em-tool:hover { background: rgba(255, 255, 255, 0.12); }
.em-tool.active { background: var(--accent); color: #fff; }
.em-tool.soon { opacity: .3; cursor: default; }
.em-tool-glyph { font-size: 15px; line-height: 1.1; }
.em-tool-key { font-size: 9.5px; opacity: .75; letter-spacing: .04em; }

/* contextual chips live in a SIDE column (owner ask: off the artwork), on the
   canvas's left edge — the tool rail owns the right */
#emSideChips {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 3;
  max-width: 200px;
}
#emSideChips > div { flex-direction: column; align-items: stretch; }
#emSideChips .download-btn { justify-content: flex-start; white-space: nowrap; }
#emCropChips { display: none; gap: 8px; }

/* right panel */
#emPanel {
  grid-row: 1 / span 2; grid-column: 2;
  border-left: 1px solid var(--border);
  background: var(--surface-0, var(--bg));
  display: flex; flex-direction: column;
  padding: 14px 16px; gap: 12px; overflow-y: auto;
}
#emTabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); }
.em-tab-btn {
  flex: 1; background: transparent; border: 0; color: var(--text-dim);
  font: inherit; font-size: 12px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; padding: 9px 0 10px; cursor: pointer;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.em-tab-btn.active { color: var(--text); border-bottom-color: var(--accent); }

.em-row { display: flex; align-items: center; gap: 10px; font-size: 12.5px; }
.em-row label { min-width: 74px; color: var(--text-dim); }
.em-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.em-row .em-val { min-width: 30px; text-align: right; color: var(--text-faint); font-variant-numeric: tabular-nums; }

.em-layer {
  display: flex; align-items: center; gap: 9px;
  border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 12px; font-size: 12.5px;
}
.em-layer b { flex: 1; font-weight: 600; }
.em-layer .lock { color: var(--text-faint); font-size: 11px; }

#emHist {
  width: 100%; height: 110px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface);
}

#emHistory {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; font-size: 12px; display: flex; flex-direction: column; gap: 6px;
}
#emHistory .em-row { justify-content: space-between; }
#emHistory .em-key { color: var(--text-faint); font-size: 11px; }
#emHistory button { background: none; border: 0; color: var(--text); cursor: pointer; font: inherit; padding: 0; }
#emHistory button:hover { color: var(--accent); }

/* quiet ghost row — takes over #emExits' bottom-pinning since it sits above it */
#emFeedback {
  margin-top: auto; width: 100%; background: none; border: 0; cursor: pointer;
  color: var(--text-faint); font: inherit; font-size: 11.5px; padding: 8px 0 0;
  text-align: center;
}
#emFeedback:hover { color: var(--accent); }

#emExits { display: flex; gap: 8px; padding-top: 10px; }
#emExits button {
  flex: 1; border-radius: 10px; padding: 10px 6px; font: inherit; font-size: 12.5px;
  cursor: pointer; display: flex; flex-direction: column; gap: 2px; align-items: center;
}
#emExits .sub { font-size: 9.5px; opacity: .7; }
#emDiscard { background: var(--surface-2); color: var(--text); border: 1px solid var(--border-strong); }
#emSaveNew { background: var(--surface-2); color: var(--text); border: 1px solid var(--accent); }
#emApply   { background: var(--accent); color: #fff; border: 1px solid var(--accent); opacity: .45; cursor: default; }

/* bottom bar under the canvas */
#emBottomBar {
  grid-row: 2; grid-column: 1;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; border-top: 1px solid var(--border);
}
#emBottomBar .spacer { flex: 1; }

#emSelModeChips .active-mode { border-color: var(--accent); color: var(--accent); }

.em-layer { cursor: pointer; }
.em-layer.active-layer { border-color: var(--accent); background: var(--surface); }

/* ── mobile: one column — canvas, compact bar, panel sheet ─────────────────── */
@media (max-width: 768px) {
  #editMode {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto auto;
  }
  #emCanvasWrap { grid-row: 1; grid-column: 1; }
  #emBottomBar {
    grid-row: 2; grid-column: 1;
    flex-wrap: wrap; gap: 6px; padding: 8px 10px;
  }
  #emPanel {
    grid-row: 3; grid-column: 1;
    border-left: 0; border-top: 1px solid var(--border);
    max-height: 40vh;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  }
  /* the rail lies down along the bottom of the canvas, thumb-reachable.
     10 tools ≈ 468px — wider than most phones, so the rail SCROLLS instead of
     clipping its end tools into unreachability (audit 2026-08-27) */
  #emRail {
    left: 8px; right: 8px; transform: none;
    top: auto; bottom: 10px;
    flex-direction: row; gap: 2px; padding: 4px;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #emRail::-webkit-scrollbar { display: none; }
  .em-tool { flex: 0 0 auto; width: 44px; min-height: 44px; padding: 6px 0 4px; }
  /* contextual chips: thin single-row strips just above the rail — each group
     scrolls horizontally instead of wrapping into a block over the subject
     (owner screenshot: two wrapped groups stacked seven rows deep on a face) */
  #emSideChips {
    left: 8px; right: 8px; top: auto; bottom: 64px; transform: none;
    max-width: none; align-items: stretch; gap: 6px;
  }
  #emSideChips > div {
    flex-direction: row; flex-wrap: nowrap; justify-content: flex-start;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #emSideChips > div::-webkit-scrollbar { display: none; }
  #emSideChips > div.em-hud { padding: 4px 6px; }
  #emSideChips .download-btn { flex: 0 0 auto; font-size: 11.5px; padding: 8px 10px; }
  #emSideChips .em-hud-dim { flex: 0 0 auto; align-self: center; font-size: 11px; }
  #emExits button { padding: 13px 6px; }
  .em-layer { padding: 11px 12px; }
  .em-layer input[type="checkbox"] { width: 18px; height: 18px; }
  #emBottomBar .download-btn { padding: 9px 12px; }
}

/* layer rows: real thumbnails, checkerboard behind transparency */
.em-thumb {
  width: 44px; height: 33px; flex: 0 0 auto;
  border: 1px solid var(--border); border-radius: 4px;
  background: repeating-conic-gradient(rgba(128,128,128,.32) 0% 25%, rgba(128,128,128,.10) 0% 50%) 0 0 / 12px 12px;
}
.em-eye {
  background: none; border: 0; padding: 0; width: 24px;
  color: var(--text); cursor: pointer; font-size: 13px; line-height: 1;
}
.em-eye:hover { color: var(--accent); }

.em-rename {
  background: none; border: 0; padding: 0 2px;
  color: var(--text-faint); cursor: pointer; font-size: 12px; line-height: 1;
}
.em-layer:hover .em-rename, .em-layer.active-layer .em-rename { color: var(--text); }
.em-rename:hover { color: var(--accent); }

/* ── docked mode: the editor lives IN the generate page's canvas area ─────── */
#editMode.em-docked {
  position: relative; inset: auto; z-index: 5;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  overflow: hidden;
  width: 100%;
}
#editMode.em-docked #emPanel { max-height: none; }

/* Text tab: one row per text on the image (imgflip-style fields) */
.em-text-row { display: flex; gap: 6px; align-items: center; }
.em-text-row input[type="text"] { flex: 1; font-size: 13px; padding: 8px 10px; }
.em-text-row .em-text-size { width: 58px; font-size: 12px; padding: 8px 6px; }
.em-text-row.sel input[type="text"] { border-color: var(--accent); }
.em-text-del {
  background: none; border: 1px solid var(--border); border-radius: 8px;
  color: #f87171; cursor: pointer; padding: 6px 9px; font: inherit; font-size: 12px;
}
.em-text-del:hover { border-color: #f87171; }

/* drag-to-reorder: the insertion line shows where the layer will land */
.em-layer.drag-over { box-shadow: 0 -2px 0 var(--accent); }
.em-layer[draggable="true"] { cursor: grab; }

/* Narrow docked desktops (769-1250px: resized windows, iPad landscape): the
   340px panel + 200px chip column + rail were eating the whole image. Slim the
   panel and give the chips the mobile bottom-strip treatment (audit 2026-08-27). */
@media (min-width: 769px) and (max-width: 1250px) {
  #editMode.em-docked { grid-template-columns: 1fr 280px; }
  #editMode.em-docked #emSideChips {
    left: 8px; right: 8px; top: auto; bottom: 64px; transform: none;
    max-width: none; align-items: stretch; gap: 6px;
  }
  #editMode.em-docked #emSideChips > div {
    flex-direction: row; flex-wrap: nowrap; justify-content: flex-start;
    overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  #editMode.em-docked #emSideChips > div::-webkit-scrollbar { display: none; }
  #editMode.em-docked #emSideChips .download-btn { flex: 0 0 auto; }
}
