/* ---------- Palette (defined once) ---------- */
:root {
  /* Brand */
  --near-black: #0F1114;
  --graphite: #2B2F36;
  --sand: #E6D2B8;
  --rust: #A14E2D;
  --amber: #D8893A;
  /* Lighter, warmer tint of sand for the light page */
  --paper: #F5EDE0;

  --serif: Charter, "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --rail: 10rem;
  --gutter: 16px;
}

/* Light theme (default) */
:root, :root[data-theme="light"] {
  color-scheme: light;
  --bg: var(--paper);
  --ink: var(--near-black);
  --ink-2: var(--graphite);
  --muted: color-mix(in srgb, var(--graphite) 86%, var(--paper));
  --rule: var(--sand);
  --rule-strong: color-mix(in srgb, var(--graphite) 62%, var(--paper));
  --surface: color-mix(in srgb, var(--sand) 30%, var(--paper));
  --selected: color-mix(in srgb, var(--sand) 42%, var(--paper));
  --accent: var(--rust);
  --on-accent: var(--paper);
}

/* Dark theme */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: var(--near-black);
  --ink: color-mix(in srgb, var(--sand) 90%, var(--near-black));
  --ink-2: color-mix(in srgb, var(--sand) 80%, var(--near-black));
  --muted: color-mix(in srgb, var(--sand) 70%, var(--near-black));
  --rule: var(--graphite);
  --rule-strong: color-mix(in srgb, var(--sand) 46%, var(--near-black));
  --surface: var(--graphite);
  --selected: var(--graphite);
  --accent: var(--amber);
  --on-accent: var(--near-black);
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 1rem/1.5 var(--sans);
  min-height: 100vh;
}
h1, h2, h3 { font-family: var(--serif); font-weight: 500; line-height: 1.25; margin: 0; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.2rem; }
p { margin: 0; }
a { color: var(--accent); text-underline-offset: 0.18em; }
ul, ol { margin: 0; padding: 0; }
time, .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

