/* =============================================
   DocPilot - Styles
   ============================================= */

/* CSS Variables */
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-tertiary: #f0f1f3;
  --text-primary: #1a1a2e;
  --text-secondary: #6c757d;
  --text-muted: #adb5bd;
  --border-color: #e2e5e9;
  --accent: #4f6ef7;
  --accent-hover: #3b5de7;
  --accent-light: #eef1ff;
  --success: #10b981;
  --warning: #f59e0b;
  --error: #ef4444;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;
}

[data-theme="dark"] {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #252540;
  --text-primary: #e8e8f0;
  --text-secondary: #9a9ab0;
  --text-muted: #6a6a80;
  --border-color: #2d2d45;
  --accent: #6b8aff;
  --accent-hover: #8ba3ff;
  --accent-light: #1e2240;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  transition: background 0.3s, color 0.3s;
}

/* =============================================
   Header
   ============================================= */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-secondary);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  font-size: 1.3rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.badge {
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
  -webkit-text-fill-color: var(--accent);
}

.header-right {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: none;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--text-secondary);
  transition: all 0.2s;
}

.icon-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

/* =============================================
   Buttons
   ============================================= */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
  font-family: var(--font-sans);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

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

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

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

.btn-sm {
  padding: 6px 12px;
  font-size: 0.8rem;
}

/* =============================================
   Upload Zone
   ============================================= */
.main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

.upload-zone {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
}

.upload-zone-content {
  text-align: center;
  max-width: 500px;
  padding: 60px 40px;
  border: 2px dashed var(--border-color);
  border-radius: var(--radius-lg);
  background: var(--bg-secondary);
  transition: all 0.3s;
  cursor: pointer;
}

.upload-zone-content:hover,
.upload-zone.drag-over .upload-zone-content {
  border-color: var(--accent);
  background: var(--accent-light);
  transform: scale(1.02);
}

.upload-icon {
  font-size: 4rem;
  margin-bottom: 16px;
}

.upload-zone-content h2 {
  font-size: 1.5rem;
  margin-bottom: 8px;
}

.upload-hint {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: 4px;
}

.upload-sub {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 20px;
}

.file-input {
  display: none;
}

.upload-privacy {
  margin-top: 20px;
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* =============================================
   Loading State
   ============================================= */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 40vh;
  gap: 16px;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border-color);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading-detail {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* =============================================
   Error State
   ============================================= */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 40vh;
  gap: 12px;
  text-align: center;
}

.error-icon {
  font-size: 3rem;
}

.error-message {
  color: var(--text-secondary);
  max-width: 400px;
  margin-bottom: 8px;
  white-space: pre-wrap;
}

/* =============================================
   Result View
   ============================================= */
.result-view {
  display: flex;
  flex-direction: column;
  gap: 0;
  height: calc(100vh - 80px);
}

.result-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  flex-shrink: 0;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.file-name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
}

.file-badge {
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  flex-shrink: 0;
}

.file-size, .convert-time {
  color: var(--text-muted);
  font-size: 0.8rem;
  flex-shrink: 0;
}

.toolbar-right {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

/* =============================================
   Split Panel
   ============================================= */
.split-panel {
  display: grid;
  grid-template-columns: 350px 1fr;
  flex: 1;
  border: 1px solid var(--border-color);
  border-top: none;
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  overflow: hidden;
  min-height: 0;
}

.panel-left {
  border-right: 1px solid var(--border-color);
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-right {
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.panel-header h3 {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

/* =============================================
   Info Grid
   ============================================= */
.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.info-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  font-weight: 600;
}

.info-value {
  font-size: 0.9rem;
  font-weight: 500;
  word-break: break-all;
}

.raw-markdown-section h4 {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.raw-preview {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.5;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 12px;
  max-height: 300px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text-primary);
}

/* =============================================
   Markdown Body (rendered)
   ============================================= */
.markdown-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px 32px;
  line-height: 1.7;
  font-size: 0.95rem;
}

.markdown-body h1 { font-size: 1.8rem; margin: 0 0 16px; padding-bottom: 8px; border-bottom: 1px solid var(--border-color); }
.markdown-body h2 { font-size: 1.4rem; margin: 24px 0 12px; }
.markdown-body h3 { font-size: 1.15rem; margin: 20px 0 8px; }
.markdown-body h4 { font-size: 1rem; margin: 16px 0 8px; }
.markdown-body p { margin: 0 0 12px; }
.markdown-body ul, .markdown-body ol { margin: 0 0 12px; padding-left: 24px; }
.markdown-body li { margin: 4px 0; }
.markdown-body blockquote {
  margin: 12px 0;
  padding: 8px 16px;
  border-left: 4px solid var(--accent);
  background: var(--bg-secondary);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
}
.markdown-body code {
  font-family: var(--font-mono);
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.85em;
}
.markdown-body pre {
  margin: 12px 0;
  border-radius: var(--radius-sm);
  overflow-x: auto;
}
.markdown-body pre code {
  background: none;
  padding: 0;
}
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
  font-size: 0.9rem;
}
.markdown-body th, .markdown-body td {
  border: 1px solid var(--border-color);
  padding: 8px 12px;
  text-align: left;
}
.markdown-body th {
  background: var(--bg-secondary);
  font-weight: 600;
}
.markdown-body a {
  color: var(--accent);
  text-decoration: none;
}
.markdown-body a:hover {
  text-decoration: underline;
}
.markdown-body img {
  max-width: 100%;
  border-radius: var(--radius-sm);
}
.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 20px 0;
}

