/* ============================================================
   DigitalCraw — Design System
   Single source of truth for colors, fonts, spacing, and components.
   Load this FIRST on every page.
   ============================================================ */

/* ── 1. Design Tokens ── */
:root {
  /* Brand */
  --brand-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  --brand-primary: #667eea;
  --brand-secondary: #764ba2;

  /* Accent palette */
  --accent-purple: #7c3aed;
  --accent-purple-hover: #6d28d9;
  --accent-green: #10b981;
  --accent-green-hover: #059669;
  --accent-blue: #3b82f6;
  --accent-blue-hover: #2563eb;
  --accent-amber: #f59e0b;
  --accent-red: #ef4444;
  --accent-red-hover: #dc2626;

  /* Semantic */
  --color-success: #22c55e;
  --color-warning: #f59e0b;
  --color-danger: #ef4444;
  --color-info: #3b82f6;

  /* Neutrals */
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;

  /* Surfaces */
  --surface-primary: #ffffff;
  --surface-secondary: var(--gray-50);
  --surface-elevated: #ffffff;
  --surface-overlay: rgba(0, 0, 0, 0.5);

  /* Borders */
  --border-light: var(--gray-200);
  --border-default: var(--gray-300);
  --border-focus: var(--accent-purple);

  /* Typography */
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
    'Helvetica Neue', Arial, 'Microsoft JhengHei', 'PingFang TC',
    sans-serif;
  --font-mono: 'Consolas', 'Monaco', 'Fira Code', 'Courier New', monospace;

  --text-xs: 0.75rem;   /* 12px */
  --text-sm: 0.875rem;  /* 14px */
  --text-base: 1rem;    /* 16px */
  --text-lg: 1.125rem;  /* 18px */
  --text-xl: 1.25rem;   /* 20px */
  --text-2xl: 1.5rem;   /* 24px */
  --text-3xl: 2rem;     /* 32px */

  --leading-tight: 1.25;
  --leading-normal: 1.5;
  --leading-relaxed: 1.7;

  /* Spacing scale (4px base) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;
  --space-12: 48px;

  /* Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 20px 40px rgba(0, 0, 0, 0.15);

  /* Transitions */
  --transition-fast: 0.15s ease;
  --transition-normal: 0.2s ease;
  --transition-slow: 0.3s ease;

  /* Z-index scale */
  --z-dropdown: 100;
  --z-sticky: 200;
  --z-modal-backdrop: 900;
  --z-modal: 1000;
  --z-popover: 1100;
  --z-toast: 1200;
}

/* ── 2. CSS Reset & Base ── */
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--gray-800);
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

code, pre, kbd, samp {
  font-family: var(--font-mono);
}

/* ── 3. Button System ── */
.btn, [class*="btn-"] {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  line-height: var(--leading-tight);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-normal);
  white-space: nowrap;
  text-decoration: none;
}
.btn:disabled, [class*="btn-"]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
}

/* Primary */
.btn-primary {
  background: var(--brand-gradient);
  color: #fff;
  border-color: transparent;
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

/* Secondary */
.btn-secondary {
  background: var(--surface-primary);
  color: var(--gray-700);
  border-color: var(--border-default);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--gray-50);
  border-color: var(--gray-400);
}

/* Danger */
.btn-danger {
  background: var(--accent-red);
  color: #fff;
}
.btn-danger:hover:not(:disabled) {
  background: var(--accent-red-hover);
  transform: translateY(-1px);
}

/* Ghost */
.btn-ghost {
  background: transparent;
  color: var(--gray-600);
  border-color: transparent;
}
.btn-ghost:hover:not(:disabled) {
  background: var(--gray-100);
  color: var(--gray-800);
}

/* Icon button */
.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--gray-500);
  border: 1px solid var(--border-light);
}
.btn-icon:hover:not(:disabled) {
  background: var(--gray-100);
  color: var(--gray-700);
  border-color: var(--border-default);
}

/* Sizes */
.btn-sm { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); }
.btn-lg { padding: var(--space-3) var(--space-6); font-size: var(--text-base); }

