:root {
  color-scheme: light dark;

  /* design tokens: light */
  --bg: #EDF0F4;
  --surface: #FFFFFF;
  --ink: #18212B;
  --muted: #6B7684;
  --line: #D9DFE7;
  --accent: #3D5AFE;
  --accent-ink: #FFFFFF;
  --danger: #D6455D;
  --code-bg: #E6EAF0;

  --radius-card: 14px;
  --radius-button: 12px;
  --radius-round: 26px;
  --radius-top: 18px;

  --z-dock: 10;
  --z-toast: 20;
  --z-reader: 30;
  --z-sheet: 40;

  --shadow-soft: 0 1px 2px rgb(24 33 43 / 0.06), 0 8px 24px rgb(24 33 43 / 0.08);
  --shadow-sheet: 0 -8px 40px rgb(24 33 43 / 0.18);

  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10151C;
    --surface: #1A222C;
    --ink: #E8EDF3;
    --muted: #8A96A5;
    --line: #2A3441;
    --accent: #7C93FF;
    --accent-ink: #10151C;
    --danger: #FF7A8E;
    --code-bg: #232D39;
    --shadow-soft: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.32);
    --shadow-sheet: 0 -8px 40px rgb(0 0 0 / 0.5);
  }
}

:root[data-theme="dark"] {
  --bg: #10151C;
  --surface: #1A222C;
  --ink: #E8EDF3;
  --muted: #8A96A5;
  --line: #2A3441;
  --accent: #7C93FF;
  --accent-ink: #10151C;
  --danger: #FF7A8E;
  --code-bg: #232D39;
  --shadow-soft: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.32);
  --shadow-sheet: 0 -8px 40px rgb(0 0 0 / 0.5);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100dvh;
  max-width: 640px;
  margin-inline: auto;
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior-y: none;
}

body.locked { overflow: hidden; }

/* ---------- list ---------- */

.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
}

.icon:hover { color: var(--ink); background: color-mix(in srgb, var(--surface) 70%, transparent); }

/* ---------- list ---------- */

main {
  padding: calc(env(safe-area-inset-top) + 0.75rem) 1rem calc(150px + env(safe-area-inset-bottom));
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.note {
  position: relative;
  border-radius: var(--radius-card);
  overflow: hidden;
  cursor: pointer;
  touch-action: pan-y;
  outline-offset: 2px;
}

.swipe-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 1.25rem;
  background: var(--danger);
  color: #fff;
  font-size: 0.875rem;
  font-weight: 650;
}

.note-card {
  position: relative;
  padding: 0.875rem 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-soft);
  transform: translateX(var(--swipe, 0));
  transition: transform 180ms ease;
}

.note.swiping .note-card { transition: none; }

.note:focus-visible { outline: 2px solid var(--accent); }

.note h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 660;
  line-height: 1.4;
  letter-spacing: -0.01em;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.note .md {
  margin-top: 0.25rem;
  max-height: calc(9.5 * 1.5 * 0.9375rem);
  overflow: hidden;
}

