/* StickerPage — mobile-first, dark default, big touch targets */
/* cache-bust: v3 */

:root {
  --bg: #0f1419;
  --bg-elev: #161b22;
  --bg-elev2: #1f2630;
  --border: #2d333b;
  --fg: #e6edf3;
  --fg-muted: #7d8590;
  --accent: #58a6ff;
  --accent-fg: #0d1117;
  --danger: #f85149;
  --success: #3fb950;
  --radius: 10px;
  --tap: 44px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --bg-elev: #f6f8fa;
    --bg-elev2: #eaeef2;
    --border: #d0d7de;
    --fg: #1f2328;
    --fg-muted: #59636e;
    --accent: #0969da;
    --accent-fg: #ffffff;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topnav {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  padding-top: max(0.75rem, env(safe-area-inset-top));
}
.topnav .brand {
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--fg);
}
.topnav nav {
  margin-left: auto;
  display: flex;
  gap: 0.25rem;
}
.topnav nav a {
  padding: 0.5rem 0.85rem;
  border-radius: var(--radius);
  color: var(--fg);
  font-weight: 500;
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
}
.topnav nav a:hover { background: var(--bg-elev2); text-decoration: none; }

.menu { position: relative; margin-left: 0.25rem; }
.menu-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  padding: 0.4rem 0.75rem;
  min-height: var(--tap);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  font-family: inherit;
}
.menu-toggle:hover { background: var(--bg-elev2); }
.menu-toggle[aria-expanded="true"] { background: var(--bg-elev2); }
.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 220px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 20;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  padding: 0.25rem 0;
}
.menu-item {
  display: block;
  width: 100%;
  padding: 0.7rem 1rem;
  background: transparent;
  border: none;
  color: var(--fg);
  font-size: 0.95rem;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  text-decoration: none;
  min-height: var(--tap);
  line-height: 1.2;
}
.menu-item:hover { background: var(--bg-elev2); text-decoration: none; color: var(--fg); }
.menu-item-muted { color: var(--fg-muted); font-size: 0.85rem; }
.menu-form { margin: 0; padding: 0; }
.menu-form .menu-item { width: 100%; }

main {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  padding: 1rem;
  flex: 1 1 auto;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.7rem 1.1rem;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev2);
  color: var(--fg);
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.1s ease;
}
.btn:hover  { background: var(--border); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.btn-primary:hover { filter: brightness(1.1); background: var(--accent); }
.btn-danger  { background: var(--danger); color: white; border-color: var(--danger); }
.btn-ghost   { background: transparent; }
.btn-block   { width: 100%; }
.btn-lg      { font-size: 1.1rem; padding: 1rem 1.5rem; min-height: 56px; }

input[type="text"], input[type="search"], input[type="password"], input[type="email"], input[type="tel"], input[type="number"], input[type="url"], textarea, select {
  width: 100%;
  padding: 0.65rem 0.85rem;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--fg);
  font-size: 1rem;
  font-family: inherit;
}
input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
label { display: block; margin-bottom: 0.4rem; font-weight: 500; color: var(--fg-muted); font-size: 0.9rem; }
.field { margin-bottom: 1rem; }

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2rem 1rem;
  text-align: center;
  background: var(--bg-elev);
  transition: background 0.1s, border-color 0.1s;
  cursor: pointer;
}
.dropzone:hover, .dropzone.drag-over {
  background: var(--bg-elev2);
  border-color: var(--accent);
}
.dropzone .hint { color: var(--fg-muted); font-size: 0.9rem; }

.capture-group {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
@media (min-width: 480px) {
  .capture-group { grid-template-columns: 1fr 1fr; }
}

.preview-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}
.preview-card img.preview {
  display: block;
  width: 100%;
  max-height: 60vh;
  object-fit: contain;
  border-radius: 6px;
  background: #000;
}

.matches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.5rem;
  margin: 1rem 0;
}
.match-card {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.1s, transform 0.1s;
}
.match-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.match-card .thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  background: #000;
}
.match-card .meta {
  padding: 0.5rem 0.6rem;
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
}
.match-card .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.match-card .sim {
  font-size: 0.75rem;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
}
.match-card .badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(0,0,0,0.65);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 600;
}

