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

:root {
  --bg: #0e1018;
  --surface: #161923;
  --surface2: #1e2230;
  --border: rgba(255,255,255,0.07);
  --accent: #4fc3f7;
  --text: #dde1f0;
  --text-dim: #7a7e96;
  --viewer-w: clamp(320px, 32vw, 560px);
  --viewer-h: clamp(260px, 42vh, 480px);
  --hm-aspect: 1;
}

@supports (height: 1dvh) {
  :root { --viewer-h: clamp(260px, 42dvh, 480px); }
}

html, body { height: 100%; overflow: hidden; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  display: flex;
  flex-direction: column;
}


/* ── Main ── */
#main { flex: 1; min-height: 0; display: flex; overflow: hidden; }

/* ── Left: 3D viewer ── */
#viewer-panel {
  width: var(--viewer-w);
  flex-shrink: 0;
  position: relative;
  background: #ebebeb;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

/* Draggable separator between the structure and landscape panels. */
#panel-resizer {
  position: relative;
  z-index: 600;
  width: 18px;
  flex: 0 0 18px;
  border: 0;
  border-left: 1px solid rgba(255,255,255,0.08);
  border-right: 1px solid rgba(255,255,255,0.08);
  padding: 0;
  background: var(--surface2);
  cursor: col-resize;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#panel-resizer::before {
  content: "";
  position: absolute;
  inset: 0 7px;
  background: rgba(79,195,247,0.45);
  transition: background-color 0.15s, box-shadow 0.15s;
}
#panel-resizer::after {
  content: "↔";
  position: absolute;
  top: 50%; left: 50%;
  width: 30px; height: 54px;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(79,195,247,0.75);
  border-radius: 9px;
  background: #263141;
  color: #8edcff;
  box-shadow: 0 3px 12px rgba(0,0,0,0.35);
  font: 700 17px/1 monospace;
  transition: color 0.15s, background-color 0.15s, transform 0.15s;
}
#panel-resizer:hover::before,
#panel-resizer:focus-visible::before,
#panel-resizer.is-dragging::before,
body.panel-resizing #panel-resizer::before {
  background: var(--accent);
  box-shadow: 0 0 0 2px rgba(79,195,247,0.18);
}
#panel-resizer:hover::after,
#panel-resizer:focus-visible::after,
#panel-resizer.is-dragging::after {
  background: var(--accent);
  color: #0e1018;
  transform: translate(-50%, -50%) scale(1.08);
}
#panel-resizer:focus { outline: 0; }
#panel-resizer:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
body.panel-resizing,
body.panel-resizing * { cursor: col-resize !important; }
#three-mount { flex: 1; min-height: 0; }
#three-mount canvas { display: block; width: 100% !important; height: 100% !important; }