.note.clipped .md {
  -webkit-mask-image: linear-gradient(to bottom, #000 78%, transparent 100%);
  mask-image: linear-gradient(to bottom, #000 78%, transparent 100%);
}

.time {
  display: block;
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.empty {
  margin: 4rem auto;
  max-width: 22rem;
  padding: 0 0.5rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.9375rem;
}

/* ---------- markdown ---------- */

.md { font-size: 0.9375rem; line-height: 1.6; color: var(--ink); }
.md-plain { white-space: pre-wrap; overflow-wrap: anywhere; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }

.md p, .md ul, .md ol, .md blockquote, .md pre { margin: 0.5em 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 0.75em 0 0.35em; font-size: 1.0625em; line-height: 1.35; }
.md ul, .md ol { padding-left: 1.15rem; }
.md li { margin: 0.15em 0; }
.md li::marker { color: var(--muted); }
.md code { padding: 0.1em 0.35em; background: var(--code-bg); border-radius: 5px; font-size: 0.9em; }
.md pre { padding: 0.75rem; background: var(--code-bg); border-radius: 10px; overflow-x: auto; }
.md pre code { padding: 0; background: none; }
.md blockquote { padding-left: 0.75rem; border-left: 3px solid var(--line); color: var(--muted); }
.md a { color: var(--accent); }
.md img { max-width: 100%; height: auto; }
.md li:has(input[type="checkbox"]) { list-style: none; margin-left: -1.15rem; }

.md label.task {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  min-height: 40px;
  cursor: pointer;
}

/* Reader sprints on thumb accuracy; cards should stay compact. */
#reader-body label.task { min-height: 44px; }

/* Native checkboxes look muddy on dark surfaces; draw the box instead. */
.md input[type="checkbox"] {
  flex: none;
  appearance: none;
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  margin: 0;
  background: transparent;
  border: 2px solid var(--line);
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease;
}

.md input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 13l4 4L19 7'/%3E%3C/svg%3E");
  background-size: 16px 16px;
  background-position: center;
  background-repeat: no-repeat;
}

.md input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- dock ---------- */

#new {
  width: 48px;
  height: 48px;
  font-size: 1.25rem;
}

.dock .circle { box-shadow: var(--shadow-soft); }

/* Floats over the list: bottom-anchored, 14 px sides, 12 px bottom (+ inset + keyboard). */
.dock {
  position: fixed;
  inset: auto 0 0;
  z-index: var(--z-dock);
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: calc(640px + 28px);
  margin-inline: auto;
  padding: 0 14px calc(12px + env(safe-area-inset-bottom) + var(--keyboard, 0px));
}

.dock-row {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

/* Tag filter row: scrolls sideways, scrollbar hidden. */
.filters {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  padding: 1px;
  margin: -1px;
}

.filters::-webkit-scrollbar { display: none; }
.filters[hidden] { display: none; }

.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  height: 32px;
  padding: 0 0.75rem;
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}

@supports (backdrop-filter: blur(14px)) {
  .chip {
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
  }
}

@supports not (backdrop-filter: blur(14px)) {
  .chip { background: var(--surface); }
}

.chip[aria-pressed="true"] {
  color: var(--accent-ink);
  background: var(--accent);
  border-color: var(--accent);
}

/* Chips attached to a note card. */
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 0.5rem;
}

.tag {
  padding: 4px 8px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.2;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
}

/* Tag suggestions above the toolbar. */
.suggest {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  padding-bottom: 0.5rem;
}

.suggest::-webkit-scrollbar { display: none; }
.suggest[hidden] { display: none; }

.pill {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  height: 48px;
  padding: 0 0.875rem;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius-round);
  box-shadow: var(--shadow-soft);
}

@supports (backdrop-filter: blur(14px)) {
  .pill {
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
  }
}

@supports not (backdrop-filter: blur(14px)) {
  .pill { background: var(--surface); }
}

.pill:focus-within { border-color: var(--accent); }
.pill svg { flex: none; }

.pill input {
  flex: 1;
  min-width: 0;
  height: 100%;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: none;
  border: 0;
}

.pill input::placeholder { color: var(--muted); }
.pill input:focus-visible { outline: none; }
.pill input::-webkit-search-cancel-button { display: none; }

.round {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  font: inherit;
  font-size: 1.375rem;
  font-weight: 500;
  line-height: 1;
  color: var(--accent-ink);
  background: var(--accent);
  border: 0;
  border-radius: 999px;
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  touch-action: manipulation;
}

.round:active { transform: scale(0.96); }

/* ---------- overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  z-index: var(--z-reader);
}

.overlay[hidden] { display: none; }

/* ---------- reader ---------- */

.reader-scroll {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  width: 100%;
  max-width: 640px;
  margin-inline: auto;
  padding: calc(env(safe-area-inset-top) + 1rem) 1.25rem 1rem;
  touch-action: manipulation;
}

#reader-title {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.02em;
  overflow-wrap: anywhere;
}

#reader-body { margin-top: 1rem; font-size: 17px; line-height: 1.6; }

/* End of the content: bottom of the viewport on short notes, after the text on long ones. */
.reader-hint {
  margin: auto 0 0;
  padding: 1.5rem 0 0.25rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
}

/* Bottom bar: same 3-slot rhythm as the home dock, all navigation in the thumb zone. */
.reader-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 640px;
  margin-inline: auto;
  padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
}

.circle {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  touch-action: manipulation;
}

.circle:active { transform: scale(0.96); }
.circle.danger { color: var(--danger); }

.pill-action {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  height: 48px;
  padding: 0 1rem;
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 650;
  color: var(--accent-ink);
  background: var(--accent);
  border: 0;
  border-radius: 999px;
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  touch-action: manipulation;
}

