:root {
  --bg: #fbfaf7;
  --panel: #f1efe9;
  --text: #22262a;
  --muted: #6b7178;
  --line: #ded9cf;
  --accent: #1f4e5f;
  --accent-text: #ffffff;
  --danger: #a32e2e;
  --warn: #9a5b00;
  --ok: #2f7a45;
  --ff: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, "Noto Serif Khmer", "Khmer UI", serif;
  --fs: 16px;
  --lh: 1.65;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191b;
    --panel: #202326;
    --text: #e6e4df;
    --muted: #9aa0a6;
    --line: #34383c;
    --accent: #5fa8c0;
    --accent-text: #0e1a1f;
    --danger: #e07a7a;
    --warn: #e0a04a;
    --ok: #6cc287;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, select, input, textarea { font: inherit; color: inherit; }
button, select {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  min-height: 40px;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; border-color: transparent; }
button.danger { color: var(--danger); }
a.btnlink { display: inline-block; background: var(--accent); color: var(--accent-text); padding: 10px 16px; border-radius: 8px; text-decoration: none; font-weight: 600; }
code { background: var(--panel); padding: 1px 5px; border-radius: 4px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 20px 0; }

/* Login & setup */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login > form, .login > div { width: 100%; max-width: 420px; }
.login h1 { margin: 0 0 4px; font-size: 28px; }
.login label { display: block; margin: 14px 0 4px; font-weight: 600; font-size: 14px; }
.login input {
  width: 100%; padding: 10px 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; font-size: 16px;
}
.login button { width: 100%; margin-top: 20px; }
.login.setup { align-items: start; }
.muted { color: var(--muted); }
.err { color: var(--danger); }

/* App layout */
#app { height: 100%; height: 100dvh; display: flex; flex-direction: column; }
.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.bar .status { flex: 1; text-align: center; font-size: 13px; color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.warn { color: var(--warn); }
.status.ok { color: var(--ok); }
.inline { margin: 0; }
.main { flex: 1; display: flex; min-height: 0; position: relative; }

#list {
  width: 300px; flex: none; border-right: 1px solid var(--line);
  background: var(--panel); display: flex; flex-direction: column; min-height: 0;
}
#list input { margin: 10px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
#items { list-style: none; margin: 0; padding: 0 6px 12px; overflow-y: auto; flex: 1; }
.item {
  display: block; width: 100%; text-align: left; background: transparent;
  border: 1px solid transparent; border-radius: 8px; padding: 8px 10px; margin-bottom: 2px;
}
.item.active { background: var(--bg); border-color: var(--line); }
.item .t { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .m { display: block; font-size: 12px; color: var(--muted); }
.badge { display: inline-block; padding: 0 6px; border-radius: 99px; border: 1px solid var(--line); margin-right: 6px; font-size: 11px; }
.badge.ready { color: var(--warn); border-color: var(--warn); }
.badge.published { color: var(--ok); border-color: var(--ok); }
.empty { padding: 12px; color: var(--muted); font-size: 14px; }

#editor { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#title {
  border: 0; outline: 0; background: transparent; padding: 18px 20px 8px;
  font-family: var(--ff); font-weight: 700; font-size: calc(var(--fs) * 1.6); line-height: 1.25; width: 100%;
}
#body {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent;
  padding: 8px 20px 20px; font-family: var(--ff); font-size: var(--fs); line-height: var(--lh); width: 100%;
}
.preview {
  flex: 1; overflow-y: auto; padding: 8px 20px 20px; font-family: var(--ff); font-size: var(--fs); line-height: var(--lh);
}
.preview img { max-width: 100%; height: auto; }
.preview blockquote { margin: 1em 0; padding-left: 14px; border-left: 3px solid var(--line); color: var(--muted); }
.tools {
  position: relative;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--panel);
}
.tools .count { flex: 1 1 100%; font-size: 12px; color: var(--muted); }
.tools button, .tools select { flex: 1 1 auto; }

/* Text style panel */
#fmt {
  position: absolute; bottom: 100%; left: 12px; width: min(330px, calc(100% - 24px));
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; margin-bottom: 6px; box-shadow: 0 4px 18px rgba(0,0,0,.25); z-index: 5;
}
#fmt label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
#fmt select { display: block; width: 100%; margin-top: 4px; }
#fmt .row { display: flex; align-items: center; gap: 8px; }
#fmt .row span { font-size: 13px; color: var(--muted); }
#fmt .row strong { min-width: 2ch; text-align: center; }
#fmt .row button { flex: none; }

#backdrop { display: none; }

@media (min-width: 820px) {
  #app.previewing #btnList { display: none; }
  #app:not(.previewing) #list {
    position: absolute; z-index: 20; top: 0; bottom: 0; left: 0; width: 320px;
    transform: translateX(-102%); transition: transform .2s ease; box-shadow: 2px 0 12px rgba(0,0,0,.25);
  }
  #app:not(.previewing) #list.open { transform: none; }
  #app:not(.previewing) #backdrop.show { display: block; position: absolute; inset: 0; z-index: 10; background: rgba(0,0,0,.35); }
  .tools .count { flex: 1 1 auto; }
  .tools button, .tools select { flex: none; }
  #title, #body, .preview { padding-left: max(24px, calc((100% - 760px) / 2)); padding-right: max(24px, calc((100% - 760px) / 2)); }
}
@media (max-width: 819px) {
  #list {
    position: absolute; z-index: 20; top: 0; bottom: 0; left: 0; width: min(86%, 340px);
    transform: translateX(-102%); transition: transform .2s ease; box-shadow: 2px 0 12px rgba(0,0,0,.25);
  }
  #list.open { transform: none; }
  #backdrop.show { display: block; position: absolute; inset: 0; z-index: 10; background: rgba(0,0,0,.35); }
  #title { padding: 14px 14px 6px; }
  #body, .preview { padding: 6px 14px 14px; }
}

/* Preview typography: compact headings and tighter spacing around lists */
.preview > :first-child { margin-top: 0; }
.preview h1, .preview h2, .preview h3, .preview h4, .preview h5, .preview h6 {
  line-height: 1.25; margin: 1.1em 0 .35em;
}
.preview .ptitle { font-size: 1.9em; font-weight: 700; line-height: 1.2; margin: 0 0 .7em; }
.preview h1 { font-size: 1.5em; }
.preview h2 { font-size: 1.3em; }
.preview h3 { font-size: 1.15em; }
.preview h4, .preview h5, .preview h6 { font-size: 1em; }
.preview p { margin: 0 0 .9em; }
.preview ul, .preview ol { margin: .2em 0 .9em; padding-left: 1.4em; }
.preview li { margin: .15em 0; }
.preview hr { border: 0; border-top: 1px solid var(--line); margin: 1.2em 0; }
