/* ================================================================
   Documents — Enterprise Document Management (AgentOS)
   Self-contained: no external CSS/JS. White / blue / slate.
   ================================================================ */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --color-primary: #2563EB;
  --color-primary-hover: #1D4ED8;
  --color-primary-light: #EFF6FF;
  --bg-body: #F1F5F9;
  --bg-sidebar: #FFFFFF;
  --bg-main: #F8FAFC;
  --bg-card: #FFFFFF;
  --bg-input: #F1F5F9;
  --bg-hover: #F1F5F9;
  --text-primary: #0F172A;
  --text-secondary: #475569;
  --text-muted: #64748B;
  --text-inverse: #FFFFFF;
  --border-light: #E2E8F0;
  --border-medium: #CBD5E1;
  --color-success: #10B981;
  --color-warning: #D97706;
  --color-error: #DC2626;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-full: 9999px;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 16px 48px rgba(15, 23, 42, 0.18);
  --ease-out: cubic-bezier(0.33, 1, 0.68, 1);
}

html.dark {
  --color-primary-light: #123653;
  --bg-body: #111111;
  --bg-sidebar: #1E1E1E;
  --bg-main: #1E1E1E;
  --bg-card: #252526;
  --bg-input: #2D2D2D;
  --bg-hover: #2D2D2D;
  --text-primary: #FFFFFF;
  --text-secondary: #CCCCCC;
  --text-muted: #A6A6A6;
  --border-light: #3E3E3E;
  --border-medium: #5A5A5A;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.55);
}

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg-body);
  color: var(--text-primary);
  font-size: 0.875rem;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  clip-path: inset(50%); overflow: hidden; white-space: nowrap;
}

:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; }

/* hidden attr must win over component display rules (drawer, bulk bar, empty state) */
[hidden] { display: none !important; }

/* ---------- Layout: sidebar + main, internal scroll only ---------- */
.app-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 20px 14px;
  overflow-y: auto;
}

.sidebar-top { display: flex; flex-direction: column; gap: 8px; }

.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 8px; margin-bottom: 12px;
  text-decoration: none; color: inherit;
}

.brand-logo {
  width: 34px; height: 34px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}

.brand-name { font-weight: 700; font-size: 1.05rem; letter-spacing: -0.01em; }

.btn-new-task {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 10px 16px; margin-bottom: 8px;
  background: var(--color-primary); color: #fff; border: 0;
  border-radius: var(--radius-sm);
  font-family: var(--font-sans); font-size: 0.875rem; font-weight: 600;
  cursor: pointer; text-decoration: none;
  transition: background 0.2s var(--ease-out), transform 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
}

.btn-new-task:hover { background: var(--color-primary-hover); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3); }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; margin-top: 4px; }

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; width: 100%;
  min-height: 42px; line-height: 1.4;
  border: 0; background: transparent; border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-family: var(--font-sans); font-size: 0.875rem; font-weight: 500;
  cursor: pointer; text-decoration: none; text-align: left;
  transition: background 0.15s var(--ease-out), color 0.15s var(--ease-out);
}

.nav-item:hover { background: var(--bg-hover); color: var(--text-primary); }

.nav-item.active {
  background: var(--color-primary-light);
  color: var(--color-primary);
  font-weight: 600;
}

.nav-item.active svg { stroke: var(--color-primary); }

.sidebar-bottom { padding-top: 16px; border-top: 1px solid var(--border-light); }

.workspace-info { display: flex; align-items: center; gap: 10px; }

.workspace-icon {
  width: 32px; height: 32px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-input); border-radius: var(--radius-sm);
  color: var(--text-secondary);
}

.workspace-text { display: flex; flex-direction: column; min-width: 0; }

.workspace-name {
  font-size: 0.78rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.workspace-plan { font-size: 0.72rem; color: var(--text-muted); }

/* ---------- Main column ---------- */
.main-content {
  display: flex; flex-direction: column;
  min-width: 0; height: 100vh; overflow: hidden;
  background: var(--bg-main);
}

.page-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 16px;
  padding: 20px 28px 12px; flex-shrink: 0;
}

.page-head h1 { margin: 0; font-size: 1.35rem; letter-spacing: -0.01em; }

