:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --surface-2: #efece6;
  --text: #1d1c1a;
  --muted: #6d695f;
  --border: #e2ded6;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --shadow: 0 1px 2px rgba(30, 25, 15, .06), 0 4px 16px rgba(30, 25, 15, .06);
  --radius: 10px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[data-theme="dark"] {
  --bg: #131312;
  --surface: #1c1b19;
  --surface-2: #262522;
  --text: #ebe8e2;
  --muted: #9b968c;
  --border: #2f2d2a;
  --accent: #8cbcaa;
  --accent-text: #10201b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text); font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased; display: flex; flex-direction: column;
}
[hidden] { display: none !important; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 18px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 650; letter-spacing: -.01em; font-size: 15px; }
.spacer { flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 36px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 550; cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s; white-space: nowrap; user-select: none;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.block { width: 100%; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover:not(:disabled) { color: var(--text); background: var(--surface-2); }
.btn.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.icon-btn {
  width: 36px; height: 36px; display: inline-grid; place-items: center; border-radius: 8px;
  border: 1px solid transparent; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:disabled { opacity: .3; cursor: default; }
[data-theme="dark"] .sun { display: none; }
[data-theme="light"] .moon { display: none; }

/* Gallery */
.gallery { padding: 18px; max-width: 1400px; width: 100%; margin: 0 auto; }
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  height: 32px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--muted); font: inherit; font-weight: 550; cursor: pointer;
}
.chip[aria-selected="true"] { background: var(--text); color: var(--bg); border-color: transparent; }
.chip .count { opacity: .6; margin-left: 6px; font-weight: 500; }

.dropzone {
  border: 1.5px dashed var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px;
  color: var(--muted); background: var(--surface); transition: border-color .15s, background .15s;
}
.dropzone .dz-inner { display: flex; gap: 14px; align-items: center; }
.dropzone strong { color: var(--text); }
body.dragging .dropzone { border-color: var(--accent); background: var(--surface-2); }
.dropzone.loaded { display: none; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); cursor: pointer; display: flex; flex-direction: column;
  transition: transform .15s;
}
.card:hover { transform: translateY(-2px); }
.card .thumb { position: relative; background: var(--surface-2); aspect-ratio: 3 / 4; overflow: hidden; }
.card canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; object-fit: cover; }
.card .info { display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.card .name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .tag { font-size: 12px; color: var(--muted); }
.card .dl { width: 32px; height: 32px; }
.card .badge {
  position: absolute; left: 8px; top: 8px; font-size: 11px; font-weight: 600; padding: 2px 8px;
  border-radius: 999px; background: rgba(0, 0, 0, .55); color: #fff; backdrop-filter: blur(4px);
}

/* Editor */
.editor { flex: 1; display: grid; grid-template-columns: 1fr 300px; min-height: 0; }
.stage-wrap { display: flex; flex-direction: column; min-height: 0; padding: 16px; gap: 10px; }
.stage {
  position: relative; flex: 1; min-height: 0; display: grid; place-items: center; touch-action: none;
}
.stage canvas#view { display: block; border-radius: 4px; box-shadow: var(--shadow); }
.overlay { position: absolute; left: 0; top: 0; pointer-events: none; overflow: hidden; }
.overlay .handle { fill: rgba(255, 255, 255, .85); stroke: var(--accent); stroke-width: 2.5; }
.overlay .handle.active { fill: var(--accent); stroke: #fff; }
.overlay .edge { fill: none; stroke: #fff; stroke-width: 1; stroke-dasharray: 4 3; opacity: .9; }
.overlay .edge2 { fill: none; stroke: #000; stroke-width: 1; opacity: .35; }
.loupe {
  position: absolute; width: 140px; height: 140px; border-radius: 50%; pointer-events: none;
  box-shadow: 0 0 0 2px #fff, 0 4px 18px rgba(0, 0, 0, .35);
}
.stage-nav { display: flex; align-items: center; justify-content: center; gap: 10px; }
.stage-title { font-weight: 600; min-width: 0; text-align: center; }
.hint { margin: 0; text-align: center; color: var(--muted); font-size: 12.5px; }

.panel {
  border-left: 1px solid var(--border); background: var(--surface); padding: 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 18px;
}
.panel h3 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 650; }
.panel section, .panel details { display: flex; flex-direction: column; gap: 8px; }
.row { display: grid; grid-template-columns: 84px 1fr 44px; align-items: center; gap: 8px; font-size: 13px; }
.row output { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.row select { grid-column: span 2; height: 32px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; padding: 0 8px; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.advanced summary { cursor: pointer; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 650; list-style: none; }
.advanced summary::before { content: "▸ "; }
.advanced[open] summary::before { content: "▾ "; }
.advanced[open] summary { margin-bottom: 6px; }
.download { margin-top: auto; }
.meta { font-size: 12px; color: var(--muted); text-align: center; }

/* Small controls */
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; }
.icon-btn.sm { width: 30px; height: 30px; }
.btn.danger { color: #b3412f; }
[data-theme="dark"] .btn.danger { color: #e58a76; }

.segmented {
  display: inline-flex; padding: 3px; gap: 2px; border-radius: 9px; background: var(--surface-2);
  border: 1px solid var(--border);
}
.segmented button {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 12px; border: 0;
  border-radius: 7px; background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  font-size: 13px; cursor: pointer; white-space: nowrap;
}
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.segmented.full { display: flex; }
.segmented.full button { flex: 1; justify-content: center; }

.stage-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.zoomctl { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
#viewZoomLabel { min-width: 42px; text-align: center; }
.stage[data-mode="view"] { cursor: grab; }
.stage[data-mode="view"].zoomed-out { cursor: default; }
.stage[data-mode="art"] { cursor: move; }
.stage[data-mode="corners"] { cursor: crosshair; }
.stage.dragging { cursor: grabbing; }
.stage[data-mode="mask"], .stage[data-mode="mask"].dragging { cursor: crosshair; }
.overlay .brush { fill: none; stroke: #fff; }
.overlay .brush2 { fill: none; stroke: rgba(0, 0, 0, .55); }

#maskTools, .subgroup, .stack { display: flex; flex-direction: column; gap: 8px; }
.overlay .lasso { fill: rgba(255, 60, 40, .12); stroke: #fff; }
.overlay .lasso2 { fill: none; stroke: rgba(0, 0, 0, .55); }
.overlay .lpt { fill: #fff; stroke: rgba(0, 0, 0, .6); }
.overlay .lpt.first { fill: var(--accent); stroke: #fff; }
.subgroup { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 2px; }
.sublabel { font-size: 12px; font-weight: 650; color: var(--muted); }
.segmented.full.small button { height: 26px; font-size: 12px; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; line-height: 1.4; cursor: pointer; }
.check input { margin-top: 2px; accent-color: var(--accent); width: 16px; height: 16px; flex: none; }
.check small { color: var(--muted); font-size: 12px; }
.btnrow { display: flex; gap: 6px; }
.btnrow .btn { flex: 1; }
.note.center { text-align: center; margin-top: 4px; }

.callout {
  background: var(--surface-2); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 8px; padding: 10px 12px; font-size: 13px; line-height: 1.5;
}
.callout ol { margin: 6px 0; padding-left: 18px; }
.callout li { margin: 3px 0; }
.note { margin: 0; font-size: 12px; color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.field > span { color: var(--muted); font-size: 12px; }
.field input[type="text"], .field select {
  height: 34px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; padding: 0 10px; width: 100%;
}
.tagpick { display: flex; flex-wrap: wrap; gap: 6px; }
.tagpick .chip { height: 28px; padding: 0 11px; font-size: 12.5px; }
.tagadd { display: flex; gap: 6px; }
.tagadd input { flex: 1; min-width: 0; }

.card.add {
  border-style: dashed; box-shadow: none; background: transparent; align-items: center; justify-content: center;
  color: var(--muted); min-height: 200px; gap: 10px; text-align: center; padding: 18px;
}
.card.add:hover { color: var(--text); border-color: var(--accent); }
.card.add .plus {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2);
}
.card.add strong { color: var(--text); font-size: 14px; }
.card.add small { font-size: 12px; max-width: 200px; }
.card .badge.mine { left: auto; right: 8px; background: var(--accent); color: var(--accent-text); }
.library-links { display: flex; justify-content: center; gap: 6px; margin-top: 22px; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 550;
  box-shadow: var(--shadow); z-index: 20; max-width: 90vw;
}

/* Mobile */
@media (max-width: 820px) {
  .topbar { padding: 8px 12px; }
  .brand { font-size: 14px; }
  #uploadLabel { display: none; }
  #uploadBtn { padding: 0 10px; }
  .gallery { padding: 12px; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .card .info { padding: 8px 10px; }
  .card .tag { display: none; }
  /* The photo stays put at the top; only the controls scroll underneath it. */
  .editor { display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
  .stage-wrap { padding: 8px 10px 6px; gap: 6px; flex: none; height: 54vh; height: 54dvh; }
  .stage-tools { gap: 6px; flex-wrap: nowrap; }
  .stage-tools .segmented button { padding: 0 8px; gap: 4px; font-size: 12.5px; }
  #viewOut, #viewIn, #viewZoomLabel { display: none; }
  .stage-nav .icon-btn { width: 32px; height: 32px; }
  .hint { font-size: 11.5px; line-height: 1.35; }
  .panel { border-left: 0; border-top: 1px solid var(--border); flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .download {
    position: sticky; bottom: 0; margin: 0 -16px -16px; padding: 12px 16px max(12px, env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--border);
  }
  .dropzone .dz-inner svg { display: none; }
}
@media (max-width: 420px) {
  .stage-tools .segmented button svg { display: none; }
  .stage-tools .segmented button { padding: 0 9px; }
  .grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .card .name { font-size: 12.5px; }
}