.sr-only {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip { position: absolute; left: -999px; top: 0; padding: 0.75rem 1rem; background: var(--accent); color: var(--on-accent); z-index: 10; }
.skip:focus { left: 0; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
main:focus { outline: none; }

/* ---------- Layout ---------- */
main {
  max-width: 46rem;
  margin: 0 auto;
  padding: 1.25rem var(--gutter) calc(5.5rem + env(safe-area-inset-bottom));
}
main.enter { animation: fade 0.18s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
main[data-view="todo"] { max-width: 76rem; }

.page-head {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: 0.25rem 2.75rem 0.9rem 0; margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--rule);
}
.page-head .meta { margin-left: auto; }
.crumb { font-size: 0.9rem; color: var(--muted); margin-bottom: 0.9rem; }
.quiet { color: var(--muted); }
.foot { margin-top: 3rem; padding-top: 1rem; border-top: 1px solid var(--rule); font-size: 0.875rem; }
.meta { font-size: 0.875rem; color: var(--muted); }
.hint { font-size: 0.85rem; color: var(--muted); }
.error { color: var(--accent); font-size: 0.9rem; min-height: 0; }
.error:empty { display: none; }
.notice { border-left: 3px solid var(--accent); padding: 0.5rem 0.9rem; margin: 1rem 0; }
.tag {
  display: inline-block; margin-left: 0.5rem; padding: 0 0.4rem; font-size: 0.72rem; letter-spacing: 0.04em;
  text-transform: uppercase; border: 1px solid var(--rule-strong); color: var(--muted); line-height: 1.5;
}
.print-only { display: none; }

/* ---------- Navigation ---------- */
.nav {
  position: fixed; z-index: 5; left: 0; right: 0; bottom: 0;
  background: var(--bg); border-top: 1px solid var(--rule);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav ul { list-style: none; display: grid; grid-template-columns: repeat(5, 1fr); }
.nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 56px; padding: 6px 2px; color: var(--muted); text-decoration: none; font-size: 0.72rem;
  border-top: 2px solid transparent; margin-top: -1px;
}
.nav a[aria-current="page"] { color: var(--accent); border-top-color: var(--accent); }
.nav svg, .top-settings svg {
  width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.5;
  stroke-linecap: round; stroke-linejoin: round;
}
.nav-settings { display: none !important; }
.top-settings {
  position: absolute; top: 0.6rem; right: 6px; width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center; color: var(--muted);
}
.top-settings:hover, .nav a:hover { color: var(--accent); }

@media (min-width: 800px) {
  .top-settings { display: none; }
  .nav {
    top: 0; right: auto; bottom: 0; width: var(--rail);
    border-top: 0; border-right: 1px solid var(--rule);
    display: flex; flex-direction: column; padding: 1.25rem 0.5rem;
  }
  .nav ul { display: flex; flex-direction: column; gap: 2px; }
  .nav a {
    flex-direction: row; justify-content: flex-start; gap: 0.75rem; min-height: 44px;
    padding: 0 0.75rem; font-size: 0.95rem; border-top: 0; margin: 0; border-left: 2px solid transparent;
  }
  .nav a[aria-current="page"] { border-left-color: var(--accent); background: var(--selected); }
  .nav-settings { display: flex !important; margin-top: auto !important; }
  main { margin-left: var(--rail); margin-right: 0; max-width: none; padding: 2rem 2.5rem 4rem; }
  main > * { max-width: 46rem; }
  main[data-view="todo"] > * { max-width: 76rem; }
  .page-head { padding-right: 0; }
}
@media (min-width: 800px) and (max-width: 1100px) { main { padding-inline: 1.5rem; } }

/* ---------- Controls ---------- */
.btn, button, input, textarea, select { font: inherit; color: inherit; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  min-height: 44px; min-width: 44px; padding: 0 1rem; cursor: pointer; text-decoration: none;
  background: transparent; color: var(--ink); border: 1px solid var(--rule-strong); border-radius: 2px;
  font-size: 0.95rem;
}
.btn:hover:not(:disabled) { background: var(--selected); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn.danger { border-color: var(--accent); color: var(--accent); }
.btn.danger-text { border-color: transparent; color: var(--accent); margin-left: auto; }
.icon-btn { padding: 0; font-family: var(--serif); font-size: 1.05rem; }

input[type="text"], input[type="date"], input[type="number"], textarea {
  width: 100%; min-height: 44px; padding: 0.55rem 0.7rem; background: transparent; color: var(--ink);
  border: 1px solid var(--rule-strong); border-radius: 2px;
}
textarea { resize: vertical; line-height: 1.5; }
textarea.long { font-family: var(--serif); font-size: 1.05rem; }
input::placeholder { color: var(--muted); opacity: 1; }
label { font-size: 0.9rem; color: var(--ink-2); }

.form { display: flex; flex-direction: column; gap: 1rem; }
.field { display: flex; flex-direction: column; gap: 0.3rem; }
.row-fields { display: flex; flex-wrap: wrap; gap: 1rem; }
.row-fields .field { flex: 1 1 14rem; }
.row-fields .field.narrow { flex: 1 1 12rem; }
.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }

.filters { display: flex; flex-wrap: wrap; gap: 0.4rem; padding: 0.75rem 0; }
.chip {
  min-height: 44px; padding: 0 1rem; background: transparent; border: 1px solid var(--rule-strong);
  border-radius: 2px; cursor: pointer; color: var(--ink-2);
}
.chip[aria-pressed="true"] { background: var(--selected); border-color: var(--accent); color: var(--accent); font-weight: 600; }

/* ---------- Index lists (Library, Reader) ---------- */
.group-title {
  font-family: var(--sans); font-size: 0.78rem; font-weight: 600; letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--muted); margin: 2rem 0 0.25rem;
}
.index { list-style: none; }
.index li { border-bottom: 1px solid var(--rule); }
.index a { display: block; padding: 0.8rem 0; text-decoration: none; color: var(--ink); min-height: 44px; }
.index a:hover .index-title { color: var(--accent); text-decoration: underline; text-underline-offset: 0.2em; }
.index-title { display: block; font-family: var(--serif); font-size: 1.15rem; }
.index-blurb { display: block; font-size: 0.9rem; color: var(--muted); margin-top: 0.1rem; }

/* ---------- Prose / reading ---------- */
.prose, .reading { font-family: var(--serif); max-width: 66ch; }
.prose h1 { font-size: 2rem; margin-bottom: 1.25rem; }
.prose p, .prose li { font-size: 1.125rem; line-height: 1.7; }
.prose p { margin-bottom: 1.1em; }
.prose ul { margin: 0 0 1.2em; padding-left: 1.3em; }
.prose li { margin-bottom: 0.4em; padding-left: 0.2em; }
.pager { display: flex; justify-content: space-between; gap: 1rem; margin-top: 2rem; }
.pager a { display: inline-flex; align-items: center; min-height: 44px; }

.reader-bar {
  display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap;
  position: sticky; top: 0; z-index: 2; background: var(--bg);
  padding: 0.5rem 0; margin-bottom: 1.25rem; border-bottom: 1px solid var(--rule);
}
.bar-label { font-size: 0.85rem; color: var(--muted); margin-right: 0.25rem; }
.size-label { min-width: 2.2rem; text-align: center; font-family: var(--mono); font-size: 0.85rem; color: var(--muted); }
.spacer { flex: 1; }
.reading { line-height: 1.75; }
.reading h1 { font-size: 1.6em; margin-bottom: 1rem; }
.reading h2 { font-size: 1.15em; margin: 1.8em 0 0.5em; }
.reading p { margin-bottom: 1em; white-space: pre-line; }
.reading ul { margin: 0 0 1.2em; padding-left: 1.3em; }
.reading li { margin-bottom: 0.35em; }

/* ---------- Feed ---------- */
.feed { list-style: none; }
.feed-item { padding: 1rem 0 1rem 0.9rem; border-bottom: 1px solid var(--rule); border-left: 2px solid transparent; }
.feed-item.unread { border-left-color: var(--accent); }
.feed-item.unread .summary { font-weight: 600; }
.feed-meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.25rem 0.9rem; font-size: 0.85rem; color: var(--muted); }
.feed-meta .source { color: var(--ink-2); font-weight: 600; }
.feed-meta .tag { margin-left: 0; }
.unread-label { color: var(--accent); font-weight: 600; }
.summary { margin: 0.3rem 0 0.4rem; max-width: 60ch; }
.feed-item .actions { margin-top: 0.4rem; }

