/* ═══════════════════════════════════════════
   3D Print Studio — Theme System
   Dark (default) + Light mode via CSS custom properties
   ═══════════════════════════════════════════ */

/* ─── Dark Theme (default) ─── */
:root, [data-theme="dark"] {
  /* Background hierarchy */
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-input: #0a1628;
  --bg-hover: #1a4a8a;

  /* Text hierarchy */
  --text-primary: #eee;
  --text-secondary: #8899aa;
  --text-muted: #667;

  /* Accent colors */
  --accent-primary: #e94560;
  --accent-primary-hover: #ff5a75;
  --accent-secondary: #4ecdc4;
  --accent-danger: #ff4444;
  --accent-warning: #f0a040;

  /* Borders & separators */
  --border-color: #0f3460;
  --border-light: #1a3a5c;

  /* Special states */
  --active-bg: #e94560;
  --active-text-bright: #ffccd5;
  --saved-active-bg: #1a4a6a;
  --badge-bg: #0a1628;

  /* Toast */
  --toast-ok-bg: #1a4a3a;
  --toast-ok-text: #4ecdc4;
  --toast-err-bg: #4a1a1a;
  --toast-err-text: #ff6b6b;

  /* Tooltip */
  --tooltip-bg: rgba(0,0,0,0.85);
  --tooltip-text: #fff;
  --tooltip-border: rgba(255,255,255,0.2);

  /* Hint bar */
  --hint-bg: rgba(0,0,0,0.65);
  --hint-text: #aaa;

  /* Toolbar */
  --toolbar-text: #ccc;

  /* Canvas hint edit mode */
  --hint-edit-color: #4ecdc4;

  /* Scrollbar */
  --scrollbar-thumb: #1a4a8a;
  --scrollbar-track: #0a1628;
}

/* ─── Light Theme ─── */
[data-theme="light"] {
  --bg-primary: #e8ecf1;
  --bg-secondary: #ffffff;
  --bg-tertiary: #dde3ea;
  --bg-input: #f4f5f7;
  --bg-hover: #d0d7e0;

  --text-primary: #1a1a2e;
  --text-secondary: #5a6470;
  --text-muted: #8b95a1;

  --accent-primary: #c92a4a;
  --accent-primary-hover: #e04060;
  --accent-secondary: #0d8a7a;
  --accent-danger: #d63031;
  --accent-warning: #d68910;

  --border-color: #d0d5dc;
  --border-light: #e0e4e9;

  --active-bg: #c92a4a;
  --active-text-bright: #fff0f3;
  --saved-active-bg: #d4edf5;
  --badge-bg: #e4e8ed;

  --toast-ok-bg: #d4edda;
  --toast-ok-text: #0d6b4f;
  --toast-err-bg: #fce4e4;
  --toast-err-text: #c0392b;

  --tooltip-bg: rgba(255,255,255,0.95);
  --tooltip-text: #1a1a2e;
  --tooltip-border: rgba(0,0,0,0.15);

  --hint-bg: rgba(255,255,255,0.85);
  --hint-text: #5a6470;

  --toolbar-text: #3a4048;

  --hint-edit-color: #0d8a7a;

  --scrollbar-thumb: #c0c7cf;
  --scrollbar-track: #f0f2f5;
}

/* ═══════════════════════════════════════════
   Base & Layout
   ═══════════════════════════════════════════ */

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

body {
  display: flex; height: 100vh; overflow: hidden;
  background: var(--bg-primary); color: var(--text-primary);
  font-family: system-ui, sans-serif;
}

/* Custom scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }

/* ═══════════════════════════════════════════
   Sidebar
   ═══════════════════════════════════════════ */

#sidebar {
  width: 320px; min-width: 320px; background: var(--bg-secondary);
  padding: 12px; display: flex; flex-direction: column; gap: 8px;
  overflow-y: auto; z-index: 10;
}

/* Sidebar header with title + theme toggle */
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding-bottom: 8px; border-bottom: 1px solid var(--border-color);
}

#sidebar h2 {
  font-size: 16px; color: var(--accent-primary); margin: 0;
}

#theme-toggle {
  background: var(--bg-tertiary); border: 1px solid var(--border-color);
  color: var(--text-primary); cursor: pointer; font-size: 16px;
  width: 32px; height: 32px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s, transform 0.2s;
  line-height: 1;
}
#theme-toggle:hover { background: var(--bg-hover); transform: scale(1.08); }
#theme-toggle:active { transform: scale(0.95); }

/* ═══════════════════════════════════════════
   Tabs
   ═══════════════════════════════════════════ */

