/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a2e;
  --bg-surface: #16213e;
  --bg-card: #1f2b47;
  --bg-hover: #2a3a5c;
  --border: #2a3a5c;
  --text: #e0e0e0;
  --text-dim: #8892a4;
  --text-bright: #ffffff;
  --primary: #4a9eff;
  --primary-hover: #3a8eef;
  --secondary: #6c63ff;
  --accent: #00d2ff;
  --danger: #ff4757;
  --success: #2ed573;
  --warning: #ffa502;
  --slot-border: rgba(74, 158, 255, 0.4);
  --slot-bg: rgba(74, 158, 255, 0.08);
  --sidebar-w: 240px;
  --header-h: 56px;
  --footer-h: 32px;
}

html, body {
  height: 100%;
  font-family: -apple-system, 'Segoe UI', sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

/* === Layout === */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.header-left { display: flex; align-items: center; gap: 10px; }
header h1 { font-size: 16px; font-weight: 600; color: var(--text-bright); }

.badge {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--secondary);
  color: white;
  font-weight: 600;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border);
}

main {
  display: flex;
  height: calc(100vh - var(--header-h) - var(--footer-h));
}

footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--footer-h);
  padding: 0 16px;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-dim);
}

/* === Sidebar === */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-right {
  border-right: none;
  border-left: 1px solid var(--border);
}
.sidebar-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.sidebar-header h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

/* === Character List === */
.char-list {
  list-style: none;
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}
.char-list-empty {
  padding: 24px 12px;
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.6;
}
.char-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 12px;
}
.char-list-item:hover { background: var(--bg-hover); }
.char-list-item.selected {
  background: rgba(74, 158, 255, 0.15);
  border: 1px solid var(--primary);
}
.char-list-item .char-icon {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  background: var(--bg-card);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}
.char-list-item .char-info {
  flex: 1;
  min-width: 0;
}
.char-list-item .char-name {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.char-list-item .char-slot {
  font-size: 10px;
  color: var(--text-dim);
}

/* === Canvas Area === */
.canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  background: #0d1117;
}
.canvas-info {
  position: absolute;
  top: 8px;
  left: 8px;
  display: flex;
  gap: 12px;
  z-index: 5;
  font-size: 11px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.5);
  padding: 4px 10px;
  border-radius: 4px;
}

.preview-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: auto;
}

.screen-frame {
  position: relative;
  width: 960px;
  height: 540px;
  background: #111827;
  border: 2px solid var(--border);
  border-radius: 4px;
  overflow: visible;
}
.screen-frame::after {
  content: '';
  position: absolute;
  inset: 0;
  box-shadow: 0 0 0 9999px rgba(17, 24, 39, 0.6);
  z-index: 10;
  pointer-events: none;
}

.screen-empty-msg {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 14px;
}

/* === Slot Overlay === */
.slot-outline {
  position: absolute;
  border: 1px dashed var(--slot-border);
  background: var(--slot-bg);
  border-radius: 4px;
  pointer-events: none;
  transition: border-color 0.2s;
}
.slot-outline.active {
  border-color: var(--primary);
  border-style: solid;
  border-width: 2px;
}
.slot-label {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 9px;
  color: var(--primary);
  background: rgba(0,0,0,0.6);
  padding: 1px 5px;
  border-radius: 2px;
  pointer-events: none;
}

/* === Character in slot (spine player wrapper) === */
.char-slot-wrapper {
  position: absolute;
  cursor: grab;
  z-index: 2;
}
.char-slot-wrapper:active { cursor: grabbing; }
.char-slot-wrapper.selected {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}
.char-slot-wrapper .spine-player {
  pointer-events: none;
}
.resize-handle {
  position: absolute;
  right: -4px;
  bottom: -4px;
  width: 14px;
  height: 14px;
  background: var(--primary);
  border: 2px solid var(--bg);
  border-radius: 2px;
  cursor: nwse-resize;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.15s;
}
.char-slot-wrapper:hover .resize-handle,
.char-slot-wrapper.selected .resize-handle {
  opacity: 1;
}

/* === Controls Panel === */
.controls-panel {
  padding: 12px 14px;
  flex: 1;
  overflow-y: auto;
}
.controls-empty {
  padding: 24px 0;
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
}
.control-group {
  margin-bottom: 12px;
}
.control-group label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.ctrl-value {
  font-size: 13px;
  color: var(--text-bright);
}
.slider-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.slider-row input[type="range"] {
  flex: 1;
  accent-color: var(--primary);
  height: 4px;
}
.num-input {
  width: 60px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-card);
  color: var(--text-bright);
  font-size: 12px;
  text-align: right;
}
.num-input:focus {
  outline: none;
  border-color: var(--primary);
}

.control-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover { background: var(--bg-hover); }
.btn-accent {
  background: rgba(0, 210, 255, 0.1);
  color: var(--accent);
  border-color: rgba(0, 210, 255, 0.3);
}
.btn-accent:hover {
  background: rgba(0, 210, 255, 0.2);
}
.btn-danger {
  background: rgba(255, 71, 87, 0.1);
  color: var(--danger);
  border-color: rgba(255, 71, 87, 0.3);
}
.btn-danger:hover { background: rgba(255, 71, 87, 0.2); }
.btn-ghost {
  background: transparent;
  color: var(--text-dim);
}
.btn-ghost:hover { color: var(--text); background: var(--bg-hover); }
.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  padding: 4px 8px;
  cursor: pointer;
}
.btn-icon:hover { color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 11px; }

/* === Select === */
select {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  min-width: 180px;
}
select:focus { outline: none; border-color: var(--primary); }

/* === Modal === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-content {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 480px;
  width: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.modal-wide { max-width: 640px; }
.modal-content h2 {
  font-size: 18px;
  color: var(--text-bright);
  margin-bottom: 20px;
}
.setup-section {
  margin-bottom: 20px;
}
.setup-section label {
  display: block;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-bright);
}
.hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 8px;
}
.path-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.path-display {
  flex: 1;
  padding: 6px 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.setup-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 24px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}

/* === Character Grid (picker) === */
.search-row {
  margin-bottom: 12px;
}
.search-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
}
.search-input:focus { outline: none; border-color: var(--primary); }
.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  max-height: 360px;
  overflow-y: auto;
  padding: 4px;
}
.char-grid-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}
.char-grid-item:hover {
  border-color: var(--primary);
  background: var(--bg-hover);
}
.char-grid-item .char-thumb {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}
.char-grid-item .char-label {
  font-size: 11px;
  color: var(--text);
  word-break: break-all;
}

/* === Spine Player Override === */
.spine-player-container {
  background: transparent !important;
}
.spine-player-container canvas {
  background: transparent !important;
}

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

/* === Mode Cards === */
.mode-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.mode-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 24px 16px;
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
}
.mode-card:hover {
  border-color: var(--primary);
  background: var(--bg-hover);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}
.mode-card-icon {
  font-size: 40px;
  line-height: 1;
}
.mode-card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-bright);
}
.mode-card-desc {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.5;
}

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

/* === Responsive === */
@media (max-width: 1024px) {
  :root {
    --sidebar-w: 200px;
  }
  .screen-frame {
    width: 720px;
    height: 405px;
  }
}
