/**
 * Knowledge Canvas - Application Stylesheet
 * Reusable CSS classes and design tokens for consistent UI implementation
 * 
 * Usage: Include this file in your HTML:
 * <link rel="stylesheet" href="app.css">
 */

/* ========================================
   CSS CUSTOM PROPERTIES (Design Tokens)
   ======================================== */

:root {
  /* Primary Colors */
  --color-primary: #4f46e5;
  --color-primary-hover: #4338ca;
  --color-primary-dark: #312e81;
  --color-primary-light: rgba(79, 70, 229, 0.12);
  --color-primary-border: rgba(79, 70, 229, 0.18);
  --color-primary-border-strong: rgba(79, 70, 229, 0.35);

  /* Neutral Grays */
  --color-bg-canvas: #f8f9fa;
  --color-bg-white: #ffffff;
  --color-bg-secondary: #fafafa;
  --color-bg-input: rgba(248, 250, 252, 0.7);
  --color-bg-input-solid: rgba(248, 250, 252, 0.94);
  
  --color-border: #e1e5e9;
  --color-border-light: #e5e7eb;
  --color-border-lighter: #ddd;
  --color-separator: #eee;
  
  --color-text-primary: #333;
  --color-text-secondary: #4b5563;
  --color-text-tertiary: #555;
  --color-text-muted: #6b7280;
  --color-text-disabled: #9ca3af;
  --color-text-subtle: #64748b;

  /* Semantic Colors */
  --color-success: #10b981;
  --color-success-dark: #065f46;
  --color-success-light: #d1fae5;
  
  --color-warning: #f59e0b;
  --color-warning-alt: #fbbf24;
  --color-warning-dark: #92400e;
  --color-warning-light: #fef3c7;
  
  --color-error: #dc2626;
  --color-error-alt: #ef4444;
  --color-error-dark: #991b1b;
  --color-error-light: #fee2e2;
  
  --color-info: #3b82f6;

  /* Card Colors */
  --color-card-yellow: #fef3c7;
  --color-card-blue: #dbeafe;
  --color-card-green: #d1fae5;
  --color-card-pink: #fce7f3;
  --color-card-purple: #e9d5ff;

  /* Chat Bubble Colors */
  --color-chat-user: #dcf8c6;
  --color-chat-assistant: #f1f0f0;

  /* Spacing Scale */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 20px;
  --space-2xl: 32px;
  --space-3xl: 40px;

  /* Border Radius */
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;
  --radius-xl: 12px;
  --radius-pill: 999px;
  --radius-circle: 50%;

  /* Shadows */
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.1);
  --shadow-sm: 0 2px 6px rgba(0, 0, 0, 0.15);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 8px 24px rgba(0, 0, 0, 0.15);
  --shadow-2xl: 0 14px 24px rgba(15, 23, 42, 0.18);
  --shadow-primary: 0 4px 16px rgba(79, 70, 229, 0.2);
  --shadow-focus: 0 0 0 2px rgba(99, 102, 241, 0.18);
  --shadow-inset: inset 0 1px 2px rgba(15, 23, 42, 0.05);

  /* Transitions */
  --transition-fast: 0.12s ease;
  --transition-base: 0.2s ease;
  --transition-smooth: 0.3s ease-in-out;

  /* Z-Index Scale */
  --z-canvas: -1;
  --z-card: 1;
  --z-card-dragging: 1000;
  --z-toolbar: 1001;
  --z-panel: 1001;
  --z-overlay: 1002;
  --z-dropdown: 4000;
  --z-modal: 10000;

  /* Typography */
  --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-size-xs: 0.68rem;
  --font-size-sm: 0.75rem;
  --font-size-base: 0.9rem;
  --font-size-md: 1rem;
  --font-size-lg: 1.1rem;
  --font-size-xl: 1.2rem;
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
}

/* ========================================
   BUTTON COMPONENTS
   ======================================== */

.btn {
  padding: 8px 16px;
  border-radius: var(--radius-lg);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  cursor: pointer;
  border: none;
  transition: all var(--transition-base);
  font-family: var(--font-family);
  line-height: 1.5;
}

.btn-primary {
  background: var(--color-primary);
  color: white;
}

