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

html { overflow-x: clip; overscroll-behavior-x: none; }

:root {
  --accent: #7c2d12;
  --accent-dark: #6b2410;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --green: #059669;
  --amber: #d97706;
  --blue: #2563eb;
  --red: #dc2626;
}

[data-theme="dark"] {
  --bg: #18181b;
  --surface: #27272a;
  --border: #3f3f46;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --green: #10b981;
  --amber: #f59e0b;
  --blue: #60a5fa;
  --red: #f87171;
}

[data-theme="dark"] .table a { color: #fca5a5; }
[data-theme="dark"] .table a:hover { color: #fcd34d; }
[data-theme="dark"] .message-content a { color: #fca5a5; }
[data-theme="dark"] .attachment-file { color: #fca5a5; }

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

/* Nav */
.nav {
  display: flex;
  flex-direction: column;
  background: var(--accent-dark);
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  padding-top: env(safe-area-inset-top);
}
.nav-top {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: 44px;
  position: relative;
}
.nav-brand {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 16px;
  color: #fff;
  text-decoration: none;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.nav-menu-btn {
  background: none;
  border: none;
  color: rgba(255,255,255,0.85);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}
.nav-menu-btn:hover { color: #fff; }
.nav-links {
  display: flex;
  justify-content: center;
  gap: 32px;
  padding: 8px 16px 10px;
  border-top: 1px solid rgba(255,255,255,0.1);
}
.nav-links a {
  color: rgba(255,255,255,0.9);
  text-decoration: none;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.03em;
  padding: 3px 10px;
  border-radius: 6px;
}
.nav-links a:hover { color: #fff; }
.nav-links a.active {
  color: #fff;
  background: rgba(255,255,255,0.18);
}

/* Drawer */
.drawer {
  position: fixed;
  top: 0; left: 0;
  width: 220px;
  height: 100vh;
  background: var(--surface);
  z-index: 1000;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  box-shadow: 4px 0 20px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  padding: 24px 20px;
}
.drawer.open { transform: translateX(0); }
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 999;
  display: none;
}
.drawer-overlay.open { display: block; }
.drawer-top {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}
.drawer-brand { font-weight: 700; font-size: 15px; color: var(--text); }
.drawer-user { font-size: 14px; color: var(--muted); margin-bottom: 12px; }
.drawer-logout {
  width: 100%;
  padding: 9px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.drawer-logout:hover { background: #fee2e2; color: var(--red); border-color: #fca5a5; }
.drawer-link { display: block; font-size: 14px; color: var(--text); text-decoration: none; padding: 7px 10px; border-radius: 6px; margin-bottom: 2px; }
.drawer-link:hover { background: var(--bg); }
.drawer-link.active { background: var(--bg); font-weight: 600; color: var(--accent); }
.drawer-admin-link { display: block; font-size: 13px; color: var(--red); text-decoration: none; padding: 6px 10px; border-radius: 6px; margin-top: 8px; border: 1px solid color-mix(in srgb, var(--red) 30%, transparent); }
.drawer-admin-link:hover { background: color-mix(in srgb, var(--red) 8%, transparent); }
.drawer-admin-link.active { font-weight: 600; }

@media (min-width: 768px) {
  .drawer { transform: translateX(0); box-shadow: none; border-right: 1px solid var(--border); transition: none; }
  .drawer-overlay { display: none !important; }
  .nav { left: 220px; }
  .nav-menu-btn { display: none; }
  body { padding-left: 220px; }
}

/* Buttons */
.btn-primary, .btn-secondary, .btn {
  display: inline-flex;
  align-items: center;
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  transition: opacity 0.15s;
}
.btn-primary, .btn  { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover, .btn:hover { background: var(--accent-dark); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 5px; }
.btn-link { background: none; border: none; cursor: pointer; padding: 0; font-size: inherit; color: inherit; text-decoration: underline; }

/* Container */
.container { max-width: 900px; margin: 32px auto; padding: 0 24px; }

/* Page header */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 28px; gap: 16px; }
.page-header h1 { font-size: 22px; font-weight: 700; }

/* Table */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
.table th { text-align: left; padding: 10px 14px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; border-bottom: 1px solid var(--border); }
.table td { padding: 11px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
.table tr:last-child td { border-bottom: none; }
@media (hover: hover) { .table tr:hover td { background: var(--bg); } }
.table a { color: var(--accent); text-decoration: none; }
.table a:hover { text-decoration: underline; }

/* Table scroll wrapper */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label { font-size: 13px; font-weight: 600; color: var(--text); }
.field input, .field textarea, textarea {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 16px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  width: 100%;
  transition: border-color 0.15s;
}
.field input:focus, .field textarea:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--surface);
}
.form-actions { display: flex; gap: 10px; margin-top: 8px; }

/* Login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 36px 40px; width: 360px; }
.login-card h1 { font-size: 20px; font-weight: 700; margin-bottom: 28px; text-align: center; }
.login-card .field { margin-bottom: 16px; }
.login-card .btn-primary { width: 100%; justify-content: center; padding: 10px; font-size: 15px; margin-top: 8px; }

/* Messages / Board */
.message-board { max-width: 720px; margin: 0 auto 24px; display: flex; flex-direction: column; gap: 16px; }
.message { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; position: relative; }
.message-actions { position: absolute; top: 10px; right: 12px; display: flex; gap: 4px; }
.msg-action-btn { background: none; border: none; cursor: pointer; padding: 2px 4px; opacity: 0.4; line-height: 1; display: flex; align-items: center; font-size: 13px; }
.msg-action-btn:hover { opacity: 1; }
.message-meta { display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; }
.message-author { font-weight: 600; font-size: 14px; }
.message-meta time { font-size: 11px; color: var(--muted); }
.message-content { font-size: 14px; line-height: 1.6; }
.message-content p { margin-bottom: 8px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content a { color: var(--accent); }
.message-content strong { font-weight: 600; }
.message-content ul, .message-content ol { padding-left: 1.5em; margin-bottom: 8px; }
.message-content li { margin-bottom: 2px; }
.message-content table { border-collapse: collapse; margin-bottom: 8px; font-size: 13px; }
.message-content th, .message-content td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
.message-content th { background: var(--bg); font-weight: 600; }
.attachment-audio { display: block; width: 100%; margin-bottom: 4px; }
.attachment-file { font-size: 13px; color: var(--accent); text-decoration: none; }
.attachment-file:hover { text-decoration: underline; }
.compose { margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; position: sticky; bottom: 16px; }
.compose-row { display: flex; align-items: flex-end; gap: 4px; }
.compose-row textarea { flex: 1; resize: none; border-color: transparent; background: var(--bg); overflow-y: hidden; min-height: 36px; line-height: 1.5; padding: 6px 8px; }
.compose-row textarea:focus { border-color: var(--accent); }
.compose-icon-btn { padding: 4px 6px; flex-shrink: 0; background: none; border: none; cursor: pointer; color: var(--muted); display: flex; align-items: center; }
.compose-icon-btn:hover { color: var(--accent); }
.compose-icon-btn.send { background: var(--accent); color: #fff; border-radius: 6px; padding: 5px 8px; }
.compose-icon-btn.send:hover { background: var(--accent-dark); }
.compose-icon-btn.send:disabled { opacity: 0.7; cursor: default; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Document reading view */
.doc-body { max-width: 720px; margin: 0 auto; }
.doc-meta { font-size: 13px; color: var(--muted); margin-bottom: 24px; line-height: 1.8; }
.doc-meta a { color: var(--accent); text-decoration: none; }
.doc-meta a:hover { text-decoration: underline; }
.doc-content { font-size: 16px; line-height: 1.8; }
.doc-content h1, .doc-content h2, .doc-content h3 { margin-top: 1.5em; margin-bottom: 0.5em; font-weight: 700; }
.doc-content h1 { font-size: 1.4em; }
.doc-content h2 { font-size: 1.2em; }
.doc-content h3 { font-size: 1.05em; }
.doc-content p { margin-bottom: 1em; }
.doc-content em { font-style: italic; }
.doc-content strong { font-weight: 700; }
.doc-content a { color: var(--accent); }
.doc-content ul, .doc-content ol { padding-left: 1.5em; margin-bottom: 1em; }
.doc-content li { margin-bottom: 0.3em; }
.doc-content blockquote { border-left: 3px solid var(--border); margin-left: 0; padding-left: 1em; color: var(--muted); margin-bottom: 1em; }
.tier-badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }

/* Settings sections */
.settings-section { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 24px; margin-bottom: 24px; }
.settings-section h3 { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 16px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }

/* Breadcrumb */
.breadcrumb { font-size: 13px; color: var(--muted); margin-bottom: 16px; }
.breadcrumb a { color: var(--muted); text-decoration: none; }
.breadcrumb a:hover { color: var(--accent); text-decoration: underline; }

/* Browse grid */
.browse-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.browse-card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 18px;
  text-decoration: none;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.browse-card:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(0,0,0,0.07); }
.browse-card-name { font-weight: 600; font-size: 15px; margin-bottom: 4px; }
.browse-card-count { font-size: 12px; color: var(--muted); }

/* Pagination */
.pagination { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; margin-top: 20px; }
.page-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 34px; height: 34px; padding: 0 8px;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; text-decoration: none; color: var(--text);
  background: var(--surface);
}
.page-btn:hover { border-color: var(--accent); color: var(--accent); }
.page-btn.current { background: var(--accent); color: #fff; border-color: var(--accent); }
.page-btn.disabled { opacity: 0.4; pointer-events: none; }
.page-gap { color: var(--muted); padding: 0 4px; }

/* Collections */
.collection-scope {
  display: flex; align-items: center; gap: 10px; margin-bottom: 14px;
  padding: 8px 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; font-size: 13px;
}
.collection-scope-clear { color: var(--muted); text-decoration: none; margin-left: auto; }
.collection-scope-clear:hover { color: var(--red); }

/* Doc toolbar (star + collections) */
.doc-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
.star-btn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 12px; border: 1px solid var(--border); border-radius: 20px;
  background: var(--surface); color: var(--muted); font-size: 13px; cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.star-btn:hover { border-color: var(--amber); color: var(--amber); }
.star-btn.starred { border-color: var(--amber); color: var(--amber); background: color-mix(in srgb, var(--amber) 10%, transparent); }
.star-icon { font-size: 14px; }
.doc-collections { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.col-pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; border: 1px solid var(--accent); border-radius: 14px;
  font-size: 12px; color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent);
}
.col-remove {
  background: none; border: none; cursor: pointer; color: var(--muted);
  font-size: 12px; padding: 0; line-height: 1; display: inline-flex; align-items: center;
}
.col-remove:hover { color: var(--red); }
.add-col-form { display: inline-flex; align-items: center; gap: 4px; }
.add-col-select {
  height: 28px; padding: 0 6px; font-size: 12px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.doc-toolbar-link { font-size: 13px; color: var(--accent); text-decoration: none; }
.doc-toolbar-link:hover { text-decoration: underline; }

/* Document reader */
.doc-header { margin-bottom: 24px; }
.doc-header h1 { font-size: 24px; line-height: 1.3; margin-bottom: 12px; }
.doc-meta { display: flex; flex-wrap: wrap; gap: 12px; font-size: 13px; color: var(--muted); align-items: center; }
.doc-meta a { color: var(--accent); text-decoration: none; }
.doc-meta a:hover { text-decoration: underline; }

/* Search */
mark { background: #fef08a; color: #78350f; border-radius: 2px; padding: 0 2px; }
[data-theme="dark"] mark { background: #713f12; color: #fef08a; }

.search-form { margin-bottom: 24px; }
.search-bar { display: flex; gap: 8px; margin-bottom: 12px; }
.search-input {
  flex: 1; height: 42px; padding: 0 14px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
}
.search-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124,45,18,0.12); }

.search-filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filter-group { display: flex; flex-direction: column; gap: 4px; }
.filter-group label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.filter-group input, .filter-group select {
  height: 34px; padding: 0 10px; font-size: 13px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.filter-group input:focus, .filter-group select:focus { outline: none; border-color: var(--accent); }
.filter-year { width: 110px; }
.filter-year::-webkit-inner-spin-button,
.filter-year::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.filter-year { -moz-appearance: textfield; }

.search-meta { font-size: 14px; color: var(--muted); margin-bottom: 16px; }
.search-meta strong { color: var(--text); }

.result-item { padding: 14px 0; border-bottom: 1px solid var(--border); }
.result-item:last-of-type { border-bottom: none; }
.result-title { display: block; font-size: 16px; font-weight: 600; color: var(--accent); text-decoration: none; line-height: 1.35; margin-bottom: 5px; }
.result-title:hover { text-decoration: underline; }
.result-meta { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--muted); margin-bottom: 6px; align-items: center; }
.result-snippet { font-size: 13px; line-height: 1.6; color: var(--muted); }

/* Utilities */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); background: #fee2e2; border-radius: 6px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }
.ok { color: #065f46; background: #d1fae5; border-radius: 6px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }
code { font-family: ui-monospace, "SF Mono", monospace; font-size: 13px; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
h1 { font-size: 22px; font-weight: 700; margin-bottom: 24px; }
