:root {
  --bg: #111113;
  --stage: #1c1c20; /* keep in sync with STAGE in src/gl/renderer.js */
  --panel: #161618;
  --surface: #232327;
  --surface-2: #2e2e34;
  --border: #ffffff14;
  --text: #ececf1;
  --muted: #9a9aa6;
  --accent: #a8326d; /* keep in sync with ACCENT in src/gl/renderer.js */
  --accent-hover: #bd3e7e;
  --accent-soft: #a8326d33;
  --accent-line: #a8326d99;
  /* The accent as text/icon/focus colour on the dark UI (#a8326d itself is too dim there). */
  --accent-fg: #e57fb2; /* also ACCENT_FG in src/gl/renderer.js */
  --danger: #ff6b6b;
  --shadow: 0 8px 28px rgb(0 0 0 / 0.4), 0 0 0 1px var(--border);
  --panel-w: 264px;
  --sheet-h: min(46dvh, 420px);
  --btn: 36px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

/* ── Layout ─────────────────────────────────────────────────────────── */

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--panel-w);
  height: 100vh;
  height: 100dvh;
}
.app[data-panel="closed"] { grid-template-columns: minmax(0, 1fr); }
.app[data-panel="closed"] .panel { display: none; }

.stage {
  position: relative;
  min-width: 0;
  overflow: hidden;
  background: var(--stage);
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  -webkit-touch-callout: none;
}

