:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-soft: #475569;
  --muted: #94a3b8;
  --brand: #1f4e78;
  --brand-2: #2c6ea4;
  --accent: #0ea5e9;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --success: #16a34a;
  --shadow-sm: 0 1px 2px rgba(15,23,42,.06), 0 1px 3px rgba(15,23,42,.08);
  --shadow: 0 4px 12px rgba(15,23,42,.08), 0 2px 4px rgba(15,23,42,.05);
  --shadow-lg: 0 20px 40px rgba(15,23,42,.18);
  --radius: 12px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

/* ---------------- Role-based visibility ---------------- */
body.role-viewer .admin-only { display: none !important; }
.who-indicator {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-soft);
}
.who-indicator .role-pill {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--brand);
  color: white;
}
.who-indicator .role-pill.viewer { background: var(--text-soft); }

/* ---------------- Users modal ---------------- */
.users-body {
  padding: 16px 20px 18px;
  display: flex; flex-direction: column;
}
.users-list {
  display: flex; flex-direction: column;
  gap: 6px;
}
.users-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-2);
}
.users-row .name { font-weight: 600; flex: 1; }
.users-row .role-pill {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--brand);
  color: white;
}
.users-row .role-pill.viewer { background: var(--text-soft); }
.users-row .you {
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
}
.users-divider { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.users-subhead { font-size: 13px; margin: 0 0 8px; color: var(--text-soft); font-weight: 600; }
.users-add {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.users-add input, .users-add select {
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-size: 13px;
  min-width: 0;
}
.users-add #add-user-btn {
  grid-column: 1 / -1;
  justify-self: end;
  padding: 9px 18px;
}
.users-add input:focus, .users-add select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(14,165,233,.18);
}
.users-error {
  background: var(--danger-soft);
  color: var(--danger);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 12px;
  margin-top: 10px;
}
.users-error.hidden { display: none; }

/* ---------------- Dropdown menu (download etc.) ---------------- */
.dropdown { position: relative; display: inline-block; }
.dropdown .caret { font-size: 9px; margin-left: 4px; opacity: .8; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 240px;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  z-index: 50;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.dropdown-menu.hidden { display: none; }
.dropdown-item {
  display: flex;
  flex-direction: column;
  padding: 9px 12px;
  border-radius: 6px;
  text-decoration: none;
  color: var(--text);
  font-size: 13px;
  line-height: 1.3;
  cursor: pointer;
}
.dropdown-item strong { font-weight: 600; font-size: 13px; }
.dropdown-item small { color: var(--text-soft); font-size: 11px; margin-top: 1px; }
.dropdown-item:hover { background: var(--panel-2); }

/* ---------------- Hamburger ---------------- */
.hamburger {
  display: none;
  background: transparent;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  width: 40px; height: 40px;
  padding: 0;
  cursor: pointer;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 4px;
}
.hamburger span {
  display: block;
  width: 18px; height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: transform .15s, opacity .15s;
}
.topbar.menu-open .hamburger span:nth-child(1) { transform: translateY(6px) rotate(45deg); }
.topbar.menu-open .hamburger span:nth-child(2) { opacity: 0; }
.topbar.menu-open .hamburger span:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }

/* ---------------- Selection bar (gallery batch download) ---------------- */
.selection-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 12px rgba(15,23,42,.06);
  flex-shrink: 0;
}
.selection-bar.hidden { display: none; }
.selection-bar .sel-count {
  font-weight: 600;
  color: var(--text);
  font-size: 13px;
}
.picker-tile.selectable .check {
  background: white;
  border: 2px solid var(--border-strong);
  box-shadow: 0 1px 3px rgba(15,23,42,.25);
}
.picker-tile.selectable.selected .check {
  background: var(--accent);
  border-color: var(--accent);
}
.picker-tile.selectable.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(14,165,233,.25);
}
.gallery-mode-select .picker-tile { cursor: pointer; }

#gallery-select-toggle.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* ---------------- Topbar ---------------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 24px;
  box-shadow: var(--shadow-sm);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 36px; height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-2) 100%);
  box-shadow: 0 4px 10px rgba(31,78,120,.35);
  position: relative;
  flex-shrink: 0;
}
.brand-mark::after {
  content: "";
  position: absolute; left: 8px; right: 8px; bottom: 8px; height: 8px;
  background: rgba(255,255,255,.6);
  border-radius: 4px;
}
.brand h1 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { margin: 0; color: var(--text-soft); font-size: 12px; }

