:root {
  --wine-950: #3f0d19;
  --wine-900: #5d1325;
  --wine-800: #7a1f33;
  --wine-700: #933048;
  --wine-200: #edd8dc;
  --wine-100: #f6ecee;
  --wine-050: #fbf5f6;
  --ivory: #f6f0ee;
  --paper: #fffdfc;
  --ink: #2d1720;
  --muted: #6a5560;
  --line: #ddc8ce;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, sans-serif;
  background: var(--ivory);
  color: var(--ink);
}

header {
  padding: 14px 16px;
  background: linear-gradient(90deg, var(--wine-950), var(--wine-800));
  color: #fff;
  box-shadow: 0 10px 28px rgba(63, 13, 25, 0.22);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

header h1 {
  margin: 0;
  font-size: 22px;
}

header p {
  margin: 4px 0 0;
  font-size: 13px;
}

.header-logo-wrap {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.header-actions {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.scan2x-logo {
  max-height: 48px;
  width: auto;
  opacity: 0.95;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.25));
}

.profile-menu-btn {
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  min-width: 44px;
  min-height: 44px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.32);
  box-shadow: none;
}

.profile-menu-btn span {
  width: 18px;
  height: 2px;
  background: #fff;
  border-radius: 999px;
}

.profile-menu {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 30;
  min-width: 280px;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  background: rgba(63, 13, 25, 0.96);
  color: #fff;
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.28);
}

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

.profile-menu-head {
  margin-bottom: 12px;
}

.profile-form {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.profile-form label {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.82);
}

.profile-form input {
  padding: 9px 10px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.secondary-button {
  background: transparent;
  border-color: rgba(255, 255, 255, 0.32);
}

.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(28, 10, 15, 0.72);
  backdrop-filter: blur(8px);
}

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

.auth-card {
  width: min(460px, 100%);
  padding: 24px;
  border: 1px solid rgba(93, 19, 37, 0.22);
  border-radius: 16px;
  background: linear-gradient(180deg, #fffdfd, #f9eff1);
  box-shadow: 0 24px 52px rgba(24, 6, 10, 0.24);
}

.auth-card h2 {
  margin: 0 0 8px;
  color: var(--wine-950);
}

.auth-card p {
  margin: 0 0 16px;
  color: var(--muted);
}

.auth-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.auth-tab {
  padding: 8px 12px;
  box-shadow: none;
}

.auth-tab.active {
  background: var(--wine-950);
}

.auth-status {
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--wine-100);
  color: var(--wine-950);
  font-size: 14px;
}

.auth-status[data-kind="error"] {
  background: #f7d9dd;
  color: #7a0f22;
}

.auth-panel {
  display: none;
}

.auth-panel.active {
  display: grid;
  gap: 8px;
}

.auth-panel label {
  font-size: 13px;
  color: var(--muted);
}

.family-question-text {
  padding: 10px 12px;
  border: 1px solid #d8b8c0;
  border-radius: 8px;
  background: var(--wine-050);
  color: var(--wine-950);
  font-size: 14px;
  line-height: 1.4;
}

.auth-panel input {
  padding: 10px;
  border: 1px solid #cfb0b8;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

#familyAnswer {
  text-transform: uppercase;
}

.app-shell-locked {
  pointer-events: none;
  user-select: none;
}

.app-shell-locked .search-bar,
.app-shell-locked .search-status-bar,
.app-shell-locked .layout {
  filter: blur(2px);
}

.view-tabs {
  display: flex;
  gap: 8px;
  padding: 12px 16px 0;
  background: var(--paper);
}

.view-tab {
  padding: 9px 14px;
  box-shadow: none;
}

.view-tab.active {
  background: var(--wine-950);
}

.search-bar {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) repeat(4, minmax(150px, 1fr)) auto;
  gap: 10px;
  padding: 12px 16px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.search-field {
  display: grid;
  gap: 6px;
}

.search-field-wide {
  min-width: 0;
}

.search-field label {
  font-size: 13px;
  color: var(--muted);
}

.search-bar input,
.search-bar select {
  padding: 9px 10px;
  border: 1px solid #caa9b1;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
}

.search-bar input:focus,
.search-bar select:focus,
#metadataForm input:focus,
#metadataForm textarea:focus {
  outline: 2px solid rgba(147, 48, 72, 0.18);
  border-color: var(--wine-700);
}

.search-actions {
  display: flex;
  align-items: end;
  gap: 8px;
}

.search-actions button {
  padding: 9px 14px;
}

.browse-panel {
  padding: 16px;
  background: linear-gradient(180deg, #fff7f8, #f6edef);
  border-bottom: 1px solid var(--line);
}

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

.browse-intro h2 {
  margin: 0 0 6px;
  color: var(--wine-950);
}

.browse-intro p {
  margin: 0 0 14px;
  color: var(--muted);
}

.facet-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.facet-card {
  padding: 14px;
  border: 1px solid #d8b7c0;
  border-radius: 12px;
  background: #fffdfd;
  box-shadow: 0 10px 22px rgba(93, 19, 37, 0.08);
}

.facet-card h3 {
  margin: 0 0 10px;
  color: var(--wine-900);
}

.facet-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.facet-pill {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 999px;
  background: var(--wine-050);
  color: var(--wine-950);
  border: 1px solid #d8b7c0;
  box-shadow: none;
}

.facet-pill span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

.facet-pill strong {
  font-size: 12px;
  color: var(--wine-700);
}

.search-status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px 12px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.searching-indicator {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--wine-900);
  font-size: 16px;
  font-weight: 600;
}

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