.btn-primary:hover {
  background: var(--color-primary-hover);
}

.btn-primary:focus {
  outline: none;
  box-shadow: var(--shadow-focus);
}

.btn-secondary {
  background: #f3f4f6;
  color: #374151;
  border: 1px solid #d1d5db;
}

.btn-secondary:hover {
  background: #e5e7eb;
}

.btn-icon {
  background: none;
  border: none;
  padding: 4px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-base);
}

.btn-icon:hover {
  background: #f3f4f6;
}

.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  transition: background var(--transition-base);
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-close:hover {
  background: #f3f4f6;
  color: var(--color-text-primary);
}

.btn-sm {
  padding: 4px 12px;
  font-size: var(--font-size-sm);
}

.btn-lg {
  padding: 12px 24px;
  font-size: var(--font-size-md);
}

/* ========================================
   CARD COMPONENTS
   ======================================== */

.card {
  background: var(--color-bg-white);
  border: 2px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: var(--space-lg);
  box-shadow: var(--shadow-md);
  transition: border-color var(--transition-base), box-shadow var(--transition-base);
}

.card:hover {
  border-color: var(--color-primary);
  box-shadow: var(--shadow-primary);
}

.card-list {
  background: white;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  margin-bottom: var(--space-sm);
  border-left: 4px solid var(--color-primary);
  transition: all var(--transition-base);
}

.card-list:hover {
  box-shadow: var(--shadow-md);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-sm);
}

.card-title {
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  margin: 0;
}

.card-body {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  line-height: 1.4;
}

/* ========================================
   INPUT COMPONENTS
   ======================================== */

.input,
.input-text {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--color-border-lighter);
  border-radius: var(--radius-sm);
  font-size: var(--font-size-base);
  font-family: var(--font-family);
  transition: border-color var(--transition-base), box-shadow var(--transition-base);
  background: var(--color-bg-white);
}

.input:focus,
.input-text:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: var(--shadow-focus);
}

.input-chat {
  border: none;
  border-radius: var(--radius-xl);
  padding: 12px 16px;
  background: var(--color-bg-input);
  color: #334155;
  font-size: 0.95rem;
  transition: background var(--transition-base), box-shadow var(--transition-base);
}

.input-chat:focus {
  background: var(--color-bg-white);
  box-shadow: var(--shadow-focus);
}

.textarea {
  width: 100%;
  border: none;
  outline: none;
  resize: vertical;
  font-family: var(--font-family);
  font-size: 14px;
  background: transparent;
  min-height: 80px;
}

.form-group {
  margin-bottom: var(--space-lg);
}

.form-label {
  display: block;
  margin-bottom: var(--space-sm);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
  font-size: var(--font-size-base);
}

/* ========================================
   BADGE & TAG COMPONENTS
   ======================================== */

.badge {
  display: inline-block;
  background: #f3f4f6;
  color: #374151;
  padding: 2px 8px;
  border-radius: var(--radius-xl);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  white-space: nowrap;
}

.badge-success {
  background: var(--color-success-light);
  color: var(--color-success-dark);
}

.badge-warning {
  background: var(--color-warning-light);
  color: var(--color-warning-dark);
}

.badge-error {
  background: var(--color-error-light);
  color: var(--color-error-dark);
}

.badge-pill {
  border-radius: var(--radius-pill);
}

.tag {
  display: inline-block;
  background: var(--color-border);
  color: var(--color-text-tertiary);
  padding: 2px 6px;
  border-radius: var(--radius-xl);
  font-size: var(--font-size-xs);
  margin-right: 4px;
  margin-bottom: 2px;
}

/* ========================================
   MODAL COMPONENTS
   ======================================== */

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal);
}

.modal-content {
  background: white;
  border-radius: var(--radius-xl);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  max-width: 600px;
  max-height: 90vh;
  width: 90%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  padding: 20px;
  border-bottom: 1px solid var(--color-border-light);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--color-bg-canvas);
}

.modal-title {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--color-border-light);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-md);
  background: var(--color-bg-canvas);
}

/* ========================================
   PANEL COMPONENTS
   ======================================== */

.panel {
  position: fixed;
  background: white;
  border: 1px solid var(--color-border-lighter);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: var(--z-panel);
}

