:root {
  --bg-root: #0a0e12;
  --bg-surface: #121820;
  --bg-elevated: #1a222d;
  --bg-hover: #222d3c;
  --border: #263140;
  --border-strong: #37475d;
  --text-primary: #eaf0f6;
  --text-secondary: #9aa7b8;
  --text-muted: #6c798a;
  --accent: #00ae42;
  --accent-hover: #00c94d;
  --accent-soft: rgba(0, 174, 66, 0.14);
  --accent-blue: #2f81f7;
  --accent-amber: #f59e0b;
  --accent-red: #f85149;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-lg: 0 18px 48px rgba(0, 0, 0, 0.48);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-root);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.mono { font-family: var(--font-mono); font-size: 12px; }

/* ---------- Buttons & Inputs ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

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

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #041309;
  font-weight: 700;
}

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

.btn-blue {
  background: var(--accent-blue);
  border-color: var(--accent-blue);
  color: #ffffff;
  font-weight: 600;
}

.btn-danger {
  background: rgba(248, 81, 73, 0.12);
  border-color: rgba(248, 81, 73, 0.35);
  color: #ff7b72;
}

.btn-sm { padding: 5px 9px; font-size: 12px; }
.btn-lg { padding: 10px 16px; font-size: 14px; font-weight: 700; }

.input, .select, .textarea {
  width: 100%;
  background: var(--bg-root);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 7px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
}

.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
}

.label {
  display: block;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.form-group { margin-bottom: 12px; }

.status-box {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  border: 1px solid rgba(0, 174, 66, 0.35);
  color: #86efac;
  font-size: 12.5px;
}

.error-box {
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: rgba(248, 81, 73, 0.12);
  border: 1px solid rgba(248, 81, 73, 0.4);
  color: #ff948e;
  font-size: 12.5px;
}

.warn-box {
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.4);
  color: #fcd34d;
  font-size: 12px;
}

/* ---------- Login Page ---------- */
.login-page {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    radial-gradient(circle at 20% 20%, rgba(0, 174, 66, 0.14), transparent 45%),
    radial-gradient(circle at 80% 80%, rgba(47, 129, 247, 0.12), transparent 45%),
    var(--bg-root);
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  box-shadow: var(--shadow-lg);
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.brand-logo {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #00ae42, #047857);
  border: 1px solid rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  color: #03150a;
  flex-shrink: 0;
}

.brand-title { margin: 0; font-size: 18px; font-weight: 700; }
.brand-sub { margin: 2px 0 0; font-size: 12px; color: var(--text-secondary); }

.auth-tabs, .tab-group {
  display: inline-flex;
  background: var(--bg-root);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-bottom: 16px;
}

.tab-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 6px 11px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
}

.tab-btn:hover:not(:disabled) {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.tab-btn.active {
  background: var(--accent);
  color: #041309;
}

/* ---------- Studio App Layout ---------- */
.studio-app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}

.topbar {
  height: 52px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.studio-body {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
  overflow: hidden;
}

.side-panel {
  width: 300px;
  background: var(--bg-surface);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-height: 0;
  overflow: hidden;
}

.side-panel.left { border-right: 1px solid var(--border); }
.side-panel.right { width: 325px; border-left: 1px solid var(--border); }

.panel-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.panel-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.panel-footer {
  padding: 11px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- Dropzone & Model Cards ---------- */
.dropzone {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-md);
  padding: 14px 12px;
  text-align: center;
  background: rgba(255, 255, 255, 0.015);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
  margin-bottom: 12px;
}

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

.model-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.model-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  background: var(--bg-root);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 9px;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.model-item:hover { border-color: var(--border-strong); }
.model-item.active {
  border-color: var(--accent);
  background: rgba(0, 174, 66, 0.09);
}

.model-thumb {
  width: 42px;
  height: 42px;
  border-radius: 6px;
  object-fit: cover;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
}

.model-meta { min-width: 0; flex: 1; }
.model-name {
  font-weight: 600;
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-sub {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 700;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-secondary);
}

.badge-green {
  background: var(--accent-soft);
  border-color: rgba(0, 174, 66, 0.4);
  color: #6ee7b7;
}

.badge-blue {
  background: rgba(47, 129, 247, 0.14);
  border-color: rgba(47, 129, 247, 0.4);
  color: #93c5fd;
}

/* ---------- 3D Build Plate Viewport ---------- */
.viewport-stage {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  background: radial-gradient(circle at 50% 42%, #19222d 0%, #090d11 78%);
}

.viewport-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
  touch-action: none;
}

.plate-info-bar {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  z-index: 5;
}

.plate-pill {
  pointer-events: auto;
  background: rgba(18, 24, 32, 0.88);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 6px 11px;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  max-width: 100%;
  overflow: hidden;
}

.viewer-hud {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(18, 24, 32, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 6px 9px;
  display: flex;
  align-items: center;
  gap: 5px;
  max-width: calc(100% - 24px);
  overflow-x: auto;
  scrollbar-width: none;
  z-index: 6;
  box-shadow: var(--shadow-lg);
}
.viewer-hud::-webkit-scrollbar { display: none; }

.floating-nav-pad {
  position: absolute;
  right: 12px;
  bottom: 68px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 6;
}

.nav-pad-btn {
  min-width: 40px;
  height: 38px;
  padding: 0 9px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: rgba(18, 24, 32, 0.9);
  backdrop-filter: blur(10px);
  color: var(--text-primary);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.nav-pad-btn.nav-pad-text { font-size: 11px; text-transform: uppercase; }
.nav-pad-btn.active { background: var(--accent); border-color: var(--accent); color: #041309; }

/* ---------- Layer Preview Scrubber ---------- */
.layer-scrubber {
  position: absolute;
  left: 12px;
  bottom: 68px;
  background: rgba(18, 24, 32, 0.92);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  width: 240px;
  max-width: calc(100% - 82px);
  z-index: 7;
  box-shadow: var(--shadow-lg);
}

.legend-row {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 7px;
  font-size: 10.5px;
  color: var(--text-secondary);
}

.legend-dot {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.legend-dot i {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.viewport-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 14, 18, 0.78);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  z-index: 10;
}

/* ---------- Settings Sections & Stats ---------- */
.sec-box {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 11px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, 0.015);
}

.sec-title {
  margin: 0 0 9px;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.slice-summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
  margin-bottom: 10px;
}

.stat-tile {
  background: var(--bg-root);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 9px;
}
.stat-tile .k { font-size: 10.5px; color: var(--text-muted); text-transform: uppercase; }
.stat-tile .v { font-size: 15px; font-weight: 700; font-family: var(--font-mono); color: var(--text-primary); }

/* ---------- Database Browser Grid ---------- */
.db-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 12px;
}

.db-card {
  background: var(--bg-root);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.db-card img {
  width: 100%;
  height: 145px;
  object-fit: cover;
  background: var(--bg-elevated);
}

.db-card-body {
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex: 1;
}

.db-card-title {
  font-weight: 600;
  font-size: 13px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- Modals, Spinner, Toast ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 12, 0.76);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 100;
}

.modal {
  width: 100%;
  max-width: 880px;
  max-height: 88dvh;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-sm { max-width: 560px; }

.modal-head {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-head h3 { margin: 0; font-size: 15.5px; }

.modal-body {
  padding: 15px 16px;
  overflow-y: auto;
  flex: 1;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  display: inline-block;
  flex-shrink: 0;
}
.spinner-lg { width: 32px; height: 32px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast-container {
  position: fixed;
  bottom: 18px;
  right: 18px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-width: 340px;
}

.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  color: var(--text-primary);
  padding: 9px 13px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  box-shadow: var(--shadow-lg);
}
.toast.error { border-left-color: var(--accent-red); }
.toast.info { border-left-color: var(--accent-blue); }

.mobile-only { display: none; }

@media (max-width: 980px) {
  .mobile-only { display: inline-flex; }
  .hide-mobile { display: none !important; }
  .topbar { height: auto; flex-wrap: wrap; padding: 6px 8px; gap: 6px; }
  .topbar-left, .topbar-right { overflow-x: auto; scrollbar-width: none; }
  .side-panel.left, .side-panel.right {
    position: absolute;
    top: 0;
    bottom: 0;
    width: min(330px, 88vw);
    z-index: 25;
    box-shadow: var(--shadow-lg);
    transition: transform 0.2s ease;
  }
  .side-panel.left { left: 0; transform: translateX(-105%); }
  .side-panel.right { right: 0; transform: translateX(105%); }
  .side-panel.open { transform: translateX(0); }
  .nav-pad-btn { min-width: 44px; height: 42px; }
  .floating-nav-pad { right: 10px; bottom: calc(64px + env(safe-area-inset-bottom, 0px)); }
  .viewer-hud { bottom: max(10px, env(safe-area-inset-bottom, 0px)); }
  .layer-scrubber { bottom: calc(64px + env(safe-area-inset-bottom, 0px)); width: 210px; }
}