.toolbar {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: flex-end;
}
.search-wrap { position: relative; flex: 1 1 320px; max-width: 420px; }
.search-wrap::before {
  content: "🔍";
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  opacity: .55;
  font-size: 13px;
}
#search {
  width: 100%;
  padding: 10px 14px 10px 34px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--panel-2);
  transition: border-color .15s, background .15s, box-shadow .15s;
}
#search:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--panel);
  box-shadow: 0 0 0 3px rgba(14,165,233,.18);
}

select, input[type="text"], input[type="number"], input[type="search"], textarea {
  font-family: inherit;
  font-size: 14px;
}

#filter-cat {
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  cursor: pointer;
}

.btn {
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
  transition: transform .05s, box-shadow .15s, background .15s, color .15s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--brand);
  color: white;
  box-shadow: 0 1px 2px rgba(31,78,120,.3);
}
.btn-primary:hover { background: var(--brand-2); }
.btn-primary:disabled {
  background: var(--border-strong);
  color: white;
  cursor: not-allowed;
  box-shadow: none;
}
.btn-ghost {
  background: transparent;
  color: var(--text-soft);
  border-color: var(--border-strong);
}
.btn-ghost:hover { background: var(--panel-2); color: var(--text); }
.btn-danger {
  background: white;
  color: var(--danger);
  border-color: var(--danger);
}
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 6px 10px; font-size: 12px; }

/* ---------------- Items list ---------------- */
main#items {
  max-width: 1400px;
  margin: 24px auto;
  padding: 0 24px 48px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.item {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: box-shadow .15s, border-color .15s;
  overflow: hidden;
}
.item:hover { box-shadow: var(--shadow); border-color: var(--border-strong); }

.item-head {
  display: grid;
  grid-template-columns: 60px minmax(0, 1.6fr) 130px 80px minmax(0, 1.2fr) auto;
  gap: 16px;
  padding: 14px 18px;
  align-items: start;
}
.item-id {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--muted);
  font-size: 13px;
  padding-top: 8px;
}
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  font-weight: 600;
}
.field input[type="text"],
.field input[type="number"],
.field textarea,
.field select {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
  background: transparent;
  width: 100%;
  resize: vertical;
  color: var(--text);
  transition: background .12s, border-color .12s, box-shadow .12s;
  font-family: inherit;
  font-size: 14px;
}
.field textarea { min-height: 32px; line-height: 1.4; }
.field input:hover,
.field textarea:hover,
.field select:hover {
  background: var(--panel-2);
  border-color: var(--border);
}
.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: none;
  background: var(--panel);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(14,165,233,.18);
}
.field .desc-input { font-weight: 600; font-size: 15px; }
.field .qty-input { text-align: center; max-width: 60px; }

.item-actions {
  display: flex;
  gap: 6px;
  padding-top: 4px;
  align-items: center;
}

.item-photos {
  padding: 0 18px 16px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  flex-wrap: wrap;
  border-top: 1px dashed var(--border);
  padding-top: 14px;
  margin-top: 0;
}

.photo-thumb {
  position: relative;
  width: 120px;
  height: 90px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel-2);
  cursor: zoom-in;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  transition: transform .12s, box-shadow .12s;
}
.photo-thumb:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.photo-thumb img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.photo-thumb .photo-remove {
  position: absolute;
  top: 4px; right: 4px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: rgba(15,23,42,.75);
  color: white;
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; line-height: 1;
  opacity: 0;
  transition: opacity .12s, background .12s;
}
.photo-thumb:hover .photo-remove { opacity: 1; }
.photo-thumb .photo-remove:hover { background: var(--danger); }

.add-photo {
  width: 120px;
  height: 90px;
  border-radius: 8px;
  border: 2px dashed var(--border-strong);
  background: var(--panel-2);
  color: var(--text-soft);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-direction: column;
  font-size: 12px;
  font-weight: 600;
  gap: 4px;
  transition: border-color .12s, color .12s, background .12s;
  flex-shrink: 0;
}
.add-photo:hover {
  border-color: var(--brand);
  color: var(--brand);
  background: white;
}
.add-photo .plus { font-size: 22px; line-height: 1; font-weight: 400; }

.empty-photos {
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
  padding-top: 30px;
}

