:root {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-alt: #181b22;
  --panel-inset: #12151b;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --muted: #9aa2b1;
  --accent: #4f8cff;
  --accent-fade: #2b3a5c;
  --danger: #ff6b6b;
  --success: #4caf7d;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-pill: 999px;
  --header-h: 84px;
  --sidebar-w: 380px;
  --glass-blur: 18px;
}
html[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-alt: #181b22;
  --panel-inset: #12151b;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --muted: #9aa2b1;
  --accent: #4f8cff;
  --accent-fade: #2b3a5c;
  --danger: #ff6b6b;
  --success: #4caf7d;
  --glass-bg: rgba(24, 27, 34, 0.72);
}
html[data-theme="light"] {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-alt: #eef0f3;
  --panel-inset: #f7f8fa;
  --border: #e2e4e9;
  --text: #14171f;
  --muted: #6b7280;
  --accent: #2f6fed;
  --accent-fade: #dbe7fd;
  --danger: #d6483f;
  --success: #1f9d5c;
  --glass-bg: rgba(255, 255, 255, 0.72);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Header */
header {
  position: sticky;
  top: 0;
  /* Above in-content overlays (preview/mask tools z:2-3), below the
     fullscreen preview (.preview-body.fullscreen z:9), popup menus
     (.pl-menu z:20), and modals (.modal-overlay z:10). */
  z-index: 5;
  padding: 20px 28px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
}
header h1 { margin: 0; font-size: 20px; font-weight: 600; }
header p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.brand { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
.brand-dot { color: var(--accent); }

/* Icon / pill buttons */
.icon-btn {
  width: 40px; height: 40px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.icon-btn svg { width: 18px; height: 18px; }
html[data-theme="light"] .icon-btn .icon-sun { display: none; }
html[data-theme="dark"] .icon-btn .icon-moon { display: none; }

/* User avatar + dropdown */
.user-menu { position: relative; }
.avatar-btn {
  width: 40px; height: 40px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  overflow: hidden;
}
.avatar-btn:hover { border-color: var(--accent); }
.avatar-btn img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-btn .avatar-initials {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.user-dropdown {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: 0 8px 24px rgba(0,0,0,.15);
  padding: 12px;
  min-width: 200px;
  z-index: 3;
}
.user-dropdown[hidden] { display: none; }
.user-dropdown .user-email {
  font-size: 13px;
  color: var(--muted);
  word-break: break-all;
  margin-bottom: 4px;
}
.user-dropdown button.ghost { width: 100%; margin-top: 8px; }

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.pill-btn:hover { border-color: var(--accent); color: var(--accent); }
.pill-btn svg { width: 15px; height: 15px; }

/* Panels */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px;
}
.panel.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  overflow-y: hidden;
  z-index: 1;
  border-radius: 0;
  border-top: 0;
  border-left: 0;
  border-bottom: 0;
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  padding-top: calc(var(--header-h) + 18px);
  display: flex;
  flex-direction: column;
}
@media (max-width: 860px) {
  .panel.sidebar {
    position: static;
    width: auto;
    height: auto;
    overflow-y: visible;
    padding-top: 18px;
    border-right: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--panel-alt);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    display: block;
  }
}
.panel h2 {
  margin: 0 0 12px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.section + .section { margin-top: 22px; }
.section-tiles {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
@media (max-width: 860px) {
  .section-tiles { flex: none; }
}
.field-caption {
  display: block;
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

/* Upload dropzone */
.drop {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-md);
  padding: 20px 16px;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
  transition: border-color .15s;
}
.drop:hover { border-color: var(--accent); }
.drop-icon { display: block; width: 34px; height: 34px; margin: 0 auto 10px; color: var(--muted); }
.hint-row { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 10px; }
.drop .hint { font-size: 13px; }
.drop-preview { display: none; }
input[type=file] { display: none; }

/* Tiles */
.type-tabs { display: flex; gap: 8px; margin-bottom: 12px; flex: none; }
.type-tab {
  flex: 1;
  padding: 7px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--panel-inset);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
}
.type-tab:hover { border-color: var(--accent); color: var(--accent); }
.type-tab.active { border-color: var(--accent); color: var(--accent); background: var(--accent-fade); }

.tiles-search { position: relative; margin-bottom: 12px; flex: none; }
.tiles-search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  color: var(--muted);
  pointer-events: none;
}
.tiles-search input[type=text] { padding-left: 32px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: min-content;
  gap: 16px;
  align-content: start;
  flex: 1 1 auto;
  min-height: 120px;
  max-height: 360px;
  overflow-y: auto;
  padding: 2px 2px 2px 0;
}
.tile {
  position: relative;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  background: var(--panel-inset);
  transition: border-color .15s, transform .1s, box-shadow .15s;
}
.tile:hover { transform: translateY(-1px); border-color: var(--accent); }
.tile.selected { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-fade); }
.tile img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .tile-info {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 8px;
  background: linear-gradient(to top, rgba(0, 0, 0, .72), rgba(0, 0, 0, 0));
}
.tile .name { font-size: 12px; color: #fff; }
.tile .surface { font-size: 11px; color: rgba(255, 255, 255, .8); }

label.field { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
select, input[type=text] {
  width: 100%;
  padding: 9px 10px;
  background: var(--panel-inset);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
}
input[type=text]::placeholder { color: var(--muted); }

/* Buttons */
button.primary {
  padding: 12px 18px;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
button.primary:disabled { background: var(--accent-fade); color: var(--muted); cursor: not-allowed; }
.modal button.primary { width: 100%; margin-top: 14px; }
.modal button.primary.danger { background: var(--danger); }
button.ghost {
  margin-top: 10px;
  padding: 8px 14px;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 13px;
  cursor: pointer;
}
button.link {
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  text-decoration: underline;
}

/* Preview — current state */
.preview-panel { display: flex; flex-direction: column; }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.panel-head h2 { margin: 0; }
.panel-head .hint { font-size: 12px; color: var(--muted); text-transform: none; letter-spacing: 0; }
.preview-body {
  position: relative;
  min-height: 340px;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  background: var(--panel-inset);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.preview-body img { max-width: 100%; max-height: 68vh; display: block; transition: transform .15s ease; }
.preview-body.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 9; /* above mobile bottom-nav (8) / drawer (7) / dock (6), below .modal-overlay (10) */
  border-radius: 0;
  background: var(--bg);
}
.preview-body.fullscreen img { max-width: 100vw; max-height: 100vh; }
.preview-tools {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 6px;
  z-index: 2;
}
.preview-tools .icon-btn { width: 32px; height: 32px; }
.preview-tools .icon-btn svg { width: 15px; height: 15px; }

/* Mask painting */
#maskCanvas {
  position: absolute;
  z-index: 1;
  opacity: .45;
  touch-action: none;
  transition: transform .15s ease; /* follow the image's zoom transition */
}
#maskCanvas.editing {
  cursor: none; /* #maskBrushCursor stands in for the pointer */
}
#maskCanvas.editing.rect-tool {
  cursor: crosshair;
}
#maskCanvas.selected {
  opacity: .65;
  /* Glow hugs the painted strokes, not the canvas box. */
  filter: drop-shadow(0 0 3px rgba(255, 255, 255, .95));
}
#maskBrushCursor {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  /* Dark halo inside and out keeps the outline visible on any background. */
  border: 1.5px solid rgba(255, 255, 255, .9);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .45), inset 0 0 0 1px rgba(0, 0, 0, .45);
}
.preview-tools .icon-btn.active,
.mask-tools .icon-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-fade);
}
.mask-tools {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--glass-bg);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  z-index: 2;
}
.mask-tools .icon-btn { width: 32px; height: 32px; }
.mask-tools .icon-btn svg { width: 15px; height: 15px; }
.mask-tools input[type=range] { width: 110px; accent-color: var(--accent); }
.mask-tools button.primary { padding: 7px 14px; font-size: 13px; }
.mask-tools button.primary.danger { background: var(--danger); }
.mask-tools button.ghost { margin-top: 0; padding: 6px 12px; font-size: 13px; }
.mask-status {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 2;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: var(--radius-pill);
  background: var(--glass-bg);
  border: 1px solid var(--border);
  color: var(--success);
}
.mask-status.error { color: var(--danger); }

