/* Scan Ladder X-Change — Dark gradient theme */

:root {
    --bg-dark: #1a1a2e;
    --bg-mid: #16213e;
    --bg-light: #0f3460;
    --accent: #e94560;
    --accent-hover: #ff6b81;
    --success: #2ecc71;
    --warning: #f39c12;
    --danger: #e74c3c;
    --text: #ecf0f1;
    --text-dim: #95a5a6;
    --text-muted: #7f8c8d;
    --border: #2c3e50;
    --surface: rgba(255, 255, 255, 0.05);
    --surface-hover: rgba(255, 255, 255, 0.08);
    --radius: 8px;
    --sidebar-width: 280px;
    --header-height: 56px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
    background: linear-gradient(135deg, var(--bg-dark), var(--bg-mid), var(--bg-light));
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}

/* ---- Toolbar ---- */
#app-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0;
    background: rgba(0, 0, 0, 0.35);
    border-bottom: 1px solid var(--border);
    z-index: 100;
}

.toolbar-brand {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 16px;
    border-right: 1px solid var(--border);
    height: 100%;
}

.toolbar-brand .logo {
    height: 24px;
    width: auto;
}

.brand-name {
    font-size: 0.85rem;
    font-weight: 600;
    white-space: nowrap;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 0 8px;
    border-right: 1px solid var(--border);
    height: 100%;
}

.toolbar-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    padding: 4px 10px;
    border-radius: 4px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.toolbar-btn:hover:not(:disabled) {
    background: var(--surface-hover);
    color: var(--text);
}

.toolbar-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.toolbar-btn .material-symbols-outlined {
    font-size: 20px;
}

.toolbar-label {
    font-size: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.toolbar-center {
    flex: 1;
    text-align: center;
}

.toolbar-right {
    padding: 0 16px;
}

.status-badge {
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: 500;
    background: var(--surface);
    color: var(--text-dim);
}

.scan-filename {
    font-size: 0.8rem;
    color: var(--text-dim);
    font-weight: 500;
}

.status-badge.active { background: rgba(46, 204, 113, 0.15); color: var(--success); }
.status-badge.error  { background: rgba(231, 76, 60, 0.2); color: var(--danger); }

/* ---- Main layout ---- */
#app-main {
    display: flex;
    height: calc(100vh - var(--header-height));
}

/* ---- Sidebar ---- */
#sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: rgba(0, 0, 0, 0.2);
    border-right: 1px solid var(--border);
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#sidebar h2 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-dim);
    margin-bottom: 4px;
}

#sidebar h3 {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-dim);
    margin-bottom: 8px;
}

.sidebar-section {
    border-top: 1px solid var(--border);
    padding-top: 12px;
}

/* Threshold slider */
.threshold-control {
    margin-bottom: 12px;
}

.threshold-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.threshold-header label {
    font-size: 0.75rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

#threshold-value {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

#rmse-threshold {
    width: 100%;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border);
    border-radius: 2px;
    outline: none;
}

#rmse-threshold::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

#rmse-threshold::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

