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

:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f9fafb;
  --bg-tertiary: #f3f4f6;
  --text-primary: #111827;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --border-focus: #3b82f6;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: #eff6ff;
  --success: #10b981;
  --success-light: #ecfdf5;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --chat-user: #3b82f6;
  --chat-user-text: #ffffff;
  --chat-ai: #f3f4f6;
  --chat-ai-text: #111827;
  --toolbar-bg: #f9fafb;
  --scrollbar-track: #f1f1f1;
  --scrollbar-thumb: #c1c1c1;
  --bar-before: #9ca3af;
  --bar-after: #3b82f6;
}

[data-theme="dark"] {
  --bg-primary: #111827;
  --bg-secondary: #1f2937;
  --bg-tertiary: #374151;
  --text-primary: #f9fafb;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  --border: #374151;
  --border-focus: #60a5fa;
  --accent: #60a5fa;
  --accent-hover: #3b82f6;
  --accent-light: #1e3a5f;
  --success: #34d399;
  --success-light: #064e3b;
  --danger: #f87171;
  --danger-light: #7f1d1d;
  --chat-user: #3b82f6;
  --chat-user-text: #ffffff;
  --chat-ai: #1f2937;
  --chat-ai-text: #f9fafb;
  --toolbar-bg: #1f2937;
  --scrollbar-track: #1f2937;
  --scrollbar-thumb: #4b5563;
  --bar-before: #4b5563;
  --bar-after: #60a5fa;
}

[data-theme="sepia"] {
  --bg-primary: #fdf8f0;
  --bg-secondary: #f5ead6;
  --bg-tertiary: #e8d5b5;
  --text-primary: #5c4b37;
  --text-secondary: #8b7355;
  --text-muted: #a89880;
  --border: #e0d0b8;
  --border-focus: #c49a6c;
  --accent: #c49a6c;
  --accent-hover: #a67c52;
  --accent-light: #f5ead6;
  --success: #7cb342;
  --success-light: #f1f8e9;
  --danger: #d32f2f;
  --danger-light: #fce4ec;
  --chat-user: #c49a6c;
  --chat-user-text: #ffffff;
  --chat-ai: #f5ead6;
  --chat-ai-text: #5c4b37;
  --toolbar-bg: #f5ead6;
  --scrollbar-track: #f5ead6;
  --scrollbar-thumb: #c49a6c;
  --bar-before: #c4b39a;
  --bar-after: #a67c52;
}

[data-theme="ocean"] {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --text-primary: #e2e8f0;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border: #334155;
  --border-focus: #38bdf8;
  --accent: #38bdf8;
  --accent-hover: #0ea5e9;
  --accent-light: #1e3a5f;
  --success: #34d399;
  --success-light: #064e3b;
  --danger: #f87171;
  --danger-light: #7f1d1d;
  --chat-user: #0ea5e9;
  --chat-user-text: #ffffff;
  --chat-ai: #1e293b;
  --chat-ai-text: #e2e8f0;
  --toolbar-bg: #1e293b;
  --scrollbar-track: #1e293b;
  --scrollbar-thumb: #475569;
  --bar-before: #475569;
  --bar-after: #38bdf8;
}

[data-theme="rose"] {
  --bg-primary: #fdf2f8;
  --bg-secondary: #fce7f3;
  --bg-tertiary: #fbcfe8;
  --text-primary: #831843;
  --text-secondary: #9d174d;
  --text-muted: #be185d;
  --border: #fbcfe8;
  --border-focus: #ec4899;
  --accent: #ec4899;
  --accent-hover: #db2777;
  --accent-light: #fce7f3;
  --success: #10b981;
  --success-light: #ecfdf5;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --chat-user: #ec4899;
  --chat-user-text: #ffffff;
  --chat-ai: #fce7f3;
  --chat-ai-text: #831843;
  --toolbar-bg: #fce7f3;
  --scrollbar-track: #fce7f3;
  --scrollbar-thumb: #ec4899;
  --bar-before: #d8a3bd;
  --bar-after: #ec4899;
}