/* ── Buttons & floating bars ─────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex: none;
  height: var(--btn);
  min-width: var(--btn);
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.12s, color 0.12s, transform 0.08s;
}
/* Hover styles only where there's a real pointer: on touch screens they stick after a tap. */
@media (hover: hover) {
  .btn:hover { background: var(--surface-2); }
  .btn.primary:hover { background: var(--accent-hover); }
  .btn.ghost:hover { color: var(--text); }
}
/* Press feedback (the main feedback on touch). */
.btn:active:not(:disabled) { background: var(--surface-2); transform: scale(0.94); }
.btn.primary:active:not(:disabled) { background: var(--accent-hover); }
@media (prefers-reduced-motion: reduce) { .btn:active:not(:disabled) { transform: none; } }
.btn:focus-visible, .preset:focus-visible { outline: 2px solid var(--accent-fg); outline-offset: 1px; }
.btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
.btn[aria-pressed="true"] { color: var(--accent-fg); }
.btn.primary { background: var(--accent); color: #fff; padding: 0 12px; }
.btn.danger { color: var(--danger); }
.btn.ghost { color: var(--muted); }
.icon { width: 18px; height: 18px; flex: none; }

.bar {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 2px;
  width: max-content;
  max-width: calc(100% - 24px - var(--safe-left) - var(--safe-right));
  padding: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.bar::-webkit-scrollbar { display: none; }
.toolbar { top: calc(12px + var(--safe-top)); }
.selection-bar, .crop-bar, .erase-bar, .pick-bar { bottom: calc(12px + var(--safe-bottom)); }
.bar-hint { padding: 0 10px; color: var(--muted); white-space: nowrap; }
.sep { flex: none; width: 1px; height: 20px; margin: 0 4px; background: var(--border); }
.zoom { min-width: 58px; font-variant-numeric: tabular-nums; }

.opacity {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.opacity span { min-width: 34px; text-align: right; }
.opacity input { width: 96px; accent-color: var(--accent); }
.brush { display: flex; align-items: center; gap: 8px; padding: 0 8px; color: var(--muted); font-variant-numeric: tabular-nums; }
.brush input { width: 140px; accent-color: var(--accent); }
.brush span:last-child { min-width: 44px; }
/* Remove-background bar: download / analysis progress along its bottom edge. */
.bar-progress {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 2px;
  height: 2px;
  overflow: hidden;
  border-radius: 1px;
  background: var(--border);
}
.bar-progress span { display: block; width: 0; height: 100%; background: var(--accent-fg); transition: width 0.2s; }
.bar-progress.indeterminate span { width: 30%; animation: bar-slide 1.2s ease-in-out infinite; }
@keyframes bar-slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
@media (prefers-reduced-motion: reduce) { .bar-progress.indeterminate span { animation-duration: 3s; } }

/* ── Shapes menu & style panel ─────────────────────────────────────── */

.popover {
  position: absolute;
  top: calc(66px + var(--safe-top));
  left: 50%;
  z-index: 6;
  transform: translateX(-50%);
  padding: 6px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.shape-menu { display: grid; grid-template-columns: repeat(4, 76px); gap: 2px; }
.shape-option {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 10px 4px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  cursor: pointer;
}
.shape-option:focus-visible, .shape-option:active { background: var(--surface-2); color: var(--text); outline: none; }

.style-panel {
  position: absolute;
  left: 50%;
  bottom: calc(12px + var(--safe-bottom));
  z-index: 6;
  display: grid;
  gap: 10px;
  width: min(420px, calc(100% - 24px));
  padding: 12px;
  transform: translateX(-50%);
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.style-panel section { display: grid; gap: 10px; }
.style-panel textarea {
  width: 100%;
  min-height: 56px;
  max-height: 30vh;
  padding: 8px 10px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 system-ui, sans-serif;
  user-select: text;
}
.style-panel textarea:focus, .style-panel select:focus { outline: 2px solid var(--accent-fg); outline-offset: -1px; }
.style-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.style-row .sep { margin: 0 2px; }
.style-panel select {
  height: 36px;
  min-width: 120px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.style-panel label { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.style-panel input[type="color"] {
  width: 34px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: none;
  cursor: pointer;
}
.style-panel input[type="checkbox"], .style-panel input[type="range"] { accent-color: var(--accent); }
.style-panel input[type="range"] { width: 120px; }
.shape-types { display: flex; flex-wrap: wrap; gap: 2px; }
.shape-type[aria-checked="true"] { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent-line); color: var(--accent-fg); }
.style-panel footer { display: flex; justify-content: flex-end; }

/* ── Empty state, drop overlay, toast ────────────────────────────────── */

.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 4px;
  padding: 24px;
  text-align: center;
  color: var(--empty-ink, var(--muted)); /* set from the background colour */
  pointer-events: none;
}
.empty .btn { pointer-events: auto; margin-top: 12px; }
.empty-title { margin: 0; font-size: 18px; font-weight: 600; }
.empty-sub { margin: 0; }
/* Touch screens get words for tapping instead of dropping and pasting. */
.for-touch { display: none; }
@media (hover: none) and (pointer: coarse) {
  .for-mouse { display: none; }
  .for-touch { display: inline; }
}

.drop-overlay {
  position: absolute;
  inset: 10px;
  z-index: 10;
  display: none;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: #a8326d1f;
  font-size: 16px;
  font-weight: 600;
  pointer-events: none;
}
.drop-active .drop-overlay { display: grid; }

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(72px + var(--safe-bottom));
  z-index: 20;
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--surface-2);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 0.15s, transform 0.15s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Debug overlay (F3). */
.debug-hud {
  position: absolute;
  top: calc(64px + var(--safe-top));
  left: 12px;
  z-index: 4;
  max-width: calc(100% - 24px);
  margin: 0;
  padding: 8px 10px;
  overflow: hidden;
  border-radius: 8px;
  background: rgb(0 0 0 / 0.72);
  color: #d8f3dc;
  font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  white-space: pre;
  pointer-events: none;
}

/* ── Tooltips (tooltip.js) ───────────────────────────────────────────── */

.tooltip {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 50;
  max-width: min(280px, calc(100vw - 16px));
  padding: 7px 10px;
  border-radius: 8px;
  background: #0b0b0d;
  color: var(--text);
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.45), 0 0 0 1px var(--border);
  font-size: 12px;
  line-height: 1.35;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  translate: 0 4px;
  transition: opacity 0.12s ease, translate 0.12s ease, visibility 0.12s;
}
.tooltip[data-side="below"] { translate: 0 -4px; }
.tooltip.show { opacity: 1; visibility: visible; translate: 0 0; }
.tooltip-head { display: flex; align-items: center; gap: 10px; }
.tooltip-head strong { font-weight: 600; white-space: nowrap; }
.tooltip-head .keys { margin-left: auto; }
.tooltip-detail { margin-top: 2px; color: var(--muted); }
@media (prefers-reduced-motion: reduce) { .tooltip, .tooltip[data-side="below"] { translate: none; } }

.keys { display: inline-flex; gap: 3px; }
kbd {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 5px;
  background: var(--surface-2);
  box-shadow: inset 0 -1px 0 rgb(0 0 0 / 0.45), 0 0 0 1px var(--border);
  color: var(--text);
  font: 600 11px/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  white-space: nowrap;
}

/* ── Layers panel ────────────────────────────────────────────────────── */

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
}
.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 8px 6px 16px;
}
.panel-head h2 {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.count { flex: 1; color: var(--muted); font-size: 11px; }
.sheet-grip { display: none; }
/* The way to the keyboard shortcuts: quiet, at the bottom of the layer list. Touch screens don't need it. */
.panel-foot { padding: 6px 8px; border-top: 1px solid var(--border); }
.link-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 6px 8px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.link-btn .icon { width: 16px; height: 16px; }
.link-btn:focus-visible { outline: 2px solid var(--accent-fg); outline-offset: -2px; }
@media (hover: hover) { .link-btn:hover { background: var(--surface); color: var(--text); } }
@media (hover: none) { .panel-foot { display: none; } }
.panel-empty { margin: 8px 16px; color: var(--muted); }

.layer-list {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 0 8px 12px;
  overflow-y: auto;
  list-style: none;
}
.layer {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 5px 4px 5px 6px;
  border-radius: 8px;
  cursor: pointer;
}
@media (hover: hover) {
  .layer:hover { background: var(--surface); }
  .shape-option:hover { background: var(--surface-2); color: var(--text); }
  .preset:hover { border-color: var(--accent-line); }
}
.layer.selected { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent-line); }
.layer.dragging { opacity: 0.4; }
.layer.drop-above { box-shadow: inset 0 2px 0 var(--accent-fg); }
.layer.drop-below { box-shadow: inset 0 -2px 0 var(--accent-fg); }
.layer.is-hidden .thumb, .layer.is-hidden .name { opacity: 0.4; }
.thumb {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  overflow: hidden;
  border-radius: 6px;
  background: repeating-conic-gradient(#2a2a2f 0 25%, #36363c 0 50%) 0 0 / 10px 10px;
}
.thumb-img { background-repeat: no-repeat; }
.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rename {
  width: 100%;
  min-width: 0;
  padding: 4px 6px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  user-select: text;
}

/* ── Dialogs ─────────────────────────────────────────────────────────── */

.dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgb(0 0 0 / 0.55); }
.dialog form { display: grid; gap: 16px; padding: 20px; }
.dialog h2 { margin: 0; font-size: 16px; }
.dialog label { display: grid; flex: 1; gap: 6px; color: var(--muted); font-size: 12px; }
.dialog input[type="number"], .dialog select {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  user-select: text;
}
.dialog input[type="color"] {
  width: 100%;
  height: 36px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
.dialog .check { display: flex; align-items: center; gap: 8px; align-self: end; height: 36px; font-size: 13px; color: var(--text); }
.dialog .check input { accent-color: var(--accent); width: 16px; height: 16px; }
.row { display: flex; align-items: end; gap: 10px; }
.swap { margin-bottom: 0; }
.presets { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 6px; }
.preset {
  display: grid;
  gap: 2px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.preset[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.preset.wide { grid-column: 1 / -1; }
.dialog input:disabled, .dialog .btn:disabled { opacity: 0.4; }
.preset span { color: var(--muted); font-size: 11px; }
.hint { margin: -6px 0 0; color: var(--muted); font-variant-numeric: tabular-nums; }
.dialog .text { margin: 0; line-height: 1.5; }
.dialog footer { display: flex; align-items: center; gap: 8px; }

.keys-dialog { width: min(760px, calc(100vw - 32px)); }
.keys-head { display: flex; align-items: center; justify-content: space-between; margin: -6px -8px -6px 0; }
.keys-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 18px 32px;
  max-height: min(64dvh, 560px);
  overflow-y: auto;
}
.keys-list h3 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.keys-list dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; align-items: center; margin: 0; }
.keys-list dt { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; color: var(--muted); font-size: 12px; }
.keys-list dd { margin: 0; }
.keys-dialog .hint { margin: 0; }
.grow { flex: 1; }

.fatal { max-width: 32em; margin: 20vh auto; padding: 0 24px; font-size: 15px; text-align: center; }

/* ── Small screens: layers become a bottom sheet ─────────────────────── */

@media (max-width: 760px) {
  .app, .app[data-panel="closed"] { grid-template-columns: minmax(0, 1fr); }
  .app[data-panel="closed"] .panel { display: flex; }
  .panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 15;
    height: calc(var(--sheet-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    border-left: 0;
    border-radius: 16px 16px 0 0;
    box-shadow: var(--shadow);
    transform: translateY(100%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0.2s;
  }
  .app[data-panel="open"] .panel { transform: none; visibility: visible; }
  .app[data-panel="open"] :is(.selection-bar, .crop-bar, .erase-bar, .pick-bar, .style-panel) { bottom: calc(var(--sheet-h) + var(--safe-bottom) + 12px); }
  .hide-sm { display: none; }
  /* Eraser bar spans the screen; the slider takes whatever room is left. */
  .erase-bar { width: calc(100% - 24px); }
  .erase-bar .brush { flex: 1 1 auto; min-width: 0; }
  .erase-bar .brush input { flex: 1; width: auto; min-width: 60px; }
  .erase-bar .brush span:first-child { display: none; }
  /* Selection bar: opacity on its own row so every button stays reachable. */
  .selection-bar { flex-wrap: wrap; justify-content: center; row-gap: 2px; }
  .selection-bar .opacity { flex: 1 1 auto; padding: 4px 10px 0; }
  .selection-bar .opacity input { flex: 1; width: auto; }
  .selection-bar #selection-count { padding: 4px 0 0 10px; }
  .selection-bar .sep { display: none; }
  /* The separator after opacity becomes a line break: count + opacity on top, buttons below. */
  .selection-bar .opacity + .sep { display: block; flex-basis: 100%; height: 0; margin: 0; background: none; }
  .opacity input { width: 72px; }
  /* The layer sheet has a handle; dragging its top edge down closes it. */
  .sheet-grip { display: block; flex: none; width: 36px; height: 4px; margin: 8px auto 0; border-radius: 2px; background: var(--surface-2); }
  .sheet-grip, .panel-head { touch-action: none; }
  .panel-head { padding-top: 4px; }
  /* Dialogs become bottom sheets, within thumb reach. */
  .dialog {
    width: 100%;
    max-width: 100%;
    max-height: calc(100dvh - 24px - var(--safe-top));
    margin: auto 0 0;
    border-radius: 16px 16px 0 0;
    padding-bottom: var(--safe-bottom);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .dialog[open] { animation: sheet-up 0.22s ease-out; }
  .dialog form { padding: 18px 16px 20px; }
  .dialog footer .btn { height: 42px; }
  .keys-list { max-height: none; }
  /* Toasts at the top, clear of the bottom bars and the layer sheet. */
  .toast { top: calc(70px + var(--safe-top)); bottom: auto; transform: translate(-50%, -8px); }
  .toast.show { transform: translate(-50%, 0); }
  /* Remove-background bar: the status line shows (on its own row) only while something runs. */
  .pick-bar { flex-wrap: wrap; justify-content: center; row-gap: 2px; }
  .pick-bar #pick-hint.busy { display: block; flex-basis: 100%; padding: 4px 10px 0; text-align: center; }
}

/* Keeps the selection bar's buttons on one row on common phone widths. */
@media (max-width: 440px) {
  .selection-bar .btn { min-width: 38px; padding: 0 4px; }
}

@media (max-width: 400px) {
  .toolbar .label, .toolbar .sep, .erase-bar .label, .pick-bar .label, .pick-bar .sep { display: none; }
  .pick-bar .btn { padding: 0 6px; }
  .toolbar .btn { min-width: 38px; }
  .toolbar .btn.primary { padding: 0 8px; }
  .style-panel select { flex: 1 0 100%; }
}

@media (max-width: 380px) {
  .selection-bar .btn { min-width: 32px; }
}

@media (max-width: 340px) {
  .selection-bar .btn { min-width: 30px; padding: 0 2px; }
}

@media (pointer: coarse) {
  :root { --btn: 42px; }
}

@keyframes sheet-up { from { transform: translateY(100%); } }
@media (prefers-reduced-motion: reduce) { .dialog[open] { animation: none; } }
