:root {
  --accent: #3361cc;
  --accent-soft: #e5ecf9;
  --bg: #ffffff;
  --bg-2: #f6f7f7;
  --line: #dcdcde;
  --fg: #2c3338;
  --fg-2: #646970;
  --fg-3: #8c8f94;
  --sel: #e5ecf9;
  --sel-fg: #2c3338;
  --danger: #d63638;
  --warn-bg: #fcf9e8;
  --warn-line: #dba617;
  --logo-fg: #fff;
  --text-size: 17px;
  --line-len: 760px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #5e85e2; --accent-soft: #23304a; --bg: #1d2327; --bg-2: #2c3338; --line: #3c434a;
    --fg: #f0f0f1; --fg-2: #a7aaad; --fg-3: #8c8f94; --sel: #3361cc; --sel-fg: #fff;
    --warn-bg: #3a3420; --logo-fg: #1d2327; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --accent: #5e85e2; --accent-soft: #23304a; --bg: #1d2327; --bg-2: #2c3338; --line: #3c434a;
  --fg: #f0f0f1; --fg-2: #a7aaad; --fg-3: #8c8f94; --sel: #3361cc; --sel-fg: #fff;
  --warn-bg: #3a3420; --logo-fg: #1d2327; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--fg);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased; overflow: hidden;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, select, textarea { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.icon-btn { width: 36px; height: 36px; display: inline-grid; place-items: center; border-radius: 6px; color: var(--fg-2); }
.icon-btn:hover { background: var(--bg-2); color: var(--fg); }
.icon-btn.on { color: var(--accent); }
.link-btn { color: var(--accent); font-weight: 500; padding: 4px 6px; border-radius: 4px; }
.link-btn:hover { background: var(--accent-soft); }
.danger { color: var(--danger) !important; }
.btn { padding: 8px 14px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); font-weight: 500; }
.btn:hover { border-color: var(--fg-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.muted { color: var(--fg-2); }
.err { color: var(--danger); min-height: 1.2em; margin: 0; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 330px 1fr; height: 100dvh; }
.app.focus { grid-template-columns: 0 1fr; }
.app.focus .list-pane { visibility: hidden; }
.bar { display: flex; align-items: center; gap: 4px; height: 56px; padding: 0 10px; flex: none; }
.bar h2 { flex: 1; margin: 0; font-size: 16px; font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }

/* nav drawer */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; opacity: 0; pointer-events: none; transition: opacity .2s; }
.nav {
  position: fixed; z-index: 21; top: 0; bottom: 0; left: 0; width: 270px; background: var(--bg-2);
  border-right: 1px solid var(--line); transform: translateX(-100%); transition: transform .2s ease;
  display: flex; flex-direction: column; padding: 16px 10px calc(10px + env(safe-area-inset-bottom)); overflow-y: auto;
}
.app.nav-open .nav { transform: none; }
.app.nav-open .scrim { opacity: 1; pointer-events: auto; }
.nav-item, .nav-tag { display: flex; align-items: center; gap: 12px; width: 100%; padding: 9px 12px; border-radius: 6px; text-align: left; }
.nav-item:hover, .nav-tag:hover { background: var(--line); }
.nav-item.active, .nav-tag.active { background: var(--accent); color: #fff; }
.nav-tags-head { display: flex; justify-content: space-between; align-items: center; margin: 18px 12px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--fg-3); }
.nav-tags { display: flex; flex-direction: column; }
.nav-tag { padding: 7px 12px 7px 44px; }
.nav-tag .count { margin-left: auto; font-size: 12px; opacity: .7; }
.sync-status { margin-top: auto; padding: 14px 12px 4px; font-size: 13px; color: var(--fg-2); display: flex; gap: 8px; align-items: center; }
.sync-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fg-3); flex: none; }
.sync-status.ok .dot { background: #00a32a; }
.sync-status.busy .dot { background: var(--accent); }
.sync-status.off .dot { background: var(--warn-line); }

/* list */
.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-width: 0; background: var(--bg); }
.search { display: flex; align-items: center; gap: 8px; margin: 0 12px 8px; padding: 0 10px; height: 38px; border-radius: 6px; background: var(--bg-2); color: var(--fg-3); }
.search input { flex: 1; border: 0; outline: 0; background: none; min-width: 0; color: var(--fg); }
.search:focus-within { box-shadow: 0 0 0 2px var(--accent); }
.list { flex: 1; overflow-y: auto; outline: 0; padding-bottom: env(safe-area-inset-bottom); }
.item { display: flex; gap: 8px; padding: 10px 16px 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; user-select: none; }
.item .pin { width: 16px; color: var(--accent); padding-top: 2px; }
.item .pin svg { width: 14px; height: 14px; }
.item .txt { min-width: 0; flex: 1; }
.item .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .p { color: var(--fg-2); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.list.condensed .item .p { display: none; }
.item:hover { background: var(--bg-2); }
.item.selected { background: var(--sel); color: var(--sel-fg); }
.item.selected .p { color: inherit; opacity: .8; }
.item.selected .pin { color: inherit; }
.item mark { background: rgba(255, 213, 0, .45); color: inherit; border-radius: 2px; }
.list-empty { padding: 40px 20px; text-align: center; color: var(--fg-3); }

/* editor */
.editor-pane { position: relative; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.editor-bar { border-bottom: 1px solid transparent; }
.editor-wrap { flex: 1; overflow-y: auto; display: flex; justify-content: center; }
#editor, .preview {
  width: 100%; max-width: var(--line-len); padding: 24px 32px 80px; font-size: var(--text-size); line-height: 1.6;
}
#editor { border: 0; outline: 0; resize: none; background: transparent; color: var(--fg); height: 100%; display: block; }
.app.full-lines { --line-len: 100%; }
.preview { overflow-wrap: anywhere; }
.preview h1, .preview h2, .preview h3 { line-height: 1.25; }
.preview a { color: var(--accent); }
.preview code { background: var(--bg-2); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.preview pre { background: var(--bg-2); padding: 12px; border-radius: 6px; overflow-x: auto; }
.preview pre code { background: none; padding: 0; }
.preview blockquote { margin: 0; padding-left: 14px; border-left: 3px solid var(--line); color: var(--fg-2); }
.preview img { max-width: 100%; }
.preview table { border-collapse: collapse; }
.preview td, .preview th { border: 1px solid var(--line); padding: 4px 8px; }
.preview li:has(> input[type=checkbox]) { list-style: none; margin-left: -1.3em; }
.preview input[type=checkbox] { width: 16px; height: 16px; margin-right: 6px; vertical-align: -2px; accent-color: var(--accent); cursor: pointer; }
.empty { flex: 1; display: grid; place-content: center; justify-items: center; color: var(--fg-3); gap: 6px; }
.empty svg { width: 64px; height: 64px; color: var(--line); stroke: none; }

.banner { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin: 0 16px 6px; padding: 8px 12px; border-radius: 6px; background: var(--warn-bg); border-left: 3px solid var(--warn-line); font-size: 14px; }
.banner span { flex: 1 1 240px; }

.tag-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 10px 24px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.tag-chips { display: contents; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 10px; border-radius: 14px; background: var(--bg-2); font-size: 14px; }
.chip button { width: 20px; height: 20px; display: grid; place-items: center; border-radius: 50%; color: var(--fg-3); }
.chip button:hover { background: var(--line); color: var(--fg); }
.chip button svg { width: 12px; height: 12px; }
#tagInput::-webkit-calendar-picker-indicator { display: none !important; }
#tagInput { flex: 1; min-width: 120px; border: 0; outline: 0; background: none; padding: 4px 0; }

/* side panels */
.panel {
  position: fixed; top: 56px; right: 12px; z-index: 15; width: 290px; max-height: calc(100dvh - 80px); overflow: auto;
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 28px rgba(0,0,0,.18); padding-bottom: 8px;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
.panel dl { margin: 0; padding: 8px 16px; }
.panel dt { color: var(--fg-3); font-size: 12px; margin-top: 10px; }
.panel dd { margin: 2px 0 0; }
.toggle { display: flex; justify-content: space-between; align-items: center; padding: 11px 16px; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.panel-btn { display: flex; gap: 12px; align-items: center; width: 100%; padding: 11px 16px; text-align: left; }
.panel-btn:hover, .toggle:hover { background: var(--bg-2); }

/* history */
.history { position: absolute; left: 0; right: 0; bottom: 0; z-index: 5; background: var(--bg); border-top: 1px solid var(--line); padding: 12px 20px calc(14px + env(safe-area-inset-bottom)); box-shadow: 0 -6px 20px rgba(0,0,0,.08); }
.history-top { display: flex; justify-content: space-between; gap: 12px; color: var(--fg-2); }
.history-top b { color: var(--fg); }
#histSlider { width: 100%; margin: 12px 0; accent-color: var(--accent); }
.history-btns { display: flex; justify-content: flex-end; gap: 8px; }
.app.in-history #editor { color: var(--fg-2); }

/* dialogs */
.dialog { width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 40px); padding: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--fg); }
.dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-body { padding: 4px 20px 20px; display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.dialog h3 { margin: 14px 0 0; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--fg-3); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; width: 100%; }
.row select, .dialog input { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); max-width: 60%; }
.devices { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.device { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; }
.device .n { flex: 1; min-width: 0; }
.device small { display: block; color: var(--fg-3); }
.pair-out { margin: 0; padding: 10px 14px; border-radius: 6px; background: var(--accent-soft); width: 100%; }
.pair-out b { font-size: 22px; letter-spacing: .1em; font-family: ui-monospace, monospace; }
.tag-edit { display: flex; gap: 6px; width: 100%; align-items: center; }
.tag-edit input { flex: 1; max-width: none !important; }

/* pairing */
.pair { min-height: 100dvh; display: grid; place-items: center; padding: 16px; background: var(--bg-2); overflow-y: auto; }
.pair-card { width: min(420px, 100%); background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.pair-logo { width: 48px; height: 48px; color: var(--accent); stroke: none; }
.pair-card h1 { margin: 0; font-size: 22px; }
.pair-card p { margin: 0; color: var(--fg-2); }
.pair-card label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; }
.pair-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); font-size: 16px; }
#pairCode { font-family: ui-monospace, monospace; letter-spacing: .15em; text-transform: uppercase; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0; pointer-events: none; background: #1d2327; color: #fff; padding: 10px 16px; border-radius: 8px; transition: .2s; z-index: 50; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.mobile-only { display: none; }

/* ---------- phone ---------- */
@media (max-width: 760px) {
  .app, .app.focus { grid-template-columns: 1fr; }
  .app.focus .list-pane { visibility: visible; }
  .list-pane, .editor-pane { grid-area: 1 / 1; border: 0; }
  .editor-pane { transform: translateX(100%); transition: transform .2s ease; z-index: 2; }
  .app.editing .editor-pane { transform: none; }
  .mobile-only { display: inline-grid; }
  .desktop-only { display: none; }
  #editor, .preview { padding: 12px 16px 60px; }
  .tag-bar { padding-left: 16px; padding-right: 16px; }
  .panel { left: 12px; right: 12px; width: auto; }
  .bar { padding-top: env(safe-area-inset-top); height: calc(56px + env(safe-area-inset-top)); }
  .item { padding: 12px 16px 12px 10px; }
}