[data-theme="mint"] {
  --bg-primary: #f0fdfa;
  --bg-secondary: #ccfbf1;
  --bg-tertiary: #99f6e4;
  --text-primary: #134e4a;
  --text-secondary: #0d9488;
  --text-muted: #14b8a6;
  --border: #99f6e4;
  --border-focus: #14b8a6;
  --accent: #14b8a6;
  --accent-hover: #0d9488;
  --accent-light: #ccfbf1;
  --success: #10b981;
  --success-light: #ecfdf5;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --chat-user: #14b8a6;
  --chat-user-text: #ffffff;
  --chat-ai: #ccfbf1;
  --chat-ai-text: #134e4a;
  --toolbar-bg: #ccfbf1;
  --scrollbar-track: #ccfbf1;
  --scrollbar-thumb: #14b8a6;
  --bar-before: #94d9ce;
  --bar-after: #0d9488;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { opacity: 0.8; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  height: 48px;
  z-index: 10;
}
.header-left, .header-right { display: flex; align-items: center; gap: 12px; }
.header h1 { font-size: 16px; font-weight: 700; }
.header a { font-size: 13px; color: var(--text-secondary); white-space: nowrap; }
.header a:hover { color: var(--accent); text-decoration: none; }

.btn {
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-tertiary); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.full-width { width: 100%; }
.toolbar-btn { font-size: 12px; padding: 5px 10px; }

.theme-select {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
}

.main-layout { display: flex; height: calc(100vh - 48px); }

.sidebar-left {
  width: 380px;
  min-width: 380px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-section:last-child { border-bottom: none; flex: 1; overflow: hidden; }
.sidebar-title { font-size: 14px; font-weight: 700; margin-bottom: 2px; }
.sidebar-hint { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; line-height: 1.4; }
.sidebar-subtitle { font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; }
.sidebar-textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
  font-family: inherit;
}
.sidebar-textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

.sidebar-presets { margin-top: 4px; }
.preset-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.preset-btn {
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
}
.preset-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 0;
  min-height: 100px;
}
.chat-msg {
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  max-width: 90%;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.chat-msg.user { background: var(--chat-user); color: var(--chat-user-text); align-self: flex-end; border-bottom-right-radius: 4px; }
.chat-msg.assistant { background: var(--bg-tertiary); color: var(--text-primary); align-self: flex-start; border-bottom-left-radius: 4px; }
.chat-input-row { display: flex; gap: 6px; align-items: flex-end; }
.chat-input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 12px;
  line-height: 1.4;
  resize: none;
  font-family: inherit;
}
.chat-input:focus { outline: none; border-color: var(--border-focus); }
.chat-send-btn { padding: 8px 12px; font-size: 12px; white-space: nowrap; }

.editor-main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
.editor-tabs { display: flex; gap: 2px; }
.editor-tab {
  padding: 5px 12px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}
.editor-tab.active { background: var(--bg-primary); color: var(--text-primary); box-shadow: 0 1px 2px rgba(0,0,0,0.06); }
.editor-tab:hover:not(.active) { color: var(--text-primary); }
.editor-actions { display: flex; gap: 6px; }
.editor-content { flex: 1; display: flex; overflow: hidden; }
.editor-pane { flex: 1; overflow: hidden; }
.editor-pane.hidden { display: none; }
.pane-scroll { overflow-y: auto; padding: 20px 28px; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--text-secondary); font-size: 15px; }
.empty-sub { font-size: 13px; color: var(--text-muted); margin-top: 8px; }
.hidden { display: none !important; }

.section-title { font-size: 14px; font-weight: 700; margin: 18px 0 8px; color: var(--text-primary); }
.section-title:first-child { margin-top: 0; }