/* ---------------- Modal / Picker ---------------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15,23,42,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-backdrop.hidden { display: none; }
.modal {
  background: var(--panel);
  width: 100%;
  max-width: 980px;
  height: 86vh;
  max-height: 86vh;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
/* Compact modals (Users, annotation picker) size to their content. */
.modal.small {
  height: auto;
  max-height: 86vh;
}
.modal-head {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
}
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-close {
  background: transparent; border: none;
  font-size: 26px; line-height: 1;
  cursor: pointer;
  color: var(--text-soft);
  padding: 4px 10px;
  border-radius: 6px;
}
.modal-close:hover { background: var(--panel-2); color: var(--text); }

.modal-toolbar {
  padding: 12px 20px;
  display: flex; gap: 10px; align-items: center;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  flex-shrink: 0;
}
.modal-toolbar #picker-search {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: white;
}
.upload-btn {
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: white;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-soft);
}
.upload-btn:hover { color: var(--text); border-color: var(--muted); }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: 12px; }

.picker-grid {
  padding: 16px 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  grid-auto-rows: max-content;   /* never stretch rows to fill height */
  align-content: start;
  gap: 14px;
  overflow-y: auto;
  flex: 1 1 0;
  min-height: 0;                 /* required so the grid can actually scroll */
}
.picker-tile {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  background: var(--panel-2);
  cursor: pointer;
  aspect-ratio: 4 / 3;
  min-width: 0;
  transition: transform .1s, border-color .12s, box-shadow .12s;
}
.picker-tile:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.picker-tile img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.picker-tile.selected { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(14,165,233,.25); }
.picker-tile.in-use::after {
  content: "Already added";
  position: absolute; left: 6px; bottom: 6px;
  background: rgba(15,23,42,.75);
  color: white;
  font-size: 10px;
  padding: 3px 6px;
  border-radius: 4px;
  font-weight: 600;
}

/* Gallery-specific badges (shown on the All Photos view) */
.picker-tile .usage-badge {
  position: absolute; left: 6px; bottom: 6px;
  font-size: 10px;
  padding: 3px 7px;
  border-radius: 999px;
  font-weight: 600;
  color: white;
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
}

/* "VIDEO" pill for video tiles */
.picker-tile .video-badge,
.photo-thumb .video-badge {
  position: absolute;
  top: 6px; left: 6px;
  font-size: 9px;
  letter-spacing: .08em;
  padding: 2px 7px;
  border-radius: 999px;
  font-weight: 700;
  color: white;
  background: rgba(220, 38, 38, .9);
  box-shadow: 0 1px 3px rgba(0,0,0,.35);
}
.photo-thumb .video-badge { font-size: 8px; padding: 2px 6px; }

/* Centered play overlay on video thumbnails (in case the SVG isn't loaded yet) */
.photo-thumb.is-video::after {
  content: "▶";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: rgba(255,255,255,.9);
  pointer-events: none;
  text-shadow: 0 1px 3px rgba(0,0,0,.5);
}
.usage-badge.assigned { background: var(--success); }
.usage-badge.unassigned { background: #f59e0b; }
.picker-tile.gallery { cursor: zoom-in; }
.picker-tile.gallery:hover { transform: translateY(-2px); }
.picker-tile .check {
  position: absolute; top: 6px; right: 6px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: white;
  border: 2px solid var(--border-strong);
  display: flex; align-items: center; justify-content: center;
  color: white;
  font-size: 14px;
  font-weight: 700;
}
.picker-tile.selected .check { background: var(--accent); border-color: var(--accent); }
.picker-tile.selected .check::after { content: "✓"; }
.picker-tile .filename {
  position: absolute; left: 0; right: 0; top: 0;
  background: linear-gradient(to bottom, rgba(15,23,42,.7), transparent);
  color: white;
  font-size: 10px;
  padding: 4px 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity .15s;
}
.picker-tile:hover .filename { opacity: 1; }

/* ---------------- Lightbox ---------------- */
.lightbox {
  position: fixed; inset: 0;
  background: rgba(10, 12, 18, .94);
  display: flex; flex-direction: column;
  z-index: 200;
}
.lightbox.hidden { display: none; }

.lb-toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 18px;
  background: rgba(15, 23, 42, .6);
  border-bottom: 1px solid rgba(255,255,255,.08);
  color: white;
  flex-shrink: 0;
}
.lb-toolbar .btn-ghost {
  background: rgba(255,255,255,.06);
  border-color: rgba(255,255,255,.18);
  color: rgba(255,255,255,.88);
}
.lb-toolbar .btn-ghost:hover { background: rgba(255,255,255,.15); color: white; }
.lb-toolbar .btn-ghost.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}
.lb-caption {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: rgba(255,255,255,.7);
}

