:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-soft: #f8fafc;
  --border: #d9e0e6;
  --border-strong: #c3ccd4;
  --text: #1f2a33;
  --muted: #64748b;
  --accent: #1a5276;
  --accent-2: #0f766e;
  --danger: #c0392b;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.045);
  --radius: 12px;
  --viewer-bg: #e9edf1;
  --focus-ring: rgba(26, 82, 118, 0.18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --panel: #1a222b;
    --panel-soft: #141b22;
    --border: #2d3945;
    --border-strong: #3a4753;
    --text: #e6edf3;
    --muted: #9fb0bf;
    --accent: #5fa8d3;
    --accent-2: #2dd4bf;
    --danger: #ff6b6b;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.24);
    --viewer-bg: #232b33;
    --focus-ring: rgba(95, 168, 211, 0.22);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

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

button {
  font: inherit;
  cursor: pointer;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.app-header h1 {
  font-size: 1.1rem;
  margin: 0;
  white-space: nowrap;
}

.dropzone {
  flex: 1;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  padding: 8px 14px;
  color: var(--muted);
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.dropzone.dragover,
.dropzone:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.file-meta {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

.empty-state {
  margin: 60px auto;
  max-width: 480px;
  text-align: center;
  color: var(--muted);
  font-size: 0.95rem;
}

/* Main layout: big 3D viewer on the left, a scrollable control
   sidebar (isosurface / molecule / per-element panels) on the right.
   Sidebar gets a fixed comfortable width rather than a fraction, since
   its content (sliders, color pickers) has a natural fixed width and
   gains nothing from stretching on wide screens. */
.app-main {
  display: none;
  flex: 1;
  gap: 14px;
  padding: 14px 20px 20px;
  min-height: 0;
  grid-template-columns: 1fr minmax(300px, 340px);
  grid-template-rows: 1fr;
  grid-template-areas: "viewer sidebar";
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.viewer-panel { grid-area: viewer; }
.sidebar-panel { grid-area: sidebar; overflow-y: auto; }

.panel-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.panel-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.btn-small {
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 0.8rem;
}

.btn-small:hover {
  border-color: var(--accent);
}

.btn-small:disabled {
  opacity: 0.45;
  cursor: default;
  border-color: var(--border);
}

.btn-small.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.switch {
  position: relative;
  display: inline-block;
  width: 34px;
  height: 19px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  background: var(--border-strong);
  transition: background 0.15s;
  border-radius: 20px;
  cursor: pointer;
}

.switch-slider::before {
  content: "";
  position: absolute;
  height: 13px;
  width: 13px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  transition: transform 0.15s;
  border-radius: 50%;
}

.switch input:checked + .switch-slider {
  background: var(--accent);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(15px);
}

.switch input:disabled + .switch-slider {
  opacity: 0.5;
  cursor: default;
}

#viewer-controls-row input[type="range"] {
  accent-color: var(--accent);
  width: 120px;
}

#viewer-controls-row input[type="range"]:disabled {
  opacity: 0.45;
}

.sidebar-panel input[type="range"] {
  accent-color: var(--accent);
  flex: 1;
  min-width: 60px;
}

.toolbar-divider {
  width: 1px;
  align-self: stretch;
  background: var(--border);
  margin: 2px 2px;
}

#vib-amplitude-label {
  min-width: 2.4em;
}

.chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px 2px 10px;
  font-size: 0.78rem;
  color: var(--accent);
}

.chip-index {
  opacity: 0.7;
  font-size: 0.85em;
}

.chip-remove {
  cursor: pointer;
  font-size: 0.95em;
  line-height: 1;
  padding: 0 2px;
}

.chip-remove:hover {
  color: var(--danger);
}

/* Small inline label used next to the amplitude/threshold sliders */
.filter-label {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}

.viewer-3d {
  flex: 1;
  min-height: 220px;
  position: relative;
}

/* Element-color key, overlaid in the corner of the 3D view. Built
   dynamically in viewer.js from the elements present in the loaded
   structure. pointer-events: none so it never intercepts clicks meant
   for the 3Dmol canvas underneath. */
.viewer-legend {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 6px 10px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  pointer-events: none;
  max-height: calc(100% - 20px);
  overflow-y: auto;
}

.viewer-legend:empty {
  display: none;
}

.viewer-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.76rem;
  color: var(--text);
  white-space: nowrap;
}

.viewer-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}

/* Small rotating X/Y/Z axis indicator, overlaid opposite the legend so
   the two never collide. pointer-events: none so it never intercepts
   clicks meant for the 3Dmol canvas underneath. */
.axes-gizmo {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 5;
  pointer-events: none;
  display: none;
}

.axes-gizmo.visible {
  display: block;
}


/* ---- Control sidebar (isosurface / molecule / elements) ---- */

.control-section {
  border-bottom: 1px solid var(--border);
  padding: 12px 14px;
}

.control-section:last-child {
  border-bottom: none;
}

.control-section-title {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 0 10px;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.control-row:last-child {
  margin-bottom: 0;
}

.control-row .filter-label {
  min-width: 78px;
  flex-shrink: 0;
}

.control-row .value-label {
  min-width: 3.6em;
  text-align: right;
  font-size: 0.78rem;
  color: var(--muted);
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
}

/* Native color input, reset to a small round swatch so it reads as a
   "pick a color" control rather than a leftover browser default. */
input[type="color"] {
  -webkit-appearance: none;
  appearance: none;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  background: none;
  cursor: pointer;
  flex-shrink: 0;
}

input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; border-radius: 50%; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 50%; }
input[type="color"]::-moz-color-swatch { border: none; border-radius: 50%; }

.iso-lobe-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.iso-lobe-row .filter-label {
  min-width: 30px;
  flex-shrink: 0;
  font-weight: 600;
}

/* Per-element override rows, built dynamically once a molecule is
   loaded (one row per distinct element present in the structure). */
.element-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.element-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.element-row .element-symbol {
  min-width: 26px;
  font-weight: 600;
  font-size: 0.85rem;
}

.element-visibility {
  flex-shrink: 0;
}

.element-row input[type="range"] {
  flex: 1;
  min-width: 0;
}

.element-row .value-label {
  min-width: 3.2em;
  text-align: right;
  font-size: 0.76rem;
  color: var(--muted);
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
}

.hint-text {
  font-size: 0.76rem;
  color: var(--muted);
  line-height: 1.4;
  margin: 6px 0 0;
}

.no-file-hint {
  font-size: 0.8rem;
  color: var(--muted);
  padding: 4px 0;
}

/* ---- Isosurface rebuild indicator ----
   addVolumetricData() runs marching cubes synchronously and blocks
   the main thread — there's no real "progress" to show, only that
   something is happening. A small centered spinner + label over the
   viewer communicates that without pretending to track real progress. */
.iso-spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 16px 22px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 0.8rem;
  color: var(--muted);
  pointer-events: none;
  z-index: 20;
}

/* The [hidden] attribute and the class above both set `display`, at
   equal CSS specificity (0,1,0 each) — with this stylesheet loaded
   after the browser's default one, `.iso-spinner { display: flex }`
   would otherwise always win over `[hidden] { display: none }`, so
   toggling the hidden property from JS would silently do nothing.
   This rule breaks the tie explicitly. */
.iso-spinner[hidden] {
  display: none;
}

@media (prefers-color-scheme: dark) {
  .iso-spinner {
    background: rgba(30, 34, 39, 0.9);
  }
}

.iso-spinner-ring {
  width: 26px;
  height: 26px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: iso-spin 0.7s linear infinite;
}

@keyframes iso-spin {
  to { transform: rotate(360deg); }
}