.tabs { display: flex; gap: 4px; }
.tabs .tab-btn {
  flex: 1; padding: 6px 0; background: var(--bg-input); color: var(--text-secondary);
  border: none; cursor: pointer; font-size: 13px; border-radius: 6px 6px 0 0;
  transition: background 0.15s, color 0.15s;
}
.tabs .tab-btn:hover { color: var(--text-primary); }
.tabs .tab-btn.active { background: var(--bg-tertiary); color: var(--accent-secondary); }

.tab-content { display: none; overflow-y: auto; max-height: 45vh; }
.tab-content.active { display: flex; flex-direction: column; }

/* ═══════════════════════════════════════════
   Parts Library — Category & Items
   ═══════════════════════════════════════════ */

#file-list { flex: 1; overflow-y: auto; }

.cat-group { margin-bottom: 8px; }

.cat-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--accent-secondary); padding: 4px 8px; margin: 4px 0;
  border-bottom: 1px solid var(--border-light);
}

#file-list .part-item {
  padding: 8px 10px; margin: 2px 0; border-radius: 6px; cursor: pointer;
  background: var(--bg-tertiary); transition: background 0.15s; font-size: 13px;
  display: flex; align-items: center; gap: 6px; color: var(--text-primary);
}
#file-list .part-item:hover { background: var(--bg-hover); }
#file-list .part-item.active {
  background: var(--active-bg); font-weight: bold; color: #fff;
}
#file-list .part-item .info { flex: 1; min-width: 0; }
#file-list .part-item .info small {
  display: block; color: var(--text-secondary); font-size: 11px;
}
#file-list .part-item.active .info small { color: var(--active-text-bright); }

/* Item action buttons */
#file-list .part-item .add-btn,
#file-list .part-item .del-btn,
#file-list .part-item .rename-btn,
#file-list .part-item .regen-btn {
  background: none; border: none; cursor: pointer;
  font-size: 14px; padding: 0 2px; line-height: 1; opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
#file-list .part-item .add-btn     { color: var(--accent-secondary); }
#file-list .part-item .del-btn     { color: var(--text-muted); font-size: 14px; }
#file-list .part-item .rename-btn  { color: var(--text-muted); font-size: 12px; }
#file-list .part-item .regen-btn   { color: var(--text-muted); font-size: 12px; }

#file-list .part-item:hover .add-btn    { opacity: 1; }
#file-list .part-item:hover .del-btn    { opacity: 1; }
#file-list .part-item:hover .rename-btn { opacity: 1; }
#file-list .part-item:hover .regen-btn  { opacity: 1; }

#file-list .part-item .del-btn:hover    { color: var(--accent-danger); }
#file-list .part-item .rename-btn:hover { color: var(--accent-warning); }
#file-list .part-item .regen-btn:hover  { color: var(--accent-primary); }

#file-list .part-item .rename-input {
  flex: 1; min-width: 0; background: var(--bg-input); color: var(--text-primary);
  border: 1px solid var(--accent-secondary); border-radius: 4px;
  padding: 2px 6px; font-size: 12px;
}

#file-list .empty { color: var(--text-muted); text-align: center; padding: 20px 10px; font-size: 14px; }

/* Prototype badge */
.prototype-badge {
  font-size: 9px; color: var(--accent-secondary); background: var(--badge-bg);
  padding: 0 4px; border-radius: 2px;
}

/* ═══════════════════════════════════════════
   Regen Form (inline, dynamic)
   ═══════════════════════════════════════════ */

.regen-form {
  padding: 6px 8px; margin: 2px 0; background: var(--bg-input);
  border-radius: 4px; font-size: 11px;
}
.regen-form .regen-row {
  display: flex; align-items: center; gap: 4px; margin: 2px 0;
}
.regen-form .regen-label {
  color: var(--text-secondary); min-width: 60px;
}
.regen-form .regen-param {
  flex: 1; width: 40px; background: var(--bg-secondary); color: var(--text-primary);
  border: 1px solid var(--border-color); border-radius: 3px;
  padding: 1px 4px; font-size: 11px;
}
.regen-form .regen-param:focus { outline: none; border-color: var(--accent-secondary); }
.regen-form .regen-unit {
  color: var(--text-muted); font-size: 10px;
}
.regen-form .regen-actions {
  display: flex; gap: 4px; margin-top: 4px;
}
.regen-form .regen-apply {
  flex: 1; padding: 3px; background: var(--accent-primary); color: #fff;
  border: none; border-radius: 3px; cursor: pointer; font-size: 11px;
}
.regen-form .regen-apply:hover { background: var(--accent-primary-hover); }
.regen-form .regen-cancel-btn {
  padding: 3px 8px; background: var(--bg-tertiary); color: var(--text-secondary);
  border: none; border-radius: 3px; cursor: pointer; font-size: 11px;
}
.regen-form .regen-cancel-btn:hover { color: var(--text-primary); }