.lb-attached {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  background: rgba(15, 23, 42, .5);
  border-bottom: 1px solid rgba(255,255,255,.08);
  color: rgba(255,255,255,.85);
  font-size: 12px;
  flex-shrink: 0;
  flex-wrap: wrap;
}
.lb-attached-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: rgba(255,255,255,.55);
}
.lb-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
  min-width: 0;
}
.lb-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 10px;
  background: rgba(14, 165, 233, .18);
  border: 1px solid rgba(14, 165, 233, .55);
  border-radius: 999px;
  color: white;
  font-size: 12px;
  max-width: 320px;
}
.lb-chip .chip-id {
  font-variant-numeric: tabular-nums;
  color: rgba(255,255,255,.6);
  font-weight: 700;
  font-size: 11px;
}
.lb-chip .chip-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
}
.lb-chip .chip-x {
  background: rgba(255,255,255,.12);
  border: none;
  color: white;
  width: 18px; height: 18px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  padding: 0;
  margin-left: 2px;
}
.lb-chip .chip-x:hover { background: var(--danger); }
.lb-chips-empty {
  color: rgba(255,255,255,.45);
  font-style: italic;
}

.lb-attach-wrap { position: relative; }
.lb-attach-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  width: 360px;
  max-height: 360px;
  background: var(--panel);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  z-index: 5;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.lb-attach-menu.hidden { display: none; }
.lb-attach-menu input {
  padding: 10px 12px;
  border: none;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  outline: none;
}
.lb-attach-list {
  overflow-y: auto;
  flex: 1;
}
.lb-attach-row {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.lb-attach-row:hover { background: var(--panel-2); }
.lb-attach-row:last-child { border-bottom: none; }
.lb-attach-row .row-id {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  flex-shrink: 0;
}
.lb-attach-row .row-desc {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lb-attach-row .row-cat {
  color: var(--muted);
  font-size: 11px;
  flex-shrink: 0;
}
.lb-attach-list-empty {
  padding: 16px;
  text-align: center;
  color: var(--muted);
  font-style: italic;
}

.lb-stage {
  flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  overflow: hidden;
}
.lb-img-wrap {
  position: relative;
  display: inline-block;
  max-width: 100%; max-height: 100%;
  line-height: 0;
  box-shadow: 0 10px 50px rgba(0,0,0,.6);
}
.lb-img-wrap img,
.lb-img-wrap video {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 200px);
  object-fit: contain;
  border-radius: 2px;
  user-select: none;
  -webkit-user-drag: none;
  background: black;
}
.lb-img-wrap video { width: auto; }
#lb-img.hidden, #lb-video.hidden { display: none; }

.lb-anno-layer {
  position: absolute; inset: 0;
  pointer-events: none;
}
.lb-anno-layer.editing { pointer-events: auto; cursor: crosshair; touch-action: none; }
.lb-anno-layer.editing .anno-box { cursor: pointer; pointer-events: auto; }

.anno-box {
  position: absolute;
  border: 2px solid var(--accent);
  background: rgba(14, 165, 233, .12);
  border-radius: 3px;
  pointer-events: auto;
  cursor: pointer;
  transition: background .12s, border-color .12s;
  box-sizing: border-box;
}
.anno-box:hover {
  background: rgba(14, 165, 233, .25);
  border-color: #60d3ff;
}
.anno-box.untagged {
  border-color: #f59e0b;
  background: rgba(245, 158, 11, .12);
}
.anno-box.untagged:hover {
  background: rgba(245, 158, 11, .25);
  border-color: #fbbf24;
}

.anno-tooltip {
  position: absolute;
  background: rgba(15, 23, 42, .96);
  color: white;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  max-width: 320px;
  z-index: 5;
  pointer-events: none;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
  border: 1px solid rgba(255,255,255,.08);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .12s, transform .12s;
}
.anno-tooltip.show { opacity: 1; transform: translateY(0); }
.anno-tooltip .tt-status {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  padding: 2px 8px;
  border-radius: 999px;
  margin-bottom: 6px;
}
.anno-tooltip .tt-status.cataloged { background: var(--success); color: white; }
.anno-tooltip .tt-status.untagged { background: #f59e0b; color: white; }
.anno-tooltip .tt-title { font-weight: 600; line-height: 1.3; }
.anno-tooltip .tt-meta {
  color: rgba(255,255,255,.65);
  font-size: 11px;
  margin-top: 4px;
}

.anno-delete {
  position: absolute;
  top: -10px; right: -10px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  border: 2px solid white;
  cursor: pointer;
  display: none;
  align-items: center; justify-content: center;
  font-size: 14px; line-height: 1;
  font-weight: bold;
  padding: 0;
}
.lb-anno-layer.editing .anno-box .anno-delete { display: flex; }

.anno-draft {
  position: absolute;
  border: 2px dashed var(--accent);
  background: rgba(14, 165, 233, .1);
  pointer-events: none;
  border-radius: 3px;
}

.lb-edit-help {
  background: rgba(15, 23, 42, .8);
  color: rgba(255,255,255,.85);
  padding: 10px 18px;
  text-align: center;
  font-size: 13px;
  border-top: 1px solid rgba(255,255,255,.08);
  flex-shrink: 0;
}
.lb-edit-help.hidden { display: none; }

/* ---------------- Annotation tag picker modal ---------------- */
/* Sits above the lightbox (z-index 200) so it appears on top of the photo. */
#anno-picker { z-index: 300; }
.modal.small { max-width: 560px; }
.anno-picker-body {
  padding: 16px 20px 20px;
  display: flex; flex-direction: column;
}
.anno-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 4px;
}
.anno-picker-body select,
.anno-picker-body input[type="text"] {
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-size: 14px;
  background: white;
}
.anno-picker-body select:focus,
.anno-picker-body input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(14,165,233,.18);
}
.modal-foot {
  display: flex; gap: 8px; justify-content: flex-end;
  margin-top: 16px;
}

