:root {
  --ink: #1b1d22;
  --ink-raised: #23262d;
  --ink-line: #34383f;
  --paper: #f7f5f0;
  --paper-dim: #eae6dc;
  --slate: #9298a3;
  --slate-dim: #6b7280;
  --amber: #e2a33d;
  --amber-dim: #7a5a25;
  --moss: #4c7a5d;
  --rust: #c1503f;
  --font-ui: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "JetBrains Mono", "SFMono-Regular", Menlo, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--ink);
  color: var(--paper);
  -webkit-font-smoothing: antialiased;
}
code { font-family: var(--font-mono); }

.app { display: flex; height: 100vh; overflow: hidden; }

/* ---------------- Sidebar ---------------- */
.sidebar {
  width: 268px;
  flex-shrink: 0;
  background: var(--ink-raised);
  border-right: 1px solid var(--ink-line);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 6px 20px; }
.brand-mark {
  font-family: var(--font-mono);
  font-size: 20px;
  color: var(--amber);
  border: 1px solid var(--amber-dim);
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px;
}
.brand-name { font-weight: 600; font-size: 14px; }
.brand-sub { font-size: 11px; color: var(--slate); font-family: var(--font-mono); }

.sidebar-section { margin-bottom: 18px; overflow-y: auto; }
.sidebar-heading {
  display: flex; justify-content: space-between; align-items: center;
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--slate);
  padding: 0 6px 8px;
}
.icon-btn {
  background: transparent; border: 1px solid var(--ink-line); color: var(--slate);
  width: 20px; height: 20px; border-radius: 5px; cursor: pointer; font-size: 13px;
  display: flex; align-items: center; justify-content: center; line-height: 1;
}
.icon-btn:hover { border-color: var(--amber); color: var(--amber); }

.collection-list, .item-list { display: flex; flex-direction: column; gap: 2px; }
.side-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 8px; border-radius: 6px; cursor: pointer; font-size: 13px;
  color: var(--paper-dim);
}
.side-row:hover { background: rgba(255,255,255,0.04); }
.side-row.active { background: rgba(226,163,61,0.12); color: var(--amber); }
.side-row .count { font-family: var(--font-mono); font-size: 10px; color: var(--slate); }
.side-row .title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.search-input {
  width: 100%; background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper);
  border-radius: 6px; padding: 6px 9px; font-size: 12px; margin-bottom: 8px; font-family: var(--font-ui);
}
.search-input:focus { outline: none; border-color: var(--amber-dim); }

.collection-links { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; padding: 0 6px; }
.collection-links a, .link-btn {
  font-size: 11px; color: var(--slate); text-decoration: none; font-family: var(--font-mono);
  background: none; border: none; text-align: left; cursor: pointer; padding: 0;
}
.collection-links a:hover, .link-btn:hover { color: var(--amber); }

.sidebar-footer-group { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.sidebar-footer-btn {
  background: transparent;
  border: 1px dashed var(--ink-line);
  color: var(--slate);
  padding: 10px;
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  display: flex; align-items: center; gap: 8px; justify-content: center;
}
.sidebar-footer-btn:hover { color: var(--amber); border-color: var(--amber-dim); }

/* ---------------- Settings drawer ---------------- */
.settings-block { margin-bottom: 22px; }
.settings-label {
  display: block; font-family: var(--font-mono); font-size: 10px; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--slate); margin-bottom: 8px;
}
.settings-block input[type=text] {
  width: 100%; background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper);
  border-radius: 6px; padding: 8px 10px; font-size: 12px;
}
.settings-link { font-family: var(--font-mono); font-size: 12px; color: var(--amber); text-decoration: none; }
.settings-link:hover { text-decoration: underline; }

