:root {
  --bg: #fafafa;
  --surface: #f0f0f0;
  --surface-2: #e7e7e7;
  --text: #383a42;
  --muted: #888888;
  --border: #dfdfdf;
  --accent: #e06c75;
  --link: #4078f2;
  --highlight: #888888;
  --danger: #e45649;
  --ok: #50a14f;
  --btn-bg: #e7e7e7;
  --btn-fg: #383a42;
  --btn-press: #e4d0f4;
  --btn-active: #f4d0d0;
  --lvl1: #e06c75;
  --lvl2: #d19a66;
  --lvl3: #c678dd;
  --lvl4: #61afef;
  --lvl5: #be85e6;
  --lvl6: #82b1ff;
  --lvl7: #c792ea;
  --lvl8: #b3e5fc;
  --radius: 0px;
  --font: Menlo, Monaco, "SF Mono", monospace;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  overscroll-behavior-y: none;
}

button, input, select, textarea {
  font-family: var(--font);
  color: inherit;
  border-radius: 0;
}

.view { display: none; }
.view.active { display: flex; flex-direction: column; }

/* ------------------------------------------------------------------ */
/* login                                                               */
/* ------------------------------------------------------------------ */

#view-login {
  height: 100dvh;
  justify-content: center;
}
.login {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 30px;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}
.login h1 { font-size: 20px; margin: 0 0 4px; color: var(--accent); }
.login p { color: var(--muted); margin: 0 0 8px; font-size: 13px; }

.search {
  width: 100%;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 0;
  padding: 10px 12px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  font-size: 14px;
}
.search:focus { border-color: var(--text); }

.btn {
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 11px 14px;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.btn:active { background: var(--btn-press); }
.btn.secondary { background: var(--surface); color: var(--text); }
.btn:disabled { opacity: .5; pointer-events: none; }

.iconbtn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 0;
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 17px;
  flex: none;
}
.iconbtn:active { background: var(--surface-2); }
.iconbtn.plain { color: var(--text); }

/* ------------------------------------------------------------------ */
/* pager (two full-screen pages, swipe like the homescreen)            */
/* ------------------------------------------------------------------ */

.pager {
  display: none;
  height: 100dvh;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.pager::-webkit-scrollbar { display: none; }
.pager.active { display: flex; }

.page {
  flex: none;
  min-width: 0;
  width: 100vw;
  height: 100dvh;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-snap-align: start;
  -webkit-overflow-scrolling: touch;
}

/* --- page 1: the text --- */

.text-flow {
  padding: calc(var(--safe-top) + 58px) 14px calc(70px + var(--safe-bottom));
}

.entry-sec {
  content-visibility: auto;
  contain-intrinsic-size: auto 400px;
  padding-bottom: 6px;
}
.entry-head {
  margin: 18px 0 4px;
  overflow-wrap: anywhere;
}
.head-text {
  color: var(--accent);
  font-size: 15px;
  font-weight: 400;
}

.para, .rh, .org-comment { overflow-wrap: anywhere; }
.para { margin: 0; white-space: pre-wrap; }

.rh {
  margin: 6px 0 2px;
  line-height: 1.5;
  font-size: 15px;
  font-weight: 400;
}
.rh.lvl2 { color: var(--lvl2); }
.rh.lvl3 { color: var(--lvl3); }
.rh.lvl4 { color: var(--lvl4); }

.org-comment {
  color: var(--muted);
  border-left: 2px solid var(--border);
  padding-left: 10px;
  margin: 0;
  white-space: pre-wrap;
  font-size: 14px;
}

a { color: var(--link); }

/* invisible raw-text markers (kept in the DOM so saving is lossless) */
.hid { display: none; }
.props-hid { display: none; }
.bold { font-weight: 700; }
[contenteditable]:focus { outline: none; }
.editing { caret-color: var(--text); }

/* link chips: visible label via CSS, raw text hidden inside */
.linkchip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0 6px;
  font-size: 13px;
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: bottom;
}
.linkchip:active { background: var(--btn-press); }
.chipicon { width: 13px; height: 13px; flex: none; }
.chip-label { overflow: hidden; text-overflow: ellipsis; }
.chip-label::after { content: attr(data-vis); }


.empty { color: var(--muted); text-align: center; padding: 40px 20px; }

.flash { animation: flashbg 1.6s ease-out; }
@keyframes flashbg {
  0% { background: var(--highlight); color: #fff; }
  100% { background: transparent; }
}


/* --- bottom bar: Emacs mode-line style, camera+mic left, scroll right --- */

.bottom-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 10px calc(4px + var(--safe-bottom));
  background: transparent;
  z-index: 10;
}
.bar-group { display: flex; gap: 8px; }

.bar-btn {
  width: 46px;
  height: 46px;
  border-radius: 0;
  border: 1px solid var(--border);
  background: var(--btn-bg);
  color: var(--btn-fg);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.bar-btn:active { background: var(--btn-press); }
.bar-btn svg { width: 21px; height: 21px; }
.bar-btn.rec { background: var(--btn-active); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(244, 208, 208, 0.9); }
  50% { box-shadow: 0 0 0 10px rgba(244, 208, 208, 0); }
}