/* ---------- To do ---------- */
.segmented { display: flex; margin: 0.75rem 0 1rem; border: 1px solid var(--rule-strong); border-radius: 2px; }
.segmented button {
  flex: 1; min-height: 44px; padding: 0 0.25rem; background: transparent; border: 0; cursor: pointer;
  color: var(--ink-2); font-size: 0.9rem;
}
.segmented button + button { border-left: 1px solid var(--rule-strong); }
.segmented button[aria-pressed="true"] { background: var(--selected); color: var(--accent); font-weight: 600; }

.col-title { display: flex; align-items: baseline; gap: 0.6rem; padding-bottom: 0.4rem; border-bottom: 1px solid var(--rule); }
.count { font-family: var(--mono); font-size: 0.85rem; color: var(--muted); }
.add { display: flex; gap: 0.5rem; margin: 0.75rem 0; }
.add input { flex: 1; }
.tasks { list-style: none; }
.task { padding: 0.85rem 0; border-bottom: 1px solid var(--rule); }
.task:focus { outline: none; }
.task:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.task-title { font-weight: 600; }
.task-note { color: var(--ink-2); font-size: 0.92rem; margin-top: 0.15rem; }
.task .meta { margin: 0.3rem 0 0.35rem; }
.task .meta .tag { margin-left: 0.6rem; }
.empty { padding: 0.75rem 0; font-size: 0.9rem; }