.spinner {
  width: 46px; height: 46px;
  border: 4px solid var(--accent-fade);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  margin: 0 auto 14px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.status { font-size: 14px; }
.error { color: var(--danger); }
.muted { color: var(--muted); font-size: 13px; }
a.download { display: inline-block; margin-top: 12px; color: var(--accent); font-size: 13px; }

/* Before/after comparison slider */
.compare {
  position: relative;
  width: 100%;
  max-height: 70vh;
  line-height: 0;
  user-select: none;
  border-radius: var(--radius-md);
  overflow: hidden;
}
.compare img {
  display: block;
  max-width: 100%;
  max-height: 70vh;
  margin: 0 auto;
  border-radius: 0;
}
.compare .before {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.compare .cmp-divider {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  background: #fff;
  transform: translateX(-1px);
  pointer-events: none;
}
.compare .cmp-handle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #fff;
  color: #111;
  font-size: 15px;
  font-weight: 700;
  line-height: 34px;
  text-align: center;
  box-shadow: 0 1px 6px rgba(0,0,0,.5);
}
.compare .cmp-tag {
  position: absolute;
  top: 10px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: rgba(255,255,255,.92);
  color: #14171f;
  line-height: 1.4;
  pointer-events: none;
}
.cmp-tag-before { left: 10px; }
.cmp-tag-after { right: 10px; }
.compare .cmp-range {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: ew-resize;
}

/* Reused badge */
.badge {
  display: inline-block;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  background: var(--accent-fade);
  color: var(--accent);
  margin-bottom: 10px;
}

/* Pipeline dashboard */
.mat-card { position: relative; cursor: pointer; }
.mat-thumb {
  position: relative;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--panel-inset);
  transition: border-color .15s, transform .1s, box-shadow .15s;
}
.mat-card:hover .mat-thumb { transform: translateY(-1px); border-color: var(--accent); }
.mat-card.selected .mat-thumb { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-fade); }
.mat-thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.mat-check {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: none;
  align-items: center;
  justify-content: center;
}
.mat-check svg { width: 12px; height: 12px; }
.mat-card.selected .mat-check { display: flex; }
.mat-card .name {
  font-size: 12px;
  color: var(--text);
  margin-top: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}