.page-sub { margin: 4px 0 0; font-size: 0.83rem; color: var(--text-muted); }

.page-head-actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

/* ---------- Buttons / inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-family: var(--font-sans); font-size: 0.85rem; font-weight: 600;
  padding: 9px 18px; border-radius: var(--radius-sm);
  cursor: pointer; border: 0;
  transition: background 0.2s var(--ease-out), transform 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out);
}

.btn:active { transform: translateY(0) scale(0.98); }

.btn-primary { background: var(--color-primary); color: #fff; box-shadow: 0 1px 3px rgba(37, 99, 235, 0.2); }

.btn-primary:hover { background: var(--color-primary-hover); box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25); }

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

.btn-secondary:hover { background: var(--bg-hover); color: var(--text-primary); border-color: var(--border-medium); }

.btn-danger { background: rgba(220, 38, 38, 0.08); border: 1px solid rgba(220, 38, 38, 0.25); color: var(--color-error); }

.btn-danger:hover { background: rgba(220, 38, 38, 0.15); }

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

.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; flex-shrink: 0;
  border-radius: 50%; border: 1px solid var(--border-light);
  background: var(--bg-card); color: var(--text-secondary); cursor: pointer;
}

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

.LangSwitch { display: inline-flex; border: 1px solid var(--border-light); border-radius: var(--radius-full); overflow: hidden; background: var(--bg-card); flex-shrink: 0; }

.LangSwitch-btn { border: 0; background: none; padding: 6px 10px; font-family: var(--font-sans); font-size: 0.72rem; font-weight: 700; color: var(--text-muted); cursor: pointer; }

.LangSwitch-btn[aria-pressed="true"] { background: var(--color-primary); color: #fff; }

.icon-moon { display: none; }
html.dark .icon-sun { display: none; }
html.dark .icon-moon { display: block; }

.select, .text-input, textarea.text-input {
  font-family: var(--font-sans); font-size: 0.82rem;
  color: var(--text-primary);
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 8px 10px; outline: none;
}

.select:focus, .text-input:focus { border-color: var(--color-primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 0 28px 12px; flex-shrink: 0;
}

.search-box {
  display: flex; align-items: center; gap: 8px; flex: 1; min-width: 200px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 0 12px; color: var(--text-muted);
}

.search-box:focus-within { border-color: var(--color-primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }

.search-box input {
  flex: 1; border: 0; background: none; outline: none;
  font-family: var(--font-sans); font-size: 0.85rem;
  color: var(--text-primary); padding: 9px 0; min-width: 0;
}

.search-box input::placeholder { color: var(--text-muted); }

/* ---------- Stats (compact) ---------- */
.stats {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
  padding: 0 28px 12px; flex-shrink: 0;
}

.stat-card {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  padding: 10px 14px; box-shadow: var(--shadow-sm);
  min-width: 0;
}

.stat-card > div { display: flex; flex-direction: column; min-width: 0; }

.stat-icon {
  width: 30px; height: 30px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
}

.stat-blue { background: var(--color-primary-light); color: var(--color-primary); }
.stat-amber { background: #FEF3C7; color: var(--color-warning); }
html.dark .stat-amber { background: rgba(217, 119, 6, 0.15); }
.stat-green { background: rgba(16, 185, 129, 0.1); color: var(--color-success); }
.stat-slate { background: var(--bg-input); color: var(--text-secondary); }

.stat-value { font-size: 1.05rem; font-weight: 700; line-height: 1.2; }

.stat-label { font-size: 0.72rem; color: var(--text-muted); white-space: nowrap; }

/* ---------- Bulk bar ---------- */
.bulk-bar {
  display: flex; align-items: center; gap: 10px;
  margin: 0 28px 10px; padding: 8px 14px;
  background: var(--color-primary-light);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  font-size: 0.82rem; font-weight: 600; color: var(--text-primary);
  flex-shrink: 0;
}

/* ---------- Table card (the only scroll region) ---------- */
.table-card {
  flex: 1; min-height: 0;
  margin: 0 28px 20px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  display: flex; flex-direction: column; overflow: hidden;
}

.table-scroll { flex: 1; overflow: auto; min-height: 0; }

.doc-table { width: 100%; border-collapse: collapse; font-size: 0.83rem; }

.doc-table thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--bg-input);
  text-align: left; font-size: 0.72rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 10px 12px; border-bottom: 1px solid var(--border-light);
  white-space: nowrap;
}