.searching-pulse {
  width: 14px;
  height: 14px;
  border-radius: 999px;
  background: var(--wine-700);
  box-shadow: 0 0 0 rgba(147, 48, 72, 0.45);
  animation: archive-pulse 1.4s ease-out infinite;
}

.searching-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.searching-dots span {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--wine-700);
  animation: archive-dot-bounce 1s infinite ease-in-out;
}

.searching-dots span:nth-child(2) {
  animation-delay: 0.15s;
}

.searching-dots span:nth-child(3) {
  animation-delay: 0.3s;
}

.search-stats {
  display: flex;
  align-items: center;
  gap: 18px;
  color: var(--muted);
  font-size: 13px;
}

button {
  border: 1px solid var(--wine-900);
  border-radius: 6px;
  background: var(--wine-800);
  color: #fff;
  cursor: pointer;
  transition: background 120ms ease, transform 120ms ease, box-shadow 120ms ease;
  box-shadow: 0 6px 16px rgba(93, 19, 37, 0.16);
}

button:hover:not(:disabled) {
  background: var(--wine-700);
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  box-shadow: none;
}

.layout {
  display: grid;
  grid-template-columns: 55% 45%;
  height: calc(100vh - 130px);
  min-height: 0;
  overflow: hidden;
}

.results-pane {
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--paper);
  min-height: 0;
}

#resultsTable {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

#resultsTable thead th {
  position: sticky;
  top: 0;
  background: var(--wine-100);
  border-bottom: 1px solid #d8b8c0;
  text-align: left;
  padding: 8px 7px;
}

#resultsTable tbody td {
  padding: 7px;
  border-bottom: 1px solid #efe4e7;
  vertical-align: top;
}

#resultsTable tbody tr:nth-child(even) {
  background: var(--wine-050);
}

#resultsTable tbody tr.selected {
  background: var(--wine-200);
}

.preview-pane {
  display: flex;
  flex-direction: column;
  background: #f4e8eb;
  min-height: 0;
}

.preview-actions {
  display: flex;
  gap: 8px;
  padding: 8px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 8px 0;
}

.tab {
  border: 1px solid #d7b4bc;
  background: #f3e2e6;
  padding: 6px 10px;
  color: var(--wine-950);
  box-shadow: none;
}

.tab.active {
  background: #e6c6cf;
}

.panel {
  display: none;
  flex: 1 1 auto;
  margin: 8px;
  padding: 10px;
  border: 1px solid #d8b7c0;
  background: #fff7f8;
  min-height: 0;
  overflow: auto;
  white-space: pre-wrap;
}

.panel.active {
  display: block;
}

.panel-toolbar {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 10px;
}

.panel-toolbar button[hidden] {
  display: none !important;
}

#metadataList .metadata-item {
  border: 1px solid #dec0c7;
  background: #fffafb;
  padding: 8px;
  margin-bottom: 8px;
}

.activity-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.activity-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 8px 7px;
  text-align: left;
  background: var(--wine-100);
  border-bottom: 1px solid #d8b8c0;
}

.activity-table tbody td {
  padding: 8px 7px;
  border-bottom: 1px solid #efe4e7;
  vertical-align: top;
  white-space: normal;
}

.activity-table tbody tr:nth-child(even) {
  background: var(--wine-050);
}

#metadataList .meta-head {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 5px;
}

#metadataForm {
  display: grid;
  gap: 8px;
}

#metadataForm .row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

#metadataForm input,
#metadataForm textarea {
  padding: 8px;
  border: 1px solid #cfb0b8;
  border-radius: 4px;
  background: #fff;
  color: var(--ink);
}

#metadataForm textarea {
  min-height: 80px;
}

@media (max-width: 960px) {
  .header-inner {
    flex-direction: column;
    align-items: flex-start;
  }

  .header-logo-wrap {
    width: 100%;
    justify-content: flex-start;
  }

  .header-actions {
    width: 100%;
    justify-content: space-between;
    align-items: center;
  }

  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 45% 55%;
  }

  .search-bar {
    grid-template-columns: 1fr;
  }

  .search-actions {
    align-items: stretch;
  }

  .facet-grid {
    grid-template-columns: 1fr;
  }

  .search-status-bar,
  .search-stats {
    flex-direction: column;
    align-items: flex-start;
  }

  .profile-menu {
    left: 0;
    right: auto;
    width: min(320px, calc(100vw - 32px));
  }
}

@keyframes archive-pulse {
  0% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(147, 48, 72, 0.45);
  }
  70% {
    transform: scale(1);
    box-shadow: 0 0 0 12px rgba(147, 48, 72, 0);
  }
  100% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(147, 48, 72, 0);
  }
}

@keyframes archive-dot-bounce {
  0%, 80%, 100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}
