/* ============================================================
   BalinexDocs — App stylesheet
   Theme tokens, dark mode, layout, components.
   ============================================================ */

/* ---------- Theme tokens ---------- */
:root {
  --bg:           #f7f8fa;
  --bg-elev:     #ffffff;
  --bg-soft:     #f1f3f6;
  --border:      #e5e7eb;
  --border-strong:#d1d5db;
  --text:        #111827;
  --text-muted:  #6b7280;
  --text-soft:   #9ca3af;
  --primary:     #3056d3;
  --primary-hov: #1e3fa3;
  --primary-soft:#eaf0ff;
  --danger:      #dc2626;
  --danger-hov:  #b91c1c;
  --success:     #16a34a;
  --success-soft:#dcfce7;
  --error-soft:  #fee2e2;
  --shadow-sm:   0 1px 2px rgba(15, 23, 42, .06);
  --shadow:      0 4px 14px rgba(15, 23, 42, .08);
  --radius-sm:   6px;
  --radius:      10px;
  --radius-lg:   14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --topbar-h:    56px;
}

[data-theme="dark"] {
  --bg:           #0f1115;
  --bg-elev:     #161922;
  --bg-soft:     #1d212c;
  --border:      #262a36;
  --border-strong:#363b4a;
  --text:        #e6e8ef;
  --text-muted:  #9aa1b2;
  --text-soft:   #6c7283;
  --primary:     #5b7df0;
  --primary-hov: #7891ff;
  --primary-soft:#1e2742;
  --danger:      #f87171;
  --danger-hov:  #ef4444;
  --success:     #34d399;
  --success-soft:#052e1f;
  --error-soft:  #3a1418;
  --shadow-sm:   0 1px 2px rgba(0, 0, 0, .35);
  --shadow:      0 8px 24px rgba(0, 0, 0, .35);
}

@media (prefers-color-scheme: dark) {
  [data-theme="auto"] {
    --bg:           #0f1115;
    --bg-elev:     #161922;
    --bg-soft:     #1d212c;
    --border:      #262a36;
    --border-strong:#363b4a;
    --text:        #e6e8ef;
    --text-muted:  #9aa1b2;
    --text-soft:   #6c7283;
    --primary:     #5b7df0;
    --primary-hov: #7891ff;
    --primary-soft:#1e2742;
    --danger:      #f87171;
    --danger-hov:  #ef4444;
    --success:     #34d399;
    --success-soft:#052e1f;
    --error-soft:  #3a1418;
    --shadow-sm:   0 1px 2px rgba(0, 0, 0, .35);
    --shadow:      0 8px 24px rgba(0, 0, 0, .35);
  }
}

/* ---------- Reset & base ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  background: var(--bg-soft);
  padding: 2px 6px;
  border-radius: 4px;
}

/* ---------- Topbar ---------- */
.topbar {
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-h);
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar-inner {
  max-width: 1400px;
  margin: 0 auto;
  height: 100%;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 24px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  color: var(--text);
  font-size: 15px;
}
.brand:hover { text-decoration: none; }
.brand-mark { font-size: 18px; }

.topnav {
  display: flex;
  gap: 4px;
  margin-left: 12px;
}
.topnav a {
  color: var(--text-muted);
  padding: 6px 12px;
  border-radius: 6px;
  font-weight: 500;
}
.topnav a:hover { color: var(--text); background: var(--bg-soft); text-decoration: none; }
.topnav a.active { color: var(--primary); background: var(--primary-soft); }

.topbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 999px;
  text-decoration: none;
  color: inherit;
  transition: background 0.15s;
}
.user-chip:hover { background: var(--bg-soft); }
.user-meta {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.15;
}
.user-name { font-weight: 600; color: var(--text); font-size: 13px; }
.user-role { font-size: 10px; color: var(--text-soft); text-transform: uppercase; letter-spacing: .04em; }

/* Avatar (used in topbar, admin lists, profile page) */
.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.user-avatar-sm { width: 28px; height: 28px; }
.user-avatar-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 12px;
  color: var(--primary);
  background: var(--primary-soft);
  border-color: var(--primary-soft);
}

/* Profile page */
.profile-card { display: flex; flex-direction: column; gap: 12px; }
.profile-summary { display: flex; gap: 16px; align-items: center; }
.profile-avatar-wrap { flex-shrink: 0; }
.profile-avatar {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-soft);
  border: 2px solid var(--border);
}
.profile-avatar-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 32px;
  color: var(--primary);
  background: var(--primary-soft);
  border-color: var(--primary-soft);
}
.profile-meta { display: flex; flex-direction: column; gap: 4px; }
.profile-username { font-size: 18px; font-weight: 600; }
.profile-role { font-size: 13px; }