#viewer-btns {
  position: absolute; top: 8px; right: 8px;
  display: flex; flex-direction: column; gap: 4px; z-index: 10;
}
.vbtn {
  padding: 4px 8px; font-size: 11px; cursor: pointer;
  background: rgba(255,255,255,0.80); color: #333;
  border: 1px solid rgba(0,0,0,0.12); border-radius: 6px;
  backdrop-filter: blur(6px); white-space: nowrap;
}
.vbtn:hover { border-color: #555; }
.vbtn.on { border-color: #4a9e5c; color: #2d7a44; }

#nav-panel {
  position: absolute; bottom: 8px; right: 8px;
  background: rgba(255,255,255,0.80);
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 8px; padding: 7px 10px;
  font-size: 11px; backdrop-filter: blur(8px); z-index: 10;
  display: flex; flex-direction: column; gap: 5px; align-items: flex-end;
  min-width: 130px;
}
.nav-ds-link {
  display: block; width: 100%; text-align: center;
  padding: 3px 8px; border-radius: 5px;
  font-size: 11px; font-family: monospace; text-decoration: none;
  border: 1px solid rgba(0,0,0,0.12); color: #555;
  transition: border-color .15s, color .15s;
}
.nav-ds-link:hover { border-color: #0077aa; color: #0077aa; }
.nav-ds-link.active { border-color: #4a9e5c; color: #2d7a44; pointer-events: none; }
.nav-sg-link {
  font-size: 10px; font-family: monospace;
  color: #777; text-decoration: none;
  padding: 2px 4px;
}
.nav-sg-link:hover { color: #0077aa; }
.nav-sep { width: 100%; border: none; border-top: 1px solid rgba(0,0,0,0.1); margin: 2px 0; }

#dist-display {
  position: absolute; top: 8px; left: 8px;
  background: rgba(255,255,255,0.80);
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 8px; padding: 7px 12px;
  font-size: 12px; font-family: monospace;
  backdrop-filter: blur(8px); z-index: 10;
  color: #555;
  pointer-events: none;
  min-width: 140px; text-align: center;
  letter-spacing: 0.3px;
}
#dist-display b { color: #0077aa; font-size: 13px; }

#atom-legend {
  position: absolute; bottom: 8px; left: 8px;
  background: rgba(255,255,255,0.80);
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 8px; padding: 6px 10px;
  font-size: 11px; backdrop-filter: blur(6px); z-index: 10;
}
#atom-legend .row { display: flex; align-items: center; gap: 6px; padding: 2px 0; }
#atom-legend .dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
#atom-legend .name { color: #222; }
#atom-legend .desc { color: #666; font-size: 10px; }

/* ── Right: landscape grid ── */
#grid-panel {
  flex: 1; min-width: 0; min-height: 0;
  overflow-y: auto;
  padding: 10px 10px 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg);
}

#grid-panel::-webkit-scrollbar { width: 5px; }
#grid-panel::-webkit-scrollbar-track { background: transparent; }
#grid-panel::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 3px; }

#hm-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr));
  gap: 8px;
  align-content: start;
  min-width: 0;
}

/* landscape tile */
.hm-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  cursor: crosshair;
  transition: border-color 0.15s;
  min-width: 0;
}
.hm-tile:hover { border-color: rgba(79,195,247,0.3); }
.hm-tile.active-model { border-color: var(--accent); }

.tile-header {
  padding: 5px 8px 2px;
  font-size: 11px; font-weight: 600;
  color: var(--text-dim);
  display: flex; justify-content: space-between; align-items: center;
  letter-spacing: 0.2px;
}
.tile-header .model-name { color: var(--text); }
.tile-header .emin-badge {
  font-size: 10px; font-family: monospace;
  color: #4fc3f7; background: rgba(79,195,247,0.1);
  padding: 1px 5px; border-radius: 3px;
}

.tile-canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: var(--hm-aspect, 1);
  background: #000;
}
.tile-canvas-wrap canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
}
canvas.hm-base { image-rendering: pixelated; }
canvas.hm-overlay { cursor: crosshair; }
canvas.hm-overlay:focus-visible {
  outline: 2px solid rgba(79,195,247,0.85);
  outline-offset: -2px;
}

/* mini colorbar */
.tile-footer {
  display: flex; align-items: center; gap: 5px;
  padding: 3px 8px 5px;
}
.cb-canvas { flex: 1; height: 6px; border-radius: 3px; }
.cb-lo, .cb-hi { font-size: 9px; font-family: monospace; color: var(--text-dim); white-space: nowrap; }


/* tooltip */
.hm-tooltip {
  position: fixed;
  background: rgba(14,16,24,0.94);
  border: 1px solid var(--border);
  border-radius: 5px; padding: 4px 8px;
  font-size: 11px; font-family: monospace;
  pointer-events: none; z-index: 100;
  display: none; line-height: 1.5;
  backdrop-filter: blur(8px);
  white-space: nowrap;
}

/* ── Load hint overlay ── */
#load-hint {
  position: fixed; inset: 0; z-index: 500;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
  transition: opacity 0.4s ease;
}
#load-hint-box {
  background: rgba(14,16,24,0.88);
  border: 1px solid rgba(79,195,247,0.35);
  border-radius: 12px;
  padding: 18px 32px;
  text-align: center;
  backdrop-filter: blur(8px);
  box-shadow: 0 0 0 4px rgba(79,195,247,0.06);
  font-size: 14px;
  color: #dde1f0;
}
#load-hint-box .key {
  display: inline-block;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 5px;
  padding: 1px 7px;
  font-family: monospace;
  font-size: 14px;
}
#load-hint-box p {
  margin: 6px 0 0;
  font-size: 12px;
  color: #7a7e96;
}