.webhook-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.webhook-row {
  border: 1px solid var(--ink-line); border-radius: 8px; padding: 8px 10px; font-size: 11px;
  display: flex; flex-direction: column; gap: 4px;
}
.webhook-row .wh-url { font-family: var(--font-mono); color: var(--paper-dim); word-break: break-all; }
.webhook-row .wh-events { color: var(--slate); }
.webhook-row .wh-actions { display: flex; gap: 8px; margin-top: 2px; }
.webhook-row .wh-actions button {
  background: none; border: none; color: var(--slate); cursor: pointer; font-size: 10px; padding: 0;
}
.webhook-row .wh-actions button:hover { color: var(--amber); }
.webhook-row .wh-actions button.wh-delete:hover { color: var(--rust); }
.webhook-form { border-top: 1px solid var(--ink-line); padding-top: 12px; }
.webhook-form input { margin-bottom: 8px; }
.webhook-events { display: flex; gap: 12px; font-size: 11px; color: var(--slate); margin-bottom: 10px; }
.webhook-events label { display: flex; align-items: center; gap: 4px; }

/* ---------------- Auth screen ---------------- */
.auth-screen {
  height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--ink);
}
.auth-card {
  width: 320px; background: var(--ink-raised); border: 1px solid var(--ink-line);
  border-radius: 14px; padding: 30px 26px; text-align: center;
}
.auth-mark { margin: 0 auto 14px; width: 40px; height: 40px; font-size: 24px; border-radius: 8px; }
.auth-card h1 { font-size: 18px; margin: 0 0 6px; }
.auth-sub { font-size: 12px; color: var(--slate); margin: 0 0 18px; }
.auth-error {
  background: rgba(193,80,63,0.12); color: #e78579; border: 1px solid rgba(193,80,63,0.3);
  border-radius: 6px; padding: 8px 10px; font-size: 12px; margin-bottom: 14px; text-align: left;
}
.auth-card input {
  width: 100%; background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper);
  border-radius: 7px; padding: 9px 11px; font-size: 13px; margin-bottom: 10px;
}
.auth-card input:focus { outline: none; border-color: var(--amber-dim); }
.auth-submit { width: 100%; margin-top: 4px; }
.auth-switch { margin-top: 14px; font-size: 11px; color: var(--slate); }
.auth-switch button { background: none; border: none; color: var(--amber); cursor: pointer; font-size: 11px; padding: 0; }

/* ---------------- Current user row ---------------- */
.current-user {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 11px; color: var(--slate); padding: 0 6px 4px;
}
.current-user #currentUsername { font-family: var(--font-mono); color: var(--paper-dim); }

/* ---------------- Frontmatter add-field row ---------------- */
.field-add-row { display: flex; gap: 8px; }

/* ---------------- Image frontmatter field ---------------- */
.image-field-row {
  display: grid; grid-template-columns: 56px 1fr auto; gap: 10px; align-items: center;
}
.image-field-row .field-key {
  font-family: var(--font-mono); font-size: 12px; color: var(--amber);
  background: transparent; border: none; padding: 7px 0; grid-column: 1 / -1;
}
.image-thumb {
  width: 56px; height: 56px; border-radius: 6px; object-fit: cover;
  border: 1px solid var(--ink-line); background: var(--ink); cursor: pointer;
}
.image-thumb.empty {
  display: flex; align-items: center; justify-content: center; color: var(--slate); font-size: 10px;
}
.image-field-meta { display: flex; flex-direction: column; gap: 6px; }
.image-field-meta input {
  background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper);
  border-radius: 6px; padding: 6px 9px; font-size: 12px;
}
.image-field-meta .image-src { font-family: var(--font-mono); color: var(--slate); font-size: 11px; }

.picker-grid { margin-top: 14px; }

/* ---------------- Confirm modal ---------------- */
.modal p { color: var(--slate); font-size: 13px; line-height: 1.6; margin: 0 0 16px; }
.modal .modal-danger-name { font-family: var(--font-mono); color: var(--paper); }

/* ---------------- Main ---------------- */
.main { flex: 1; overflow-y: auto; background: var(--ink); }