/* ── 4. Form Controls ── */
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  color: var(--gray-800);
  background: var(--surface-primary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.12);
}
.form-input::placeholder, .form-textarea::placeholder {
  color: var(--gray-400);
}
.form-textarea {
  resize: vertical;
  min-height: 80px;
}
.form-label {
  display: block;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--gray-700);
  margin-bottom: var(--space-1);
}

/* ── 5. Card ── */
.ds-card {
  background: var(--surface-elevated);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition-normal), transform var(--transition-normal);
}
.ds-card:hover {
  box-shadow: var(--shadow-lg);
  transform: translateY(-2px);
}

/* ── 6. Badge / Chip ── */
.ds-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  font-size: var(--text-xs);
  font-weight: 500;
  border-radius: var(--radius-full);
  white-space: nowrap;
}
.ds-badge-purple { background: rgba(124,58,237,0.1); color: var(--accent-purple); }
.ds-badge-green  { background: rgba(16,185,129,0.1); color: var(--accent-green); }
.ds-badge-blue   { background: rgba(59,130,246,0.1); color: var(--accent-blue); }
.ds-badge-amber  { background: rgba(245,158,11,0.1); color: var(--accent-amber); }
.ds-badge-red    { background: rgba(239,68,68,0.1);  color: var(--accent-red); }
.ds-badge-gray   { background: var(--gray-100);       color: var(--gray-600); }

/* ── 7. Modal ── */
.ds-modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--surface-overlay);
  backdrop-filter: blur(4px);
  z-index: var(--z-modal-backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: ds-fadeIn var(--transition-fast);
}
.ds-modal {
  background: var(--surface-elevated);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  z-index: var(--z-modal);
  animation: ds-slideUp var(--transition-slow);
}
.ds-modal-header {
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ds-modal-header h3 {
  margin: 0;
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--gray-900);
}
.ds-modal-body {
  padding: var(--space-6);
}
.ds-modal-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border-light);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
}

