:root {
  --bg: #ffffff;
  --bg-secondary: #f5f5f5;
  --text: #1a1a1a;
  --text-secondary: #666666;
  --border: #e0e0e0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success: #16a34a;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --sidebar-width: 240px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111111;
    --bg-secondary: #1a1a1a;
    --text: #e5e5e5;
    --text-secondary: #999999;
    --border: #333333;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* Nav */
.nav-wrapper {
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

.nav-brand {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  text-decoration: none;
}

.nav a {
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius);
  transition: color 0.15s;
}

.nav a:hover, .nav a.active {
  color: var(--accent);
}

.nav-search {
  margin-left: auto;
}

.nav-search input {
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  width: 220px;
}

.nav-user {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* Layout */
.layout {
  display: flex;
  min-height: calc(100vh - 53px);
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  border-right: 1px solid var(--border);
  background: var(--bg-secondary);
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar-content {
  padding: 1rem 0;
}

.sidebar-content h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  padding: 0 0.75rem;
  margin-bottom: 0.5rem;
}

.sidebar-tag-link {
  display: block;
  padding: 0.35rem 0.75rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.85rem;
  transition: background 0.1s;
}

.sidebar-tag-link:hover {
  background: var(--border);
}

.sidebar-tag-link.active {
  color: var(--accent);
  font-weight: 600;
}

/* Content */
.content {
  flex: 1;
  overflow-y: auto;
  min-width: 0;
}

/* Document list */
.doc-list-page {
  padding: 1rem;
}

.doc-list {
  display: flex;
  flex-direction: column;
}

.doc-list-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0.5rem;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: var(--text);
  transition: background 0.1s;
}

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

.doc-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
  width: 2rem;
  text-align: center;
}

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

.doc-title {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

/* Document viewer */
.doc-viewer {
  display: flex;
  min-height: calc(100vh - 53px);
}

.doc-viewer-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #f0f0f0;
  min-width: 0;
}

@media (prefers-color-scheme: dark) {
  .doc-viewer-content {
    background: #0a0a0a;
  }
}

.doc-viewer-sidebar {
  width: 320px;
  padding: 1.5rem;
  border-left: 1px solid var(--border);
  overflow-y: auto;
  max-height: calc(100vh - 53px);
  flex-shrink: 0;
}

.doc-viewer-sidebar h3 {
  font-size: 1.1rem;
  margin-bottom: 0.75rem;
}

.doc-viewer-sidebar h4 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin: 1.25rem 0 0.5rem;
}

.detail-meta dt {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.detail-meta dd {
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.doc-image {
  max-width: 100%;
  max-height: calc(100vh - 53px);
  object-fit: contain;
  margin: auto;
}

/* PDF viewer */
.pdf-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

#pdf-page-info {
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-width: 120px;
  text-align: center;
}

.pdf-canvas-container {
  flex: 1;
  overflow: auto;
  display: flex;
  justify-content: center;
  padding: 1rem;
}

#pdf-canvas {
  max-width: 100%;
  box-shadow: var(--shadow);
}

/* Annotations */
.annotation-item {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.annotation-page {
  font-size: 0.75rem;
  color: var(--accent);
  font-weight: 600;
}

.annotation-content {
  font-size: 0.85rem;
  white-space: pre-wrap;
}

.annotation-meta {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 0.25rem;
}

.annotation-form {
  margin-top: 0.75rem;
}

.annotation-form textarea {
  width: 100%;
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  font-family: inherit;
  resize: vertical;
  margin-bottom: 0.35rem;
}

.empty-note {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.btn-link {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: inherit;
  text-decoration: underline;
}

.btn-link:hover {
  color: var(--danger);
}

/* Revisions */
.revision-item {
  padding: 0.25rem 0;
  font-size: 0.85rem;
}

.revision-item a {
  color: var(--accent);
  text-decoration: none;
}

.alias-list {
  list-style: none;
  font-size: 0.85rem;
}

.alias-list li {
  padding: 0.15rem 0;
}

/* Tags */
.tag-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.tag-chip {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.8rem;
}

/* Tags page */
.tags-page {
  max-width: 600px;
  margin: 1.5rem auto;
  padding: 0 1.5rem;
}

.tags-page h2 {
  margin-bottom: 1rem;
}

.tag-create {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.tag-create input, .tag-create select {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
}

.tag-create input {
  flex: 1;
  min-width: 150px;
}

.tag-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.tag-name {
  font-size: 0.9rem;
}

/* Upload */
.upload-page {
  max-width: 700px;
  margin: 1.5rem auto;
  padding: 0 1.5rem;
}

.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 3rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.upload-zone.dragover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 5%, transparent);
}

.upload-zone p {
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
}

.upload-options {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.upload-options label {
  font-size: 0.85rem;
  color: var(--text-secondary);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.upload-options select {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
}

.upload-list {
  margin-top: 1rem;
}

.upload-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

.upload-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-item-status {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.upload-item-status.done { color: var(--success); }
.upload-item-status.error { color: var(--danger); }
.upload-item-status.dup { color: var(--text-secondary); }

.upload-progress {
  width: 80px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.upload-progress-bar {
  height: 100%;
  background: var(--accent);
  transition: width 0.15s;
}

/* Search */
.search-heading {
  font-size: 1rem;
  color: var(--text-secondary);
  margin-bottom: 1rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s;
  text-decoration: none;
}

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

.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-danger {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}

.btn-danger:hover { background: var(--danger-hover); }

.btn-sm {
  padding: 0.25rem 0.5rem;
  font-size: 0.8rem;
}

/* Login */
.login-card {
  max-width: 360px;
  margin: 15vh auto;
  padding: 2.5rem;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.login-card h1 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
}

.login-card p {
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
}

/* Utility */
.hidden { display: none; }
.loading { text-align: center; padding: 3rem; color: var(--text-secondary); }
.empty-state { text-align: center; padding: 4rem 1rem; color: var(--text-secondary); }
.error { color: var(--danger); }
.grid-sentinel { height: 1px; }

/* Responsive */
@media (max-width: 768px) {
  .layout {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 200px;
  }
  .doc-viewer {
    flex-direction: column;
  }
  .doc-viewer-sidebar {
    width: 100%;
    max-height: none;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