/* ═══════════════════════════════════════════
   Metadata Panel
   ═══════════════════════════════════════════ */

#meta-panel {
  padding: 6px 0; border-top: 1px solid var(--border-color); font-size: 12px;
}
#meta-panel h3 { font-size: 13px; color: var(--accent-secondary); margin: 0 0 6px 0; }
#meta-panel table { width: 100%; border-collapse: collapse; }
#meta-panel td { padding: 3px 6px; }
#meta-panel td.label { color: var(--text-secondary); white-space: nowrap; }
#meta-panel td.value { color: var(--text-primary); text-align: right; font-family: monospace; }
#meta-panel td.note { color: var(--text-muted); font-size: 11px; }
#meta-panel .empty { color: var(--text-muted); text-align: center; padding: 8px; }

/* Inline param editor input */
.param-edit {
  width: 50px; text-align: right; background: var(--bg-tertiary);
  color: var(--text-primary); border: 1px solid var(--bg-hover);
  border-radius: 4px; font-family: monospace; font-size: 12px;
  padding: 1px 4px;
}
.param-edit:focus { outline: none; border-color: var(--accent-secondary); }

.meta-regen-btn {
  width: 100%; margin-top: 6px; padding: 6px; background: var(--accent-primary);
  color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 12px;
}
.meta-regen-btn:hover { background: var(--accent-primary-hover); }
.meta-variant-note { font-size: 10px; color: var(--text-muted); text-align: center; padding: 4px; }

/* ═══════════════════════════════════════════
   Workbench — Placed Parts & Saved Assemblies
   ═══════════════════════════════════════════ */

.section-title {
  font-size: 11px; color: var(--text-muted); text-transform: uppercase;
  letter-spacing: 1px; padding: 4px 0; margin-top: 2px;
}

#placed-list { flex: 1; overflow-y: auto; min-height: 0; }

#placed-list .placed-item {
  padding: 6px 8px; margin: 2px 0; border-radius: 4px; cursor: pointer;
  background: var(--bg-input); font-size: 12px; display: flex;
  align-items: center; gap: 6px; color: var(--text-primary);
}
#placed-list .placed-item:hover { background: var(--bg-tertiary); }
#placed-list .placed-item.selected { background: var(--active-bg); color: #fff; }
#placed-list .placed-item .idx { color: var(--text-muted); font-size: 11px; min-width: 18px; }
#placed-list .placed-item.selected .idx { color: var(--active-text-bright); }
#placed-list .placed-item .pname {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#placed-list .placed-item .rm-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 12px; padding: 0 2px;
}
#placed-list .placed-item:hover .rm-btn { color: var(--accent-danger); }

#placed-actions {
  display: flex; gap: 6px; padding: 8px 0; border-top: 1px solid var(--border-color);
}
#placed-actions button {
  flex: 1; padding: 5px 0; background: var(--bg-tertiary); color: var(--toolbar-text);
  border: none; border-radius: 4px; cursor: pointer; font-size: 11px;
}
#placed-actions button:hover { background: var(--bg-hover); color: var(--text-primary); }
#placed-actions button.primary { background: var(--accent-primary); color: #fff; }
#placed-actions button.primary:hover { background: var(--accent-primary-hover); }

/* Saved assembly items */
.saved-item {
  padding: 6px 8px; margin: 2px 0; border-radius: 4px; cursor: pointer;
  background: var(--bg-input); font-size: 12px; display: flex;
  align-items: center; gap: 6px; transition: background 0.15s;
  color: var(--text-primary);
}
.saved-item:hover { background: var(--bg-tertiary); }
.saved-item.active {
  background: var(--saved-active-bg); border-left: 2px solid var(--accent-secondary);
}
.saved-item .sname {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.saved-item .smeta { font-size: 10px; color: var(--text-muted); }
.saved-item .sdel {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 12px; padding: 0 2px; opacity: 0; transition: opacity 0.15s, color 0.15s;
}
.saved-item:hover .sdel { opacity: 1; }
.saved-item .sdel:hover { color: var(--accent-danger); }

/* Save inline form */
#save-form { display: none; gap: 4px; padding: 6px 0; }
#save-form input {
  flex: 1; padding: 4px 8px; background: var(--bg-input); color: var(--text-primary);
  border: 1px solid var(--border-color); border-radius: 4px; font-size: 12px;
}
#save-form input:focus { outline: none; border-color: var(--accent-secondary); }
#save-form button {
  padding: 4px 10px; border-radius: 4px; border: none; cursor: pointer; font-size: 11px;
}
#save-form .confirm { background: var(--accent-secondary); color: #0a1628; }
#save-form .cancel { background: var(--bg-tertiary); color: var(--text-secondary); }

