:root {
  --bg: #ffffff;
  --text: #1c1c1e;
  --secondary: #8e8e93;
  --tertiary: #c7c7cc;
  --sep: rgba(60, 60, 67, 0.14);
  --fill: rgba(120, 120, 128, 0.12);
  --accent: #007aff;
  --danger: #ff3b30;
  --grouped: #f2f2f7;
  --card: #ffffff;
  --glass: rgba(249, 249, 249, 0.86);
  --shadow: rgba(0, 0, 0, 0.16);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --text: #ffffff;
    --secondary: #8e8e93;
    --tertiary: #48484a;
    --sep: rgba(84, 84, 88, 0.5);
    --fill: rgba(120, 120, 128, 0.24);
    --accent: #0a84ff;
    --danger: #ff453a;
    --grouped: #000000;
    --card: #1c1c1e;
    --glass: rgba(37, 37, 39, 0.88);
    --shadow: rgba(0, 0, 0, 0.5);
  }
}

* { -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Helvetica Neue", sans-serif;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- memo pages ---------- */

#app {
  position: fixed;
  inset: 0;
  transition: transform 0.38s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.28s ease;
}
body.ov #app { transform: scale(0.86); opacity: 0; pointer-events: none; }

.pager {
  display: flex;
  height: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
}
.pager::-webkit-scrollbar { display: none; }

.page {
  flex: 0 0 100%;
  height: 100%;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

.page-inner {
  min-height: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 16px) 20px calc(env(safe-area-inset-bottom) + 100px);
}

.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }

.title {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 34px;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.01em;
  padding: 4px 0;
}
.title::placeholder { color: var(--tertiary); }

.items { list-style: none; margin: 0; padding: 0; position: relative; }

.item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 11px 0;
  border-bottom: 0.5px solid var(--sep);
  background: var(--bg);
  cursor: pointer;
  overflow-wrap: anywhere;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.item .marker {
  flex: 0 0 auto;
  min-width: 1.3em;
  color: var(--secondary);
  font-variant-numeric: tabular-nums;
}
.item[data-style="plain"] .marker { display: none; }
.item .text { flex: 1; white-space: pre-wrap; }
.item.lift-src { visibility: hidden; }
.item.appear { animation: appear 0.25s ease-out; }
.item.collapse {
  height: 0 !important;
  padding-top: 0;
  padding-bottom: 0;
  border-bottom-width: 0;
  overflow: hidden;
  transition: height 0.22s ease, padding 0.22s ease;
}

textarea.edit, .composer {
  display: block;
  width: 100%;
  box-sizing: border-box;
  margin: 0;
  border: 0;
  outline: none;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: inherit;
  -webkit-user-select: text;
  user-select: text;
}
textarea.edit { padding: 0; }
.composer { padding: 11px 0; caret-color: var(--accent); }
textarea.edit { caret-color: var(--accent); }
.composer::placeholder { color: var(--tertiary); }

.hint {
  margin: 28px 0 0;
  color: var(--secondary);
  opacity: 0.8;
  font-size: 15px;
  line-height: 1.7;
  -webkit-user-select: none;
  user-select: none;
}
.blank { flex: 1; min-height: 160px; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

/* ---------- toolbar: trash · pages · new ---------- */

.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  pointer-events: none;
}
.tool {
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  border: 0;
  border-radius: 999px;
  color: var(--text);
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  box-shadow: 0 0 0 0.5px var(--sep), 0 8px 24px rgba(0, 0, 0, 0.08);
  transition: transform 0.15s ease, opacity 0.2s ease;
}
.tool:active { transform: scale(0.92); }
.tool:disabled { opacity: 0.3; pointer-events: none; }
.tool.round { width: 50px; }
.tool.pill { gap: 10px; padding: 0 18px; color: var(--secondary); }
.tool svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tool.pill svg { width: 19px; height: 19px; }
.dots { display: flex; align-items: center; gap: 7px; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--tertiary); transition: background 0.2s; }
.dots span.on { background: var(--text); }

/* The sheet waiting past the last page; its hint sits near the leading edge so it shows early in the swipe */
.new-sheet { display: flex; align-items: center; padding-left: 48px; box-sizing: border-box; }
.new-hint {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--secondary);
  font-size: 17px;
  font-weight: 600;
  opacity: var(--p, 0);
  transform: scale(calc(0.8 + 0.2 * var(--p, 0)));
}
.new-hint svg { width: 44px; height: 44px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }

.page.tearing { transition: transform 0.22s ease-in, opacity 0.22s ease-in; transform: translateY(24px) scale(0.94); opacity: 0; }

/* ---------- long-press lift, menu ---------- */

.scrim {
  position: fixed;
  inset: 0;
  z-index: 50;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.1);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  opacity: 0;
  transition: opacity 0.2s ease;
}
.scrim.show { opacity: 1; }