.doc-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}

.doc-table tbody tr { cursor: pointer; transition: background 0.12s; }

.doc-table tbody tr:hover { background: var(--bg-hover); }

.doc-table .num { text-align: right; }

.col-check { width: 36px; }

.doc-cell { display: flex; align-items: center; gap: 10px; min-width: 0; }

.file-icon {
  width: 30px; height: 30px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px;
  font-size: 0.6rem; font-weight: 800; letter-spacing: 0.02em;
}

.file-pdf { background: rgba(220, 38, 38, 0.1); color: #DC2626; }
.file-docx { background: rgba(37, 99, 235, 0.1); color: #2563EB; }
.file-xlsx { background: rgba(16, 185, 129, 0.12); color: #059669; }
.file-csv { background: rgba(217, 119, 6, 0.12); color: #D97706; }
.file-txt { background: var(--bg-input); color: var(--text-secondary); }

.doc-name {
  font-weight: 600; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 280px;
}

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

.kb-tag {
  display: inline-block;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  font-size: 0.75rem; font-weight: 600;
  padding: 3px 10px; border-radius: var(--radius-full);
  white-space: nowrap;
}

.status-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.75rem; font-weight: 600;
  padding: 4px 10px; border-radius: var(--radius-full);
  white-space: nowrap;
}

.status-indexed { background: rgba(16, 185, 129, 0.1); color: #059669; }
html.dark .status-indexed { color: #34D399; }
.status-processing { background: var(--color-primary-light); color: var(--color-primary); }
.status-failed { background: rgba(220, 38, 38, 0.1); color: #DC2626; }

.spinner {
  width: 12px; height: 12px; flex-shrink: 0;
  border: 2px solid currentColor; border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.row-actions { position: relative; text-align: right; }

.action-menu {
  position: absolute; right: 0; top: calc(100% + 4px);
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  min-width: 160px; padding: 4px; z-index: 30;
}

.action-menu button {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: none; border: 0; padding: 8px 10px;
  font-family: var(--font-sans); font-size: 0.8rem; font-weight: 500;
  color: var(--text-primary); border-radius: 6px; cursor: pointer;
  text-align: left;
}

.action-menu button:hover { background: var(--bg-hover); }

.action-menu button.danger { color: var(--color-error); }

.action-menu button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Empty states ---------- */
.empty-state {
  padding: 48px 24px; text-align: center; color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}

.empty-title { margin: 8px 0 0; font-size: 0.95rem; font-weight: 700; color: var(--text-primary); }

.empty-sub { margin: 0 0 8px; font-size: 0.82rem; max-width: 380px; }

.empty-state .btn + .btn { margin-top: 4px; }

/* ---------- Drawer ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(15, 23, 42, 0.45);
  animation: fade-in 0.18s var(--ease-out);
}

@keyframes fade-in { from { opacity: 0; } }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 61;
  width: min(420px, 100vw);
  background: var(--bg-card);
  border-left: 1px solid var(--border-light);
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  animation: drawer-in 0.22s var(--ease-out);
}

@keyframes drawer-in { from { transform: translateX(40px); opacity: 0; } }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 16px 20px; border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.drawer-head h2 { margin: 0; font-size: 1rem; }

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

.drawer-foot {
  display: flex; gap: 10px; justify-content: flex-end;
  padding: 14px 20px; border-top: 1px solid var(--border-light);
  flex-shrink: 0;
}

.drawer-foot:empty { display: none; }

.detail-title-row { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 4px; }

.detail-title-row h3 {
  margin: 0; font-size: 1rem; line-height: 1.4;
  overflow-wrap: anywhere; flex: 1;
}

.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; margin: 14px 0; font-size: 0.83rem; }

.kv dt { color: var(--text-muted); font-weight: 500; }

.kv dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

.tag-row { display: flex; flex-wrap: wrap; gap: 6px; }

.tag-chip {
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  font-size: 0.75rem; font-weight: 600;
  padding: 3px 10px; border-radius: var(--radius-full);
}

.desc-text { font-size: 0.83rem; color: var(--text-secondary); line-height: 1.55; }

.section-label {
  display: block; margin: 16px 0 8px;
  font-size: 0.72rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-muted);
}

.error-box {
  background: rgba(220, 38, 38, 0.07);
  border: 1px solid rgba(220, 38, 38, 0.25);
  border-radius: var(--radius-sm);
  padding: 12px 14px; font-size: 0.83rem;
}

.error-box strong { color: var(--color-error); }

.form-group { margin-bottom: 14px; }

.form-group > label { display: block; margin-bottom: 6px; font-size: 0.78rem; font-weight: 600; }

.form-group .text-input, .form-group textarea.text-input, .form-group .select { width: 100%; }

textarea.text-input { min-height: 84px; resize: vertical; line-height: 1.5; }

.dropzone {
  border: 1.5px dashed var(--border-medium);
  border-radius: var(--radius-md);
  padding: 26px 16px; text-align: center;
  color: var(--text-muted); font-size: 0.83rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.dropzone:hover, .dropzone.dragover { border-color: var(--color-primary); background: var(--color-primary-light); }

.dropzone strong { color: var(--color-primary); }

.file-chip {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 10px 12px; margin-top: 10px; font-size: 0.83rem;
}

.file-chip .doc-name { max-width: none; flex: 1; }

.progress-track { height: 6px; background: var(--bg-input); border-radius: 3px; overflow: hidden; margin-top: 10px; }

.progress-fill { height: 100%; width: 0; background: var(--color-primary); border-radius: 3px; transition: width 0.2s; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; z-index: 62;
  left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(420px, calc(100vw - 32px));
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 22px;
  animation: modal-in 0.18s var(--ease-out);
}

@keyframes modal-in { from { transform: translate(-50%, -48%) scale(0.97); opacity: 0; } }

.modal h2 { margin: 0 0 8px; font-size: 1.02rem; }

.modal p { margin: 0 0 18px; font-size: 0.85rem; color: var(--text-secondary); line-height: 1.55; overflow-wrap: anywhere; }

.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ---------- Toasts ---------- */
.toast-wrap {
  position: fixed; right: 20px; bottom: 20px; z-index: 70;
  display: flex; flex-direction: column; gap: 8px;
}

.toast {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-left: 3px solid var(--color-success);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  padding: 10px 14px; font-size: 0.83rem; font-weight: 500;
  max-width: 340px;
  animation: toast-in 0.2s var(--ease-out);
}

.toast.error { border-left-color: var(--color-error); }

.toast svg { flex-shrink: 0; }

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

@keyframes toast-in { from { transform: translateX(20px); opacity: 0; } }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 900px) {
  .app-layout { grid-template-columns: 64px 1fr; }
  .sidebar { padding: 16px 8px; }
  .brand-name, .btn-new-task span, .nav-item span, .workspace-text { display: none; }
  .btn-new-task, .nav-item { justify-content: center; }
  .page-head, .toolbar, .stats { padding-left: 16px; padding-right: 16px; }
  .table-card { margin: 0 16px 16px; }
  .bulk-bar { margin: 0 16px 10px; }
}

@media (max-width: 640px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .page-head h1 { font-size: 1.15rem; }
  .doc-table thead { display: none; }
  .doc-table, .doc-table tbody, .doc-table tr, .doc-table td { display: block; width: 100%; }
  .doc-table tbody tr {
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    margin: 10px; width: auto;
    background: var(--bg-card);
  }
  .doc-table td { border: 0; padding: 6px 14px; }
  .doc-table td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.68rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: 0.04em;
    color: var(--text-muted); margin-bottom: 2px;
  }
  .doc-table td.col-check::before, .doc-table td.row-actions::before { content: none; }
  .doc-table td.row-actions { text-align: left; }
  .doc-name { max-width: none; }
  .action-menu { position: static; box-shadow: none; border: 0; padding: 0; margin-top: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* Smooth page entry (all app pages share the same motion) */
@keyframes page-in { from { opacity: 0; transform: translateY(6px); } }

.main-content { animation: page-in 0.28s ease-out; }