.users-table td:first-child { width: 40px; padding-right: 4px; }

.theme-icon-light, .theme-icon-dark { display: none; }
[data-theme="light"] .theme-icon-dark { display: inline; }
[data-theme="dark"]  .theme-icon-light { display: inline; }
[data-theme="auto"]  .theme-icon-dark { display: inline; }
@media (prefers-color-scheme: dark) {
  [data-theme="auto"] .theme-icon-dark  { display: none; }
  [data-theme="auto"] .theme-icon-light { display: inline; }
}

/* ---------- Subnav ---------- */
.subnav {
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.subnav-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  gap: 4px;
}
.subnav a {
  color: var(--text-muted);
  padding: 12px 14px;
  border-bottom: 2px solid transparent;
  font-weight: 500;
}
.subnav a:hover { color: var(--text); text-decoration: none; }
.subnav a.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------- Layout ---------- */
.app-main { min-height: calc(100vh - var(--topbar-h)); }
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 28px 24px 60px;
}
.container-narrow { max-width: 720px; }

.page-title {
  margin: 0 0 20px;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.section-title { margin: 28px 0 12px; font-size: 16px; font-weight: 600; color: var(--text-muted); }
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.muted { color: var(--text-muted); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s, box-shadow .15s;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { text-decoration: none; }
.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hov); border-color: var(--primary-hov); color: #fff; }
.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--bg-soft); }
.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--border);
}
.btn-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-block { width: 100%; }
.btn-sm { padding: 4px 10px; font-size: 12.5px; }

.inline-form { display: inline; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  align-items: end;
}
.form-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.form-actions { display: flex; gap: 8px; margin-top: 8px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--text-muted); }
.field input, .field select, .field textarea,
.form-row input, .form-row select {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus, .form-row input:focus, .form-row select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.field-actions { justify-content: flex-end; }
.hint { font-size: 12px; color: var(--text-muted); }

.checkbox { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.checkbox input { width: auto; }

/* ---------- Alerts ---------- */
.alert {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-bottom: 16px;
}
.alert-error { background: var(--error-soft); color: var(--danger); border-color: transparent; }
.alert-ok    { background: var(--success-soft); color: var(--success); border-color: transparent; }

/* ---------- Auth pages ---------- */
.page-auth { background: var(--bg); }
.auth-main {
  min-height: calc(100vh - var(--topbar-h));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 32px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}
.auth-title { margin: 0 0 6px; font-size: 22px; }
.auth-sub { margin: 0 0 22px; color: var(--text-muted); }

/* ---------- Stats ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.stat-card {
  padding: 18px 20px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.stat-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 6px; }
.stat-value { font-size: 28px; font-weight: 700; }

/* ---------- Cards ---------- */
.card-grid, .card-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}
.doc-card, .project-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  transition: border-color .15s, transform .1s;
}
.doc-card:hover, .project-card:hover { border-color: var(--primary); text-decoration: none; transform: translateY(-1px); }
.doc-card-icon { font-size: 30px; }
.doc-card-name, .project-name { font-weight: 600; margin-bottom: 2px; }
.doc-card-meta, .project-meta { font-size: 12.5px; color: var(--text-muted); }

/* ---------- Tables ---------- */
.data-table {
  width: 100%;
  margin-top: 16px;
  border-collapse: collapse;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.data-table th, .data-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  vertical-align: top;
}
.data-table th {
  background: var(--bg-soft);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}
.data-table tr:last-child td { border-bottom: none; }

.row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.inline-edit { display: inline-block; }
.inline-edit > summary { cursor: pointer; }
.inline-edit[open] > summary { margin-bottom: 6px; }
.inline-edit[open] > .form-row { padding: 8px; background: var(--bg-soft); border-radius: var(--radius-sm); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--bg-soft);
  color: var(--text-muted);
  text-transform: capitalize;
}
.badge-admin { background: var(--primary-soft); color: var(--primary); }
.badge-user  { background: var(--bg-soft); color: var(--text-muted); }