.empty-state {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--fg-muted);
}

.action-row {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.action-row .btn { flex: 1 1 auto; }

.spacer { height: 1rem; }
.error  { color: var(--danger); padding: 0.75rem; border-radius: var(--radius); background: rgba(248,81,73,0.12); margin: 0.5rem 0; }
.notice { color: var(--fg-muted); font-size: 0.9rem; padding: 0.5rem; }

dialog.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--fg);
  padding: 0;
  max-width: 92vw;
  width: 540px;
  max-height: 90vh;
}
dialog.modal::backdrop { background: rgba(0,0,0,0.6); }
.modal-body  { padding: 1rem; overflow: auto; max-height: calc(90vh - 60px); }
.modal-image { display: block; width: 100%; max-height: 50vh; object-fit: contain; background: #000; border-radius: 6px; }
.modal-foot  { display: flex; gap: 0.5rem; padding: 0.75rem 1rem; border-top: 1px solid var(--border); }
.modal-foot .btn { flex: 1 1 auto; }
.modal-close { position: absolute; top: 6px; right: 6px; }

dialog.modal.lightbox {
  width: min(95vw, 1100px);
  max-width: 95vw;
  max-height: 95vh;
}
.lightbox-body {
  padding: 0.5rem;
  background: #000;
  display: flex;
  justify-content: center;
  align-items: center;
}
#lightbox-image {
  display: block;
  max-width: 100%;
  max-height: calc(95vh - 80px);
  width: auto;
  height: auto;
  object-fit: contain;
}

.kv-list { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 0.75rem; }
.kv-row {
  display: grid;
  grid-template-columns: 1fr 2fr auto;
  gap: 0.5rem;
}
.kv-row .btn-rm {
  padding: 0;
  width: var(--tap);
  height: var(--tap);
  font-size: 1.2rem;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg-muted);
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.5rem;
}
.gallery-item {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.gallery-item.is-primary { border-color: var(--accent); }
.gallery-item img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  background: #000;
  display: block;
}
.gallery-actions {
  position: absolute;
  top: 4px;
  right: 4px;
  display: flex;
  gap: 4px;
}
.gallery-actions button {
  background: rgba(0,0,0,0.65);
  color: #fff;
  border: none;
  border-radius: 4px;
  width: 28px;
  height: 28px;
  font-size: 0.85rem;
  cursor: pointer;
}

/* ---------- Browse: masonry grid of sticker tiles ---------- */
/* JS distributes tiles into N column divs; each new tile lands in the shortest column.
   We avoid CSS `column-count`: it re-flows all tiles when items are appended (Load more
   shuffles every existing item between columns), destroying the user's place. */
.browse-grid {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}
.browse-col {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.browse-tile {
  display: block;
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  text-decoration: none;
  color: var(--fg);
}
.browse-tile:hover { border-color: var(--accent); text-decoration: none; }
.browse-tile img {
  display: block;
  width: 100%;
  height: auto;
  background: #000;
}
.browse-tile-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  color: var(--fg-muted);
  background: var(--bg-elev2);
  font-size: 0.9rem;
}
.browse-tile-caption {
  display: block;
  padding: 0.4rem 0.55rem;
  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.browse-tile-count {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  font-size: 0.75rem;
  pointer-events: none;
}

.spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }
[hidden] { display: none !important; }