.pill-action:active { transform: scale(0.99); }

/* ---------- composer ---------- */

#composer { justify-content: flex-end; }

.write {
  width: 100%;
  max-width: 640px;
  margin-inline: auto;
  padding: 0.875rem 1rem calc(0.875rem + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
  border-radius: var(--radius-top) var(--radius-top) 0 0;
  box-shadow: var(--shadow-sheet);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding-bottom: 0.625rem;
}

.toolbar button {
  width: 40px;
  height: 40px;
  padding: 0;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
}

.toolbar button:hover { color: var(--ink); background: var(--bg); }

.status {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

#editor {
  display: block;
  width: 100%;
  min-height: 7.5rem;
  font: inherit;
  font-size: 17px;
  line-height: 1.65;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  padding: 0.875rem 1rem;
  resize: none;
  overflow-y: auto;
  scrollbar-width: thin;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

#editor:hover { border-color: color-mix(in srgb, var(--line) 45%, var(--muted)); }

#editor:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}

#editor::placeholder { color: var(--muted); }

.write-foot {
  display: flex;
  justify-content: flex-end;
  padding-top: 0.75rem;
}

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

dialog {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 640px;
  max-height: none;
  border: 0;
  background: none;
  z-index: var(--z-sheet);
}

dialog::backdrop { background: rgb(0 0 0 / 0.4); }

dialog[open] { position: fixed; inset: auto 0 0; margin-inline: auto; }

.sheet {
  padding: 0.5rem 1.25rem calc(1.25rem + env(safe-area-inset-bottom));
  background: var(--surface);
  border-radius: var(--radius-top) var(--radius-top) 0 0;
  box-shadow: var(--shadow-sheet);
  animation: slide-up 220ms ease-out;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.sheet h2 { margin: 0; font-size: 1rem; font-weight: 680; }

.label { margin: 1rem 0 0.5rem; font-size: 0.8125rem; color: var(--muted); }

.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.25rem;
  padding: 0.25rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-button);
}

.seg button {
  min-height: 40px;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 9px;
  cursor: pointer;
  touch-action: manipulation;
}

.seg button[aria-checked="true"] {
  color: var(--accent);
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--accent);
}

@keyframes slide-up {
  from { transform: translateY(100%); }
}

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

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(130px + env(safe-area-inset-bottom) + var(--keyboard, 0px));
  transform: translateX(-50%);
  z-index: var(--z-toast);
  display: flex;
  align-items: center;
  gap: 0.875rem;
  max-width: min(30rem, calc(100vw - 2rem));
  padding: 0.375rem 0.375rem 0.375rem 0.875rem;
  background: var(--ink);
  color: var(--bg);
  border-radius: 999px;
  font-size: 0.875rem;
  box-shadow: 0 10px 28px rgb(0 0 0 / 0.28);
  animation: rise 160ms ease-out;
}

#toast[hidden] { display: none; }

#toast button {
  min-height: 36px;
  padding: 0 0.875rem;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 650;
  color: inherit;
  background: rgb(127 127 127 / 0.28);
  border: 0;
  border-radius: 999px;
  cursor: pointer;
}

@keyframes rise {
  from { transform: translate(-50%, 0.5rem); opacity: 0; }
}

/* ---------- reduced motion ---------- */

body.reduced-motion *,
body.reduced-motion *::before,
body.reduced-motion *::after {
  animation-duration: 0.001ms !important;
  transition-duration: 0.001ms !important;
}

/* ---------- a11y ---------- */

:where(.round, .icon, .circle, .pill-action, .toolbar button, .seg button, .note, #toast button):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- trash ---------- */

.overlay-title {
  margin: 0;
  padding: calc(env(safe-area-inset-top) + 1rem) 1.25rem 0.5rem;
  font-size: 1.125rem;
  font-weight: 680;
  letter-spacing: -0.01em;
}

.sheet-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
  min-height: 48px;
  margin-top: 0.75rem;
  padding: 0 0.875rem;
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-button);
  cursor: pointer;
  touch-action: manipulation;
}

.sheet-row-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--muted);
}

.trash-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.trash-item {
  padding: 0.875rem 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-soft);
}

.trash-item h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 640;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.trash-item p {
  margin: 0.25rem 0 0.75rem;
  font-size: 0.8125rem;
  color: var(--muted);
}

.trash-item button {
  min-height: 36px;
  padding: 0 0.875rem;
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 650;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
}
