/* AI Playground — Design System
   Inherits KreativeLand portal dark theme tokens */

/* --- Design Tokens --- */
:root {
  --bg-base: #09090b;
  --bg-surface: #111114;
  --bg-elevated: #18181c;
  --bg-hover: #1f1f24;
  --bg-input: #0d0d10;

  --text-primary: #f0f0f3;
  --text-secondary: #8b8b99;
  --text-muted: #5a5a6e;

  --accent: #22d3ee;
  --accent-hover: #06b6d4;
  --accent-glow: rgba(34, 211, 238, 0.25);
  --accent-subtle: rgba(34, 211, 238, 0.08);

  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #34d399;
  --warning: #f59e0b;

  --border: rgba(255, 255, 255, 0.06);
  --border-hover: rgba(255, 255, 255, 0.12);
  --border-accent: rgba(34, 211, 238, 0.3);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.4);

  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --duration: 0.2s;

  --sidebar-width: 60px;
  --status-bar-height: 48px;
  --conv-sidebar-width: 260px;

  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

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

html, body {
  height: 100%;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-base);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--accent-glow); color: var(--text-primary); }

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

/* --- Status Bar --- */
.status-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--status-bar-height);
  background: rgba(17, 17, 20, 0.85);
  backdrop-filter: blur(16px) saturate(180%);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
}

.logo {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.02em;
  color: var(--accent);
}

.status-bar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background var(--duration) var(--ease);
}
.status-dot.online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.offline { background: var(--danger); }
.status-dot.checking { background: var(--warning); animation: pulse 1.5s ease infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.vram-bar {
  width: 100px; height: 16px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  overflow: hidden;
  position: relative;
  border: 1px solid var(--border);
}
.vram-bar .vram-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-hover));
  transition: width 0.5s var(--ease);
  width: 0%;
}
.vram-bar .vram-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-primary);
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

/* --- App Layout --- */
.app-layout {
  display: flex;
  height: 100vh;
  padding-top: var(--status-bar-height);
}

/* --- Sidebar --- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 0;
  gap: 4px;
  flex-shrink: 0;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  width: 52px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--duration) var(--ease);
  font-size: 10px;
  font-family: var(--font-sans);
  font-weight: 500;
}
.nav-btn:hover { background: var(--bg-hover); color: var(--text-secondary); }
.nav-btn.active { background: var(--accent-subtle); color: var(--accent); }

/* --- Main Content --- */
.main-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.tab-panel { display: none; height: 100%; }
.tab-panel.active { display: flex; }

/* --- Chat Layout --- */
.chat-layout { display: flex; width: 100%; height: 100%; }

.conv-sidebar {
  width: var(--conv-sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.btn-new-chat {
  margin: 12px;
  gap: 8px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}

.conv-item {
  padding: 10px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: background var(--duration) var(--ease);
  margin-bottom: 2px;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--accent-subtle); }
.conv-item-title {
  font-size: 13px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
.conv-item-model {
  font-size: 10px;
  color: var(--text-muted);
  white-space: nowrap;
}
.conv-item-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  transition: all var(--duration);
  flex-shrink: 0;
}
.conv-item:hover .conv-item-delete { opacity: 1; }
.conv-item-delete:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

/* --- Chat Area --- */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.model-select { flex: 1; max-width: 300px; }

.system-prompt-editor {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-settings {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.settings-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-secondary);
}
.settings-row label:first-child { min-width: 80px; }

.range-value {
  font-family: var(--font-mono);
  font-size: 12px;
  min-width: 28px;
  color: var(--accent);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.welcome-message {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-muted);
}
.welcome-message h2 {
  font-size: 24px;
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.message {
  display: flex;
  gap: 12px;
  max-width: 800px;
  animation: fadeIn 0.2s var(--ease);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user { flex-direction: row-reverse; align-self: flex-end; }
.message.user .message-content {
  background: var(--accent-subtle);
  border: 1px solid var(--border-accent);
}

.message-avatar {
  width: 28px; height: 28px;
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.message.assistant .message-avatar { background: var(--accent-subtle); color: var(--accent); }

.message-content {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.65;
  min-width: 0;
  overflow-wrap: break-word;
}

.message-content p { margin-bottom: 8px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content ul, .message-content ol { padding-left: 20px; margin-bottom: 8px; }
.message-content h1, .message-content h2, .message-content h3 {
  margin: 12px 0 6px;
  font-weight: 600;
}
.message-content h1 { font-size: 1.3em; }
.message-content h2 { font-size: 1.15em; }
.message-content h3 { font-size: 1.05em; }

.message-content code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-elevated);
  padding: 2px 5px;
  border-radius: 4px;
}

.message-content pre {
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
  position: relative;
}
.message-content pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.5;
}

.copy-btn {
  position: absolute;
  top: 6px; right: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 11px;
  font-family: var(--font-sans);
  opacity: 0;
  transition: opacity var(--duration);
}
.message-content pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text-primary); border-color: var(--border-hover); }

.message-content blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--text-secondary);
}

.message-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
}
.message-content th, .message-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  font-size: 13px;
}
.message-content th { background: var(--bg-elevated); font-weight: 600; }

.message-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
  font-family: var(--font-mono);
}

.thinking-dots {
  display: flex; gap: 4px; padding: 8px 0;
}
.thinking-dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.4s ease infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* --- Chat Input --- */
.chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  max-height: 200px;
  resize: none;
}

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--duration) var(--ease);
  white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); border-color: var(--border-hover); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary { background: var(--accent-subtle); border-color: var(--border-accent); color: var(--accent); }