/* =============================================
   Toast
   ============================================= */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  background: var(--text-primary);
  color: var(--bg-primary);
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  transition: all 0.3s;
  max-width: 400px;
}

.toast.hidden {
  display: none !important;
}

.toast.success { background: var(--success); }
.toast.error { background: var(--error); }

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

/* =============================================
   App Layout (Sidebar + Content)
   ============================================= */
.app-layout {
  display: flex;
  height: calc(100vh - 72px);
  gap: 0;
}

/* Sidebar */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-header h3 {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* Search Box */
.search-box {
  position: relative;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
}

.search-input {
  width: 100%;
  padding: 8px 12px 8px 36px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.2s;
}

.search-input:focus {
  border-color: var(--accent);
}

.search-icon {
  position: absolute;
  left: 22px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.9rem;
  color: var(--text-muted);
  pointer-events: none;
}

/* Document List */
.document-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.doc-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  border: 1px solid transparent;
}

.doc-item:hover {
  background: var(--bg-tertiary);
}

.doc-item.active {
  background: var(--accent-light);
  border-color: var(--accent);
}

.doc-icon {
  font-size: 1.3rem;
  flex-shrink: 0;
}

.doc-info {
  flex: 1;
  min-width: 0;
}

.doc-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.doc-delete {
  background: none;
  border: none;
  font-size: 0.9rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0;
  transition: all 0.2s;
}

.doc-item:hover .doc-delete {
  opacity: 1;
}

.doc-delete:hover {
  background: var(--error);
  color: #fff;
}

.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* Content Panel */
.content-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.content-panel > * {
  flex-shrink: 0;
}

.content-panel .upload-zone,
.content-panel .loading-state,
.content-panel .error-state,
.content-panel .result-view {
  flex: 1;
  min-height: 0;
}

.content-panel .result-view {
  height: auto;
}

/* =============================================
   Modal
   ============================================= */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}

.modal-content {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  max-width: 600px;
  width: 100%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
}

.modal-content.modal-large {
  max-width: 800px;
  max-height: 85vh;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.modal-header h3 {
  font-size: 1rem;
  color: var(--text-primary);
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: background 0.2s;
}

.modal-close:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

/* Search Results */
.search-results {
  padding: 16px 20px;
  max-height: 60vh;
  overflow-y: auto;
}

.search-result-item {
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}

.search-result-item:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent);
}

.search-result-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.search-result-snippet {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.5;
}

.search-result-snippet mark {
  background: var(--accent-light);
  color: var(--accent);
  padding: 0 2px;
  border-radius: 2px;
}

.search-result-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 6px;
}

/* =============================================
   AI Chat
   ============================================= */
.ai-chat {
  display: flex;
  flex-direction: column;
  height: 100%;
}

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

.ai-message {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 0.85rem;
  line-height: 1.5;
  word-wrap: break-word;
}

.ai-message.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.ai-message.assistant {
  align-self: flex-start;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.ai-message.assistant pre {
  font-size: 0.75rem;
  max-width: 100%;
  overflow-x: auto;
}

.ai-message.assistant code {
  font-size: 0.78em;
}

.ai-input-area {
  display: flex;
  gap: 8px;
  padding: 16px 20px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-secondary);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.ai-input {
  flex: 1;
  resize: none;
  min-height: 48px;
  max-height: 120px;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-family: var(--font-sans);
  outline: none;
}

.ai-input:focus {
  border-color: var(--accent);
}

/* =============================================
   Responsive
   ============================================= */
@media (max-width: 900px) {
  .split-panel {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .panel-left {
    border-right: none;
    border-bottom: 1px solid var(--border-color);
    max-height: 40vh;
  }

  .result-toolbar {
    flex-wrap: wrap;
    gap: 8px;
  }

  .toolbar-left {
    min-width: 100%;
  }

  .file-name {
    max-width: 150px;
  }
}

@media (max-width: 600px) {
  .main {
    padding: 12px;
  }

  .upload-zone-content {
    padding: 40px 20px;
  }

  .markdown-body {
    padding: 16px;
  }
}