:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #f2f2f7;
  --text: #1c1c1e;
  --muted: #6e6e73;
  --faint: #aeaeb2;
  --border: rgba(0, 0, 0, 0.09);
  --accent: #f25c05;
  --accent-soft: rgba(242, 92, 5, 0.12);
  --gold: #ffb300;
  --danger: #d70015;
  --ok: #34c759;
  --warn: #ff9f0a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --text: #f2f2f7;
    --muted: #aeaeb2;
    --faint: #636366;
    --border: rgba(255, 255, 255, 0.12);
    --accent-soft: rgba(242, 92, 5, 0.22);
  }
}
* { box-sizing: border-box; }
.sheet[hidden], .menu[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
#app { display: flex; height: 100svh; }

/* ---------- sidebar ---------- */
#sidebar {
  width: 330px; flex: none; display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border);
}
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 8px; }
.brand h1 { font-size: 22px; margin: 0; letter-spacing: -0.02em; }
.btn-primary {
  background: var(--accent); color: #fff; border: 0; border-radius: 10px;
  font-size: 14px; font-weight: 600; padding: 8px 13px; cursor: pointer;
  text-decoration: none; display: inline-block;
}
.btn-primary:active { transform: scale(0.97); }
.btn-quiet {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; font-size: 14px; font-weight: 500; padding: 7px 12px;
  cursor: pointer; text-decoration: none; display: inline-block;
}
.btn-quiet.danger { color: var(--danger); }
.btn-quiet:active { transform: scale(0.97); }

#sync-line {
  display: flex; align-items: center; gap: 8px; margin: 2px 16px 10px;
  padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted); font-size: 13px;
  cursor: pointer; text-align: left; width: calc(100% - 32px);
}
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--faint); }
.dot-ok { background: var(--ok); }
.dot-pending { background: var(--warn); }
.dot-busy { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.dot-fail { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.35; } }

.search-wrap { padding: 0 16px 10px; }
#search {
  width: 100%; padding: 9px 12px; border-radius: 10px; font-size: 15px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}

#tag-list { padding: 2px 10px 8px; display: flex; flex-wrap: wrap; gap: 6px; }
#tag-list button {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  border-radius: 999px; padding: 4px 11px; font-size: 13px; cursor: pointer;
}
#tag-list button.active { background: var(--accent-soft); color: var(--accent); border-color: transparent; font-weight: 600; }

#note-list { list-style: none; margin: 0; padding: 0 10px 12px; overflow-y: auto; flex: 1; }
#note-list li {
  padding: 11px 12px; border-radius: 12px; cursor: pointer; position: relative;
  border-bottom: 1px solid transparent;
}
#note-list li + li { border-top: 1px solid var(--border); border-radius: 0; }
#note-list li.selected { background: var(--accent-soft); border-radius: 12px; border-top-color: transparent; }
#note-list li.selected + li { border-top-color: transparent; }
.nl-title { font-weight: 600; font-size: 15px; display: flex; align-items: center; gap: 7px; }
.nl-title .unsynced { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }
.nl-title .pin { color: var(--gold); font-size: 12px; }
.nl-meta { color: var(--muted); font-size: 12.5px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nl-tags { color: var(--accent); }
.nl-agent { color: var(--faint); font-style: italic; }
.nl-empty { color: var(--muted); padding: 18px 12px; font-size: 14px; }

.side-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
.who { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- editor pane ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--surface); position: relative; }
.editor-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--border); gap: 8px;
}
#btn-back { display: none; }
.editor-tools { display: flex; gap: 7px; margin-left: auto; position: relative; }
.menu {
  position: absolute; top: 52px; right: 14px; z-index: 30; min-width: 210px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.16); padding: 5px; display: flex; flex-direction: column;
}
.menu button {
  background: none; border: 0; text-align: left; padding: 9px 11px; border-radius: 8px;
  font-size: 14.5px; color: var(--text); cursor: pointer;
}
.menu button:hover { background: var(--surface-2); }

#editor { flex: 1; overflow: hidden; }
#editor .cm-editor { height: 100%; }
#preview { flex: 1; overflow-y: auto; padding: 22px 26px; line-height: 1.6; font-size: 16px; }
#preview h1, #preview h2, #preview h3 { letter-spacing: -0.01em; }
#preview code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 0.92em; }
#preview pre { background: var(--surface-2); padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
#preview pre code { background: none; padding: 0; }
#preview a { color: var(--accent); }

