:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --side: #fbfbfa;
  --text: #1f2328;
  --muted: #6b7280;
  --line: #e7e5e4;
  --hover: #f1f0ee;
  --active: #e8effd;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --mark: #fde68a;
  --danger: #dc2626;
  --code: #f3f4f6;
  --shadow: 0 10px 30px rgba(0, 0, 0, .12);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #18181b; --panel: #1f1f23; --side: #1b1b1e; --text: #e7e7ea; --muted: #9a9aa3;
  --line: #2e2e33; --hover: #26262b; --active: #1e2a44; --accent: #4f8cff; --mark: #6b5a12;
  --danger: #f87171; --code: #2a2a30; --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18181b; --panel: #1f1f23; --side: #1b1b1e; --text: #e7e7ea; --muted: #9a9aa3;
    --line: #2e2e33; --hover: #26262b; --active: #1e2a44; --accent: #4f8cff; --mark: #6b5a12;
    --danger: #f87171; --code: #2a2a30; --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }
kbd { font: 12px var(--mono); padding: 1px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: var(--panel); }
.muted { color: var(--muted); }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 14px; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.primary { background: var(--accent); color: var(--accent-text); }
.icon-btn[aria-pressed="true"] { color: var(--accent); }
.icon-btn[aria-pressed="true"] svg { fill: currentColor; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-size: 15px; }
.login-error { margin: 0; color: var(--danger); font-size: 13px; min-height: 1em; }

/* Layout */
.app { display: grid; grid-template-columns: minmax(280px, 360px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.side-head { display: flex; gap: 6px; align-items: center; padding: 12px; padding-top: max(12px, env(safe-area-inset-top)); position: relative; }
.search { flex: 1; display: flex; align-items: center; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 9px; padding: 0 8px; min-width: 0; }
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.search svg { color: var(--muted); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; padding: 8px 0; }
.search input::-webkit-search-cancel-button { display: none; }
.search .hint { border: 0; background: none; color: var(--muted); padding: 0; }
.menu { position: absolute; right: 12px; top: 52px; z-index: 20; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px; min-width: 200px; display: grid; }
.menu button { border: 0; background: none; text-align: left; padding: 8px 10px; border-radius: 6px; }
.menu button:hover { background: var(--hover); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

.tagbar { display: flex; gap: 6px; padding: 0 12px 8px; overflow-x: auto; scrollbar-width: none; }
.tagbar:empty { display: none; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 99px; padding: 2px 10px; font-size: 12.5px; white-space: nowrap; color: var(--muted); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.list-title { padding: 4px 16px 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); display: flex; justify-content: space-between; }
.list-title button { border: 0; background: none; color: var(--accent); font-size: 12px; text-transform: none; letter-spacing: 0; padding: 0; }

.list { list-style: none; margin: 0; padding: 0 6px max(12px, env(safe-area-inset-bottom)); overflow-y: auto; flex: 1; overscroll-behavior: contain; }
.item { padding: 9px 10px; border-radius: 8px; cursor: pointer; display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; }
.item:hover { background: var(--hover); }
.item.sel { background: var(--active); }
.item .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; gap: 6px; align-items: center; }
.item .t .pin { color: var(--accent); font-size: 11px; }
.item .s { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word; }
.item .d { font-size: 11.5px; color: var(--muted); display: flex; gap: 8px; }
.item .d .tg { color: var(--accent); }
.item mark, .body mark.hl { background: var(--mark); color: inherit; border-radius: 2px; }
.list .more { text-align: center; padding: 10px; }
.list .none { padding: 24px 12px; color: var(--muted); text-align: center; }

/* Editor */
.editor { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--panel); }
.empty { margin: auto; text-align: center; padding: 24px; max-width: 420px; }
.doc { display: flex; flex-direction: column; min-height: 0; height: 100%; }
.doc-bar { display: flex; align-items: center; gap: 4px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
.doc-bar .back { display: none; }
.toolbar { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; flex: 1; min-width: 0; }
.toolbar button { border: 0; background: none; min-width: 32px; height: 32px; border-radius: 6px; color: var(--muted); font-size: 14px; flex: none; }
.toolbar button:hover { background: var(--hover); color: var(--text); }
.status { font-size: 12px; color: var(--muted); white-space: nowrap; padding: 0 6px; }
.status.err { color: var(--danger); }
.trash-banner { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 10px 16px; background: color-mix(in srgb, var(--danger) 10%, var(--panel)); font-size: 14px; }
.paper { flex: 1; overflow-y: auto; padding: 28px clamp(16px, 5vw, 64px) 120px; }
.paper > * { max-width: 760px; margin-left: auto; margin-right: auto; display: block; width: 100%; }
.title { border: 0; outline: 0; background: none; font-size: 26px; font-weight: 700; padding: 0; line-height: 1.3; }
.tags { border: 0; outline: 0; background: none; font-size: 13px; color: var(--accent); padding: 6px 0 0; }
.meta { font-size: 12px; color: var(--muted); padding: 2px 0 16px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.body { outline: 0; min-height: 50vh; word-wrap: break-word; position: relative; }
.body.blank::before { content: attr(data-placeholder); color: var(--muted); position: absolute; pointer-events: none; }
.body p { margin: 0 0 .6em; }
.body h1, .body h2, .body h3 { line-height: 1.3; margin: 1.1em 0 .4em; }
.body h2 { font-size: 1.3em; }
.body ul, .body ol { padding-left: 1.5em; margin: 0 0 .6em; }
.body a { color: var(--accent); }
.body pre, .body code { font-family: var(--mono); font-size: 13.5px; background: var(--code); border-radius: 6px; }
.body code { padding: 1px 4px; }
.body pre { padding: 10px 12px; overflow-x: auto; white-space: pre-wrap; }
.body pre code { padding: 0; background: none; }
.body blockquote { margin: 0 0 .6em; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.body table { border-collapse: collapse; margin: 0 0 .8em; max-width: 100%; display: block; overflow-x: auto; }
.body td, .body th { border: 1px solid var(--line); padding: 4px 8px; vertical-align: top; }
.body img { max-width: 100%; height: auto; border-radius: 6px; vertical-align: bottom; }
.body img[data-up] { opacity: .45; animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { to { opacity: .75; } }
.body hr { border: 0; border-top: 1px solid var(--line); }

/* Dialog & toast */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(420px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog form { padding: 20px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 17px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
dialog input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 4px 0 0; padding: 0; }
dialog table { border-collapse: collapse; width: 100%; font-size: 14px; }
dialog td { padding: 4px 0; }
dialog td:first-child { white-space: nowrap; padding-right: 12px; }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; display: flex; gap: 12px; align-items: center; box-shadow: var(--shadow); z-index: 50; font-size: 14px; max-width: calc(100% - 32px); }
.toast button { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 0; }

.fab { display: none; }

/* Mobile: satu panel */
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .app.reading .sidebar { display: none; }
  .app:not(.reading) .editor { display: none; }
  .doc-bar .back { display: inline-grid; }
  .fab { position: fixed; right: 18px; bottom: max(18px, env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 16px; border: 0; background: var(--accent); color: var(--accent-text); box-shadow: var(--shadow); display: grid; place-items: center; z-index: 10; }
  .fab svg { width: 24px; height: 24px; }
  .app[hidden] ~ .fab, .app.reading ~ .fab { display: none; }
  .paper { padding-top: 18px; }
  .title { font-size: 22px; }
}