/* Auto Align button */
.btn-auto-align {
    width: 100%;
    padding: 10px 16px;
    background: linear-gradient(135deg, #3498db, #2980b9);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    margin-bottom: 12px;
    transition: background 0.15s, transform 0.1s;
}

.btn-auto-align:hover:not(:disabled) {
    background: linear-gradient(135deg, #2980b9, #2471a3);
}

.btn-auto-align:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-auto-align:active { transform: scale(0.97); }

/* Reference body cards */
.ref-body-card {
    display: flex;
    align-items: center;
    flex-wrap: wrap;   /* lets .ref-body-diagnostic take a full-width row */
    gap: 10px;
    padding: 8px 10px;
    border-radius: var(--radius);
    background: var(--surface);
    cursor: pointer;
    transition: background 0.15s;
    margin-bottom: 6px;
}

.ref-body-card:hover {
    background: var(--surface-hover);
}

.ref-body-card.selected {
    outline: 2px solid var(--accent);
}

.ref-body-card.active {
    outline: 2px solid var(--warning);
    background: rgba(243, 156, 18, 0.1);
}

.ref-body-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}

.ref-body-name {
    font-size: 0.85rem;
    font-weight: 500;
    flex: 1;
}

.ref-body-status {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    padding: 2px 8px;
    border-radius: 8px;
}

.ref-body-status.pending  { background: var(--surface); color: var(--text-muted); }
.ref-body-status.aligned  { background: rgba(46, 204, 113, 0.2); color: var(--success); }
.ref-body-status.failed   { background: rgba(231, 76, 60, 0.2); color: var(--danger); }
.ref-body-status.retrying { background: rgba(243, 156, 18, 0.2); color: var(--warning); }

.ref-body-rmse {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.ref-body-rmse.rmse-pass { color: var(--success); }
.ref-body-rmse.rmse-fail { color: var(--danger); font-weight: 600; }

/* Metrics display */
#metrics-display {
    font-size: 0.8rem;
}

.metric-row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.metric-label {
    color: var(--text-dim);
}

.metric-value {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.metric-value.pass { color: var(--success); }
.metric-value.fail { color: var(--danger); }

/* ---- 3D Viewer ---- */
#viewer-container {
    flex: 1;
    position: relative;
    overflow: hidden;
}

#viewer-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* Upload overlay */
#upload-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.5);
    z-index: 10;
}

#upload-overlay.hidden {
    display: none;
}

.upload-dropzone {
    text-align: center;
    padding: 48px;
    border: 2px dashed var(--border);
    border-radius: 16px;
    background: var(--surface);
    transition: border-color 0.2s, background 0.2s;
    max-width: 400px;
}

.upload-dropzone.drag-over {
    border-color: var(--accent);
    background: rgba(233, 69, 96, 0.1);
}

.upload-icon {
    color: var(--text-dim);
    margin-bottom: 16px;
}

.upload-text {
    font-size: 1.1rem;
    font-weight: 500;
    margin-bottom: 8px;
}

.upload-subtext {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 20px;
}

/* Progress bar */
#progress-container {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 16px;
    background: rgba(0, 0, 0, 0.7);
    border-radius: 20px;
    z-index: 20;
}

#progress-container.hidden {
    display: none;
}

.progress-bar {
    width: 200px;
    height: 6px;
    background: var(--surface);
    border-radius: 3px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    width: 0%;
    transition: width 0.3s;
}

#progress-label {
    font-size: 0.75rem;
    color: var(--text-dim);
    white-space: nowrap;
}

/* Instruction overlay */
#instruction-overlay {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    padding: 8px 20px;
    background: rgba(0, 0, 0, 0.7);
    border-radius: 20px;
    z-index: 15;
    font-size: 0.85rem;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
}

#instruction-overlay.hidden {
    display: none;
}

/* ---- Buttons ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}

.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary {
    background: var(--accent);
    color: white;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-export {
    width: 100%;
    background: var(--success);
    color: white;
    padding: 12px;
}
.btn-export:hover:not(:disabled) { background: #27ae60; }

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--surface-hover); }

/* Small button variant */
.btn-sm {
    padding: 5px 12px;
    font-size: 0.75rem;
}

/* Icon button (undo/redo) */
.btn-icon {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    font-size: 1.1rem;
    line-height: 1;
    padding: 3px 8px;
    min-width: 28px;
}
.btn-icon:hover:not(:disabled) { background: var(--surface-hover); }
.btn-icon:disabled { opacity: 0.3; }