#tag-row {
  display: flex; align-items: center; gap: 7px; flex-wrap: wrap;
  padding: 9px 16px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
#note-tags { display: contents; }
.tag-chip {
  background: var(--accent-soft); color: var(--accent); border: 0; cursor: pointer;
  border-radius: 999px; padding: 4px 10px; font-size: 13px; font-weight: 500;
}
#tag-input {
  border: 0; background: transparent; color: var(--text); font-size: 13.5px;
  padding: 5px 2px; min-width: 110px; outline: none;
}

#empty-state {
  position: absolute; inset: 0; display: grid; place-content: center; text-align: center;
  color: var(--muted); gap: 6px; padding: 20px;
}
#empty-state img { margin: 0 auto; border-radius: 16px; opacity: 0.92; }
#main.has-note #empty-state { display: none; }
#main:not(.has-note) #editor, #main:not(.has-note) #tag-row, #main:not(.has-note) .editor-head { visibility: hidden; }

/* ---------- sheets ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: end center;
  background: rgba(0,0,0,0.35); padding: 0;
}
@media (min-width: 700px) { .sheet { place-items: center; padding: 24px; } }
.sheet-card {
  background: var(--surface); color: var(--text); width: 100%; max-width: 560px;
  max-height: 86svh; overflow-y: auto; border-radius: 20px 20px 0 0; padding: 22px 22px calc(20px + env(safe-area-inset-bottom));
}
@media (min-width: 700px) { .sheet-card { border-radius: 20px; } }
.sheet-card h2 { margin: 0 0 14px; font-size: 20px; }
.sheet-card h3 { margin: 0 0 6px; font-size: 15px; }
.sheet-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.ai-result {
  background: var(--surface-2); border-radius: 12px; padding: 14px 16px;
  white-space: pre-wrap; line-height: 1.55; font-size: 15px; max-height: 46svh; overflow-y: auto;
}
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; font-size: 14.5px; }
.setting-block { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 14px; }
.fine { color: var(--muted); font-size: 12.5px; line-height: 1.5; margin: 4px 0 10px; }
#token-list { list-style: none; margin: 0 0 10px; padding: 0; font-size: 13.5px; }
#token-list li { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
#token-list .revoked { color: var(--faint); text-decoration: line-through; }
.token-mint { display: flex; gap: 8px; }
.token-mint input {
  flex: 1; padding: 8px 11px; border-radius: 10px; font-size: 14px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.token-new {
  background: var(--accent-soft); border-radius: 10px; padding: 10px 12px;
  font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; word-break: break-all;
}
#toast {
  position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: all 0.25s ease; z-index: 80;
  max-width: min(92vw, 480px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- mobile: one pane at a time ---------- */
@media (max-width: 760px) {
  #sidebar { width: 100%; border-right: 0; }
  #main { display: none; }
  #app.show-editor #sidebar { display: none; }
  #app.show-editor #main { display: flex; }
  #btn-back { display: inline-block; }
}

/* The new-note button is a bottle cap in the list's corner. */
#sidebar { position: relative; }
#note-list { position: relative; z-index: 1; padding-bottom: 104px; }
#tag-list { position: relative; z-index: 1; }
.fab-cap {
  position: absolute; right: 16px; bottom: calc(56px + env(safe-area-inset-bottom, 0px));
  width: 62px; height: 62px; padding: 0; border: none; background: none;
  cursor: pointer; z-index: 6; -webkit-tap-highlight-color: transparent;
  filter: drop-shadow(0 6px 14px rgba(154, 52, 18, .45));
  transition: transform .13s ease;
}
.fab-cap:active { transform: rotate(-8deg) scale(.93); }
.fab-cap svg { display: block; width: 100%; height: 100%; }

/* Face ID launch lock */
#lock-screen {
  position: fixed; inset: 0; z-index: 200; background: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
#lock-screen[hidden] { display: none; }
.lock-card {
  text-align: center; display: flex; flex-direction: column;
  align-items: center; gap: 10px; padding: 24px;
}
.lock-card h2 { margin: 6px 0 0; }
.lock-card p { margin: 0 0 8px; color: var(--muted); }
