* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: #f4f6f8; color: #1e293b; min-height: 100vh; }
h1, h2, h3 { margin: 0 0 0.5rem 0; }

body { display: flex; flex-direction: column; min-height: 100vh; }
#app { display: flex; flex-direction: column; min-height: 100vh; flex: 1; }
main { flex: 1; padding: 1.5rem; max-width: 1450px; width: 100%; margin: 0 auto; }

/* TOPBAR */
.topbar {
  display: flex; align-items: center; gap: 2rem;
  padding: 0.85rem 1.5rem; background: #1e293b; color: #f1f5f9;
}
.brand { display: flex; align-items: center; gap: 0.65rem; flex: 1; min-width: 0; }
.brand-logo { width: 32px; height: 32px; display: block; }
.topbar h1 { font-size: 1.25rem; letter-spacing: 0.02em; margin: 0; font-weight: 600; }
.user-area { display: flex; justify-content: flex-end; min-width: 0; }

/* USER AREA */
.user-area { position: relative; }
.user-area { /* flex sizing applied above for centered tabs */ }
.user-trigger {
  display: flex; align-items: center; gap: 0.5rem;
  background: transparent; color: #f1f5f9; border: 1px solid #334155;
  padding: 0.4rem 0.75rem; border-radius: 999px; cursor: pointer; font-size: 0.85rem;
}
.user-trigger:hover { background: #334155; }
.user-avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: #2563eb; color: #fff; display: inline-flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 0.8rem; text-transform: uppercase;
}
.user-email { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caret { font-size: 0.7rem; opacity: 0.7; }

/* POPOVERS */
.popover {
  position: absolute; background: #fff; border: 1px solid #e2e8f0; border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.12); padding: 0.25rem; z-index: 900; min-width: 180px;
}
.popover[hidden] { display: none; }
.user-area .popover { right: 0; top: calc(100% + 0.5rem); }
.popover-item {
  display: block; width: 100%; text-align: left; background: transparent; border: none;
  padding: 0.55rem 0.75rem; cursor: pointer; font-size: 0.9rem; color: #1e293b; border-radius: 4px;
}
.popover-item:hover { background: #f1f5f9; }
.popover-item.danger { color: #b91c1c; }
.popover-divider { height: 1px; background: #e2e8f0; margin: 0.25rem 0; }
.row-popover { position: fixed; }

/* PAGE HEADER */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap;
}
.page-header h2 { margin: 0; font-size: 1.2rem; }
.page-actions { display: flex; gap: 0.5rem; }

/* BUTTONS */
.btn {
  display: inline-block; background: #2563eb; color: #fff;
  padding: 0.5rem 1rem; border: none; border-radius: 6px;
  cursor: pointer; font-weight: 500; font-size: 0.9rem;
}
.btn:hover:not(:disabled) { background: #1d4ed8; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; border: none; color: #64748b; font-size: 1.5rem; cursor: pointer; line-height: 1; padding: 0.25rem 0.5rem; }
.btn-ghost:hover { color: #1e293b; }
.btn-sm { padding: 0.25rem 0.6rem; font-size: 0.8rem; }
.btn-secondary { background: #e2e8f0; color: #1e293b; }
.btn-secondary:hover:not(:disabled) { background: #cbd5e1; }
.btn-danger { background: #dc2626; }
.btn-danger:hover { background: #b91c1c; }

/* UPLOAD (embedded in sidebar) */
.dropzone {
  border: 2px dashed #cbd5e1; border-radius: 8px;
  padding: 3rem 1rem; text-align: center; margin-top: 1rem;
  transition: all 0.15s;
}
.dropzone.drag-over { border-color: #2563eb; background: #eff6ff; }
.hint { color: #64748b; font-size: 0.9rem; }
.status { margin-top: 1rem; padding: 0.75rem; border-radius: 6px; font-size: 0.9rem; }
.status.info { background: #dbeafe; color: #1e40af; }
.status.success { background: #dcfce7; color: #166534; }
.status.error { background: #fee2e2; color: #991b1b; }

/* Upload multi-file status */
.upload-status { margin-top: 1rem; background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 6px; padding: 0.75rem; }
.upload-status[hidden] { display: none; }
.upload-status-header { font-size: 0.9rem; font-weight: 500; color: #1e293b; margin-bottom: 0.5rem; }
.upload-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.35rem; }
.upload-item {
  display: grid;
  grid-template-columns: 24px 1fr auto auto;
  gap: 0.6rem; align-items: center;
  padding: 0.4rem 0.6rem; border-radius: 4px; background: #fff; border: 1px solid #e2e8f0;
  font-size: 0.85rem;
}
.upload-item-icon { font-weight: 700; text-align: center; }
.upload-item-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-item-size { font-family: ui-monospace, "SF Mono", monospace; color: #64748b; font-size: 0.75rem; }
.upload-item-msg { font-size: 0.75rem; color: #475569; font-family: ui-monospace, "SF Mono", monospace; }
.upload-item--queued    { opacity: 0.7; }
.upload-item--uploading { border-color: #93c5fd; background: #eff6ff; }
.upload-item--ok        { border-color: #86efac; background: #f0fdf4; }
.upload-item--ok .upload-item-icon    { color: #16a34a; }
.upload-item--error     { border-color: #fca5a5; background: #fef2f2; }
.upload-item--error .upload-item-icon { color: #dc2626; }
.upload-item--error .upload-item-msg  { color: #991b1b; }

/* DOCUMENTS TABLE */
.filters {
  display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap;
  background: #fff; padding: 0.75rem; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.filters input, .filters select {
  padding: 0.4rem 0.6rem; border: 1px solid #cbd5e1; border-radius: 4px; font-size: 0.9rem;
}
.filters input { flex: 1; min-width: 180px; }

.table-wrapper { background: #fff; border-radius: 8px; overflow: auto; box-shadow: 0 1px 3px rgba(0,0,0,0.06); }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { padding: 0.6rem 0.75rem; text-align: left; border-bottom: 1px solid #e2e8f0; white-space: nowrap; }
th { background: #f1f5f9; font-weight: 600; color: #475569; text-transform: uppercase; font-size: 0.7rem; letter-spacing: 0.05em; }
th.th-actions { width: 40px; }
tbody tr:hover { background: #f8fafc; }
.empty { padding: 2rem; text-align: center; color: #94a3b8; }

/* PAGINATION */
.pagination {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 0.85rem; border-top: 1px solid #e2e8f0; background: #fafbfc;
  font-size: 0.82rem; color: #475569; flex-wrap: wrap; gap: 0.5rem;
}
.pagination[hidden] { display: none; }
.page-info { font-family: ui-monospace, "SF Mono", monospace; }
.page-controls { display: flex; align-items: center; gap: 0.3rem; }
.page-controls label { display: inline-flex; align-items: center; gap: 0.35rem; margin-right: 0.6rem; }
.page-controls select { padding: 0.2rem 0.4rem; border: 1px solid #cbd5e1; border-radius: 4px; font-size: 0.8rem; }
.page-controls .btn-sm { padding: 0.15rem 0.55rem; min-width: 28px; font-weight: 600; }
.page-current {
  min-width: 72px; text-align: center; font-family: ui-monospace, monospace;
  padding: 0 0.4rem; color: #1e293b;
}
.kebab-btn {
  background: transparent; border: 1px solid transparent; color: #475569;
  width: 30px; height: 30px; border-radius: 6px; cursor: pointer; font-size: 1.2rem; line-height: 1;
}
.kebab-btn:hover { background: #f1f5f9; border-color: #e2e8f0; }

.badge {
  display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px;
  font-size: 0.75rem; font-weight: 500;
}
.badge-pending   { background: #f1f5f9; color: #475569; }
.badge-analyzing { background: #fef9c3; color: #854d0e; }
.badge-converting{ background: #fde68a; color: #78350f; }
.badge-extracting{ background: #bfdbfe; color: #1e3a8a; }
.badge-ready     { background: #dcfce7; color: #166534; }
.badge-failed    { background: #fee2e2; color: #991b1b; }
.badge-digital   { background: #e0f2fe; color: #075985; }
.badge-scanned   { background: #fde68a; color: #78350f; }
.badge-hybrid    { background: #ddd6fe; color: #5b21b6; }
.badge-howto-llm { background: #dcfce7; color: #166534; }
.badge-howto-md  { background: #e0e7ff; color: #3730a3; }

/* SIDEBAR */
.sidebar-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,0.35); z-index: 950;
}
.sidebar-backdrop[hidden] { display: none; }
.sidebar {
  position: fixed; top: 0; right: 0; height: 100vh; background: #fff;
  border-left: 1px solid #e2e8f0; box-shadow: -8px 0 24px rgba(15,23,42,0.12);
  z-index: 960; display: flex; flex-direction: column;
  transition: width 0.18s ease;
}
.sidebar[hidden] { display: none; }
.sidebar[data-size="small"]   { width: 380px; }
.sidebar[data-size="medium"]  { width: 520px; }
.sidebar[data-size="wide"]    { width: 720px; }
.sidebar[data-size="xlarge"]  { width: min(1000px, 95vw); }
.sidebar-header {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.85rem 1.25rem; border-bottom: 1px solid #e2e8f0;
}
.sidebar-header h3 { flex: 1; margin: 0; font-size: 1rem; }
.sidebar-actions { display: flex; gap: 0.4rem; }
.sidebar-body { flex: 1; overflow: auto; padding: 1rem 1.25rem; }

/* SIDEBAR — DETAIL VIEW */
.detail-section { margin-bottom: 1.5rem; }
.detail-section h4 { font-size: 0.85rem; text-transform: uppercase; color: #475569; margin: 0 0 0.5rem 0; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 0.5rem 1rem; font-size: 0.85rem; }
.detail-grid .k { color: #64748b; }
.detail-grid .v { font-family: ui-monospace, "SF Mono", monospace; word-break: break-word; }

/* SIDEBAR — METRICS */
.metric-row { display: flex; justify-content: space-between; padding: 0.4rem 0; border-bottom: 1px solid #f1f5f9; font-size: 0.9rem; }
.metric-label { color: #64748b; }
.metric-value { font-family: ui-monospace, "SF Mono", monospace; }
.metric-bar { background: #e2e8f0; border-radius: 4px; height: 6px; overflow: hidden; margin-top: 0.25rem; }
.metric-bar-fill { background: #2563eb; height: 100%; }

/* SIDEBAR — CODE VIEWERS (JSON / MD) */
.code-viewer {
  background: #0f172a; color: #e2e8f0; border-radius: 6px;
  padding: 0.75rem; font-family: ui-monospace, "SF Mono", monospace; font-size: 0.78rem;
  white-space: pre-wrap; word-break: break-word; overflow: auto;
  line-height: 1.45; margin: 0;
}
.viewer-toolbar { display: flex; gap: 0.5rem; margin-bottom: 0.75rem; flex-wrap: wrap; }

/* Markdown rendered preview */
.md-preview {
  font-size: 0.92rem; line-height: 1.55; color: #1e293b;
  padding: 0.25rem 0.25rem 1rem;
}
.md-preview h1 { font-size: 1.4rem; margin: 1.2rem 0 0.5rem; padding-bottom: 0.3rem; border-bottom: 1px solid #e2e8f0; }
.md-preview h2 { font-size: 1.18rem; margin: 1.1rem 0 0.5rem; padding-bottom: 0.25rem; border-bottom: 1px solid #f1f5f9; }
.md-preview h3 { font-size: 1.05rem; margin: 1rem 0 0.4rem; }
.md-preview h4 { font-size: 0.95rem; margin: 0.85rem 0 0.3rem; color: #475569; }
.md-preview h5, .md-preview h6 { font-size: 0.9rem; margin: 0.75rem 0 0.25rem; color: #64748b; }
.md-preview h1:first-child, .md-preview h2:first-child, .md-preview h3:first-child { margin-top: 0; }
.md-preview p { margin: 0.5rem 0; }
.md-preview ul, .md-preview ol { margin: 0.5rem 0; padding-left: 1.4rem; }
.md-preview li { margin: 0.2rem 0; }
.md-preview li > p { margin: 0.2rem 0; }
.md-preview code {
  background: #f1f5f9; padding: 0.08rem 0.32rem; border-radius: 3px;
  font-family: ui-monospace, "SF Mono", monospace; font-size: 0.85em;
  color: #0f172a;
}
.md-preview pre {
  background: #0f172a; color: #e2e8f0; padding: 0.65rem 0.85rem;
  border-radius: 5px; overflow: auto; font-size: 0.78rem; line-height: 1.5;
  margin: 0.6rem 0;
}
.md-preview pre code { background: transparent; color: inherit; padding: 0; font-size: inherit; }
.md-preview table {
  border-collapse: collapse; width: 100%; margin: 0.6rem 0;
  font-size: 0.82rem; display: block; overflow-x: auto;
}
.md-preview th, .md-preview td {
  border: 1px solid #e2e8f0; padding: 0.4rem 0.6rem; text-align: left; vertical-align: top;
}
.md-preview th { background: #f1f5f9; font-weight: 600; }
.md-preview blockquote {
  border-left: 3px solid #cbd5e1; padding: 0.1rem 0.75rem;
  margin: 0.5rem 0; color: #64748b; background: #f8fafc; border-radius: 0 4px 4px 0;
}
.md-preview hr { border: none; border-top: 1px solid #e2e8f0; margin: 1rem 0; }
.md-preview a { color: #2563eb; text-decoration: none; }
.md-preview a:hover { text-decoration: underline; }
.md-preview img { max-width: 100%; height: auto; border-radius: 4px; }
.md-preview strong { color: #0f172a; }

/* SIDEBAR — TABLES */
.tables-list { display: flex; flex-direction: column; gap: 1rem; }
.table-block { border: 1px solid #e2e8f0; border-radius: 6px; background: #fff; overflow: hidden; }
.table-block-header { padding: 0.6rem 0.8rem; background: #f8fafc; border-bottom: 1px solid #e2e8f0; }
.table-block-header h4 { margin: 0; font-size: 0.95rem; color: #0f172a; }
.table-block-header .hint { margin: 0.25rem 0 0 0; font-size: 0.8rem; }
.table-block-meta { font-size: 0.72rem; color: #64748b; margin-top: 0.3rem; font-family: ui-monospace, "SF Mono", monospace; }
.data-table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.data-table th, .data-table td { border-bottom: 1px solid #f1f5f9; padding: 0.4rem 0.6rem; text-align: left; vertical-align: top; }
.data-table th { background: #f1f5f9; font-weight: 600; font-size: 0.7rem; text-transform: uppercase; color: #475569; letter-spacing: 0.04em; position: sticky; top: 0; }
.data-table tbody tr:nth-child(odd) { background: #fdfdfe; }
.data-table tbody tr:hover { background: #f1f5f9; }
.data-table td.num { text-align: right; font-family: ui-monospace, "SF Mono", monospace; }

/* SIDEBAR — IMAGE GALLERY */
.image-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.75rem; }
.image-card {
  background: #f8fafc; border-radius: 6px; overflow: hidden;
  border: 1px solid #e2e8f0; cursor: pointer; transition: transform 0.12s;
  display: flex; flex-direction: column;
}
.image-card:hover { transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.08); }
.image-card img { width: 100%; aspect-ratio: 4/3; object-fit: contain; background: #fff; display: block; }
.image-card .meta { padding: 0.5rem; font-size: 0.75rem; color: #475569; }
.image-card .meta .kind { display: inline-block; padding: 0.1rem 0.4rem; background: #e0f2fe; border-radius: 999px; font-size: 0.7rem; margin-right: 0.25rem; color: #075985; }
.image-card .meta .desc { color: #334155; margin-top: 0.25rem; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* SIDEBAR — GRAPH */
.graph-container { display: flex; flex-direction: column; height: 100%; gap: 0.75rem; }
.graph-toolbar { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.graph-canvas { flex: 1; background: #fff; border: 1px solid #e2e8f0; border-radius: 6px; min-height: 300px; }
.graph-types { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.graph-types label { font-size: 0.78rem; padding: 0.15rem 0.5rem; background: #f1f5f9; border-radius: 999px; cursor: pointer; }
.graph-details { background: #f8fafc; border-radius: 4px; padding: 0.5rem; font-size: 0.78rem; max-height: 180px; overflow: auto; font-family: ui-monospace, monospace; white-space: pre-wrap; }

/* SIDEBAR — FILE PREVIEW */
.file-preview iframe { width: 100%; height: 60vh; border: 1px solid #e2e8f0; border-radius: 6px; }
.file-preview img { max-width: 100%; max-height: 60vh; display: block; margin: 0 auto; border-radius: 6px; background: #f8fafc; }
.file-preview .placeholder { padding: 2rem; text-align: center; background: #f8fafc; border-radius: 6px; color: #64748b; }

/* API KEYS */
.api-key-create { display: flex; gap: 0.5rem; margin: 0.75rem 0; }
.api-key-create input { flex: 1; padding: 0.4rem 0.6rem; border: 1px solid #cbd5e1; border-radius: 4px; font-size: 0.9rem; }
.api-key-create input:focus { outline: 2px solid #2563eb; outline-offset: -1px; border-color: #2563eb; }
.api-key-fresh { margin: 0.75rem 0; }
.api-key-fresh[hidden] { display: none; }
.api-key-secret {
  display: flex; gap: 0.5rem; align-items: center;
  background: #0f172a; color: #f1f5f9; padding: 0.5rem 0.75rem; border-radius: 4px;
  margin-top: 0.3rem;
}
.api-key-secret code {
  flex: 1; font-family: ui-monospace, "SF Mono", monospace; font-size: 0.8rem;
  word-break: break-all;
}
.api-key-table { width: 100%; font-size: 0.82rem; border-collapse: collapse; margin-top: 0.5rem; }
.api-key-table th, .api-key-table td { padding: 0.45rem 0.5rem; text-align: left; border-bottom: 1px solid #e2e8f0; }
.api-key-table th { background: #f1f5f9; font-weight: 600; text-transform: uppercase; font-size: 0.7rem; color: #475569; }
.api-key-row.revoked { opacity: 0.55; }
.api-key-row code { font-family: ui-monospace, monospace; font-size: 0.78rem; }

/* MODALS */
.modal { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.6); }
.modal-panel {
  position: relative; background: #fff; border-radius: 8px;
  width: min(440px, 90vw); max-height: 85vh; overflow: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.modal-panel-wide { width: min(960px, 95vw); }
.modal-panel header { display: flex; justify-content: space-between; align-items: center; padding: 1rem 1.25rem; border-bottom: 1px solid #e2e8f0; }

/* IMAGE VIEWER */
.image-body { padding: 1rem 1.25rem; }
.image-body img { max-width: 100%; max-height: 60vh; display: block; margin: 0 auto; background: #f8fafc; border-radius: 4px; }
.image-body .image-meta { margin-top: 1rem; font-size: 0.85rem; }
.image-body .image-meta .row { display: flex; gap: 0.5rem; padding: 0.25rem 0; border-bottom: 1px solid #f1f5f9; }
.image-body .image-meta .row .k { color: #64748b; min-width: 130px; }
.image-body .image-meta .row .v { font-family: ui-monospace, monospace; }

/* AUTH OVERLAY */
.auth-overlay {
  position: fixed; inset: 0; background: linear-gradient(135deg, #1e293b, #0f172a);
  display: flex; align-items: center; justify-content: center; z-index: 2000; padding: 1rem;
}
.auth-overlay[hidden] { display: none; }
.auth-card {
  background: #fff; border-radius: 12px; padding: 2rem;
  width: min(400px, 100%); box-shadow: 0 20px 50px rgba(0,0,0,0.4);
}
.auth-brand { display: flex; align-items: center; gap: 0.6rem; justify-content: center; margin-bottom: 1.5rem; }
.auth-brand img { width: 40px; height: 40px; }
.auth-brand h1 { margin: 0; font-size: 1.4rem; color: #1e293b; }
.auth-form { display: flex; flex-direction: column; gap: 0.85rem; }
.auth-form[hidden] { display: none; }
.auth-form h2 { font-size: 1.1rem; text-align: center; margin: 0; }
.auth-form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: #475569; }
.auth-form input { padding: 0.55rem 0.7rem; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.95rem; }
.auth-form input:focus { outline: 2px solid #2563eb; outline-offset: -1px; border-color: #2563eb; }
.auth-links { text-align: center; font-size: 0.85rem; }
.auth-links a { color: #2563eb; text-decoration: none; }
.auth-links a:hover { text-decoration: underline; }

/* FOOTER */
.site-footer {
  background: #fff; border-top: 1px solid #e2e8f0;
  padding: 1rem 1.5rem; font-size: 0.8rem; color: #475569;
}
.footer-inner {
  max-width: 1450px; margin: 0 auto;
  display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
}
.footer-inner .muted { color: #94a3b8; }