.empty-state { display: flex; align-items: center; justify-content: center; height: 100%; padding: 40px; }
.empty-card { max-width: 420px; text-align: center; }
.empty-mark { font-family: var(--font-mono); color: var(--amber-dim); font-size: 22px; letter-spacing: 0.3em; margin-bottom: 10px; }
.empty-card h1 { font-size: 20px; margin: 0 0 10px; }
.empty-card p { color: var(--slate); font-size: 13px; line-height: 1.6; }
.empty-card code { background: var(--ink-raised); padding: 1px 5px; border-radius: 4px; color: var(--paper-dim); }

.editor { max-width: 880px; margin: 0 auto; padding: 28px 32px 80px; }
.editor-topbar {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 22px; position: sticky; top: 0; background: var(--ink);
  padding: 10px 0; z-index: 5;
}
.crumbs { font-family: var(--font-mono); font-size: 13px; color: var(--slate); }
.crumbs #crumbItem { color: var(--paper); }
.crumb-sep { margin: 0 6px; color: var(--ink-line); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.save-status { font-size: 11px; color: var(--moss); font-family: var(--font-mono); min-width: 90px; text-align: right; }
.save-status.dirty { color: var(--amber); }
.save-status.saving { color: var(--slate); }

.btn {
  border: 1px solid transparent; border-radius: 7px; padding: 8px 14px;
  font-size: 13px; font-weight: 500; cursor: pointer; font-family: var(--font-ui);
}
.btn-primary { background: var(--amber); color: #1b1d22; }
.btn-primary:hover { background: #eeb257; }
.btn-ghost { background: transparent; border-color: var(--ink-line); color: var(--paper-dim); }
.btn-ghost:hover { border-color: var(--slate); }
.btn-danger { color: var(--rust); }
.btn-danger:hover { border-color: var(--rust); }
.btn-small { padding: 5px 10px; font-size: 12px; }

.editor-body { display: flex; flex-direction: column; gap: 20px; }

.card {
  background: var(--ink-raised);
  border: 1px solid var(--ink-line);
  border-radius: 12px;
  padding: 18px 20px 20px;
  position: relative;
}
.card-label {
  font-family: var(--font-mono); font-size: 10px; text-transform: uppercase;
  letter-spacing: 0.1em; color: var(--slate); margin-bottom: 12px; display: block;
}
.card-label-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }

/* Frontmatter card: literal --- delimiters + torn perforation at the base */
.frontmatter-card { padding-bottom: 26px; background: linear-gradient(180deg, #26292f, var(--ink-raised) 40px); }
.frontmatter-fields { display: flex; flex-direction: column; gap: 10px; }
.field-row { display: grid; grid-template-columns: 140px 1fr auto; gap: 10px; align-items: center; }
.field-row input[type=text], .field-row input[type=date] {
  background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper);
  border-radius: 6px; padding: 7px 9px; font-size: 13px; font-family: var(--font-ui); width: 100%;
}
.field-row .field-key {
  font-family: var(--font-mono); font-size: 12px; color: var(--amber);
  background: transparent; border: 1px dashed transparent; padding: 7px 4px;
}
.field-row .field-key.editable { border-color: var(--ink-line); }
.field-row input:focus { outline: none; border-color: var(--amber-dim); }
.remove-field { background: none; border: none; color: var(--slate); cursor: pointer; font-size: 14px; }
.remove-field:hover { color: var(--rust); }
.perforation {
  position: absolute; left: 20px; right: 20px; bottom: 10px; height: 0;
  border-bottom: 1px dashed var(--ink-line);
}

/* ---------------- Formatting toolbar ---------------- */
.format-toolbar {
  display: flex; align-items: center; gap: 4px; margin-bottom: 8px;
  padding: 6px; background: var(--ink); border: 1px solid var(--ink-line);
  border-radius: 8px; flex-wrap: wrap;
}
.format-toolbar button {
  background: transparent; border: 1px solid transparent; color: var(--paper-dim);
  min-width: 30px; height: 28px; border-radius: 5px; cursor: pointer;
  font-size: 12px; font-family: var(--font-ui); padding: 0 7px;
}
.format-toolbar button:hover { border-color: var(--amber-dim); color: var(--amber); }
.toolbar-sep { width: 1px; height: 18px; background: var(--ink-line); margin: 0 4px; }

.body-textarea {
  width: 100%; min-height: 340px; resize: vertical;
  background: var(--ink); border: 1px solid var(--ink-line); border-radius: 8px;
  color: var(--paper); padding: 14px; font-family: var(--font-mono); font-size: 13px; line-height: 1.6;
}
.body-textarea:focus { outline: none; border-color: var(--amber-dim); }
.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent; border: 1px solid var(--ink-line); color: var(--slate);
  font-size: 11px; padding: 4px 10px; border-radius: 6px; cursor: pointer;
}
.tab.active { color: var(--amber); border-color: var(--amber-dim); }
.body-preview {
  background: var(--paper); color: #1b1d22; border-radius: 8px; padding: 20px;
  min-height: 340px; font-size: 14px; line-height: 1.7;
}
.body-preview h1, .body-preview h2, .body-preview h3 { margin-top: 0.8em; }
.body-preview img { max-width: 100%; border-radius: 6px; }

.resource-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.resource-tile {
  border: 1px solid var(--ink-line); border-radius: 8px; overflow: hidden; background: var(--ink);
  position: relative; cursor: pointer;
}
.resource-tile img { width: 100%; height: 72px; object-fit: cover; display: block; }
.resource-tile .name { font-size: 10px; color: var(--slate); padding: 4px 6px; font-family: var(--font-mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.resource-tile .remove-res {
  position: absolute; top: 4px; right: 4px; background: rgba(27,29,34,0.8); color: var(--paper);
  border: none; border-radius: 4px; width: 18px; height: 18px; cursor: pointer; font-size: 12px; line-height: 1;
}
.upload-btn { cursor: pointer; }
.empty-note { color: var(--slate); font-size: 12px; }

/* ---------------- Drawer ---------------- */
.drawer {
  position: fixed; top: 0; right: 0; width: 380px; height: 100%;
  background: var(--ink-raised); border-left: 1px solid var(--ink-line);
  padding: 20px; overflow-y: auto; z-index: 40;
  box-shadow: -10px 0 30px rgba(0,0,0,0.4);
}
.drawer-header { display: flex; justify-content: space-between; align-items: center; }
.drawer-header h2 { font-size: 15px; margin: 0; }
.drawer-sub { color: var(--slate); font-size: 12px; margin: 8px 0 18px; line-height: 1.5; }
.component-list { display: flex; flex-direction: column; gap: 10px; }
.component-item { border: 1px solid var(--ink-line); border-radius: 8px; padding: 12px; }
.component-item h3 { font-size: 13px; margin: 0 0 4px; }
.component-item p { font-size: 11px; color: var(--slate); margin: 0 0 10px; }
.component-item .field { margin-bottom: 8px; }
.component-item label { font-size: 10px; color: var(--slate); text-transform: uppercase; letter-spacing: 0.06em; display: block; margin-bottom: 4px; font-family: var(--font-mono); }
.component-item input, .component-item textarea {
  width: 100%; background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper);
  border-radius: 5px; padding: 6px 8px; font-size: 12px; font-family: var(--font-ui);
}
.component-item textarea { min-height: 50px; resize: vertical; }

/* ---------------- Modal ---------------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: flex;
  align-items: center; justify-content: center; z-index: 60;
}
.modal { background: var(--ink-raised); border: 1px solid var(--ink-line); border-radius: 12px; padding: 22px; width: 340px; }
.modal h3 { margin: 0 0 14px; font-size: 15px; }
.modal input { width: 100%; background: var(--ink); border: 1px solid var(--ink-line); color: var(--paper); border-radius: 6px; padding: 8px 10px; font-size: 13px; margin-bottom: 14px; }
.modal .modal-check { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; font-size: 12px; color: var(--paper-dim); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 760px) {
  .sidebar { position: fixed; z-index: 30; height: 100%; }
  .drawer { width: 100%; }
}
