:root {
  --bg: #16181d;
  --bg-panel: #1e2128;
  --bg-card: #23262e;
  --border: #33373f;
  --text: #e7e9ee;
  --text-dim: #9aa0ab;
  --accent: #4f8cff;
  --accent-hover: #6b9dff;
  --radius: 8px;
  --content-max: 1180px;
  --rail-width: 168px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ---- accessibility helpers ---- */

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

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  z-index: 200;
  background: var(--accent);
  color: #fff;
  padding: 10px 16px;
  border-radius: 0 0 var(--radius) 0;
}
.skip-link:focus { left: 0; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---- top bar ---- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

.brand {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
}
.brand span { font-weight: 400; color: var(--text-dim); }

.filter-toggle {
  display: none;
  padding: 8px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  flex: 1;
}

#search {
  flex: 1;
  min-width: 180px;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
}
#search:focus { outline: none; border-color: var(--accent); }

.kind-toggle {
  display: flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.kind-toggle button {
  padding: 8px 14px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
}
.kind-toggle button.active { background: var(--accent); color: #fff; }

select {
  padding: 8px 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  max-width: 220px;
}

.account-nav {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 14px;
}
.account-nav a { color: var(--accent); text-decoration: none; white-space: nowrap; }
.account-nav a:hover { color: var(--accent-hover); }
.account-nav button {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  padding: 0;
}
.account-nav button:hover { color: var(--text); }

.back-link { color: var(--accent); text-decoration: none; font-size: 14px; white-space: nowrap; }
.back-link:hover { color: var(--accent-hover); }

.status-msg { color: var(--text-dim); font-size: 14px; }
.status-msg.error { color: #ff8080; }

/* ---- 3-column shell: content centred, ad rails either side ---- */

.layout {
  display: grid;
  grid-template-columns: var(--rail-width) minmax(0, 1fr) var(--rail-width);
  gap: 24px;
  justify-content: center;
  max-width: calc(var(--content-max) + 2 * var(--rail-width) + 48px);
  margin: 0 auto;
  padding: 0 20px;
}

.layout-content { min-width: 0; max-width: var(--content-max); }

.ad-rail { padding-top: 20px; }
.ad-slot {
  position: sticky;
  top: 88px;
  min-height: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: #5a6070;
  font-size: 12px;
  text-align: center;
}

main { padding: 20px 0; }

.result-count { margin: 0 0 12px; color: var(--text-dim); font-size: 13px; }

/* ---- grid ---- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 14px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s, transform .15s;
  display: flex;
  flex-direction: column;
}
.card:hover { border-color: var(--accent); transform: translateY(-2px); }

.card-thumb {
  position: relative;
  aspect-ratio: 1 / 1;
  background: #101216
    linear-gradient(45deg, #1b1d22 25%, transparent 25%, transparent 75%, #1b1d22 75%),
    linear-gradient(45deg, #1b1d22 25%, transparent 25%, transparent 75%, #1b1d22 75%);
  background-size: 100%, 16px 16px, 16px 16px;
  background-position: 0 0, 0 0, 8px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.card-thumb img { width: 100%; height: 100%; object-fit: contain; }
.card-thumb.thumb-missing { color: #4a5060; font-size: 11px; }

.origin-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: rgba(79, 140, 255, 0.9);
  color: #fff;
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 999px;
}

.card-body { padding: 8px 10px 10px; }
.card-name {
  font-size: 13px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 0 0 4px;
}
.card-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  color: var(--text-dim);
}
.kind-badge {
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .03em;
  background: #2e3440;
}
.kind-badge.model { color: #7ecbff; }
.kind-badge.material { color: #ffcf7e; }

.sentinel { height: 1px; }

.empty-state, .loading { text-align: center; color: var(--text-dim); padding: 40px 0; }
.loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
}
.loading[hidden] { display: none; }

/* ---- detail ---- */

.back-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  padding: 0 0 16px;
}
.back-btn:hover { color: var(--accent-hover); }

.detail-card { display: flex; gap: 28px; flex-wrap: wrap; }

.detail-thumb {
  width: 380px;
  max-width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.detail-thumb img { width: 100%; height: 100%; object-fit: contain; }
.detail-thumb model-viewer { width: 100%; height: 100%; --poster-color: transparent; }

.detail-info { flex: 1; min-width: 280px; }
.detail-info h2 { margin: 0 0 4px; font-size: 22px; }
.detail-info h2:focus { outline: none; }

.mono {
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  color: var(--text-dim);
  font-size: 12px;
  margin: 0 0 12px;
  word-break: break-all;
}

.detail-description {
  margin: 0 0 16px;
  line-height: 1.6;
  font-size: 14px;
  color: #c9cdd6;
  max-width: 60ch;
}

.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.tag {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  color: var(--text-dim);
}

.detail-table { border-collapse: collapse; margin-bottom: 16px; width: 100%; }
.detail-table td { padding: 5px 0; font-size: 13px; vertical-align: top; }
.detail-table td:first-child { color: var(--text-dim); width: 40%; max-width: 160px; padding-right: 12px; }

.hash-copy {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-dim);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 12px;
  padding: 2px 8px;
  cursor: pointer;
}
.hash-copy:hover { color: var(--text); border-color: var(--accent); }

.scalar-row { display: inline-flex; align-items: center; gap: 8px; }
.albedo-swatch {
  width: 16px; height: 16px;
  border-radius: 4px;
  border: 1px solid var(--border);
  display: inline-block;
}

.detail-license { font-size: 13px; margin-bottom: 6px; }
.detail-license span { color: var(--text-dim); margin-left: 6px; }
.detail-attribution { font-size: 13px; color: var(--text-dim); margin: 0 0 10px; }

.insert-btn {
  margin-top: 8px;
  padding: 10px 22px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.insert-btn:hover:not(:disabled) { background: var(--accent-hover); }
.insert-btn:disabled { opacity: .45; cursor: not-allowed; }

.insert-status { font-size: 13px; color: var(--text-dim); margin-top: 8px; max-width: 52ch; }

/* ---- downloads / maps / similar ---- */

.detail-downloads, .detail-maps, .similar-section { margin-top: 40px; }
.detail-downloads h3, .detail-maps h3, .similar-section h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 14px;
  color: var(--text-dim);
}

.download-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; max-width: 520px; }
.download-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.download-list a { color: var(--accent); text-decoration: none; font-size: 14px; }
.download-list a:hover { color: var(--accent-hover); text-decoration: underline; }
.download-size { color: var(--text-dim); font-size: 12px; white-space: nowrap; }

.map-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.map-cell { margin: 0; background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.map-cell img { width: 100%; aspect-ratio: 1/1; object-fit: cover; display: block; }
.map-cell figcaption { padding: 6px 8px; font-size: 11px; color: var(--text-dim); text-align: center; }

/* ---- responsive ---- */

@media (max-width: 1280px) {
  .ad-rail { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr); max-width: var(--content-max); }
}

@media (max-width: 900px) {
  .detail-thumb { width: 100%; }
  .detail-info { min-width: 0; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

@media (max-width: 700px) {
  .topbar { gap: 10px; padding: 10px 14px; }
  .filter-toggle { display: block; order: 3; }
  .account-nav { order: 2; margin-left: auto; font-size: 13px; gap: 10px; }
  /* Collapsed by default on small screens: expanded, the six controls used
     to eat most of the viewport above the fold. */
  .controls {
    order: 4;
    flex-basis: 100%;
    display: none;
    flex-direction: column;
    align-items: stretch;
  }
  .controls.open { display: flex; }
  select, #search { max-width: none; width: 100%; }
  .layout { padding: 0 14px; }
  .detail-table td:first-child { width: 45%; }
  .download-list li { flex-wrap: wrap; }
}

@media (max-width: 420px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
  .brand span { display: none; }
}