/* ---------- Empty states ---------- */
.empty {
  padding: 18px;
  text-align: center;
  color: var(--text-muted);
  background: var(--bg-elev);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty-large {
  padding: 60px 24px;
  text-align: center;
  background: var(--bg-elev);
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
}
.empty-icon { font-size: 48px; opacity: .7; margin-bottom: 8px; }
.empty-large h2 { margin: 0 0 6px; font-size: 18px; }
.empty-large p { margin: 0 0 14px; color: var(--text-muted); }

/* ---------- Panels ---------- */
.panel {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 16px 0;
}
.panel-summary { font-weight: 600; cursor: pointer; padding: 4px 0; }

/* ---------- Search ---------- */
.search-form { display: flex; gap: 8px; align-items: center; }
.search-form input[type="search"] {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  color: var(--text);
  min-width: 280px;
  font-size: 14px;
}
.search-form-large { margin-bottom: 18px; }
.search-form-large input[type="search"] { flex: 1; padding: 12px 16px; font-size: 15px; }

.search-results { list-style: none; padding: 0; margin: 16px 0; }
.search-result {
  padding: 14px 16px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
}
.search-result-title { font-size: 15px; font-weight: 600; }
.search-result-meta { font-size: 12.5px; color: var(--text-muted); margin: 4px 0; display: flex; align-items: center; gap: 6px; }
.search-result-snippet { font-size: 13px; color: var(--text-muted); margin-top: 6px; }

/* ---------- Error page ---------- */
.error-box { text-align: center; padding: 60px 24px; }
.error-code { font-size: 64px; font-weight: 700; color: var(--text-soft); margin-bottom: 6px; }

/* ============================================================
   Viewer (project page) — sidebar + iframe
   ============================================================ */
.page-viewer { overflow: hidden; }
.page-viewer .app-main { height: calc(100vh - var(--topbar-h)); min-height: 0; }

.viewer-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: calc(100vh - var(--topbar-h));
  background: var(--bg);
  transition: grid-template-columns 0.22s ease;
}

/* Collapsed sidebar: column shrinks to 0; sidebar contents are clipped via
   overflow:hidden on .viewer-sidebar (already set). We keep display:flex
   so transitions work and focus/ARIA stay sane. */
body.sidebar-collapsed .viewer-layout {
  grid-template-columns: 0 1fr;
}
body.sidebar-collapsed .viewer-sidebar {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}
.viewer-sidebar { transition: opacity 0.18s ease; }

.sidebar-toggle-btn {
  flex-shrink: 0;
  font-size: 16px;
  padding: 4px 10px;
  line-height: 1;
}
.sidebar-toggle-icon {
  display: inline-block;
  transition: transform 0.22s ease;
}
body.sidebar-collapsed .sidebar-toggle-icon {
  transform: rotate(180deg);
}

.viewer-sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.viewer-sidebar-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.viewer-project-name {
  font-weight: 700;
  font-size: 14px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-search { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.viewer-search input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.viewer-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* ---- Tree ---- */
.tree { padding: 8px; overflow-y: auto; flex: 1; font-size: 13.5px; }
.tree-folder { margin: 0; }
.tree-folder > summary { list-style: none; cursor: pointer; }
.tree-folder > summary::-webkit-details-marker { display: none; }
.tree-folder[open] > summary > .tree-caret { transform: rotate(90deg); }
.tree-caret {
  display: inline-block;
  width: 14px;
  text-align: center;
  font-size: 10px;
  color: var(--text-soft);
  transition: transform .15s;
}
.tree-children { padding-left: 16px; }
.tree-label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 5px;
  color: var(--text);
  user-select: none;
}
.tree-label:hover { background: var(--bg-soft); text-decoration: none; }
.tree-folder-label { font-weight: 600; color: var(--text); }
.tree-file-label { color: var(--text-muted); cursor: pointer; }
.tree-file-label.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.tree-icon { font-size: 12px; opacity: .8; }
.tree-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

/* ---- Viewer content ---- */
.viewer-content {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}
.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.viewer-breadcrumb {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-frame {
  flex: 1;
  width: 100%;
  border: none;
  background: #ffffff;  /* Always white inside iframe – the docs decide their own theme */
  display: block;
}

.viewer-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 60px 24px;
  color: var(--text-muted);
}
.viewer-empty h2 { color: var(--text); margin: 8px 0 4px; }

/* ---------- Responsive ---------- */
@media (max-width: 800px) {
  .topbar-inner { padding: 0 14px; gap: 12px; }
  .user-chip .user-meta { display: none; } /* keep avatar, hide name on narrow */
  .topnav a { padding: 6px 10px; font-size: 13px; }
  .container { padding: 18px 14px 40px; }
  /* Slimmer sidebar on narrow screens, both panes always visible. */
  .viewer-layout { grid-template-columns: 220px 1fr; }
}
