:root {
  color-scheme: light dark;
  --bg: #fff;
  --text: #111;
  --muted: #888;
  --border: #ddd;
  --border-subtle: #eee;
  --input-bg: #fff;
  --code-bg: #f3f3f3;
}

/* System preference is the default signal (no explicit choice stored)... */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171d;
    --text: #e7e7ea;
    --muted: #9a9fae;
    --border: #33353f;
    --border-subtle: #2a2c35;
    --input-bg: #1e2027;
    --code-bg: #1e2027;
  }
}

/* ...but an explicit toggle choice (stamped as data-theme on <html>) always wins. */
:root[data-theme="dark"] {
  --bg: #16171d;
  --text: #e7e7ea;
  --muted: #9a9fae;
  --border: #33353f;
  --border-subtle: #2a2c35;
  --input-bg: #1e2027;
  --code-bg: #1e2027;
}

/* `color-scheme` controls native form-control rendering (checkboxes,
   scrollbars, etc) and is otherwise independent of the CSS variables above
   — without pinning it here too, an explicit "Light" choice would still
   render dark checkboxes on a dark-OS system, since "light dark" always
   defers to the OS preference regardless of data-theme. */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; color: var(--text); background: var(--bg); }
.topnav {
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.topnav a { text-decoration: none; font-weight: 600; color: var(--text); }
.topnav-brand { display: flex; align-items: center; gap: 0.5rem; }
.nav-logo { height: 1.5rem; width: auto; display: block; }

.theme-toggle { display: flex; gap: 0.25rem; }
.theme-toggle button {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2rem 0.45rem;
  font-size: 0.9rem;
  line-height: 1.4;
  color: var(--text);
  cursor: pointer;
}
.theme-toggle button.active {
  background: var(--border-subtle);
  border-color: var(--muted);
}

.container { max-width: 900px; margin: 0 auto; padding: 1.5rem; }
.container.narrow { max-width: 480px; }

.list-header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.doc-header { display: flex; justify-content: flex-end; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.doc-actions { display: flex; gap: 0.5rem; }

.doc-list { list-style: none; padding: 0; }
.doc-list li { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; padding: 0.6rem 0; border-bottom: 1px solid var(--border-subtle); }

.badge {
  display: inline-block;
  font-size: 0.7rem;
  padding: 0.1rem 0.4rem;
  margin-left: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
}
.doc-list a { color: var(--text); }
.meta { color: var(--muted); font-size: 0.8rem; display: flex; gap: 0.75rem; white-space: nowrap; }
.empty { color: var(--muted); }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }

.button {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  background: #2563eb;
  color: #fff;
  border-radius: 4px;
  text-decoration: none;
  border: none;
  font-size: 0.9rem;
  cursor: pointer;
}
.button.secondary { background: #6b7280; }
.button.danger { background: #b91c1c; }

.error { color: #ef4444; }

form { display: flex; flex-direction: column; gap: 0.75rem; }
label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; }
label.checkbox { flex-direction: row; align-items: center; gap: 0.5rem; }
input[type="text"], input[type="password"] {
  padding: 0.4rem;
  font-size: 1rem;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
}
button[type="submit"], #save-btn { cursor: pointer; }

.editor-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  gap: 1rem;
}
.toolbar-actions { display: flex; align-items: center; gap: 0.75rem; }
#status-indicator { font-size: 0.85rem; min-width: 8rem; text-align: right; }
#status-indicator.status-dirty { color: #ca8a04; }
#status-indicator.status-error { color: #ef4444; }

.editor-panes { display: flex; gap: 1rem; height: 65vh; }
#content, #preview { flex: 1; overflow: auto; border: 1px solid var(--border); border-radius: 4px; background: var(--input-bg); color: var(--text); }
#content { font-family: ui-monospace, monospace; padding: 0.75rem; resize: none; }
#preview { padding: 0.75rem; }

.password-panel { margin-top: 1rem; }
.password-fields {
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 320px;
}

.markdown-body img { max-width: 100%; }
.markdown-body pre { background: var(--code-bg); padding: 0.75rem; overflow-x: auto; border-radius: 4px; }
.markdown-body code { background: var(--code-bg); padding: 0.1rem 0.3rem; border-radius: 3px; }
.markdown-body .task-list-item { list-style: none; margin-left: -1.4rem; }
.markdown-body .task-list-item-checkbox { margin-right: 0.4rem; }
#preview .task-list-item-checkbox { cursor: pointer; }
