/* -- Reset --------------------------------------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

/* -- Tokens -------------------------------------- */
:root {
  --sidebar-w: 252px;
  --panel-w:   296px;
  --topbar-h:  48px;

  --bg:        #f4f6fb;
  --surface:   #ffffff;
  --surface-2: #f1f4fa;
  --surface-3: #e8edf7;
  --border:    #d7deea;
  --border-s:  #e5ebf5;

  --text:     #1f2937;
  --text-dim: #4b5563;
  --muted:    #6b7280;

  --accent:        #3559d8;
  --accent-h:      #2f4ec0;
  --accent-dim:    rgba(53,89,216,.14);
  --on:            #0f9f6e;
  --on-dim:        rgba(15,159,110,.12);
  --on-border:     rgba(15,159,110,.30);
  --danger:        #dc4c4c;
  --danger-dim:    rgba(220,76,76,.12);
  --danger-border: rgba(220,76,76,.30);

  --radius: 6px;
  --font:   -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --ease:   0.18s ease;
  --slide:  0.22s ease;
}

@media (prefers-color-scheme: dark) {
	:root {
    --bg:        #0c0c10;
    --surface:   #131318;
    --surface-2: #1b1b22;
    --surface-3: #22222c;
    --border:    #28283a;
    --border-s:  #1e1e2a;

    --text:     #e0e0ec;
    --text-dim: #a0a0bc;
    --muted:    #60607c;

    --accent:        #5b7cfb;
    --accent-h:      #4a68f5;
    --accent-dim:    rgba(91,124,251,.14);
    --on:            #34d399;
    --on-dim:        rgba(52,211,153,.12);
    --on-border:     rgba(52,211,153,.30);
    --danger:        #f87171;
    --danger-dim:    rgba(248,113,113,.12);
    --danger-border: rgba(248,113,113,.30);
	}
}

/* -- Base ---------------------------------------- */
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.4;
  background: var(--bg);
  color: var(--text);
  display: flex;          /* sidebar | main | panel in one row */
  height: 100dvh;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { cursor: pointer; font-family: inherit; font-size: 13px; border: none; }
select, input { font-family: inherit; font-size: 13px; }

/* -------------------------------------------------
   SIDEBAR
   Desktop: in-flow, collapses via width → canvas grows
   Mobile:  position:fixed overlay, slides via transform
------------------------------------------------- */
#sidebar {
  flex-shrink: 0;
  width: var(--sidebar-w);
  overflow: hidden;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  transition: width var(--slide);
}
#sidebar.closed { width: 0; }

/* Inner content keeps its natural width so it clips cleanly during transition */
.sidebar-top {
  min-width: var(--sidebar-w);
  padding: 14px 12px 10px;
  border-bottom: 1px solid var(--border-s);
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

/* Search */
.search-wrap { position: relative; display: flex; align-items: center; }
.search-icon { position: absolute; left: 9px; color: var(--muted); pointer-events: none; }
#search {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 7px 10px 7px 30px;
  outline: none;
  transition: border-color var(--ease);
}
#search::placeholder { color: var(--muted); }
#search:focus { border-color: var(--accent); }

.search-results {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.search-results.hidden { display: none; }
.search-item {
  display: block; padding: 8px 12px;
  color: var(--text-dim); font-size: 12.5px; cursor: pointer;
  border-bottom: 1px solid var(--border-s);
  transition: background var(--ease), color var(--ease);
}
.search-item:last-child { border-bottom: none; }
.search-item:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }
.search-item.active { background: var(--surface-3); color: var(--text); text-decoration: none; }

/* Model list */
#model-list {
  overflow-y: auto;
  flex: 1;
  padding: 6px 0;
  min-width: var(--sidebar-w);
}
#model-list::-webkit-scrollbar { width: 3px; }
#model-list::-webkit-scrollbar-track { background: transparent; }
#model-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.char-header {
  width: 100%; display: flex; align-items: center;
  justify-content: space-between; gap: 8px;
  padding: 8px 14px; background: none;
  color: var(--text-dim); font-size: 13px; font-weight: 500; text-align: left;
  transition: background var(--ease), color var(--ease);
}
.char-header:hover { background: var(--surface-2); color: var(--text); }
.char-group.open .char-header { color: var(--text); }