.mat-card .surface { font-size: 11px; color: var(--muted); text-align: center; text-transform: capitalize; }

.pipeline-panel { margin-top: 20px; }
.pipeline-track {
  display: flex;
  align-items: flex-start;
  overflow-x: auto;
  padding: 12px 4px 6px;
}
.pl-node { flex: 0 0 auto; width: 112px; text-align: center; cursor: pointer; }
.pl-thumb {
  position: relative;
  width: 88px;
  height: 88px;
  margin: 0 auto;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--panel-inset);
  transition: border-color .15s, box-shadow .15s;
}
.pl-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.pl-node:hover .pl-thumb { border-color: var(--accent); }
.pl-node.selected .pl-thumb { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-fade); }
.pl-label {
  font-size: 12px;
  margin-top: 8px;
  padding: 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pl-badge {
  display: inline-block;
  margin-top: 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  padding: 1px 8px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  background: var(--panel-inset);
  color: var(--muted);
}
.pl-node.head .pl-badge { background: var(--accent-fade); border-color: transparent; color: var(--accent); }
.pl-connector {
  flex: 0 0 32px;
  height: 2px;
  background: var(--border);
  align-self: flex-start;
  margin-top: 43px; /* thumb height 88px / 2 - connector 2px / 2 */
}
.pl-node { position: relative; }
.pl-menu-btn {
  position: absolute;
  top: 4px;
  right: 16px; /* (node 112px - thumb 88px) / 2 + 4px inset */
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  cursor: pointer;
  opacity: 0;
  transition: opacity .12s;
  z-index: 3;
}
.pl-menu-btn svg { width: 14px; height: 14px; }
.pl-node:hover .pl-menu-btn,
.pl-menu-btn:focus-visible,
.pl-node.menu-open .pl-menu-btn { opacity: 1; }
@media (hover: none) { .pl-menu-btn { opacity: 1; } }
.pl-menu-btn:hover { background: rgba(0, 0, 0, .8); }
.pl-menu {
  /* Fixed + JS-positioned: the pipeline track scrolls (overflow-x: auto) and
     would clip an absolutely positioned menu. */
  position: fixed;
  top: 0;
  left: 0;
  min-width: 150px;
  padding: 4px;
  background: var(--panel);
  border: 1.5px solid var(--muted);
  border-radius: var(--radius-sm);
  box-shadow: 0 12px 28px rgba(0, 0, 0, .45), 0 3px 8px rgba(0, 0, 0, .3);
  z-index: 20;
}
html[data-theme="light"] .pl-menu {
  border-color: var(--border);
  box-shadow: 0 12px 28px rgba(0, 0, 0, .2), 0 3px 8px rgba(0, 0, 0, .12);
}
.pl-menu button {
  display: block;
  width: 100%;
  padding: 7px 10px;
  background: none;
  border: 0;
  border-radius: 4px;
  text-align: left;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  cursor: pointer;
}
.pl-menu button:hover { background: var(--panel-inset); }
.pl-node-add .pl-thumb {
  border-style: dashed;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 26px;
}
.pl-node-add .pl-thumb .spinner { width: 28px; height: 28px; border-width: 3px; margin: 0; }
.pl-node-add .pl-label { color: var(--muted); }

.step-track {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  font-size: 11px;
  color: var(--muted);
}
.step-dot { flex: 0 0 auto; width: 8px; height: 8px; border-radius: 50%; background: var(--border); }
.step-dot.active { background: var(--accent); }
.step-line { flex: 1 1 24px; height: 1px; background: var(--border); }
.step-label { flex: 0 0 auto; }
.step-label.active { color: var(--accent); font-weight: 600; }

.pl-locked {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 24px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

@media (max-width: 860px) {
  .pl-node { width: 88px; }
  .pl-thumb { width: 72px; height: 72px; }
  .pl-menu-btn { right: 12px; } /* (node 88px - thumb 72px) / 2 + 4px inset */
  .pl-connector { flex-basis: 20px; margin-top: 35px; }
  .step-track { display: none; }
  header .pill-btn span { display: none; }
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 24px;
  width: 320px;
  max-width: 90vw;
  text-align: center;
}
.modal h3 { margin: 0 0 6px; font-size: 16px; }
.modal p { margin: 0 0 14px; color: var(--muted); font-size: 13px; }
.modal .qr-box { background: #fff; border-radius: 8px; padding: 12px; margin: 14px auto; width: fit-content; }
.modal .qr-link { font-size: 12px; color: var(--muted); word-break: break-all; }
.modal .remote-status { margin-top: 14px; font-size: 13px; color: var(--muted); }
.modal .remote-status .spinner {
  width: 28px; height: 28px;
  border: 3px solid var(--accent-fade);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  margin: 0 auto 8px;
}