/* Cancel button (instruction overlay) */
.btn-cancel {
    background: none;
    border: 1px solid rgba(255,255,255,0.3);
    color: var(--text-dim);
    padding: 3px 10px;
    border-radius: 10px;
    font-size: 0.7rem;
    cursor: pointer;
    margin-left: 12px;
    transition: background 0.15s, color 0.15s;
}
.btn-cancel:hover {
    background: rgba(255,255,255,0.1);
    color: var(--text);
}
.btn-toggle {
    background: rgba(100,180,255,0.15);
    border: 1px solid rgba(100,180,255,0.4);
    color: var(--text);
    padding: 3px 10px;
    border-radius: 10px;
    font-size: 0.7rem;
    cursor: pointer;
    margin-left: 8px;
    transition: background 0.15s;
}
.btn-toggle:hover {
    background: rgba(100,180,255,0.3);
}

/* Controls hint */
#controls-hint {
    position: absolute;
    bottom: 8px;
    right: 12px;
    font-size: 0.65rem;
    color: var(--text-muted);
    opacity: 0.5;
    pointer-events: none;
    z-index: 5;
}

/* ---- Right Panel ---- */
#right-panel {
    width: 240px;
    min-width: 240px;
    background: rgba(0, 0, 0, 0.2);
    border-left: 1px solid var(--border);
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.panel-header {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 12px;
}

#panel-body-info .panel-body-name {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 2px;
}

#panel-body-info .panel-body-status {
    font-size: 0.75rem;
    margin-bottom: 12px;
}

.panel-metrics-box {
    background: rgba(255, 255, 255, 0.04);
    border-radius: var(--radius);
    padding: 10px;
    margin-bottom: 12px;
}

.panel-metrics-box .metric-row {
    display: flex;
    justify-content: space-between;
    padding: 3px 0;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

.panel-metrics-box .metric-row:last-child {
    border-bottom: none;
}

.panel-coverage-bar {
    height: 3px;
    background: var(--border);
    border-radius: 2px;
    margin-top: 6px;
}

.panel-coverage-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.3s;
}

#panel-adjust .panel-section-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

#panel-adjust .adjust-mode-btns {
    margin-bottom: 10px;
}

#panel-adjust .adjust-hint {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 12px;
}

#panel-adjust .panel-adjust-actions {
    margin-top: auto;
}

#panel-adjust .btn-retry-adjust {
    width: 100%;
    padding: 8px;
    background: #3498db;
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
    margin-bottom: 6px;
}

#panel-adjust .btn-retry-adjust:hover {
    background: #2980b9;
}

#panel-adjust .btn-cancel-adjust {
    width: 100%;
    padding: 6px;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.75rem;
}

#panel-adjust .btn-cancel-adjust:hover {
    background: var(--surface);
    color: var(--text-dim);
}

.panel-empty-hint {
    color: var(--text-muted);
    font-size: 0.8rem;
    text-align: center;
    margin-top: 24px;
}

/* ---- Utility ---- */
.hidden { display: none !important; }

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Manual Adjustment */
.adjust-panel {
    padding: 12px;
    margin-top: 8px;
    background: rgba(255, 193, 7, 0.1);
    border: 1px solid rgba(255, 193, 7, 0.3);
    border-radius: var(--radius);
}

.adjust-header {
    font-weight: bold;
    color: #ffc107;
    margin-bottom: 6px;
    font-size: 13px;
}

.adjust-hint {
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 8px;
}

#adjust-metrics {
    font-size: 12px;
    color: var(--text-dim);
    margin-bottom: 6px;
    min-height: 18px;
}

.btn-adjust {
    background: rgba(255, 193, 7, 0.15);
    color: #ffc107;
    border: 1px solid rgba(255, 193, 7, 0.3);
    padding: 3px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    margin-top: 4px;
}

.btn-adjust:hover {
    background: rgba(255, 193, 7, 0.3);
}

.btn-retry-adjust {
    width: 100%;
    padding: 8px 16px;
    background: var(--bg-light);
    color: white;
    border: 1px solid #1a5276;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
    margin-top: 8px;
}

.btn-retry-adjust:hover {
    background: #1a5276;
}

.btn-cancel-adjust {
    width: 100%;
    padding: 6px 16px;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 12px;
    margin-top: 4px;
}