.char-chevron { flex-shrink: 0; color: var(--muted); transition: transform var(--ease); }
.char-group.open .char-chevron { transform: rotate(90deg); }

.char-poses { overflow: hidden; max-height: 0; transition: max-height 0.24s ease; }
.char-group.open .char-poses { max-height: 800px; }

.pose-link {
  display: block; padding: 5px 14px 5px 26px;
  color: var(--muted); font-size: 12.5px;
  transition: background var(--ease), color var(--ease);
}
.pose-link:hover { background: var(--surface-2); color: var(--text-dim); text-decoration: none; }
.pose-link.active { background: var(--accent-dim); color: var(--accent); }

/* -- Sidebar overlay (mobile) -------------------- */
#sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.55); z-index: 15;
}
#sidebar-overlay.visible { display: block; }

/* -------------------------------------------------
   MAIN
------------------------------------------------- */
#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* -- Topbar -------------------------------------- */
.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
}
.topbar-title {
  flex: 1; font-size: 14px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: 6px; }

/* Shared icon button */
.icon-btn {
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; background: none;
  border-radius: var(--radius); color: var(--text-dim); flex-shrink: 0;
  transition: background var(--ease), color var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.active { background: var(--accent-dim); color: var(--accent); }

.topbar-btn {
  height: 30px; padding: 0 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text-dim); font-weight: 500;
  transition: background var(--ease), color var(--ease), border-color var(--ease);
}
.topbar-btn:hover { background: var(--surface-3); color: var(--text); border-color: var(--accent); }

/* -- Canvas area --------------------------------- */
#canvas-area {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  padding: 14px; gap: 10px; overflow: hidden;
}

#status-bar { flex-shrink: 0; height: 18px; display: flex; align-items: center; }
#viewer-status { font-size: 12px; color: var(--muted); }
#viewer-status.error { color: var(--danger); }

#canvas-wrap {
  flex: 1; min-height: 0; position: relative;
  /*background: radial-gradient(circle at 30% 10%, #eef2ff, #dce4ff 45%, #cdd8ff 100%);*/
  background: radial-gradient(circle at 30% 10%, #dfe2e9, #b6c2e7 45%, #818caf 100%);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  text: white;
}
#canvas-wrap canvas { display: block; }

#viewer-placeholder {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 8px; padding: 24px; text-align: center;
  color: #4a5a8a; font-size: 15px;
}
.placeholder-sub { font-size: 12px; color: #7a8ab0; }
.placeholder-sub a { color: #5b7cca; }

/* -- Loading bar --------------------------------- */
#loading-bar {
  position: absolute; top: 0; left: 0; right: 0;
  height: 3px; z-index: 4; overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}
#loading-bar.active { opacity: 1; }

#loading-bar-inner {
  position: absolute; top: 0; height: 100%;
  background: var(--accent);
  border-radius: 0 3px 3px 0;
  animation: loadbar 1.6s ease-in-out infinite;
}
@keyframes loadbar {
  0%   { left: -40%; width: 40%; }
  60%  { left: 100%; width: 40%; }
  100% { left: 100%; width: 40%; }
}

#debug-output {
  flex-shrink: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); color: #6ee7b7;
  padding: 10px 14px; font-size: 12px;
  font-family: 'Menlo','Consolas',monospace;
  white-space: pre-wrap; max-height: 130px; overflow-y: auto;
}
#debug-output.hidden { display: none; }

/* -------------------------------------------------
   CONTROLS PANEL
   Desktop: in-flow, collapses via width → canvas grows
   Mobile:  position:fixed overlay, slides via transform
------------------------------------------------- */
#controls-panel {
  flex-shrink: 0;
  width: var(--panel-w);
  overflow: hidden;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  transition: width var(--slide);
}
#controls-panel.closed { width: 0; }

/* Inner wrapper keeps natural width so content clips cleanly */
.panel-inner {
  min-width: var(--panel-w);
  display: flex;
  flex-direction: column;
  height: 100%;
}

.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 10px 0 14px; height: var(--topbar-h);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
  font-size: 14px; font-weight: 600; color: var(--text);
}