/* ═══════════════════════════════════════════
   Options Row
   ═══════════════════════════════════════════ */

.options {
  display: flex; gap: 12px; font-size: 12px; padding: 4px 0;
}
.options label { cursor: pointer; display: flex; align-items: center; gap: 4px; color: var(--text-primary); }

/* ═══════════════════════════════════════════
   Info & Meta
   ═══════════════════════════════════════════ */

#info {
  padding: 8px 0 4px 0; font-size: 12px; color: var(--text-secondary);
  text-align: center; border-top: 1px solid var(--border-color);
  display: flex; flex-direction: column; gap: 2px;
}
#info span { color: var(--accent-primary); }

/* ─── Transform Info (position + rotation) ─── */
#transform-info {
  padding: 4px 8px; font-size: 11px; color: var(--text-secondary);
  text-align: center; border-top: 1px solid var(--border-color);
  line-height: 1.8;
}
#transform-info .transform-row {
  display: flex; align-items: center; gap: 2px; justify-content: center;
}
#transform-info .transform-label {
  color: var(--accent-secondary); font-weight: bold; font-size: 10px;
  text-transform: uppercase; margin-right: 6px; min-width: 24px; text-align: left;
}
#transform-info input {
  width: 56px; background: var(--bg-input); color: var(--text-primary);
  border: 1px solid var(--border-color); border-radius: 3px;
  padding: 1px 4px; font-size: 11px; font-family: monospace; text-align: right;
}
#transform-info input:focus {
  outline: none; border-color: var(--accent-secondary);
  background: var(--bg-secondary);
}

/* ═══════════════════════════════════════════
   Canvas Area
   ═══════════════════════════════════════════ */

#canvas-container { flex: 1; position: relative; }
#canvas-container canvas { display: block; }

/* ═══════════════════════════════════════════
   Hints & Tooltips
   ═══════════════════════════════════════════ */

#hint {
  position: absolute; bottom: 50px; left: 50%; transform: translateX(-50%);
  background: var(--hint-bg); padding: 6px 16px; border-radius: 20px;
  font-size: 11px; color: var(--hint-text); pointer-events: none;
}
#hint.hint-edit { color: var(--hint-edit-color); }

#dim-tooltip {
  position: absolute; display: none; pointer-events: none;
  background: var(--tooltip-bg); color: var(--tooltip-text);
  padding: 8px 12px; border-radius: 6px; font-size: 12px; font-family: monospace;
  white-space: nowrap; z-index: 10; border: 1px solid var(--tooltip-border);
}

/* ═══════════════════════════════════════════
   Toast Notifications
   ═══════════════════════════════════════════ */

#toast {
  position: absolute; bottom: 50px; left: 50%; transform: translateX(-50%);
  padding: 8px 20px; border-radius: 8px; font-size: 13px; pointer-events: none;
  opacity: 0; transition: opacity 0.3s; z-index: 20;
}
#toast.show { opacity: 1; }
#toast.ok { background: var(--toast-ok-bg); color: var(--toast-ok-text); }
#toast.err { background: var(--toast-err-bg); color: var(--toast-err-text); }

/* ═══════════════════════════════════════════
   Bottom Toolbar
   ═══════════════════════════════════════════ */

#toolbar {
  position: absolute; bottom: 0; left: 320px; right: 0; height: 36px;
  background: var(--bg-secondary); border-top: 1px solid var(--border-color);
  display: flex; align-items: center; gap: 12px; padding: 0 12px;
  font-size: 12px; color: var(--toolbar-text); z-index: 5;
}
#toolbar button {
  background: var(--bg-tertiary); color: var(--text-primary); border: none;
  border-radius: 4px; padding: 4px 10px; cursor: pointer; font-size: 11px;
}
#toolbar button:hover { background: var(--bg-hover); }
#toolbar button.primary { background: var(--accent-primary); color: #fff; }
#toolbar button.primary:hover { background: var(--accent-primary-hover); }
#toolbar label { cursor: pointer; display: flex; align-items: center; gap: 4px; color: var(--toolbar-text); }
#toolbar .sep { width: 1px; height: 20px; background: var(--border-color); }
#toolbar .mode-btn { padding: 3px 8px; }
#toolbar .mode-btn.on { background: var(--accent-primary); color: #fff; }
#toolbar .toolbar-hint { color: var(--text-muted); }