.panel-header {
  padding: var(--space-lg);
  border-bottom: 1px solid var(--color-border-light);
  background: var(--color-bg-canvas);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.panel-title {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
}

.panel-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-sm);
}

/* ========================================
   DROPDOWN COMPONENTS
   ======================================== */

.dropdown-menu {
  position: absolute;
  padding: 4px;
  border-radius: var(--radius-xl);
  border: 1px solid var(--color-primary-border);
  background: var(--color-bg-white);
  box-shadow: var(--shadow-2xl);
  min-width: 150px;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity var(--transition-fast), transform var(--transition-fast);
  z-index: var(--z-dropdown);
}

.dropdown-menu.visible {
  opacity: 1;
  transform: translateY(0);
}

.dropdown-item {
  padding: 6px 8px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.16s ease, border-color 0.16s ease;
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
}

.dropdown-item:hover {
  background: rgba(99, 102, 241, 0.08);
  border-color: rgba(79, 70, 229, 0.24);
}

.dropdown-item.selected {
  background: rgba(99, 102, 241, 0.12);
  border-color: var(--color-primary-border-strong);
}

/* ========================================
   CHAT BUBBLE COMPONENTS
   ======================================== */

.chat-bubble {
  padding: var(--space-sm);
  border-radius: var(--radius-xl);
  margin: 4px 0;
  max-width: 90%;
  line-height: 1.4;
}

.chat-bubble-user {
  background: var(--color-chat-user);
  align-self: flex-end;
}

.chat-bubble-assistant {
  background: var(--color-chat-assistant);
  align-self: flex-start;
}

.chat-bubble-pending {
  opacity: 0.7;
  font-style: italic;
}

/* Prevent code blocks from overflowing chat bubbles */
.card .chat-bubble pre {
  background: #f6f8fa;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
  max-width: 100%;
  box-sizing: border-box;
  white-space: pre;
  margin: 0 0 0.75em;
}

.card .chat-bubble code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.9em;
  word-break: break-word;
}

/* ========================================
   LOADING STATES
   ======================================== */

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f4f6;
  border-top: 3px solid var(--color-primary);
  border-radius: var(--radius-circle);
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-3xl) var(--space-xl);
  text-align: center;
}

.loading-text {
  margin-top: var(--space-lg);
  color: var(--color-text-muted);
}

/* ========================================
   EMPTY & ERROR STATES
   ======================================== */

.empty-state {
  text-align: center;
  padding: var(--space-3xl) var(--space-xl);
  color: var(--color-text-muted);
}

.empty-state-icon {
  font-size: 48px;
  margin-bottom: var(--space-lg);
  opacity: 0.5;
}

.empty-state-text {
  font-size: var(--font-size-base);
  margin-bottom: var(--space-sm);
}

.empty-state-hint {
  font-size: var(--font-size-sm);
  color: var(--color-text-disabled);
}

.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-3xl) var(--space-xl);
  text-align: center;
  color: var(--color-error);
}

.error-message {
  font-size: var(--font-size-base);
  margin-top: var(--space-sm);
}

/* ========================================
   UTILITY CLASSES - LAYOUT
   ======================================== */

.flex {
  display: flex;
}

.flex-col {
  display: flex;
  flex-direction: column;
}