.panel-body {
  flex: 1; overflow-y: auto;
  padding: 14px 12px;
  display: flex; flex-direction: column; gap: 22px;
}
.panel-body::-webkit-scrollbar { width: 3px; }
.panel-body::-webkit-scrollbar-track { background: transparent; }
.panel-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.panel-section h3 {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .07em; color: var(--muted); margin-bottom: 10px;
}

.panel-section select {
  width: 100%; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  padding: 7px 10px; outline: none; margin-bottom: 8px;
  transition: border-color var(--ease);
}
.panel-section select:focus { border-color: var(--accent); }

.btn-row { display: flex; gap: 8px; }

.btn-primary {
  flex: 1; height: 32px; background: var(--accent);
  border-radius: var(--radius); color: #fff; font-weight: 500;
  transition: background var(--ease), opacity var(--ease);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-h); }
.btn-primary:disabled { opacity: .38; cursor: default; }

.btn-outline {
  flex: 1; height: 32px; background: none;
  border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text-dim); font-weight: 500;
  transition: background var(--ease), color var(--ease), border-color var(--ease), opacity var(--ease);
}
.btn-outline:hover:not(:disabled) { background: var(--surface-3); color: var(--text); }
.btn-outline:disabled { opacity: .38; cursor: default; }

.empty-msg { font-size: 12px; color: var(--muted); }

/* Parameter / part rows */
.param-row {
  padding: 8px 10px; border: 1px solid var(--border-s);
  border-radius: var(--radius); background: var(--surface-2);
  margin-bottom: 5px; cursor: pointer;
  transition: background var(--ease), border-color var(--ease);
}
.param-row:last-child { margin-bottom: 0; }
.param-row:hover { border-color: var(--border); }
.param-row:focus { outline: none; border-color: var(--accent); }
.param-row.enabled     { background: var(--on-dim);     border-color: var(--on-border);     }
.param-row.part-hidden { background: var(--danger-dim); border-color: var(--danger-border); }

.param-header { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 5px; }
.param-name   { font-size: 11.5px; color: var(--text-dim); word-break: break-all; line-height: 1.3; }
.param-state  { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); white-space: nowrap; flex-shrink: 0; }
.param-row.enabled .param-state     { color: var(--on);     }
.param-row.part-hidden .param-state { color: var(--danger); }

.param-slider-wrap { display: flex; align-items: center; gap: 8px; cursor: default; }
.param-slider-wrap input[type="range"] { flex: 1; accent-color: var(--accent); cursor: pointer; height: 4px; margin-bottom: 0; }
.param-value { font-size: 11px; color: var(--muted); min-width: 36px; text-align: right; font-variant-numeric: tabular-nums; }

.panel-footer {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-top: 1px solid var(--border); flex-shrink: 0;
  font-size: 12px; color: var(--muted); min-width: var(--panel-w);
}
.panel-footer input[type="range"] { flex: 1; accent-color: var(--accent); cursor: pointer; }
#controls-opacity-value { min-width: 30px; text-align: right; font-variant-numeric: tabular-nums; }

/* -------------------------------------------------
   MOBILE  ≤ 800px
   Both panels become position:fixed overlays;
   width transition disabled, transform used instead.
------------------------------------------------- */
@media (max-width: 800px) {

  /* Sidebar: always full width, slides by transform */
  #sidebar {
    position: fixed; left: 0; top: 0; z-index: 20;
    width: var(--sidebar-w) !important; /* prevent width:0 from desktop rule */
    transform: translateX(calc(-1 * var(--sidebar-w)));
    transition: transform var(--slide);
    pointer-events: none;
    box-shadow: 4px 0 24px rgba(0,0,0,.45);
  }
  #sidebar.open {
    transform: translateX(0);
    pointer-events: auto;
  }
  /* closed class on mobile just re-applies the hidden transform */
  #sidebar.closed { transform: translateX(calc(-1 * var(--sidebar-w))); }

  /* Controls panel: slides from right */
  #controls-panel {
    position: fixed; right: 0; top: 0; z-index: 10;
    width: var(--panel-w) !important;
    transform: translateX(var(--panel-w));
    transition: transform var(--slide);
    pointer-events: none;
  }
  #controls-panel:not(.closed) {
    transform: translateX(0);
    pointer-events: auto;
  }
  #controls-panel.closed {
    transform: translateX(var(--panel-w));
  }
}