.board .col:not(.is-current) { display: none; }
@media (min-width: 900px) {
  .segmented { display: none; }
  .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 2rem; margin-top: 1rem; }
  .board .col:not(.is-current) { display: block; }
  .board .col + .col { border-left: 1px solid var(--rule); padding-left: 2rem; }
}

/* ---------- Journal ---------- */
.filter-bar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 0.6rem; padding: 0.75rem 0 0.5rem; }
.filter-bar .field { flex: 0 1 11rem; }
.entry-form { margin: 1rem 0; padding: 1rem 0 1.25rem; border-top: 1px solid var(--rule-strong); border-bottom: 1px solid var(--rule-strong); }
.day { margin-top: 1.75rem; }
.day-title { font-size: 0.95rem; font-weight: 600; color: var(--ink-2); padding-bottom: 0.35rem; border-bottom: 1px solid var(--rule-strong); }
.entry { padding: 0.9rem 0; border-bottom: 1px solid var(--rule); }
.entry-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.25rem 1rem; }
.stamp { font-weight: 600; font-size: 1rem; }
.pain { font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--ink-2); }
.entry-head .tag { margin-left: 0; }
.entry dl { margin: 0.4rem 0 0.2rem; display: grid; grid-template-columns: 8.5rem 1fr; gap: 0.2rem 1rem; }
.entry dt { color: var(--muted); font-size: 0.9rem; }
.entry dd { margin: 0; white-space: pre-line; max-width: 60ch; }
@media (max-width: 520px) {
  .entry dl { grid-template-columns: 1fr; gap: 0; }
  .entry dd { margin-bottom: 0.4rem; }
}
.entry .actions { margin-top: 0.3rem; }

/* ---------- Settings ---------- */
.setting { padding: 1.25rem 0; border-bottom: 1px solid var(--rule); display: flex; flex-direction: column; gap: 0.75rem; }
.setting h2, .setting legend { font-family: var(--sans); font-size: 0.78rem; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
fieldset { border: 0; margin: 0; padding: 0; }
legend { padding: 0; margin-bottom: 0.5rem; }
.choices { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.choices legend { width: 100%; }
.choice { position: relative; }
.choice input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.choice span {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 1.1rem;
  border: 1px solid var(--rule-strong); border-radius: 2px; color: var(--ink-2);
}
.choice input:checked + span { background: var(--selected); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.choice input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.setting + .meta { margin-top: 1rem; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* ---------- Print: clean one-column record, always light ---------- */
@media print {
  :root, :root[data-theme="dark"], :root[data-theme="light"] {
    color-scheme: light;
    --bg: #fff; --ink: #000; --ink-2: #000; --muted: #333; --rule: #bbb; --rule-strong: #666;
    --surface: transparent; --selected: transparent; --accent: #000; --on-accent: #fff;
  }
  *, *::before, *::after { background: none !important; box-shadow: none !important; animation: none !important; }
  body { font-size: 11pt; color: #000; }
  .nav, .top-settings, .skip, .no-print, .actions, .page-head .btn, .reader-bar, .filters { display: none !important; }
  .print-only { display: block; margin-bottom: 1rem; font-size: 10pt; }
  .print-head p:first-child { font-weight: 700; }
  main { margin: 0 !important; padding: 0 !important; max-width: none !important; }
  main > * { max-width: none !important; }
  .page-head { border-bottom: 1px solid #000; padding: 0 0 0.3rem; }
  .entry, .day-title { break-inside: avoid; }
  .tag { border-color: #666; }
  a { color: #000; text-decoration: none; }
}
