:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --side: #eef0f4;
  --text: #16181c;
  --muted: #6b7280;
  --line: #e3e6ec;
  --accent: #2f6fed;
  --accent-soft: #e6eefe;
  --accent-text: #ffffff;
  --danger: #d93a3a;
  --ok: #1f9d55;
  --warn: #d98a00;
  --pin: #e8a10c;
  --shadow: 0 8px 28px rgba(16, 24, 40, .14);
  --radius: 14px;
  --side-w: clamp(104px, 31vw, 170px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --panel: #1f2228;
    --side: #1a1d22;
    --text: #eceef2;
    --muted: #9aa1ad;
    --line: #2d3139;
    --accent: #5b8cff;
    --accent-soft: #24304a;
    --accent-text: #0d1220;
    --shadow: 0 8px 28px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px 10px max(16px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.topbar h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 10px; color: var(--muted); }
.icon-btn:active { background: var(--line); }
.sync-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--muted);
  padding: 6px 10px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line);
  max-width: 46vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sync-pill[data-state="ok"] .dot { background: var(--ok); }
.sync-pill[data-state="pending"] .dot,
.sync-pill[data-state="syncing"] .dot { background: var(--warn); }
.sync-pill[data-state="syncing"] .dot { animation: pulse 1s infinite ease-in-out; }
.sync-pill[data-state="error"] .dot { background: var(--danger); }
.sync-pill[data-state="offline"] .dot { background: var(--muted); }
@keyframes pulse { 50% { opacity: .25; } }

/* ---------- Body layout (theo bản vẽ) ---------- */
.body { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: var(--side-w); flex: none;
  background: var(--side);
  border-right: 1px solid var(--line);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding-left: env(safe-area-inset-left);
  padding-bottom: calc(90px + env(safe-area-inset-bottom));
}
.side-head {
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); padding: 14px 12px 8px;
}
.side-item {
  width: 100%; display: flex; align-items: center; gap: 6px;
  text-align: left; padding: 13px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 15px; line-height: 1.25;
  overflow-wrap: anywhere;
  position: relative;
}
.side-item .name { flex: 1; min-width: 0; }
.side-item .count { font-size: 12px; color: var(--muted); flex: none; }
.side-item.active { background: var(--panel); color: var(--accent); font-weight: 600; }
.side-item.active::before {
  content: ""; position: absolute; left: 0; top: 8px; bottom: 8px; width: 3px; border-radius: 0 3px 3px 0; background: var(--accent);
}
.side-search { color: var(--muted); border-top: 1px solid var(--line); }
.side-search.active { color: var(--accent); }
.side-add { width: 100%; text-align: left; padding: 13px 12px; color: var(--accent); font-size: 14px; font-weight: 600; }

.handle {
  flex: none; display: none; width: 22px; height: 22px; place-items: center; color: var(--muted); touch-action: none; cursor: grab;
}
.editing .handle { display: grid; }
.editing .side-item { padding-left: 4px; padding-right: 6px; gap: 2px; }
.editing .side-item .count { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--panel); }
.main-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
  padding-right: max(14px, env(safe-area-inset-right));
}
.main-head h2 { flex: 1; min-width: 0; margin: 0; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.text-btn { color: var(--accent); font-size: 15px; font-weight: 600; padding: 6px 4px; }
.text-btn.danger { color: var(--danger); }
.edit-tools { display: flex; gap: 6px; padding: 10px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.chip-btn { font-size: 13px; padding: 6px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chip-btn.danger { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

.content {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  padding-right: env(safe-area-inset-right);
}
.group-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 12px 14px 4px; }

/* ---------- Dòng nội dung + nút copy ---------- */
.note {
  display: flex; align-items: stretch; gap: 0;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.note .handle { align-self: center; margin-left: 8px; }
.note-body {
  flex: 1; min-width: 0; text-align: left; padding: 12px 10px 12px 14px;
  display: flex; flex-direction: column; gap: 4px;
}
.note-body:active { background: var(--bg); }
.note-text {
  white-space: pre-wrap; word-break: break-word; font-size: 15px;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.note-meta { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--muted); }
.note-meta .pin { color: var(--pin); display: inline-flex; align-items: center; gap: 3px; font-weight: 600; }
.note-meta .cat-tag { background: var(--accent-soft); color: var(--accent); padding: 1px 7px; border-radius: 999px; font-weight: 600; }
.copy-btn {
  flex: none; width: 74px;
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 12px; font-weight: 600; color: var(--accent);
}
.copy-btn:active { background: var(--accent-soft); }
.copy-btn.done { color: var(--ok); }
.editing .copy-btn { display: none; }
mark { background: color-mix(in srgb, var(--pin) 35%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }

.sortable-ghost { opacity: .35; }
.sortable-chosen { box-shadow: var(--shadow); }

.empty { padding: 36px 20px; text-align: center; color: var(--muted); font-size: 15px; }
.empty strong { display: block; color: var(--text); margin-bottom: 6px; font-size: 16px; }
.empty .text-btn { margin-top: 10px; }

.search-box { padding: 10px 14px; border-bottom: 1px solid var(--line); }
.search-box input {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); font-size: 16px; outline: none;
}
.search-box input:focus { border-color: var(--accent); }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: max(18px, env(safe-area-inset-right)); bottom: calc(18px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; box-shadow: var(--shadow);
}
.fab:active { transform: scale(.95); }
.fab[hidden] { display: none; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(10, 12, 16, .45); }
.modal-card {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: calc(env(safe-area-inset-top) + 14px);
  width: min(560px, calc(100% - 24px));
  max-height: calc(100dvh - env(safe-area-inset-top) - 28px);
  overflow-y: auto;
  background: var(--panel); border-radius: 18px; box-shadow: var(--shadow);
  padding: 16px;
}
.modal-card h3 { margin: 0 0 12px; font-size: 18px; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
.field input, .field textarea, .field select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); font-size: 16px; outline: none;
}
.field textarea { min-height: 130px; resize: vertical; line-height: 1.45; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.switch-row { display: flex; align-items: center; justify-content: space-between; padding: 6px 0 12px; font-size: 15px; }
.switch-row input { width: 22px; height: 22px; accent-color: var(--accent); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 4px; }
.modal-actions .spacer { flex: 1; }
.btn { padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: 15px; background: var(--bg); border: 1px solid var(--line); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.help { font-size: 13px; color: var(--muted); line-height: 1.5; margin: 0 0 12px; }
.help code { font-size: 12px; background: var(--bg); padding: 1px 5px; border-radius: 5px; }
.status-line { font-size: 13px; color: var(--muted); margin: 4px 0 12px; }
.status-line.err { color: var(--danger); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); font-size: 14px; font-weight: 600;
  padding: 9px 16px; border-radius: 999px; transition: .2s; z-index: 60; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
