:root {
  --bg: #f7f7f9;
  --panel: #ffffff;
  --border: #e3e5e8;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #0a66c2;
  --accent-hover: #084e96;
  --row-hover: #f0f4fa;
  --danger: #b42318;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f24;
    --border: #2c3038;
    --text: #e8eaed;
    --muted: #9aa0a6;
    --accent: #4aa3ff;
    --accent-hover: #2f8cff;
    --row-hover: #23272e;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: var(--row-hover); }
button.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  padding: 8px 16px;
  font-weight: 600;
}
button.primary:hover { background: var(--accent-hover); }

input[type="search"] {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  width: 240px;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand {
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}
.brand .logo {
  color: var(--accent);
  font-size: 18px;
}

.breadcrumbs {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
}
.breadcrumbs .crumb {
  color: var(--accent);
  cursor: pointer;
}
.breadcrumbs .crumb:hover { text-decoration: underline; }
.breadcrumbs .sep { margin: 0 6px; color: var(--muted); }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user {
  color: var(--muted);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main { padding: 16px; }

.signed-out {
  max-width: 520px;
  margin: 10vh auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 32px;
  text-align: center;
}
.signed-out h1 { margin-top: 0; }
.signed-out .hint {
  margin-top: 24px;
  padding: 12px;
  background: rgba(180, 35, 24, 0.08);
  border: 1px solid rgba(180, 35, 24, 0.3);
  border-radius: 6px;
  color: var(--danger);
  font-size: 13px;
}
.signed-out code {
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 3px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.toolbar select {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
}
.status {
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
}

.listing {
  list-style: none;
  padding: 0;
  margin: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.row {
  display: grid;
  grid-template-columns: 32px 1fr 100px;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.row:last-child { border-bottom: none; }
.row:hover { background: var(--row-hover); }
.row.head {
  background: var(--bg);
  cursor: default;
  font-weight: 600;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.row.head:hover { background: var(--bg); }

.row .icon {
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}
.row .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row .meta-ref {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 640px) {
  .row { grid-template-columns: 28px 1fr; }
  .row .meta-ref { display: none; }
  input[type="search"] { width: 140px; }
  .breadcrumbs { display: none; }
}

.empty {
  padding: 40px;
  text-align: center;
  color: var(--muted);
}

.loading {
  text-align: center;
  padding: 16px;
  color: var(--muted);
}

.load-more {
  display: block;
  margin: 12px auto;
}

/* Split layout */
.split {
  display: grid;
  grid-template-columns: var(--list-width, 320px) 6px 1fr;
  height: calc(100vh - 130px);
  min-height: 400px;
}

.split-divider {
  background: var(--border);
  cursor: col-resize;
  align-self: stretch;
  transition: background 0.15s;
  user-select: none;
  touch-action: none;
}
.split-divider:hover,
.split-divider:focus-visible,
.split-divider.dragging {
  background: var(--accent);
  outline: none;
}

/* While dragging, suppress pointer events on iframes so the drag isn't
   swallowed when the cursor crosses into the preview content. */
body.split-dragging iframe { pointer-events: none; }
body.split-dragging { cursor: col-resize; }

.list-pane {
  overflow: auto;
  display: flex;
  flex-direction: column;
}
.list-pane .listing { flex: 0 0 auto; }

.row.selected,
.row.selected:hover {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
}

.preview-pane {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.viewer-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.viewer-title {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}
.viewer-scribe {
  flex: none;
  padding: 3px 10px;
  font-size: 13px;
}
.viewer-scribe[hidden] { display: none; }
.viewer-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.viewer-actions a {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  text-decoration: none;
  font-size: 13px;
}
.viewer-actions a:hover {
  background: var(--row-hover);
  text-decoration: none;
}
.viewer-actions a[hidden] { display: none; }

.viewer-body {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: var(--panel);
  min-height: 0;
}
.viewer-body img,
.viewer-body video,
.viewer-body audio {
  max-width: 100%;
  max-height: 100%;
}
.viewer-body iframe {
  border: 0;
  width: 100%;
  height: 100%;
  background: #fff;
  border-radius: 4px;
}
.viewer-body pre {
  width: 100%;
  max-height: 100%;
  overflow: auto;
  background: var(--bg);
  color: var(--text);
  padding: 14px;
  border-radius: 6px;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Monaco, "Courier New", monospace;
}
.viewer-body .markdown {
  width: 100%;
  height: 100%;
  overflow: auto;
  color: var(--text);
  padding: 16px 20px;
  align-self: stretch;
}
.viewer-body .markdown h1,
.viewer-body .markdown h2,
.viewer-body .markdown h3 { margin-top: 1.2em; }
.viewer-body .markdown pre {
  background: var(--bg);
  color: var(--text);
  padding: 12px;
  border-radius: 4px;
  overflow: auto;
}
.viewer-body .markdown code {
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 3px;
  font: 0.92em ui-monospace, SFMono-Regular, Menlo, monospace;
}
.viewer-body .placeholder,
.viewer-body .unsupported {
  color: var(--muted);
  text-align: center;
  max-width: 380px;
  padding: 20px;
}

/* PDF viewer fills the body and scrolls vertically; the surrounding
   viewer-body's flex centering would otherwise compress it. */
.viewer-body:has(.pdf-viewer) {
  align-items: stretch;
  justify-content: stretch;
  padding: 0;
}
.pdf-viewer {
  width: 100%;
  height: 100%;
  overflow: auto;
  background: var(--bg);
}
.pdf-pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 12px;
}
.pdf-page {
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
  max-width: 100%;
}

@media (max-width: 800px) {
  .split {
    grid-template-columns: 1fr;
    height: auto;
  }
  .split-divider { display: none; }
  .preview-pane { min-height: 60vh; }
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  padding: 10px 16px;
  border-radius: 6px;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.toast {
  /* Error dumps from Graph are multi-line, pretty-printed JSON. */
  white-space: pre-wrap;
  max-width: min(90vw, 720px);
  max-height: 60vh;
  overflow: auto;
}

.modal {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
  width: min(90vw, 420px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.4); }
.modal h2 { margin: 0 0 8px; font-size: 16px; }
.modal-path { margin: 0 0 12px; color: var(--muted); word-break: break-all; }
.modal label { display: block; margin-bottom: 4px; font-weight: 600; }
.modal input[type="text"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.scribe-modal { width: min(90vw, 760px); }
.scribe-status { margin: 0 0 8px; color: var(--muted); }
.scribe-status:empty { display: none; }
.scribe-text {
  display: block;
  width: 100%;
  height: 60vh;
  resize: vertical;
  overflow: auto;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
button.danger {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
  font-weight: 600;
}
button.danger:hover { filter: brightness(0.9); }

.upload-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  max-height: 40vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.upload-list:empty { display: none; }
.upload-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.upload-list li:last-child { border-bottom: 0; }
.upload-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-remove {
  padding: 0 6px;
  line-height: 20px;
  background: transparent;
  border: 0;
  color: var(--muted);
}
.upload-remove:hover { color: var(--danger); }
.upload-status {
  display: inline-block;
  width: 16px;
  text-align: center;
  font-weight: 700;
}
.upload-status.progress {
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: upload-spin 0.8s linear infinite;
}
.upload-status.success { color: #1a7f37; }
.upload-status.failed { color: var(--danger); }
@keyframes upload-spin { to { transform: rotate(360deg); } }