.btn-cancel-adjust:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-dim);
}

.adjust-mode-btns {
    display: flex;
    gap: 4px;
    margin-bottom: 8px;
}

.btn-mode {
    flex: 1;
    padding: 5px 8px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

.btn-mode.active {
    background: rgba(255, 193, 7, 0.2);
    color: #ffc107;
    border-color: rgba(255, 193, 7, 0.4);
}

.btn-mode:hover {
    background: rgba(255, 193, 7, 0.15);
}

/* Issue #5 — per-body failure diagnostics in the reference body list.
   Colour-coded by category so the four failure modes are distinguishable
   at a glance without reading the full message. */
.ref-body-diagnostic {
    flex: 0 0 100%;
    width: 100%;
    font-size: 11px;
    line-height: 1.35;
    margin: 4px 0 6px;
    padding: 4px 6px;
    border-radius: 4px;
    border-left: 3px solid #e74c3c;
    background: rgba(231, 76, 60, 0.10);
    color: #ffb4ab;
}

.ref-body-diagnostic[data-category="axial_sliding"] {
    border-left-color: #f39c12;
    background: rgba(243, 156, 18, 0.10);
    color: #ffd79a;
}

.ref-body-diagnostic[data-category="low_coverage"] {
    border-left-color: #3498db;
    background: rgba(52, 152, 219, 0.10);
    color: #a9d6f5;
}

.ref-body-diagnostic[data-category="high_residual"] {
    border-left-color: #e67e22;
    background: rgba(230, 126, 34, 0.10);
    color: #f5c396;
}

/* ── Toolbar dropdown menu (Export: combined vs bodies-only) ── */
.toolbar-menu-wrap {
    position: relative;
    display: inline-flex;
}

.toolbar-menu {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    z-index: 200;
    min-width: 210px;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 6px;
    /* Opaque, not var(--surface): this floats over the 3D viewport. */
    background: var(--bg-mid);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

.toolbar-menu[hidden] {
    display: none;
}

.toolbar-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 8px 10px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text);
    font-size: 0.8rem;
    text-align: left;
    white-space: nowrap;
    cursor: pointer;
    transition: background 0.15s;
}

.toolbar-menu-item:hover {
    background: var(--surface-hover);
}

.toolbar-menu-item .material-symbols-outlined {
    font-size: 18px;
    color: var(--text-dim);
}

/* ── Arch toggle ──
   Mandible/Maxilla is a camera convention only. Styled as a segmented control
   rather than a checkbox so it reads as "which of two views", not "apply a
   transform to my scan". */
.arch-toggle {
    display: inline-flex;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 6px;
    overflow: hidden;
}

.arch-btn {
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.55);
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.02em;
    padding: 6px 12px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.arch-btn:hover {
    background: rgba(255, 255, 255, 0.06);
    color: rgba(255, 255, 255, 0.85);
}

.arch-btn.is-active {
    background: rgba(52, 152, 219, 0.22);
    color: #eaf4fc;
}

/* ── Switch mode ──
   Positions list, shown in place of the reference-body list when Switch is
   the active mode. */
.switch-position {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 10px;
}

.switch-position-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.switch-position-name {
    font-size: 13px;
    font-weight: 600;
    color: #eaf4fc;
    flex: 1;
}

.switch-position-metric {
    font-size: 11px;
    color: #2ecc71;
    font-variant-numeric: tabular-nums;
}

.switch-position-remove {
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.4);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    padding: 0 2px;
}

.switch-position-remove:hover { color: #e74c3c; }

.switch-part-select {
    width: 100%;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 5px;
    color: #dfe8f2;
    font-size: 12px;
    padding: 6px 8px;
}

#sidebar-switch .hint {
    font-size: 12px;
    line-height: 1.45;
    color: rgba(255, 255, 255, 0.55);
    margin: 8px 0 14px;
}

#sidebar-switch .reference-note {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.45);
    margin: 4px 0 0;
}
