:root {
  color-scheme: light dark;

  --bg: #fbfbf9;
  --surface: #ffffff;
  --border: #e2e0da;
  --text: #1c1b19;
  --muted: #6d6a63;
  --accent: #3a6b53;
  --accent-soft: #eaf1ed;
  --danger: #a33a2c;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181a;
    --surface: #1e2124;
    --border: #2f3438;
    --text: #e8e6e1;
    --muted: #969b9f;
    --accent: #7fb99a;
    --accent-soft: #22302a;
    --danger: #e08272;
  }
}

* { box-sizing: border-box; }

/* Elements toggled via the native `hidden` attribute (JS sets `.hidden =
   true/false`) must actually disappear. Without this, any more-specific
   rule elsewhere that sets its own `display` (`#shell`, `.upload-inline`,
   `.tag-filter`, ...) silently wins the cascade over the browser's built-in
   `[hidden] { display: none }`, and `.hidden = true` stops doing anything. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

code, .mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

/* ---------- entry screens (auth / link) ---------- */

.panel.entry {
  max-width: 26rem;
  margin: 4.5rem auto;
  padding: 2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.entry-brand {
  margin: 0 0 1.5rem;
  color: var(--accent);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.panel.entry h2 { margin: 0 0 0.4rem; font-size: 1.15rem; }

.panel.error {
  max-width: 34rem;
  margin: 3rem auto;
  padding: 1.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

form {
  display: flex;
  gap: 0.5rem;
  margin-top: 1.1rem;
  flex-wrap: wrap;
}

input, button, textarea, select {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 0.55rem 0.75rem;
}

input, textarea, select {
  flex: 1 1 12rem;
  min-width: 0;
  background: var(--bg);
  color: var(--text);
}

textarea { resize: vertical; font-family: inherit; }

input:focus-visible, button:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
  cursor: pointer;
  font-weight: 500;
}

button:hover { filter: brightness(1.08); }

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: none;
}

button.ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}

button.small {
  padding: 0.3rem 0.6rem;
  font-size: 0.85rem;
}

button.danger-text:hover { color: var(--danger); border-color: var(--danger); }

.muted { color: var(--muted); }

.msg {
  margin: 0.9rem 0 0;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  background: var(--accent-soft);
  font-size: 0.9rem;
}

.msg.bad, .error { background: transparent; color: var(--danger); }

/* ---------- app shell ---------- */

#shell {
  display: grid;
  grid-template-columns: 15rem 1fr;
  min-height: 100vh;
  transition: grid-template-columns 0.15s ease;
}

#shell.collapsed { grid-template-columns: 3.5rem 1fr; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0.75rem;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  margin: 0 0 1.5rem 0.25rem;
}

#shell.collapsed .sidebar-head { justify-content: center; margin-left: 0; }

.brand {
  margin: 0;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--accent);
  white-space: nowrap;
}

.collapse-btn {
  flex-shrink: 0;
  padding: 0.15rem 0.4rem;
  font-size: 0.85rem;
  line-height: 1.4;
}

#shell.collapsed .collapse-btn { transform: rotate(180deg); }
#shell.collapsed .brand,
#shell.collapsed .navtab-label,
#shell.collapsed .sidebar-foot { display: none; }

.sidenav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.navtab {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--muted);
  border: none;
  border-left: 3px solid transparent;
  border-radius: 0 6px 6px 0;
  padding: 0.5rem 0.6rem;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}

#shell.collapsed .navtab { justify-content: center; padding: 0.5rem 0; }

.navtab-dot {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.4rem;
  height: 1.4rem;
  border-radius: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.72rem;
}

.navtab.active .navtab-dot {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.navtab-label {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  white-space: nowrap;
}

.navtab:hover { background: var(--accent-soft); color: var(--text); }

.navtab.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}

.sidebar-foot {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#account {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.82rem;
  color: var(--muted);
}

.content {
  min-width: 0;
  padding: 1.75rem 2rem 3rem;
  max-width: 68rem;
}

.pane h2 { margin: 0 0 1.25rem; font-size: 1.2rem; }
.pane h3 { margin: 0 0 0.6rem; font-size: 0.95rem; }

@media (max-width: 760px) {
  #shell, #shell.collapsed { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    inset: auto 0 0 0;
    height: auto;
    flex-direction: row;
    align-items: center;
    padding: 0.5rem;
    border-right: none;
    border-top: 1px solid var(--border);
    z-index: 10;
  }

  .sidebar-head, .sidebar-foot { display: none; }

  .sidenav {
    flex-direction: row;
    flex: 1;
    justify-content: space-around;
  }

  .navtab {
    flex-direction: column;
    gap: 0.15rem;
    border-left: none;
    border-top: 3px solid transparent;
    border-radius: 6px;
    text-align: center;
  }

  .navtab.active { border-top-color: var(--accent); }

  .content { padding: 1.25rem 1rem 5rem; }

  .pane-doc { min-height: calc(100vh - 1.25rem - 5rem); }
}

/* ---------- controls ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1rem;
}

#search { flex: 1 1 18rem; }

.filters { display: flex; gap: 0.4rem; }

.chip {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 0.4rem 0.8rem;
  font-size: 0.88rem;
}

.chip.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
}

#toggle-semantic.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
}

.tag-filter {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0 0 1rem;
  font-size: 0.88rem;
  color: var(--muted);
}

.tag-filter button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--accent);
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

/* ---------- tags pane ---------- */

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag-list button {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 0.8rem;
  border-radius: 999px;
  font-size: 0.9rem;
  cursor: pointer;
}