.flex-row {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-wrap {
  flex-wrap: wrap;
}

.flex-1 {
  flex: 1;
}

/* ========================================
   UTILITY CLASSES - SPACING
   ======================================== */

.gap-xs { gap: var(--space-xs); }
.gap-sm { gap: var(--space-sm); }
.gap-md { gap: var(--space-md); }
.gap-lg { gap: var(--space-lg); }
.gap-xl { gap: var(--space-xl); }

.p-xs { padding: var(--space-xs); }
.p-sm { padding: var(--space-sm); }
.p-md { padding: var(--space-md); }
.p-lg { padding: var(--space-lg); }
.p-xl { padding: var(--space-xl); }

.m-xs { margin: var(--space-xs); }
.m-sm { margin: var(--space-sm); }
.m-md { margin: var(--space-md); }
.m-lg { margin: var(--space-lg); }
.m-xl { margin: var(--space-xl); }

.mb-xs { margin-bottom: var(--space-xs); }
.mb-sm { margin-bottom: var(--space-sm); }
.mb-md { margin-bottom: var(--space-md); }
.mb-lg { margin-bottom: var(--space-lg); }
.mb-xl { margin-bottom: var(--space-xl); }

.mt-xs { margin-top: var(--space-xs); }
.mt-sm { margin-top: var(--space-sm); }
.mt-md { margin-top: var(--space-md); }
.mt-lg { margin-top: var(--space-lg); }
.mt-xl { margin-top: var(--space-xl); }

/* ========================================
   UTILITY CLASSES - TEXT
   ======================================== */

.text-xs { font-size: var(--font-size-xs); }
.text-sm { font-size: var(--font-size-sm); }
.text-base { font-size: var(--font-size-base); }
.text-md { font-size: var(--font-size-md); }
.text-lg { font-size: var(--font-size-lg); }
.text-xl { font-size: var(--font-size-xl); }

.text-primary { color: var(--color-text-primary); }
.text-secondary { color: var(--color-text-secondary); }
.text-muted { color: var(--color-text-muted); }
.text-disabled { color: var(--color-text-disabled); }

.text-success { color: var(--color-success); }
.text-warning { color: var(--color-warning); }
.text-error { color: var(--color-error); }

.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

.font-normal { font-weight: var(--font-weight-normal); }
.font-medium { font-weight: var(--font-weight-medium); }
.font-semibold { font-weight: var(--font-weight-semibold); }

.uppercase {
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ========================================
   UTILITY CLASSES - DISPLAY & VISIBILITY
   ======================================== */

.hidden { display: none; }
.visible { display: block; }
.invisible { visibility: hidden; }

.w-full { width: 100%; }
.h-full { height: 100%; }

/* ========================================
   UTILITY CLASSES - BORDERS & RADIUS
   ======================================== */

.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-pill { border-radius: var(--radius-pill); }
.rounded-circle { border-radius: var(--radius-circle); }

.border { border: 1px solid var(--color-border); }
.border-light { border: 1px solid var(--color-border-light); }
.border-primary { border: 1px solid var(--color-primary); }

.border-left-primary {
  border-left: 4px solid var(--color-primary);
}

.border-left-success {
  border-left: 4px solid var(--color-success);
}

.border-left-warning {
  border-left: 4px solid var(--color-warning);
}

.border-left-error {
  border-left: 4px solid var(--color-error);
}

/* ========================================
   UTILITY CLASSES - SHADOWS
   ======================================== */

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

/* ========================================
   UTILITY CLASSES - BACKGROUNDS
   ======================================== */

.bg-white { background: var(--color-bg-white); }
.bg-canvas { background: var(--color-bg-canvas); }
.bg-secondary { background: var(--color-bg-secondary); }

.bg-success { background: var(--color-success-light); }
.bg-warning { background: var(--color-warning-light); }
.bg-error { background: var(--color-error-light); }

/* ========================================
   SCROLLBAR STYLING
   ======================================== */

.scrollable {
  overflow-y: auto;
}

.scrollable::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.scrollable::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.4);
  border-radius: var(--radius-pill);
}

.scrollable::-webkit-scrollbar-thumb:hover {
  background: rgba(148, 163, 184, 0.6);
}

.scrollable::-webkit-scrollbar-track {
  background: transparent;
}

/* ========================================
   RESPONSIVE UTILITIES
   ======================================== */

@media (max-width: 768px) {
  .panel {
    right: 10px;
    left: 10px;
    width: auto;
  }
  
  .modal-content {
    margin: 10px;
    max-width: none;
    width: auto;
  }
  
  .hide-mobile {
    display: none;
  }
}

@media (min-width: 769px) {
  .show-mobile {
    display: none;
  }
}

/* ========================================
   ACCESSIBILITY
   ======================================== */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* Focus visible for keyboard navigation */
*:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* ========================================
   TRANSITIONS & ANIMATIONS
   ======================================== */

.transition-fast {
  transition: all var(--transition-fast);
}

.transition-base {
  transition: all var(--transition-base);
}

.transition-smooth {
  transition: all var(--transition-smooth);
}

.fade-in {
  animation: fadeIn 0.3s ease;
}

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

.slide-in {
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}