@keyframes ds-fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes ds-slideUp {
  from { opacity: 0; transform: translateY(16px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ── 8. Toast / Notification ── */
.ds-toast-container {
  position: fixed;
  bottom: var(--space-6);
  right: var(--space-6);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column-reverse;
  gap: var(--space-3);
  pointer-events: none;
}
.ds-toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--gray-800);
  color: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  max-width: 380px;
  animation: ds-toastIn var(--transition-slow);
}
.ds-toast.success { background: var(--color-success); }
.ds-toast.warning { background: var(--color-warning); color: var(--gray-900); }
.ds-toast.danger  { background: var(--color-danger); }
.ds-toast.info    { background: var(--color-info); }
.ds-toast-close {
  background: none;
  border: none;
  color: inherit;
  opacity: 0.7;
  cursor: pointer;
  font-size: 16px;
  padding: 0 0 0 var(--space-2);
  line-height: 1;
}
.ds-toast-close:hover { opacity: 1; }

@keyframes ds-toastIn {
  from { opacity: 0; transform: translateX(40px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ── 9. Utility Classes ── */
.text-xs   { font-size: var(--text-xs); }
.text-sm   { font-size: var(--text-sm); }
.text-base { font-size: var(--text-base); }
.text-lg   { font-size: var(--text-lg); }
.text-xl   { font-size: var(--text-xl); }
.text-2xl  { font-size: var(--text-2xl); }

.text-muted   { color: var(--gray-500); }
.text-primary { color: var(--brand-primary); }
.text-danger  { color: var(--accent-red); }
.text-success { color: var(--accent-green); }

.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }

.rounded-sm  { border-radius: var(--radius-sm); }
.rounded-md  { border-radius: var(--radius-md); }
.rounded-lg  { border-radius: var(--radius-lg); }
.rounded-xl  { border-radius: var(--radius-xl); }
.rounded-full { border-radius: var(--radius-full); }

.shadow-sm { box-shadow: var(--shadow-sm); }
.shadow-md { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }

.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }

/* ── 10. Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gray-400); }

/* ── 11. Selection ── */
::selection {
  background: rgba(124, 58, 237, 0.2);
  color: var(--gray-900);
}

/* ── 12. Focus ring ── */
:focus-visible {
  outline: 2px solid var(--accent-purple);
  outline-offset: 2px;
}

/* ============================================================
   13. Dark Mode
   Activated by [data-theme="dark"] on <html>.
   All tokens override automatically.
   ============================================================ */

[data-theme="dark"] {
  /* Brand — slightly brighter for dark bg */
  --brand-gradient: linear-gradient(135deg, #818cf8 0%, #a78bfa 100%);
  --brand-primary: #818cf8;
  --brand-secondary: #a78bfa;

  /* Accent */
  --accent-purple: #a78bfa;
  --accent-purple-hover: #c4b5fd;
  --accent-green: #34d399;
  --accent-green-hover: #6ee7b7;
  --accent-blue: #60a5fa;
  --accent-blue-hover: #93c5fd;
  --accent-amber: #fbbf24;
  --accent-red: #f87171;
  --accent-red-hover: #fca5a5;

  /* Semantic */
  --color-success: #4ade80;
  --color-warning: #fbbf24;
  --color-danger: #f87171;
  --color-info: #60a5fa;

  /* Neutrals — inverted */
  --gray-50:  #1a1a2e;
  --gray-100: #1f2037;
  --gray-200: #2a2b42;
  --gray-300: #3d3e58;
  --gray-400: #6b6d8a;
  --gray-500: #9395b0;
  --gray-600: #b8bad0;
  --gray-700: #d4d5e4;
  --gray-800: #e8e9f0;
  --gray-900: #f4f4f8;

  /* Surfaces */
  --surface-primary: #16172b;
  --surface-secondary: #1a1a2e;
  --surface-elevated: #1f2037;
  --surface-overlay: rgba(0, 0, 0, 0.7);

  /* Borders */
  --border-light: #2a2b42;
  --border-default: #3d3e58;
  --border-focus: #a78bfa;

  /* Shadows — subtler on dark */
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.4);
  --shadow-xl: 0 20px 40px rgba(0, 0, 0, 0.5);

  color-scheme: dark;
}

/* Body background for dark */
[data-theme="dark"] body {
  background: #0f0f1e;
  color: #e8e9f0;
}

/* Force light text in dark mode for common elements */
[data-theme="dark"] h1, [data-theme="dark"] h2, [data-theme="dark"] h3,
[data-theme="dark"] h4, [data-theme="dark"] h5, [data-theme="dark"] h6 {
  color: #f4f4f8;
}
[data-theme="dark"] p, [data-theme="dark"] span, [data-theme="dark"] div,
[data-theme="dark"] li, [data-theme="dark"] td, [data-theme="dark"] th,
[data-theme="dark"] label {
  color: #d4d5e4;
}
[data-theme="dark"] .msg-content, [data-theme="dark"] .msg-sender {
  color: #e8e9f0;
}
[data-theme="dark"] .msg-content strong {
  color: #f4f4f8;
}
[data-theme="dark"] .msg-content code {
  color: #f97583;
  background: #2a2b42;
  border-color: #3d3e58;
}
[data-theme="dark"] .welcome h2 {
  color: #f4f4f8;
}
[data-theme="dark"] .welcome p {
  color: #9395b0;
}
[data-theme="dark"] .prompt-card .prompt-title {
  color: #e8e9f0;
}
[data-theme="dark"] .prompt-card .prompt-sub {
  color: #6b6d8a;
}
[data-theme="dark"] #chat-input {
  color: #e8e9f0;
}
[data-theme="dark"] #chat-input::placeholder {
  color: #6b6d8a;
}
[data-theme="dark"] .topbar h1 {
  color: #e8e9f0;
}
[data-theme="dark"] .conv-item {
  color: #9395b0;
}
[data-theme="dark"] .conv-item:hover, [data-theme="dark"] .conv-item.active {
  color: #e8e9f0;
}
[data-theme="dark"] .input-hint {
  color: #6b6d8a;
}
[data-theme="dark"] .plus-item {
  color: #d4d5e4;
}
[data-theme="dark"] .plus-item .item-hint {
  color: #6b6d8a;
}
[data-theme="dark"] .model-select {
  color: #d4d5e4;
  background: #1f2037;
  border-color: #3d3e58;
}
[data-theme="dark"] .sidebar-top h2 {
  color: #e8e9f0;
}

