:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --text: #16181d;
  --muted: #5d6573;
  --border: #dde1e7;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --accent-soft: #e8efff;
  --ok: #15803d;
  --ok-soft: #e7f6ec;
  --warn: #a15c07;
  --warn-soft: #fdf3e2;
  --err: #b91c1c;
  --err-soft: #fdecec;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --text: #e8eaee;
    --muted: #9aa3b2;
    --border: #2c313c;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --accent-soft: #1b2540;
    --ok: #4ade80;
    --ok-soft: #12261a;
    --warn: #fbbf24;
    --warn-soft: #2b2110;
    --err: #f87171;
    --err-soft: #2d1414;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--sans);
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: 0 0 0.35rem; }
h2 { font-size: 1.05rem; margin: 0 0 0.35rem; }
h3 { font-size: 0.95rem; margin: 0 0 0.25rem; }
p { margin: 0.35rem 0 0.75rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.mono { font-family: var(--mono); }

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: 0.75rem 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 0.55rem; color: var(--text); text-decoration: none; font-weight: 650; }
.logo {
  display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 7px;
  background: var(--accent); color: var(--accent-text); font: 700 13px var(--mono);
}
.topnav { display: flex; align-items: center; gap: 1rem; font-size: 0.9rem; }
.net { font-size: 0.8rem; padding: 0.15rem 0.55rem; border-radius: 999px; background: var(--ok-soft); color: var(--ok); }
.net.offline { background: var(--err-soft); color: var(--err); }

main { max-width: 980px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
section { display: grid; gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); min-width: 0;
}
.card.center { text-align: center; padding: 40px 18px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 720px) { .grid2 { grid-template-columns: 1fr; } }

.row { display: flex; gap: 0.6rem; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.tight { gap: 0.4rem; }
.row > input[type="text"], .row > input:not([type]) { flex: 1; min-width: 0; }
.spacer { flex: 1; }
.label-row { display: flex; justify-content: space-between; align-items: baseline; gap: 0.5rem; margin-bottom: 0.35rem; }

/* ---------- form controls ---------- */
.field { display: block; margin-bottom: 0.9rem; }
.field.inline { display: flex; align-items: center; gap: 0.5rem; margin: 0; }
.hint { font-size: 0.8rem; color: var(--muted); }
.hint.bad { color: var(--err); }
.hint.good { color: var(--ok); }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 0.5rem 0.65rem;
}
input:focus, select:focus, textarea:focus, button:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { width: 100%; resize: vertical; }
textarea.code, pre.code {
  font: 13px/1.5 var(--mono); tab-size: 2;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
}
textarea.code { padding: 0.75rem; }
textarea.code.invalid { border-color: var(--err); }
textarea[readonly] { opacity: 0.85; }
pre.code { margin: 0; padding: 0.75rem; overflow: auto; max-height: 360px; white-space: pre; }

.check { display: inline-flex; align-items: center; gap: 0.4rem; cursor: pointer; user-select: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
  font: inherit; font-weight: 550; text-decoration: none; white-space: nowrap;
  color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.5rem 0.9rem; cursor: pointer; min-height: 38px;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.07); background: var(--accent); }
.btn.danger { color: var(--err); }
.btn.link { border-color: transparent; background: transparent; color: var(--accent); padding: 0.25rem 0.5rem; min-height: 0; }
.btn.small { padding: 0.3rem 0.65rem; font-size: 0.85rem; min-height: 32px; }

/* ---------- alerts / status ---------- */
.alert {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 0.5rem;
  border-radius: 8px; padding: 0.6rem 0.8rem; margin: 0 0 0.9rem; font-size: 0.92rem;
}
.alert.error { background: var(--err-soft); color: var(--err); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--accent-soft); color: var(--text); }
.warn-text { color: var(--warn); }

.editor-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.9rem; }
.eyebrow { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.id { font-size: 0.95rem; word-break: break-all; }
.badges { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.badge, .status {
  font-size: 0.8rem; font-weight: 600; padding: 0.2rem 0.6rem; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.rw { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.status.saved { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.status.dirty { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.status.saving { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.status.failed, .status.conflict { background: var(--err-soft); color: var(--err); border-color: transparent; }
.actions { margin-top: 0.25rem; }
.footnote { margin: 0.75rem 0 0; }

.conflict { border-color: var(--err); }
pre.diff { min-height: 120px; }

/* ---------- recent ---------- */
.recent { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.35rem; }
.recent li { display: flex; align-items: center; gap: 0.5rem; padding: 0.45rem 0.55rem; border-radius: 8px; background: var(--surface-2); }
.recent a { flex: 1; min-width: 0; text-decoration: none; color: var(--text); }
.recent .rtitle { display: block; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent .rid { display: block; font: 12px var(--mono); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- share dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 18px;
  background: var(--surface); color: var(--text); width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
}
dialog::backdrop { background: rgba(10, 12, 16, 0.55); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin: 0; }
.share-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 0.5rem; }
@media (max-width: 640px) { .share-grid { grid-template-columns: 1fr; } }
.share-panel { border: 1px solid var(--border); border-radius: 10px; padding: 14px; display: grid; gap: 0.5rem; justify-items: center; text-align: center; min-width: 0; }
.share-panel.secret { border-color: var(--warn); }
.share-panel .row { width: 100%; }
.share-panel input { font: 12px var(--mono); }
canvas.qr { border-radius: 6px; max-width: 100%; height: auto !important; image-rendering: pixelated; }

/* ---------- toasts ---------- */
#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: grid; gap: 0.5rem; z-index: 50; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--bg); padding: 0.6rem 1rem; border-radius: 10px; font-size: 0.92rem; box-shadow: 0 6px 24px rgba(0,0,0,.2); transition: opacity .3s, transform .3s; }
.toast-error { background: var(--err); color: #fff; }
.toast-out { opacity: 0; transform: translateY(6px); }