.record-overlay {
  position: fixed;
  inset: 0;
  background: rgba(56, 58, 66, 0.35);
  display: none;
  place-items: center;
  z-index: 30;
}
.record-overlay.show { display: grid; }
.record-box {
  background: var(--btn-active);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-width: 240px;
}
.record-status { font-size: 14px; color: var(--text); }
.record-status.rec { color: var(--danger); font-weight: 600; }
.record-status.err { color: var(--danger); }
.meter {
  width: 200px;
  height: 3px;
  background: #fff;
  overflow: hidden;
}
.meter i { display: block; height: 100%; width: 0%; background: var(--danger); }

/* ------------------------------------------------------------------ */
/* camera                                                              */
/* ------------------------------------------------------------------ */

.camera-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  display: none;
  flex-direction: column;
  z-index: 70;
}
.camera-overlay.show { display: flex; }
.camera-overlay video {
  flex: 1;
  width: 100%;
  min-height: 0;
  object-fit: cover;
  background: #000;
}
.camera-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px calc(16px + var(--safe-bottom));
}
.shutter {
  width: 64px;
  height: 64px;
  border-radius: 0;
  border: 3px solid #fff;
  background: #fff;
  cursor: pointer;
}
.shutter:active { background: #bbb; }
.cam-spacer { width: 46px; }

/* --- lightbox (image viewer) --- */

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.94);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 90;
}
.lightbox.show { display: flex; }
.lightbox img {
  max-width: 96vw;
  max-height: 86vh;
  object-fit: contain;
  display: block;
}
#lightbox-close {
  position: fixed;
  top: calc(var(--safe-top) + 10px);
  right: 12px;
  background: #e7e7e7;
  color: #383a42;
}

/* --- page 2: overview + search --- */

.toc-search {
  position: sticky;
  top: 0;
  padding: calc(var(--safe-top) + 58px) 12px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}

.toc-list { padding: 0 0 calc(30px + var(--safe-bottom)); }

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  line-height: 1.4;
  border-bottom: 1px solid var(--border);
  overflow-wrap: anywhere;
  white-space: normal;
}
.toc-item:active { background: var(--btn-press); }
.toc-item.current { background: var(--btn-active); }
.toc-item.lvl1 { font-weight: 700; color: var(--lvl1); font-size: 14px; }
.toc-item.lvl2 { padding-left: 28px; color: var(--lvl2); }
.toc-item.lvl3 { padding-left: 42px; color: var(--lvl3); font-size: 13px; }
.toc-item.lvl4 { padding-left: 56px; color: var(--lvl4); font-size: 13px; }
.toc-item .snippet {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
  white-space: normal;
  overflow-wrap: anywhere;
}
.toc-item .snippet mark { background: var(--highlight); color: #fff; padding: 0 2px; }
.toc-section {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--lvl4);
  padding: 14px 14px 6px;
  font-weight: 700;
}

/* ------------------------------------------------------------------ */
/* top buttons (Emacs tab-bar style)                                   */
/* ------------------------------------------------------------------ */

.top-fab {
  position: fixed;
  top: calc(var(--safe-top) + 6px);
  width: 46px;
  height: 46px;
  border-radius: 0;
  border: 1px solid var(--border);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-size: 18px;
  display: none;
  place-items: center;
  cursor: pointer;
  z-index: 25;
}
.top-fab.active { display: grid; }
.top-fab:active { background: var(--btn-press); }
.top-fab:disabled { opacity: .35; }
.top-fab svg { width: 20px; height: 20px; }
#undo-btn { left: 10px; }
#redo-btn { left: 62px; }
#settings-fab { right: 10px; }

/* ------------------------------------------------------------------ */
/* settings                                                            */
/* ------------------------------------------------------------------ */

.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(56, 58, 66, 0.35);
  display: none;
  align-items: flex-end;
  z-index: 50;
}
.sheet-overlay.show { display: flex; }
.sheet {
  width: 100%;
  max-height: 82dvh;
  background: var(--bg);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 8px 14px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--lvl4);
  border-bottom: 1px solid var(--border);
}
.sheet-body {
  overflow-y: auto;
  padding: 10px;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 11px 13px;
  margin-bottom: 6px;
}
.row label { color: var(--muted); font-size: 13px; }
.row select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 6px 8px;
  -webkit-appearance: none;
  appearance: none;
  min-width: 110px;
  text-align: right;
  font-size: 13px;
}

.badge {
  display: inline-block;
  font-size: 12px;
  padding: 2px 7px;
  border-radius: 0;
  background: var(--surface-2);
  color: var(--text);
}
.badge.ok { background: var(--ok); color: #fff; }

/* ------------------------------------------------------------------ */
/* toast                                                               */
/* ------------------------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(70px + var(--safe-bottom));
  transform: translateX(-50%) translateY(16px);
  background: var(--btn-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 10px 14px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 80;
  max-width: 86vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); color: #fff; border-color: var(--danger); }