/* Gradient backgrounds (index, login, etc.) */
[data-theme="dark"] body.has-gradient-bg,
[data-theme="dark"] .page-header {
  background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
}

/* Links */
[data-theme="dark"] a { color: var(--accent-blue); }

/* Inputs */
[data-theme="dark"] .form-input,
[data-theme="dark"] .form-select,
[data-theme="dark"] .form-textarea,
[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea {
  background: var(--surface-secondary);
  color: var(--gray-800);
  border-color: var(--border-light);
}

/* Cards */
[data-theme="dark"] .project-card,
[data-theme="dark"] .ds-card {
  background: var(--surface-elevated);
  border-color: var(--border-light);
}

/* Modals */
[data-theme="dark"] .modal-content,
[data-theme="dark"] .ds-modal {
  background: var(--surface-elevated);
  border-color: var(--border-light);
}

/* Code blocks */
[data-theme="dark"] .code-block-wrapper {
  background: #0d1117;
}
[data-theme="dark"] .code-block-header {
  background: #161b22;
}
[data-theme="dark"] code {
  background: var(--surface-secondary);
  border-color: var(--border-light);
  color: #f97583;
}

/* Chat */
[data-theme="dark"] .chat-dock,
[data-theme="dark"] .chat-dock-body,
[data-theme="dark"] .chat-dock-input {
  background: var(--surface-primary);
}
[data-theme="dark"] .chat-dock-input-row,
[data-theme="dark"] .project-chat-modal .input-container {
  background: var(--surface-secondary);
  border-color: var(--border-light);
}

/* Message wrappers */
[data-theme="dark"] .message-wrapper {
  border-bottom-color: var(--border-light);
}

/* Sidebar */
[data-theme="dark"] .sidebar-panel {
  background: rgba(22, 23, 43, 0.97);
  border-right-color: var(--border-light);
}

/* Search input bar */
[data-theme="dark"] .search-input-container {
  background: rgba(31, 32, 55, 0.95);
  border-color: var(--border-light);
}

/* Navigation section (index page) */
[data-theme="dark"] .navigation-section {
  background: rgba(22, 23, 43, 0.95);
}

/* Tabs */
[data-theme="dark"] .tab-button.active {
  color: var(--accent-purple);
  border-bottom-color: var(--accent-purple);
}

/* Toasts */
[data-theme="dark"] .ds-toast {
  background: var(--surface-elevated);
  color: var(--gray-800);
  border: 1px solid var(--border-light);
}

/* Scrollbar */
[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: var(--gray-300);
}

/* Selection */
[data-theme="dark"] ::selection {
  background: rgba(167, 139, 250, 0.3);
}

/* ── Dark mode toggle button ── */
.theme-toggle {
  position: fixed;
  bottom: 20px;
  left: 20px;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border-light);
  background: var(--surface-elevated);
  color: var(--gray-500);
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  z-index: 999;
  transition: all 0.2s ease;
  opacity: 0.6;
}
.theme-toggle:hover {
  opacity: 1;
  transform: scale(1.08);
  box-shadow: var(--shadow-lg);
  color: var(--accent-amber);
}

/* Hide on mobile — save space */
@media (max-width: 768px) {
  .theme-toggle { display: none; }
}
[data-theme="dark"] .theme-toggle {
  color: var(--accent-amber);
  background: var(--surface-elevated);
  border-color: var(--border-default);
}

/* Smooth transition when toggling */
html.theme-transitioning,
html.theme-transitioning *,
html.theme-transitioning *::before,
html.theme-transitioning *::after {
  transition: background-color 0.3s ease,
              color 0.3s ease,
              border-color 0.3s ease,
              box-shadow 0.3s ease !important;
}
