:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --text: #1c211e;
  --muted: #66706a;
  --line: #e2ddd2;
  --accent: #1f6b45;
  --accent-soft: #e3efe7;
  --pocket: #2b4a3a;
  --pocket-edge: #3b6650;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --surface: #1b211e;
    --text: #edf1ee;
    --muted: #95a29b;
    --line: #2b3430;
    --accent: #6fcf97;
    --accent-soft: #1f3529;
    --pocket: #2b4a3a;
    --pocket-edge: #4d7f64;
    --danger: #ff8a80;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  min-height: 100dvh;
}

button { font: inherit; color: inherit; }

/* ---- Splash ------------------------------------------------------------ */

#splash {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 18px;
  background: var(--bg);
  transition: opacity .35s ease, visibility .35s;
}
#splash.done { opacity: 0; visibility: hidden; }

.pocket-anim {
  position: relative;
  width: 132px;
  height: 150px;
}

.pocket {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 92px;
  background: var(--pocket);
  border-radius: 10px 10px 34px 34px;
  z-index: 3;
  box-shadow: inset 0 3px 0 var(--pocket-edge);
}
.stitch {
  position: absolute;
  inset: 9px;
  border: 2px dashed rgb(255 255 255 / 28%);
  border-radius: 6px 6px 28px 28px;
}

.card {
  position: absolute;
  left: 22px;
  width: 88px;
  height: 112px;
  bottom: 20px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  transform: translateY(40px);
  opacity: 0;
  animation: rise .7s cubic-bezier(.2, .9, .3, 1.2) forwards;
}
.card::before, .card::after {
  content: "";
  position: absolute;
  left: 12px; right: 12px;
  height: 6px;
  border-radius: 3px;
  background: var(--line);
}
.card::before { top: 14px; background: var(--accent); right: 34px; }
.card::after { top: 28px; box-shadow: 0 12px 0 var(--line), 0 24px 0 var(--line); }
.c1 { z-index: 2; animation-delay: .35s; --tilt: 0deg; --lift: -46px; }
.c2 { z-index: 1; animation-delay: .2s; --tilt: -9deg; --lift: -28px; }
.c3 { z-index: 0; animation-delay: .05s; --tilt: 9deg; --lift: -24px; }

@keyframes rise {
  to { transform: translateY(var(--lift)) rotate(var(--tilt)); opacity: 1; }
}

.wordmark {
  font-weight: 700;
  letter-spacing: .06em;
  color: var(--muted);
  opacity: 0;
  animation: fade .4s .55s forwards;
}
@keyframes fade { to { opacity: 1; } }

/* ---- Top bar ----------------------------------------------------------- */

.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  column-gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.bar-title {
  flex: 1;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
}
.icon-btn:active { background: var(--line); }
.icon-btn[hidden] { display: grid; visibility: hidden; }

.offline {
  margin: 10px 16px 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  text-align: center;
}

/* ---- Item view --------------------------------------------------------- */

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 32px);
}

.item-head h1 {
  margin: 4px 0 2px;
  font-size: 28px;
  line-height: 1.15;
}
.item-head .sub { margin: 0; color: var(--muted); font-size: 14px; }
.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}
.chip {
  padding: 1px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  font-size: 12px;
}

.block {
  margin-top: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.block-head { padding: 12px 14px 4px; }
.block-head h2 { margin: 0; font-size: 15px; text-transform: uppercase; letter-spacing: .05em; }
.block-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.block-note {
  margin: 0;
  padding: 10px 14px 12px;
  color: var(--muted);
  font-size: 13px;
  border-top: 1px solid var(--line);
}
.block-text {
  margin: 0;
  padding: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th {
  padding: 8px 14px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  text-align: left;
}
td {
  padding: 9px 14px;
  border-top: 1px solid var(--line);
  font-size: 18px;
}
tbody th {
  padding: 9px 14px;
  border-top: 1px solid var(--line);
  color: var(--text);
  font-size: 16px;
  text-transform: none;
  letter-spacing: normal;
}
.right { text-align: right; }
.center { text-align: center; }
td.em { font-weight: 800; font-size: 22px; color: var(--accent); }
td .cell-sub { display: block; margin-top: -2px; font-size: 12px; font-weight: 500; color: var(--muted); }

.reveal {
  opacity: 0;
  transform: translateY(8px);
  animation: in .35s ease forwards;
  animation-delay: calc(var(--i, 0) * 28ms);
}
@keyframes in { to { opacity: 1; transform: none; } }

.empty { margin-top: 30vh; text-align: center; color: var(--muted); }

.item-actions { display: flex; gap: 10px; margin-top: 18px; }

.btn {
  padding: 10px 18px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--surface);
  font-weight: 600;
  cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--line); }

/* ---- Tabs and lists --------------------------------------------------- */

.tabs {
  flex-basis: 100%;
  display: flex;
  gap: 4px;
  margin: 2px -8px -6px;
  padding: 0 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none;
  padding: 10px 14px 9px;
  border-bottom: 3px solid transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 15px;
  text-decoration: none;
  white-space: nowrap;
}
.tab[aria-current="page"] { color: var(--accent); border-bottom-color: var(--accent); }
.tab .count { margin-left: 5px; font-size: 12px; font-weight: 500; opacity: .75; }

.doc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.doc-list a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: inherit;
  text-decoration: none;
}
.doc-list a:active { background: var(--accent-soft); }
.doc-list .txt { flex: 1; min-width: 0; }
.doc-list .t { display: block; font-weight: 700; font-size: 17px; }
.doc-list .s, .doc-list .d { display: block; color: var(--muted); font-size: 13px; }
.doc-list .s { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-list .kind {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
}
.doc-list .chev { flex: none; color: var(--muted); }

.list-foot { margin-top: 28px; text-align: center; }
.link-btn { padding: 6px 0; border: 0; background: none; color: var(--accent); font-weight: 600; cursor: pointer; }

/* ---- Editor ------------------------------------------------------------ */

dialog {
  width: min(560px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog form { display: grid; gap: 12px; padding: 18px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
dialog input, dialog textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* stops iOS zoom on focus */
}
dialog textarea { resize: vertical; }
.actions { display: flex; justify-content: flex-end; gap: 10px; }
.error { margin: 0; color: var(--danger); font-size: 14px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-delay: 0ms !important; transition-duration: 1ms !important; }
}

/* ---- Section switcher (inside a doc) ----------------------------------- */

.segmented {
  display: flex;
  gap: 4px;
  margin-top: 16px;
  padding: 4px;
  border-radius: 12px;
  background: var(--line);
}
.segmented button {
  flex: 1;
  padding: 9px 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
}
.segmented button[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.segmented + div > .block { margin-top: 12px; }