.btn-primary:hover { background: rgba(34, 211, 238, 0.15); }

.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--bg-base); font-weight: 600; }
.btn-accent:hover { background: var(--accent-hover); }

.btn-danger { background: rgba(239,68,68,0.1); border-color: rgba(239,68,68,0.3); color: var(--danger); }
.btn-danger:hover { background: rgba(239,68,68,0.2); }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn-sm { padding: 4px 8px; font-size: 12px; }
.btn-send, .btn-stop { width: 40px; height: 40px; padding: 0; border-radius: var(--radius-md); }

/* --- Form Controls --- */
.select, .input, .textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13px;
  padding: 8px 12px;
  transition: border-color var(--duration) var(--ease);
  width: 100%;
}
.select:focus, .input:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}
.textarea { resize: vertical; min-height: 40px; }

.select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238b8b99' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

.range {
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--bg-elevated);
  border-radius: 2px;
  outline: none;
  width: 120px;
}
.range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg-base);
}

/* Toggle */
.toggle { position: relative; display: inline-block; width: 36px; height: 20px; cursor: pointer; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: all var(--duration);
}
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  left: 2px; top: 2px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: all var(--duration);
}
.toggle input:checked + .toggle-slider { background: var(--accent-subtle); border-color: var(--border-accent); }
.toggle input:checked + .toggle-slider::before { transform: translateX(16px); background: var(--accent); }

/* --- Generation Layout --- */
.gen-layout { display: flex; width: 100%; height: 100%; }

.gen-controls {
  width: 340px;
  flex-shrink: 0;
  padding: 20px;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.gen-controls h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}

.control-group { display: flex; flex-direction: column; gap: 4px; }
.control-group label { font-size: 12px; color: var(--text-secondary); font-weight: 500; }

.control-row { display: flex; gap: 12px; }
.control-row .control-group { flex: 1; }

.btn-generate { margin-top: 8px; }

.gen-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}
.progress-bar {
  flex: 1; height: 6px;
  background: var(--bg-elevated);
  border-radius: 3px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.3s var(--ease);
  width: 0%;
}
.progress-fill.indeterminate {
  width: 30%;
  animation: indeterminate 1.5s ease infinite;
}
@keyframes indeterminate {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(400%); }
}
.progress-text { font-size: 12px; color: var(--text-secondary); white-space: nowrap; }

/* --- Gallery --- */
.gen-gallery {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-content: flex-start;
}

.gallery-empty {
  width: 100%;
  text-align: center;
  color: var(--text-muted);
  padding: 80px 20px;
}

.gallery-item {
  width: calc(50% - 6px);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  cursor: pointer;
  transition: all var(--duration) var(--ease);
}
.gallery-item:hover { border-color: var(--border-hover); transform: translateY(-2px); box-shadow: var(--shadow-md); }

.gallery-item img, .gallery-item video {
  width: 100%;
  display: block;
}

.gallery-item-info {
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
}
.gallery-item-prompt {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.gallery-item-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* --- Models Layout --- */
.models-layout {
  flex-direction: column;
  padding: 20px;
  gap: 16px;
  overflow-y: auto;
  max-width: 900px;
}

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 20px;
}
.card h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
}

.gpu-info { display: flex; flex-direction: column; gap: 10px; }
.gpu-stat { display: flex; align-items: center; gap: 12px; }
.gpu-label { font-size: 12px; color: var(--text-muted); min-width: 70px; font-weight: 500; }
.gpu-value { font-size: 13px; color: var(--text-primary); font-family: var(--font-mono); }

.vram-bar-large {
  flex: 1; max-width: 300px; height: 12px;
  background: var(--bg-elevated);
  border-radius: 6px;
  overflow: hidden;
}
.vram-bar-large .vram-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-hover));
  transition: width 0.5s var(--ease);
}

.pull-row { display: flex; gap: 8px; }
.pull-row .input { flex: 1; }

.pull-progress { margin-top: 8px; display: flex; align-items: center; gap: 10px; }

/* --- Models Table --- */
.models-table { width: 100%; border-collapse: collapse; }
.models-table th, .models-table td {
  text-align: left;
  padding: 8px 12px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.models-table th {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.models-table td { color: var(--text-primary); }
.models-table td:first-child { font-family: var(--font-mono); font-weight: 500; }
.table-empty { text-align: center; color: var(--text-muted); padding: 20px; }

.comfyui-models {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
}
.comfyui-model-group h4 {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}
.comfyui-model-group ul {
  list-style: none;
  padding: 0;
}
.comfyui-model-group li {
  padding: 4px 0;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-secondary);
}

/* --- Lightbox --- */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.9);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 12px;
}
.lightbox-close {
  position: absolute;
  top: 16px; right: 16px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 32px;
  cursor: pointer;
  line-height: 1;
}
.lightbox-img { max-width: 90vw; max-height: 80vh; border-radius: var(--radius-lg); }
.lightbox-info { font-size: 13px; color: var(--text-secondary); max-width: 600px; text-align: center; }

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

/* --- Mobile --- */
@media (max-width: 768px) {
  .conv-sidebar { display: none; }
  .gen-controls { width: 100%; border-right: none; border-bottom: 1px solid var(--border); }
  .gen-layout { flex-direction: column; }
  .gallery-item { width: 100%; }
  .sidebar { flex-direction: row; width: 100%; height: auto; padding: 0; position: fixed; bottom: 0; left: 0; z-index: 99; border-right: none; border-top: 1px solid var(--border); }
  .nav-btn { width: auto; flex: 1; }
  .app-layout { flex-direction: column; padding-bottom: 52px; }
}