.tag-list button:hover { border-color: var(--accent); color: var(--accent); }

.tag-list .count {
  margin-left: 0.4rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* ---------- results ---------- */

.results { display: flex; flex-direction: column; gap: 0.6rem; min-width: 0; }

.item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}

.item-head {
  display: flex;
  gap: 0.6rem;
  align-items: baseline;
  flex-wrap: wrap;
  margin-bottom: 0.35rem;
}

.kind {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 0.1rem 0.45rem;
  border-radius: 4px;
}

.item-date { font-size: 0.8rem; color: var(--muted); margin-left: auto; }

.item-stamp {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.75rem;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.05rem 0.35rem;
}

.item-body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0;
}

/* ---------- rendered markdown + LaTeX ---------- */

.rendered-note {
  white-space: normal;
  line-height: 1.6;
}

.rendered-note :first-child { margin-top: 0; }
.rendered-note :last-child { margin-bottom: 0; }

.rendered-note h1, .rendered-note h2, .rendered-note h3 {
  margin: 1em 0 0.4em;
  line-height: 1.3;
}

.rendered-note p { margin: 0.6em 0; }

.rendered-note code {
  background: var(--accent-soft);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.rendered-note pre {
  background: var(--accent-soft);
  padding: 0.75rem;
  border-radius: 8px;
  overflow-x: auto;
}

.rendered-note pre code { background: none; padding: 0; }

.rendered-note blockquote {
  margin: 0.6em 0;
  padding-left: 0.9em;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

.rendered-note img { max-width: 100%; border-radius: 6px; }
.rendered-note a { color: var(--accent); }

/* ---------- related items ---------- */

.item-related {
  margin: 0.6rem 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.related-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.88rem;
}

.related-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- inline file previews ---------- */

.item-preview { margin: 0.6rem 0; }

.preview-media {
  display: block;
  max-width: 100%;
  max-height: 24rem;
  border-radius: 8px;
}

audio.preview-media { max-height: none; width: 100%; }

.preview-pdf,
.preview-html {
  width: 100%;
  height: 28rem;
  border: 1px solid var(--border);
  border-radius: 8px;
}

/* ---------- fullscreen lightbox ---------- */

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  z-index: 100;
}

.lightbox-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: min(90vw, 60rem);
  max-height: 85vh;
  overflow: auto;
}

.lightbox-media {
  display: block;
  max-width: 100%;
  max-height: 78vh;
  margin: 0 auto;
  border-radius: 6px;
}

.lightbox-pdf,
.lightbox-html {
  width: min(85vw, 56rem);
  height: 80vh;
  border: none;
}

.lightbox-close {
  position: fixed;
  top: 1.5rem;
  right: 1.5rem;
  background: var(--surface);
}

.item-tags { margin-top: 0.5rem; display: flex; gap: 0.35rem; flex-wrap: wrap; }

.item-tags span {
  font-size: 0.8rem;
  color: var(--muted);
}

.download {
  display: inline-block;
  margin-top: 0.55rem;
  font-size: 0.88rem;
  padding: 0.3rem 0.7rem;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}

.empty { color: var(--muted); padding: 2rem 0; text-align: center; }

/* ---------- write: doc-style note page ---------- */

/* Fills the viewport exactly (content's own padding subtracted below) so the
   pane never grows taller than the screen -- the textarea scrolls
   internally instead, and the actions bar stays a true fixed footer rather
   than chasing the cursor or leaving dead space under a short note. */
.pane-doc {
  max-width: 42rem;
  margin: 0 auto;
  min-height: calc(100vh - 1.75rem - 3rem);
  display: flex;
  flex-direction: column;
}

.doc-form {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  margin: 0;
}

.doc-textarea {
  border: none;
  background: transparent;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1.7;
  flex: 1;
  min-height: 0;
  resize: none;
  overflow-y: auto;
}

.doc-textarea:focus-visible { outline: none; }

.doc-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;
  flex-shrink: 0;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.doc-actions .msg { margin: 0; flex: 1 1 auto; }

/* ---------- upload: compact inline form in Browse ---------- */

.upload-inline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 1rem;
  padding: 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.upload-inline .msg { flex: 1 1 100%; margin: 0; }

/* ---------- item actions & inline edit ---------- */

.item-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.55rem;
  flex-wrap: wrap;
}

.item.editing { display: flex; flex-direction: column; gap: 0.5rem; }

.edit-field { width: 100%; }

/* ---------- stats ---------- */

.stats-tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
  margin: 1rem 0 1.5rem;
}

@media (max-width: 700px) {
  .stats-tiles { grid-template-columns: repeat(2, 1fr); }
}

.stat-tile {
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 0.9rem;
  text-align: center;
}

.stat-value { font-size: 1.4rem; font-weight: 600; color: var(--accent); }
.stat-label { font-size: 0.8rem; color: var(--muted); margin-top: 0.15rem; }

.stats-activity {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 1.5rem;
}

.activity-row {
  display: grid;
  grid-template-columns: 5.5rem 1fr 2.5rem;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.activity-day { color: var(--muted); }

.activity-bar {
  display: block;
  height: 0.6rem;
  min-width: 2px;
  background: var(--accent);
  border-radius: 3px;
}

.activity-count { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }

.stats-storage {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.9rem;
}

.storage-row {
  padding: 0.5rem 0.7rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