/* ---------- Crop editor ---------- */
.crop-editor {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.crop-canvas-wrap {
  position: relative;
  background: #000;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  /* Don't allow the wrap to dwarf the viewport on small screens. */
  max-height: 65vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.crop-canvas-host { line-height: 0; max-width: 100%; max-height: 65vh; }
.crop-canvas-host canvas {
  display: block;
  max-width: 100%;
  max-height: 65vh;
  height: auto;
  width: auto;
}

.crop-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* width/height set by JS to mirror the canvas's displayed size */
  pointer-events: none;
  touch-action: none;
}
.crop-rect {
  position: absolute;
  box-sizing: border-box;
  border: 1.5px solid var(--accent);
  /* Inverted mask: a huge box-shadow darkens everything outside the rect. */
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.55);
  cursor: move;
  pointer-events: auto;
  touch-action: none;
}
.crop-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 50%;
  pointer-events: auto;
  touch-action: none;
  /* Expand the hit-area beyond the visual size for easier touch handling. */
  box-sizing: border-box;
}
.crop-handle::after {
  content: "";
  position: absolute;
  inset: -10px;
}
.crop-handle.h-nw { top: -8px; left: -8px; cursor: nwse-resize; }
.crop-handle.h-ne { top: -8px; right: -8px; cursor: nesw-resize; }
.crop-handle.h-sw { bottom: -8px; left: -8px; cursor: nesw-resize; }
.crop-handle.h-se { bottom: -8px; right: -8px; cursor: nwse-resize; }
.crop-handle.h-n  { top: -8px;    left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.crop-handle.h-s  { bottom: -8px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.crop-handle.h-w  { left: -8px;   top: 50%;  transform: translateY(-50%); cursor: ew-resize; }
.crop-handle.h-e  { right: -8px;  top: 50%;  transform: translateY(-50%); cursor: ew-resize; }

.crop-detect-status {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  pointer-events: none;
  z-index: 2;
}

.crop-sliders {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
}
.crop-sliders label {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  margin: 0;
  font-size: 0.9rem;
  color: var(--fg);
}
.crop-sliders label > input[type="range"] {
  grid-column: 1 / -1;
  width: 100%;
  margin-top: 0.25rem;
  accent-color: var(--accent);
}
.crop-slider-val {
  font-variant-numeric: tabular-nums;
  color: var(--fg-muted);
  font-size: 0.85rem;
}
.crop-reset {
  align-self: flex-start;
  font-size: 0.85rem;
  padding: 0.4rem 0.75rem;
  min-height: 36px;
}

/* ---------- Browse: property filters ---------- */
.property-filters {
  margin: 0.75rem 0 1rem;
}
.filter-rows {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}
.filter-row {
  display: grid;
  grid-template-columns: minmax(120px, auto) 1fr 36px;
  gap: 0.5rem;
  align-items: center;
  padding: 0.4rem 0.5rem;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.filter-key {
  font-weight: 500;
  color: var(--fg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.filter-value {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 6px;
  padding: 0.4rem 0.5rem;
  font-size: 0.95rem;
  min-height: 36px;
}
.filter-remove {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg-muted);
  border-radius: 6px;
  height: 36px;
  width: 36px;
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
}
.filter-remove:hover { color: var(--danger); border-color: var(--danger); }

.property-picker {
  position: relative;
}
.property-picker input {
  width: 100%;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  min-height: var(--tap);
}
.property-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 10;
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.property-suggestions .suggestion {
  background: transparent;
  border: none;
  color: var(--fg);
  text-align: left;
  padding: 0.6rem 0.75rem;
  cursor: pointer;
  font-size: 0.95rem;
  border-bottom: 1px solid var(--border);
}
.property-suggestions .suggestion:last-child { border-bottom: none; }
.property-suggestions .suggestion:hover { background: var(--bg-elev); color: var(--accent); }

.combine-toggle {
  display: flex;
  gap: 1rem;
  margin-top: 0.5rem;
  font-size: 0.9rem;
  color: var(--fg-muted);
}
.combine-toggle label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
}

/* ---------- Sticker detail: read-only metadata list ---------- */
.meta-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.4rem 1rem;
  margin: 0;
  padding: 0;
}
.meta-list dt {
  font-weight: 500;
  color: var(--fg-muted);
}
.meta-list dd {
  margin: 0;
  color: var(--fg);
  word-break: break-word;
}

@media all and (display-mode: standalone), all and (display-mode: minimal-ui), all and (display-mode: fullscreen) {
  .hide-in-pwa { display: none !important; }
}
html.is-pwa .hide-in-pwa { display: none !important; }
