:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface-alt: #f9f7f2;
  --border: #e2ddd2;
  --text: #1e2923;
  --muted: #5f6b64;
  --primary: #2d4c3e;
  --primary-hover: #21382e;
  --accent: #e09f24;
  --accent-soft: rgba(224, 159, 36, 0.22);
  --danger: #b91c1c;
  --success-bg: #ecfdf5;
  --success-text: #065f46;
  --error-bg: #fef2f2;
  --error-text: #991b1b;
  --warn-bg: #fffbeb;
  --warn-text: #92400e;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(30, 41, 35, 0.06);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.14);
}

.topbar-inner {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand-icon {
  font-size: 1.65rem;
}

.brand-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-sub {
  margin: 0;
  font-size: 0.75rem;
  opacity: 0.8;
}

.main-tabs {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: rgba(0, 0, 0, 0.22);
  padding: 0.25rem;
  border-radius: 999px;
}

.tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.95rem;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: rgba(255, 255, 255, 0.85);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.tab-btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
}

.tab-btn.is-active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.tab-count {
  font-size: 0.75rem;
  padding: 0.05rem 0.45rem;
  border-radius: 999px;
  background: rgba(45, 76, 62, 0.12);
}

.tab-btn:not(.is-active) .tab-count {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}

.search-workspace {
  display: grid;
  grid-template-columns: minmax(270px, 0.65fr) minmax(0, 1.55fr);
  gap: 1rem;
  align-items: start;
}

.search-panel {
  position: sticky;
  top: 4.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: calc(100dvh - 5.75rem);
}

.search-input-lg {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  padding: 0.75rem 1rem;
  font-size: 1rem;
}

.search-results-tall .book-results {
  max-height: calc(100dvh - 12.5rem);
}

.book-result-item.is-selected {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.location-stages {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem;
  align-items: start;
}

.location-block {
  min-width: 0;
}

.location-block .image-stage {
  width: fit-content;
  max-width: 100%;
  margin: 0 auto;
}

.location-block .stage-img {
  width: auto;
  max-width: 100%;
  max-height: calc(100dvh - 14rem);
  height: auto;
}

.location-step {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
  color: var(--muted);
}

@media (max-width: 1120px) {
  .location-stages {
    grid-template-columns: 1fr;
  }
  .location-block .stage-img {
    max-height: 40dvh;
  }
}

@media (max-width: 980px) {
  .search-workspace {
    grid-template-columns: 1fr;
  }
  .search-panel {
    position: static;
    max-height: none;
  }
  .search-results-tall .book-results {
    max-height: 300px;
  }
}

.search-input {
  width: 100%;
  padding: 0.6rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
}

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(224, 159, 36, 0.35);
}

.topbar-actions {
  display: flex;
  gap: 0.5rem;
  margin-left: auto;
}

/* Main layout */
.main-layout {
  max-width: 1440px;
  margin: 0 auto;
  padding: 1rem;
}

.workspace-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(340px, 0.95fr);
  gap: 1rem;
  align-items: start;
}

