:root {
  --bg: #fafaf9;
  --card: #ffffff;
  --text: #1c1917;
  --muted: #78716c;
  --faint: #a8a29e;
  --border: #e7e5e4;
  --accent: #3b5bdb;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 12px;
}

:root[data-theme="dark"] {
  --bg: #121212;
  --card: #1b1b1d;
  --text: #ececec;
  --muted: #8d8d95;
  --faint: #5f5f66;
  --border: #2a2a2e;
  --accent: #7c93f5;
  --danger: #f87171;
  --ok: #4ade80;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

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

h1 { font-size: 1.5rem; margin: 0 0 16px; font-weight: 600; }
h2 { font-size: 1.1rem; margin: 0; font-weight: 600; }
h3 { font-size: 1rem; margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }

/* ---------- Buttons ---------- */
button {
  font: inherit;
  font-size: 0.85rem;
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { opacity: 0.9; color: #fff; }
button.danger:hover { border-color: var(--danger); color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); border-color: transparent; }
button:disabled { opacity: 0.5; cursor: default; }

/* ---------- Top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.crumbs { display: flex; align-items: center; gap: 8px; min-width: 0; }
.crumbs a { color: var(--text); text-decoration: none; }
.crumbs .brand { font-weight: 700; }
.crumbs .sep { color: var(--faint); }
.crumbs .crumb { color: var(--muted); }
.bar-actions { display: flex; gap: 4px; }

/* ---------- Pages ---------- */
.wrap { max-width: 960px; margin: 0 auto; padding: 28px 20px; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 14px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 130px;
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s;
}
a.card:hover { border-color: var(--accent); }
.card h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .preview {
  margin: 0;
  flex: 1;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: pre-wrap;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card time { font-size: 0.75rem; color: var(--faint); }

.new-card {
  border-style: dashed;
  background: transparent;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.95rem;
  padding: 16px;
}
.new-card:hover { color: var(--accent); }

.card.soon {
  border-style: dashed;
  background: transparent;
  align-items: center;
  justify-content: center;
  color: var(--faint);
}

/* ---------- Dialogs ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.35);
}
.overlay[hidden] { display: none; }
.dialog-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(340px, 100%);
  padding: 22px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.dialog-card input {
  font: inherit;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.dialog-card input:focus { border-color: var(--accent); }
.dialog-card .row { display: flex; justify-content: flex-end; gap: 8px; }
.form-msg { margin: 0; min-height: 1.2em; font-size: 0.85rem; color: var(--danger); }

/* ---------- Editor ---------- */
body.editor {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 20px;
}
.editor-head .name {
  flex: 1;
  min-width: 140px;
  font: inherit;
  font-size: 1.2rem;
  font-weight: 600;
  padding: 4px 0;
  border: none;
  border-bottom: 1px solid transparent;
  background: transparent;
  color: var(--text);
  outline: none;
}
.editor-head .name:focus { border-bottom-color: var(--border); }
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

#status { font-size: 0.85rem; color: var(--muted); margin-right: 4px; }
#status.ok { color: var(--ok); }
#status.err { color: var(--danger); }

.quick-bar { padding: 0 20px 10px; }
.quick-bar[hidden] { display: none; }
.quick-bar input {
  width: 100%;
  font: inherit;
  font-size: 16px; /* 16px stops iOS zooming in on focus */
  padding: 10px 12px;
  border: 1px dashed var(--accent);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  outline: none;
}

.editor-main {
  flex: 1;
  display: flex;
  gap: 12px;
  min-height: 0;
  padding: 0 20px;
}

textarea {
  flex: 1;
  width: 100%;
  resize: none;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1rem;
  line-height: 1.6;
  outline: none;
}
textarea:focus { border-color: var(--accent); }

#linksPanel {
  width: 320px;
  max-width: 40%;
  overflow: auto;
  padding: 14px 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
#linksPanel[hidden] { display: none; }
#linksPanel h2 { font-size: 0.85rem; color: var(--muted); margin-bottom: 10px; }
#linksList { list-style: none; margin: 0; padding: 0; }
#linksList li { margin-bottom: 8px; font-size: 0.85rem; word-break: break-all; }
#linksList a { color: var(--accent); }

.editor-foot { padding: 8px 20px; font-size: 0.8rem; color: var(--muted); }

@media (max-width: 640px) {
  #topbar, .editor-head, .editor-main, .editor-foot { padding-left: 14px; padding-right: 14px; }
  .quick-bar { padding-left: 14px; padding-right: 14px; }
  .wrap { padding: 20px 14px; }
  .editor-main { flex-direction: column; }
  #linksPanel { width: 100%; max-width: none; max-height: 35%; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
}