.stat-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 18px; }
.stat-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.stat-card.good { border-color: var(--success); }
.stat-card.warn { border-color: var(--danger); }
.stat-value { font-size: 22px; font-weight: 800; color: var(--accent); }
.stat-card.good .stat-value { color: var(--success); }
.stat-card.warn .stat-value { color: var(--danger); }
.stat-label { font-size: 11px; font-weight: 600; color: var(--text-secondary); margin-top: 2px; }
.stat-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.stat-value-bar { height: 10px; border-radius: 5px; background: var(--bg-tertiary); overflow: hidden; margin: 4px 0 6px; }
.stat-value-bar span { display: block; height: 100%; background: var(--bar-before); border-radius: 5px; }
.stat-value-bar span.after { background: var(--bar-after); }

.prompt-box {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 12px;
}
.prompt-box pre {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: var(--text-primary);
}
.prompt-box.original { border-left: 4px solid var(--bar-before); }
.prompt-box.optimized { border-left: 4px solid var(--bar-after); }

.result-actions { margin-top: 12px; }

.chart-wrap { margin-top: 20px; max-width: 480px; }
.chart-wrap svg { width: 100%; height: auto; }
.bar-label { font-size: 11px; fill: var(--text-secondary); font-weight: 600; }
.axis-label { font-size: 12px; fill: var(--text-secondary); font-weight: 600; }
.bar-before { fill: var(--bar-before); }
.bar-after { fill: var(--bar-after); }

.compare-body { display: flex; gap: 16px; align-items: flex-start; }
.compare-col { flex: 1; min-width: 0; }
.char-count { font-size: 11px; color: var(--text-muted); margin-top: 6px; }

.lesson-body { max-width: 760px; }
.lesson-list { padding-left: 20px; margin-bottom: 14px; font-size: 13px; line-height: 1.6; color: var(--text-primary); }
.lesson-list.problems li::marker { color: var(--danger); }
.tech-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.tech-chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}
.lesson-callout {
  background: var(--accent-light);
  border-left: 4px solid var(--accent);
  border-radius: 0 8px 8px 0;
  padding: 14px 16px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 90%;
  border: 1px solid var(--border);
  max-height: 85vh;
  overflow-y: auto;
}
.modal.modal-wide { max-width: 640px; }
.modal.modal-coach {
  max-width: 860px;
  width: 92vw;
  height: 78vh;
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  padding: 20px 24px;
  overflow: hidden;
}
.coach-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  flex-shrink: 0;
}
.coach-header h2 { margin-bottom: 0; }
.modal-coach .chat-messages { flex: 1; min-height: 0; padding: 12px 4px; }
.modal-coach .chat-msg { max-width: 75%; font-size: 14px; }
.modal-coach .chat-input-row { flex-shrink: 0; padding-top: 8px; border-top: 1px solid var(--border); margin-top: 4px; }
.modal-coach .chat-input { font-size: 13px; padding: 10px 12px; }
.modal-coach .chat-send-btn { padding: 10px 18px; font-size: 13px; }
.modal h2 { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.modal-hint { font-size: 11px; color: var(--text-secondary); margin-top: -8px; margin-bottom: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

.history-list { display: flex; flex-direction: column; gap: 10px; max-height: 55vh; overflow-y: auto; }
.history-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.history-text { font-size: 12px; line-height: 1.5; margin-bottom: 4px; }
.history-meta { font-size: 11px; color: var(--text-muted); margin-bottom: 6px; }
.history-actions { display: flex; gap: 6px; }

.ai-label { display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px; color: var(--text-secondary); }
.ai-input, .ai-select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
}
.ai-input:focus, .ai-select:focus { outline: none; border-color: var(--border-focus); }
#connectionStatus { font-size: 12px; margin-bottom: 8px; padding: 6px 10px; border-radius: 6px; }
#connectionStatus.success { background: var(--success-light); color: var(--success); }
#connectionStatus.error { background: var(--danger-light); color: var(--danger); }

.loading-card {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 32px;
  text-align: center;
  border: 1px solid var(--border);
}
.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-card p { font-size: 14px; color: var(--text-secondary); }

@media (max-width: 900px) {
  .main-layout { flex-direction: column; }
  .sidebar-left { width: 100%; min-width: 0; max-height: 50vh; overflow-y: auto; }
  .pane-scroll { padding: 14px; }
}