/* loading */
#loading {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 14px; z-index: 999;
}
#loading h2 { font-size: 17px; color: var(--accent); }
#loading p  { color: var(--text-dim); font-size: 13px; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Keep linked-page row grids within their scroll container as it narrows. */
#grid-panel .hm-grid-row {
  grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr));
  min-width: 0;
}
#grid-panel .row-section,
#grid-panel .row-grid-wrap { min-width: 0; }

/* Compact overlays before the viewer becomes narrow enough to stack. */
@media (max-width: 1100px) {
  #dist-display {
    min-width: 0;
    max-width: calc(100% - 112px);
    padding-inline: 8px;
  }
  #atom-legend {
    max-width: calc(100% - 154px);
    overflow: hidden;
  }
  #nav-panel {
    min-width: 120px;
    max-width: 138px;
  }
}

/* Portrait and narrow screens give each panel the full available width. */
@media (max-width: 900px), (orientation: portrait) and (max-width: 1100px) {
  #main { flex-direction: column; }

  #viewer-panel {
    width: 100%;
    height: var(--viewer-h);
    flex: none;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  #panel-resizer {
    width: 100%;
    height: 18px;
    flex-basis: 18px;
    border: 0;
    border-top: 1px solid rgba(255,255,255,0.08);
    border-bottom: 1px solid rgba(255,255,255,0.08);
    cursor: row-resize;
  }
  #panel-resizer::before { inset: 7px 0; }
  #panel-resizer::after {
    content: "↕";
    width: 54px;
    height: 30px;
  }
  body.panel-resizing,
  body.panel-resizing * { cursor: row-resize !important; }

  #grid-panel {
    width: 100%;
    flex: 1 1 auto;
  }
}

/* Short landscape viewports work better as a compact side-by-side split. */
@media (max-height: 560px) and (orientation: landscape) {
  #main { flex-direction: row; }

  #viewer-panel {
    width: var(--viewer-w);
    height: auto;
    flex: 0 0 auto;
    border-right: 1px solid var(--border);
    border-bottom: 0;
  }

  #panel-resizer {
    width: 18px;
    height: auto;
    flex-basis: 18px;
    border-top: 0;
    border-bottom: 0;
    border-left: 1px solid rgba(255,255,255,0.08);
    border-right: 1px solid rgba(255,255,255,0.08);
    cursor: col-resize;
  }
  #panel-resizer::before { inset: 0 7px; }
  #panel-resizer::after {
    content: "↔";
    width: 30px;
    height: 54px;
  }
  body.panel-resizing,
  body.panel-resizing * { cursor: col-resize !important; }

  #grid-panel { width: auto; }

  #viewer-btns { top: 6px; right: 6px; }
  #dist-display { top: 6px; left: 6px; }
  #atom-legend { bottom: 6px; left: 6px; padding: 4px 7px; }
  #nav-panel {
    right: 6px; bottom: 6px;
    padding: 5px 7px;
    gap: 3px;
  }
}

@media (max-width: 420px) and (orientation: portrait) {
  :root { --viewer-h: clamp(240px, 40vh, 340px); }
  #viewer-btns { top: 6px; right: 6px; }
  #dist-display {
    top: 6px; left: 6px;
    font-size: 10px;
  }
  #atom-legend {
    bottom: 6px; left: 6px;
    padding: 4px 7px;
  }
  #atom-legend .desc { display: none; }
  #nav-panel {
    right: 6px; bottom: 6px;
    min-width: 108px;
    max-width: 124px;
    padding: 5px 7px;
    gap: 3px;
  }
}

@supports (height: 1dvh) {
  @media (max-width: 420px) and (orientation: portrait) {
    :root { --viewer-h: clamp(240px, 40dvh, 340px); }
  }
}