/* ---------------- Category dropdown (select) ---------------- */
.field select.cat-select {
  padding-right: 22px;
  background: transparent;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path fill='%2394a3b8' d='M0 0l5 6 5-6z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 6px center;
  cursor: pointer;
}

/* ---------------- Toast ---------------- */
.toast {
  position: fixed;
  bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: white;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none;
  z-index: 500;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }

/* ---------------- Empty / loading ---------------- */
.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--muted);
}
.empty-state h2 { font-weight: 600; color: var(--text-soft); }

/* ============================================================
   Responsive layout
   - Tablet:  max-width 900px
   - Phone:   max-width 640px
   - Small:   max-width 400px
   ============================================================ */
@media (max-width: 900px) {
  .item-head {
    grid-template-columns: 60px 1fr;
    gap: 12px;
    padding: 14px 16px;
  }
  .item-id { padding-top: 6px; grid-row: span 5; }
  .field { grid-column: 2; }
  .item-actions {
    grid-column: 2;
    justify-content: flex-end;
  }
  .toolbar { flex-wrap: wrap; }
}

@media (max-width: 640px) {
  /* Prevent iOS Safari auto-zoom on focus by ensuring inputs are >= 16px */
  input, textarea, select, button {
    font-size: 16px !important;
  }
  .field label { font-size: 10px !important; }

  /* Topbar — collapses behind hamburger on phones */
  .topbar {
    padding: 12px 14px;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
  }
  .brand { flex: 1; min-width: 0; }
  .brand h1 { font-size: 16px; }
  .subtitle { display: none; }
  .hamburger { display: inline-flex; }

  .toolbar {
    display: none;
    width: 100%;
    margin-left: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    border-top: 1px solid var(--border);
    padding-top: 12px;
    margin-top: 4px;
  }
  .topbar.menu-open .toolbar { display: flex; }
  .toolbar .btn,
  .toolbar a.btn {
    width: 100%;
    text-align: left;
    padding: 11px 14px;
    justify-content: flex-start;
  }
  .search-wrap { flex: 1 1 100%; max-width: 100%; }
  #filter-cat { width: 100%; padding: 11px 14px; }
  .who-indicator {
    flex: 1 1 100%;
    justify-content: center;
    padding: 10px;
  }

  /* Dropdown menu — render inline (not floating) inside the mobile menu */
  .dropdown { width: 100%; }
  .dropdown-menu {
    position: static;
    box-shadow: none;
    border: none;
    background: var(--panel-2);
    border-radius: 6px;
    margin-top: 4px;
    min-width: 0;
  }
  .dropdown-item strong { font-size: 14px; }

  /* Items — single column stack */
  main#items {
    padding: 0 12px 32px;
    gap: 12px;
    margin: 16px auto;
  }
  .item-head {
    grid-template-columns: 1fr !important;
    padding: 12px 14px;
    gap: 8px;
  }
  .item-id {
    grid-row: auto !important;
    padding-top: 0;
    font-size: 12px;
    color: var(--brand);
  }
  .item-head + .item-head { padding-top: 0; }
  .item-head > div[style] { display: none; } /* spacer cells */
  .item-actions { justify-content: stretch; }
  .item-actions .btn { width: 100%; }
  .field input, .field textarea, .field select { padding: 10px 12px; }
  .field .qty-input { max-width: 100px; }

  /* Photos — smaller thumbs, more compact */
  .item-photos {
    gap: 8px;
    padding: 12px 14px;
  }
  .photo-thumb {
    width: 96px;
    height: 72px;
  }
  .photo-thumb .photo-remove {
    opacity: 1;            /* always visible on touch */
    width: 26px; height: 26px;
    font-size: 16px;
  }
  .add-photo {
    width: 96px;
    height: 72px;
    font-size: 11px;
  }

  /* Modal — full screen on phones */
  .modal-backdrop { padding: 0; }
  .modal {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    border-radius: 0;
  }
  .modal.small {
    max-width: none;
    height: auto;
    max-height: 100%;
    border-radius: 0;
  }
  .modal-head { padding: 14px 16px; }
  .modal-toolbar {
    flex-wrap: wrap;
    padding: 10px 14px;
    gap: 8px;
  }
  .modal-toolbar #picker-search,
  .modal-toolbar #gallery-search {
    flex: 1 1 100%;
  }
  .modal-toolbar .spacer { flex-basis: 100%; height: 0; }
  .picker-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    grid-auto-rows: max-content;
    align-content: start;
    gap: 10px;
    padding: 12px 14px;
  }

  /* Users modal */
  .users-body { padding: 14px; }
  .users-add {
    grid-template-columns: 1fr;
  }
  .users-add #add-user-btn {
    justify-self: stretch;
    width: 100%;
  }
  .users-row {
    flex-wrap: wrap;
    gap: 8px;
  }
  .users-row .name { flex: 1 1 100%; }
  .users-row .role-pill, .users-row .you { order: 1; }
  .users-row .btn { flex: 1 1 0; }

  /* Lightbox — tighter toolbar, attached chips full width */
  .lb-toolbar {
    padding: 10px 12px;
    gap: 6px;
  }
  .lb-caption {
    font-size: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1 1 auto;
    min-width: 0;
  }
  .lb-toolbar .btn { padding: 7px 10px; font-size: 13px; }
  .lb-attached {
    padding: 8px 12px;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .lb-attached-label { font-size: 9px; }
  .lb-chip { max-width: none; }
  .lb-chip .chip-text { max-width: 70vw; }
  .lb-chip .chip-x { width: 22px; height: 22px; font-size: 14px; }
  .lb-attach-wrap { width: 100%; }
  .lb-attach-wrap .btn { width: 100%; }
  .lb-attach-menu {
    position: fixed;
    left: 8px;
    right: 8px;
    top: auto;
    bottom: 8px;
    width: auto;
    max-height: 60vh;
  }
  .lb-stage { padding: 8px; }
  .lb-img-wrap img { max-height: calc(100vh - 220px); }
  .lb-edit-help { font-size: 12px; padding: 8px 12px; }

  /* Annotation tag picker modal */
  .anno-picker-body { padding: 14px; }
  .anno-tooltip {
    max-width: 90vw;
    font-size: 12px;
  }

  /* Login card scales to viewport */
  .login-card {
    max-width: none;
    margin: 16px;
    padding: 22px 20px;
  }
}

@media (max-width: 400px) {
  .brand .brand-mark { width: 30px; height: 30px; }
  .brand h1 { font-size: 14px; }
  main#items { padding: 0 10px 24px; }
  .item-head { padding: 10px 12px; }
  .item-photos { padding: 10px 12px; }
  .photo-thumb, .add-photo { width: 84px; height: 64px; }
  .picker-grid {
    grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  }
  .toolbar .btn { padding: 8px 10px; font-size: 13px; }
  .modal-head h2 { font-size: 14px; }
}

/* Touch-friendly: bigger tap targets on devices without hover */
@media (hover: none) and (pointer: coarse) {
  .photo-thumb .photo-remove { opacity: 1; }
  .picker-tile .filename { opacity: 1; font-size: 9px; }
  .chip-x { width: 24px; height: 24px; }
  /* Avoid sticky :hover styles on tap */
  .btn:active { background: var(--brand-2); }
}