.left-column,
.right-column {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

.right-column {
  position: sticky;
  top: 4.75rem;
  max-height: calc(100dvh - 5.75rem);
  overflow-y: auto;
}

@media (max-width: 980px) {
  .workspace-grid {
    grid-template-columns: 1fr;
  }
  .right-column {
    position: static;
    max-height: none;
  }
}

/* Cards & Headers */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.card-header h2,
.card-header h3 {
  margin: 0;
  font-size: 1.05rem;
}

.stats-line {
  margin: 0.2rem 0 0;
  font-size: 0.84rem;
  color: var(--muted);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.inline-upload {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.5rem 0.85rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
}

.btn:active {
  transform: translateY(1px);
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
}

.btn-primary {
  background: var(--accent);
  color: #1c1917;
}

.btn-primary:hover {
  background: #cf8e16;
}

.btn-secondary {
  background: var(--surface-alt);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: #ece8de;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}

.btn-ghost:hover {
  background: var(--surface-alt);
  color: var(--text);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-icon {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0.25rem 0.4rem;
  border-radius: 6px;
  font-size: 0.9rem;
}

.btn-icon:hover {
  background: var(--surface-alt);
}

/* Banners & Alerts */
.banner {
  padding: 0.7rem 1rem;
  font-size: 0.88rem;
  text-align: center;
}

.banner-warn {
  background: var(--warn-bg);
  color: var(--warn-text);
  border-bottom: 1px solid #fde68a;
}

.alert {
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
}

.alert-success {
  background: var(--success-bg);
  color: var(--success-text);
}

.alert-error {
  background: var(--error-bg);
  color: var(--error-text);
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge-warn {
  background: var(--warn-bg);
  border-color: #fcd34d;
  color: var(--warn-text);
}

.badge-subtle {
  font-size: 0.7rem;
  padding: 0.08rem 0.4rem;
}

/* Image stage & SVG overlays */
.image-stage {
  position: relative;
  width: 100%;
  border-radius: 10px;
  overflow: hidden;
  background: #141a17;
  user-select: none;
  touch-action: none;
}

.stage-img {
  display: block;
  width: 100%;
  height: auto;
}

.stage-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.cubby-rect {
  fill: rgba(255, 255, 255, 0.04);
  stroke: rgba(255, 255, 255, 0.55);
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition: fill 0.15s ease, stroke 0.15s ease;
}

.cubby-rect.has-detail {
  fill: rgba(45, 212, 191, 0.1);
  stroke: rgba(94, 234, 212, 0.85);
}

.cubby-rect.unconfirmed {
  fill: rgba(251, 146, 60, 0.18);
  stroke: #fb923c;
  stroke-dasharray: 6 3;
}

.cubby-rect:hover {
  fill: rgba(224, 159, 36, 0.25);
  stroke: #fbbf24;
  stroke-width: 3;
}

.cubby-rect.is-active {
  fill: rgba(224, 159, 36, 0.32);
  stroke: #f59e0b;
  stroke-width: 4;
}

.cubby-label-bg {
  fill: rgba(15, 23, 20, 0.78);
  pointer-events: none;
}

.cubby-label-text {
  fill: #fff;
  font-size: 15px;
  font-weight: 700;
  pointer-events: none;
}

.resize-handle {
  fill: #f59e0b;
  stroke: #fff;
  stroke-width: 2;
  cursor: nwse-resize;
}

.spine-rect {
  fill: rgba(255, 255, 255, 0.03);
  stroke: rgba(255, 255, 255, 0.35);
  stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition: fill 0.15s ease, stroke 0.15s ease;
}

.spine-rect:hover {
  fill: rgba(251, 191, 36, 0.24);
  stroke: #fbbf24;
  stroke-width: 2.5;
}

.spine-rect.is-highlighted {
  fill: rgba(245, 158, 11, 0.38);
  stroke: #fbbf24;
  stroke-width: 4;
}

.analysis-mask {
  fill: rgba(15, 23, 20, 0.45);
  pointer-events: none;
}

.analysis-zone-rect {
  fill: none;
  stroke: #2dd4bf;
  stroke-width: 2.5;
  stroke-dasharray: 8 5;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.analysis-zone-bg {
  fill: rgba(15, 23, 20, 0.82);
  pointer-events: none;
}

.analysis-zone-text {
  fill: #5eead4;
  font-size: 13px;
  font-weight: 700;
  pointer-events: none;
}

.draft-rect {
  fill: rgba(224, 159, 36, 0.25);
  stroke: #f59e0b;
  stroke-width: 2.5;
  stroke-dasharray: 6 4;
  vector-effect: non-scaling-stroke;
}

/* Edit help bar */
.edit-help {
  background: var(--warn-bg);
  border: 1px solid #fde68a;
  color: var(--warn-text);
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  font-size: 0.83rem;
  margin-bottom: 0.75rem;
}

.selected-cubby-controls,
.crop-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

/* Confirm / Reassign */
.confirm-banner {
  background: var(--warn-bg);
  border: 1px solid #fcd34d;
  border-radius: 8px;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.confirm-actions,
.reassign-form {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.reassign-details {
  margin-bottom: 0.6rem;
  font-size: 0.83rem;
}

.reassign-details summary {
  cursor: pointer;
  margin-bottom: 0.4rem;
}

/* Search results & book lists */
.book-results,
.detail-book-list,
.upload-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.book-results {
  max-height: 360px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.book-result-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}

.book-result-item:hover,
.book-result-item:focus-visible {
  background: var(--surface-alt);
  border-color: var(--border);
  outline: none;
}

.book-title {
  font-weight: 600;
  font-size: 0.92rem;
}

.book-meta {
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.book-pub {
  opacity: 0.75;
}

.cubby-pill {
  flex-shrink: 0;
  background: var(--primary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
}

/* Detail book list */
.books-section {
  margin-top: 1rem;
}

.books-section h3 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
}

.detail-book-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.detail-book-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.65rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.detail-book-item:hover {
  background: var(--surface-alt);
}

.detail-book-item.is-highlighted {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.detail-book-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.detail-book-actions {
  display: flex;
  gap: 0.15rem;
  flex-shrink: 0;
}

.book-edit-form {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--border);
}

.edit-form-actions {
  display: flex;
  gap: 0.4rem;
}

.input-sm {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
  background: #fff;
  color: var(--text);
}

.add-book-details {
  margin-top: 0.75rem;
}

.add-book-details summary {
  list-style: none;
  display: inline-flex;
}

/* Batch uploads */
.upload-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.upload-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.45rem;
  border-radius: 8px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

.upload-thumb {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 6px;
  flex-shrink: 0;
}

.upload-info {
  flex: 1;
  min-width: 0;
}

.upload-name {
  font-weight: 600;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.upload-msg {
  font-size: 0.78rem;
  color: var(--muted);
}

.text-error {
  color: var(--danger);
}

.pulse-dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 0.35rem;
  animation: pulse 1.2s infinite ease-in-out;
}

@keyframes pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.9); }
  50% { opacity: 1; transform: scale(1.15); }
}

.htmx-indicator {
  display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  display: inline-flex;
}

.spinner {
  font-size: 0.8rem;
  color: var(--primary);
  font-weight: 600;
}

.empty-state {
  text-align: center;
  padding: 2rem 1rem;
}

.empty-icon {
  font-size: 2.2rem;
  margin-bottom: 0.5rem;
}

.empty-inline {
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.88rem;
}

.muted {
  color: var(--muted);
  font-size: 0.86rem;
}

/* Native Modal Dialogs (Modern Web Guidance) */
dialog.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  width: min(500px, calc(100vw - 2rem));
  max-height: calc(100dvh - 2rem);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  background: var(--surface);
  color: var(--text);
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(15, 23, 20, 0.55);
  backdrop-filter: blur(2px);
  overflow: hidden;
  overscroll-behavior: contain;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.15rem;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  margin: 0;
  font-size: 1.05rem;
}

.modal-body {
  padding: 1rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

.file-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 1.5rem 1rem;
  border: 2px dashed var(--border);
  border-radius: 10px;
  background: var(--surface-alt);
  cursor: pointer;
  text-align: center;
  font-weight: 500;
}

.file-drop:hover,
.file-drop.is-dragover,
.cubby-photo-drop.is-dragover,
.detail-stage.is-dragover {
  border-color: var(--accent);
  background: var(--warn-bg);
  outline: 2px dashed var(--accent);
  outline-offset: 2px;
}

.paste-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  justify-content: center;
  margin-top: 0.5rem;
  width: 100%;
}

.paste-preview-item {
  position: relative;
  width: 68px;
  height: 68px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #fff;
}

.paste-preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.paste-preview-count {
  width: 100%;
  font-size: 0.78rem;
  color: var(--primary);
  font-weight: 600;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.88rem;
  cursor: pointer;
}