.ghost {
  position: fixed;
  z-index: 60;
  margin: 0;
  box-sizing: border-box;
  pointer-events: none;
  transform: translate(var(--dx, 0px), var(--dy, 0px)) scale(var(--s, 1));
  transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s, box-shadow 0.2s;
}
.ghost.up { --s: 1.04; }
.ghost.dragging { transition: box-shadow 0.2s; }
.ghost.vanish { --s: 0.7; opacity: 0; }
.ghost-item {
  padding-left: 10px;
  padding-right: 10px;
  border: 0;
  border-radius: 12px;
  background: var(--card);
  box-shadow: 0 10px 36px var(--shadow);
}
@media (prefers-color-scheme: dark) { .ghost-item { background: #2c2c2e; } }
.ghost-tile .tile-card { animation: none; }
.ghost-tile.up { --s: 1.08; }

.menu {
  position: fixed;
  z-index: 70;
  min-width: 230px;
  overflow: hidden;
  border-radius: 14px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  box-shadow: 0 12px 44px var(--shadow);
}
.menu.pop { animation: pop 0.24s cubic-bezier(0.2, 0.9, 0.3, 1.2); }
.menu button {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border: 0;
  border-bottom: 0.5px solid var(--sep);
  background: none;
  color: var(--text);
  font-size: 17px;
  text-align: left;
}
.menu button:last-child { border-bottom: 0; }
.menu button:active { background: var(--fill); }
.menu svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.menu .danger { color: var(--danger); }

/* ---------- pages overview ---------- */

.overview {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--grouped);
  opacity: 0;
  transform: scale(1.08);
  transition: transform 0.38s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.28s ease;
}
body.ov .overview { opacity: 1; transform: none; }

.ov-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 16px) 20px 8px;
}
.ov-header h1 { margin: 0; font-size: 34px; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: 17px; font-weight: 600; padding: 6px 0 6px 12px; }

.ov-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px 30px calc(env(safe-area-inset-bottom) + 24px);
}

.grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 26px 30px;
}
@media (min-width: 600px) { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

.tile, .tile-add {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.tile.lift-src { visibility: hidden; }

.tile-card {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 16;
  animation: wiggle 0.26s ease-in-out infinite alternate;
}
.tile:nth-child(2n) .tile-card { animation-delay: -0.13s; animation-duration: 0.3s; }
.tile:nth-child(3n) .tile-card { animation-duration: 0.23s; }

.tile-paper {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: 16px;
  background: var(--card);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}
.tile.current .tile-paper { box-shadow: 0 0 0 2.5px var(--accent), 0 4px 16px rgba(0, 0, 0, 0.08); }
.mini {
  position: absolute;
  top: 0;
  left: 0;
  box-sizing: border-box;
  padding: 16px 20px;
  transform-origin: 0 0;
  pointer-events: none;
}
.mini-title {
  margin-bottom: 4px;
  padding: 4px 0;
  font-size: 34px;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mini-title.empty { color: var(--tertiary); }
.mini .item { background: transparent; cursor: default; }

.tile-del {
  position: absolute;
  top: -10px;
  left: -10px;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(120, 120, 128, 0.85);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}
.tile-del::before { content: ""; width: 12px; height: 2.5px; border-radius: 2px; background: #fff; }

.tile-num { font-size: 13px; color: var(--secondary); font-variant-numeric: tabular-nums; }

.tile-add { border: 0; background: none; padding: 0; }
.tile-add-card {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 9 / 16;
  box-sizing: border-box;
  border: 2px dashed var(--tertiary);
  border-radius: 16px;
  color: var(--secondary);
}
.tile-add-card svg { width: 34px; height: 34px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

.ov-footer { display: flex; justify-content: center; gap: 12px; margin-top: 34px; }
.soft-btn { border: 0; border-radius: 999px; padding: 9px 18px; background: var(--fill); color: var(--accent); font-size: 15px; font-weight: 600; }

/* ---------- confirm sheet, toast ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 8px;
  padding: 0 10px calc(env(safe-area-inset-bottom) + 10px);
  background: rgba(0, 0, 0, 0.3);
  opacity: 0;
  transition: opacity 0.22s ease;
}
.sheet.show { opacity: 1; }
.sheet-card, .sheet-cancel {
  border-radius: 14px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  transform: translateY(30px);
  transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet.show .sheet-card, .sheet.show .sheet-cancel { transform: none; }
.sheet-card { overflow: hidden; text-align: center; }
.sheet-title { margin: 0; padding: 14px 16px 2px; font-size: 13px; font-weight: 600; color: var(--secondary); }
.sheet-msg { margin: 0; padding: 0 16px 14px; font-size: 13px; color: var(--secondary); border-bottom: 0.5px solid var(--sep); }
.sheet-ok { width: 100%; padding: 17px; border: 0; background: none; color: var(--danger); font-size: 20px; }
.sheet-cancel { width: 100%; padding: 17px; border: 0; color: var(--accent); font-size: 20px; font-weight: 600; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 60px);
  z-index: 90;
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(30, 30, 32, 0.92);
  color: #fff;
  font-size: 15px;
  white-space: nowrap;
  animation: toast-in 0.2s ease-out;
}

@keyframes wiggle { from { transform: rotate(-1.1deg); } to { transform: rotate(1.1deg); } }
@keyframes pop { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }
@keyframes appear { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 6px); } to { opacity: 1; transform: translate(-50%, 0); } }

@media (prefers-reduced-motion: reduce) {
  .tile-card { animation: none; }
  #app, .overview, .ghost { transition-duration: 0.01s; }
}
