/*
 * Sheet design system for Reflections.
 *
 * Fonts are self-hosted latin-subset woff2 files from Fontsource via jsDelivr
 * (https://cdn.jsdelivr.net/npm/@fontsource/<family>@<version>/files/<file>.woff2):
 *   literata@5.3.0, source-serif-4@5.3.1, work-sans@5.3.0
 * Font URLs carry no ?v=. The same URL must be used by the preload links in
 * templates/_head.html, so a font change needs a new filename.
 */

@font-face {
  font-family: "Literata";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("/static/fonts/literata-latin-600-normal.woff2") format("woff2");
}
@font-face {
  font-family: "Source Serif 4";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("/static/fonts/source-serif-4-latin-400-normal.woff2") format("woff2");
}
@font-face {
  font-family: "Source Serif 4";
  font-style: italic;
  font-weight: 400;
  font-display: swap;
  src: url("/static/fonts/source-serif-4-latin-400-italic.woff2") format("woff2");
}
@font-face {
  font-family: "Source Serif 4";
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url("/static/fonts/source-serif-4-latin-500-normal.woff2") format("woff2");
}
@font-face {
  font-family: "Work Sans";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("/static/fonts/work-sans-latin-400-normal.woff2") format("woff2");
}
@font-face {
  font-family: "Work Sans";
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url("/static/fonts/work-sans-latin-500-normal.woff2") format("woff2");
}
@font-face {
  font-family: "Work Sans";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("/static/fonts/work-sans-latin-600-normal.woff2") format("woff2");
}

/* ---------- Tokens ---------- */

:root {
  color-scheme: light dark;
  --ground: #e7ebe6;
  --sheet: #ffffff;
  --ink: #1f2622;
  --quote: #2c3a33;
  --muted: #5d7266;
  --rule-quote: #c9d1ca;
  --rule: #e1e6e1;
  --track: #d5dcd5;
  --track-on: #ffffff;
  --accent: #4f7a5f;
  --on-accent: #ffffff;
  --danger: #b3412e;

  --reading-scale: 1;
  --essay-size: 17px;
  --title-size: 28px;
  --quote-size: 19px;
  --count-size: 64px;

  --font-ui: "Work Sans", system-ui, sans-serif;
  --font-serif: "Source Serif 4", Georgia, serif;
  --font-display: "Literata", Georgia, serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ground: #121614;
    --sheet: #1b211e;
    --ink: #e2e6e1;
    --quote: #cfd8d1;
    --muted: #8fae97;
    --rule-quote: #3a4640;
    --rule: #2c3631;
    --track: #242c28;
    --track-on: #34403a;
    --accent: #8fae97;
    --on-accent: #121614;
    --danger: #e48a78;
  }
}

@media (min-width: 720px) {
  :root {
    --essay-size: 18px;
    --title-size: 32px;
    --count-size: 84px;
  }
}

body.font-extra-small { --reading-scale: 0.85; }
body.font-small { --reading-scale: 0.93; }
body.font-medium { --reading-scale: 1; }
body.font-large { --reading-scale: 1.12; }
body.font-extra-large { --reading-scale: 1.25; }

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; }

body {
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-ui);
  margin: 0;
  -webkit-text-size-adjust: 100%;
}

:focus-visible {
  outline: 2px solid var(--muted);
  outline-offset: 2px;
}

a { color: inherit; }

/* ---------- Page structure ---------- */

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: 18px max(14px, env(safe-area-inset-right)) 18px max(14px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (min-width: 720px) {
  .page { padding-top: 48px; padding-left: 24px; padding-right: 24px; }
}

.sheet {
  background: var(--sheet);
  border-radius: 12px;
  padding: 24px 22px 26px;
}

@media (min-width: 720px) {
  .sheet { padding: 44px 52px 48px; }
}

.eyebrow {
  font: 600 11px/1.3 var(--font-ui);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 640px;
  margin: 0 auto;
  padding: 14px 18px 4px;
}

.wordmark {
  font: 600 17px/1.2 var(--font-display);
  color: var(--ink);
  text-decoration: none;
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--ink);
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }

/* ---------- Reading text ---------- */

.title {
  font: 600 calc(var(--title-size) * var(--reading-scale))/1.2 var(--font-display);
  margin: 8px 0 22px;
  text-wrap: balance;
}

.quote {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: calc(var(--quote-size) * var(--reading-scale));
  line-height: 1.5;
  color: var(--quote);
}
.quote p { margin: 0; }
.quote p + p { margin-top: 0.85em; }

.attribution {
  font: 500 11px/1.3 var(--font-ui);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: right;
  margin: 12px 0 0;
}

.quote-rule {
  width: 44px;
  height: 1px;
  background: var(--rule-quote);
  margin: 26px auto;
}

.essay {
  font-family: var(--font-serif);
  font-size: calc(var(--essay-size) * var(--reading-scale));
  line-height: 1.68;
}
.essay p { margin: 0; }
.essay p + p { margin-top: 0.85em; }

/* ---------- Controls ---------- */

.bottom-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 640px;
  margin: 0 auto;
  padding: 10px max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  background: var(--ground);
}

@media (min-width: 720px) {
  .bottom-bar { padding-left: 24px; padding-right: 24px; }
}

.seg {
  display: flex;
  background: var(--track);
  border-radius: 10px;
  padding: 3px;
}
.bottom-bar .seg { flex: 1; }

.seg > button,
.seg > a {
  flex: 1;
  font: 500 14px/1.2 var(--font-ui);
  background: none;
  border: 0;
  color: var(--ink);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  white-space: nowrap;
}
.seg > button[aria-pressed="true"],
.seg > a[aria-current="page"] {
  background: var(--track-on);
}

.flat-btn {
  font: 500 14px/1.2 var(--font-ui);
  background: none;
  border: 0;
  color: var(--ink);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
}

.switch {
  appearance: none;
  position: relative;
  flex: none;
  width: 44px;
  height: 26px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--track);
  cursor: pointer;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--track-on);
}
.switch:checked,
.switch[aria-checked="true"] {
  background: var(--accent);
}
.switch:checked::after,
.switch[aria-checked="true"]::after {
  left: auto;
  right: 3px;
  background: var(--on-accent);
}

/* ---------- Lists and messages ---------- */

.row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;
  color: inherit;
  text-decoration: none;
}
.row + .row { border-top: 1px solid var(--rule); }

.text-row {
  display: block;
  width: 100%;
  font: 500 15px/1.3 var(--font-ui);
  color: var(--ink);
  background: none;
  border: 0;
  padding: 16px 18px;
  text-align: left;
  cursor: pointer;
}
.text-row + .text-row { border-top: 1px solid var(--rule); }

.danger { color: var(--danger); }

.notice {
  font: 400 16px/1.5 var(--font-ui);
  color: var(--ink);
  text-align: center;
  padding: 40px 22px;
}

/* ---------- Toast and dialog ---------- */

.toast {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--on-accent);
  font: 500 14px/1.3 var(--font-ui);
  padding: 10px 18px;
  border-radius: 10px;
  z-index: 10000;
}

.toast.danger {
  background: var(--danger);
  color: var(--on-accent);
}

.dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(90vw, 440px);
  background: var(--sheet);
  color: var(--ink);
  border: 1px solid var(--rule);
  border-radius: 12px;
  padding: 20px;
  z-index: 10001;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.dialog textarea {
  width: 100%;
  height: 120px;
  resize: none;
  font: 400 14px/1.4 var(--font-ui);
  color: var(--ink);
  background: var(--ground);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 8px;
}

/* ---------- Home page ---------- */

.day-count {
  display: block;
  width: 100%;
  border: 0;
  color: inherit;
  font: inherit;
  text-align: center;
  padding: 30px 20px 28px;
  cursor: pointer;
}
.day-number {
  display: block;
  margin-top: 6px;
  font: 600 var(--count-size)/1 var(--font-display);
  font-variant-numeric: lining-nums tabular-nums;
  letter-spacing: -0.02em;
}
.day-number.is-duration {
  font-size: 28px;
  line-height: 1.2;
  letter-spacing: 0;
  text-wrap: balance;
}
.day-since {
  display: block;
  margin-top: 12px;
  font: 400 13px/1.3 var(--font-ui);
  color: var(--muted);
}

.reading-rows { padding: 0; }
.reading-rows .row-text { flex: 1; min-width: 0; }
.today-title {
  display: block;
  margin-top: 4px;
  font: 500 calc(18px * var(--reading-scale))/1.3 var(--font-serif);
  text-wrap: balance;
}
.today-title:empty { display: none; }
.chev { width: 18px; height: 18px; color: var(--muted); flex: none; }

@media (min-width: 720px) {
  .reading-rows .row {
    display: grid;
    grid-template-columns: 170px minmax(0, 1fr) 18px;
    gap: 16px;
    padding: 18px 22px;
  }
  .reading-rows .row-text { display: contents; }
  .reading-rows .today-title {
    margin-top: 0;
    font-size: 19px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .reading-rows .today-title:empty { display: block; }
}

/* ---------- Settings page ---------- */

.page > .eyebrow { margin: 10px 4px -4px; }
.page > .title { margin-bottom: 4px; }

.settings-group { padding: 6px 18px; }
.text-rows { padding: 0; }
.text-rows .text-row:first-child { border-radius: 12px 12px 0 0; }
.text-rows .text-row:last-child { border-radius: 0 0 12px 12px; }
.text-row:disabled { color: var(--muted); cursor: default; }

.setting-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 0;
}
.setting-item + .setting-item { border-top: 1px solid var(--rule); }
.setting-label { min-width: 0; flex: 1; }
.setting-label h3 { margin: 0; font: 500 15px/1.3 var(--font-ui); }
.setting-label p { margin: 3px 0 0; font: 400 13px/1.4 var(--font-ui); color: var(--muted); }
.setting-control { display: flex; align-items: center; gap: 4px; flex: none; }
.setting-value { font: 500 14px/1.3 var(--font-ui); text-align: right; }
.setting-id {
  font: 400 11px/1.4 ui-monospace, monospace;
  color: var(--muted);
  word-break: break-all;
  max-width: 190px;
  text-align: right;
}
.status-text::before { content: "\00b7\00a0"; }

.field {
  font: 400 15px/1.2 var(--font-ui);
  color: var(--ink);
  background: var(--track);
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
}
.field:disabled { color: var(--muted); }

.note { margin: 0 4px; font: 400 13px/1.5 var(--font-ui); color: var(--muted); }
.note.danger { color: var(--danger); }

.debug-panel {
  max-height: 220px;
  overflow-y: auto;
  padding: 14px 16px;
  font: 400 12px/1.5 ui-monospace, monospace;
  color: var(--muted);
}
.debug-entry { word-break: break-word; }
.debug-error { color: var(--danger); }
.debug-success { color: var(--accent); }

/* ---------- Buttons ---------- */

.btn {
  font: 500 15px/1.2 var(--font-ui);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 12px 20px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
}
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-quiet { background: var(--track); color: var(--ink); }
.btn-ghost { background: none; color: inherit; border-color: currentColor; }
.btn-sm { font-size: 14px; padding: 8px 14px; border-radius: 8px; }

/* ---------- Banners and injected PWA UI ---------- */

.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9999;
  padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  background: var(--track);
  color: var(--ink);
  border-bottom: 1px solid var(--rule);
  font: 500 14px/1.3 var(--font-ui);
  text-align: center;
  transform: translateY(-100%);
}
.offline-banner.is-shown { transform: translateY(0); }

.bottom-banner {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: min(90%, 560px);
  padding: 14px 16px;
  background: var(--sheet);
  color: var(--ink);
  border: 1px solid var(--rule);
  border-radius: 12px;
  font: 400 14px/1.4 var(--font-ui);
}
.bottom-banner .banner-text { flex: 1; }
.bottom-banner svg { flex: none; color: var(--muted); }
.bottom-banner .btn-ghost { border-color: var(--rule); }
.bottom-banner .banner-close {
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  padding: 6px;
  cursor: pointer;
}

.update-banner { transform: translateX(-50%) translateY(150%); }
.update-banner.is-shown { transform: translateX(-50%) translateY(0); }

.install-btn {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 8px;
  opacity: 0;
}
.install-btn.is-shown { opacity: 1; }
.install-btn:hover { opacity: 0.9; }

.toast.is-leaving { opacity: 0; }

@media (prefers-reduced-motion: no-preference) {
  .offline-banner,
  .update-banner,
  .install-btn { transition: transform 0.3s ease, opacity 0.3s ease; }
  .toast { animation: slideDown 0.3s ease; }
  .toast.is-leaving { animation: slideUp 0.3s ease forwards; }
  .modal-card { animation: slideIn 0.3s ease-out; }
  @keyframes slideDown {
    from { transform: translateX(-50%) translateY(-100%); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
  }
  @keyframes slideUp {
    from { transform: translateX(-50%) translateY(0); opacity: 1; }
    to { transform: translateX(-50%) translateY(-100%); opacity: 0; }
  }
  @keyframes slideIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
  }
}

/* ---------- Onboarding modal ---------- */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 10000;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background: var(--ground);
  overflow-y: auto;
}
.modal-card {
  width: 100%;
  max-width: 500px;
  margin: auto;
  background: var(--sheet);
  color: var(--ink);
  border-radius: 12px;
  padding: 32px 24px;
}
.modal-header { text-align: center; margin-bottom: 28px; }
.modal-header .title { margin: 8px 0 10px; font-size: 24px; }
.modal-sub { margin: 0; font: 400 14px/1.5 var(--font-ui); color: var(--muted); }

.form-group { margin-bottom: 22px; }
.form-label {
  display: block;
  margin-bottom: 8px;
  font: 500 14px/1.3 var(--font-ui);
  color: var(--muted);
}
.field-wide { width: 100%; padding: 12px; font-size: 16px; }
.field-wide:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.field::placeholder { color: var(--muted); }

.form-error {
  display: none;
  margin-bottom: 16px;
  padding: 12px;
  border: 1px solid var(--danger);
  border-radius: 8px;
  color: var(--danger);
  font: 400 14px/1.4 var(--font-ui);
  text-align: center;
}
.modal-actions { display: flex; flex-direction: column; gap: 12px; }
.modal-actions .btn-ghost { color: var(--muted); border-color: var(--rule); }
.modal-note {
  margin: 16px 0 0;
  font: 400 12px/1.4 var(--font-ui);
  color: var(--muted);
  text-align: center;
}

/* ---------- Offline page ---------- */

.offline-sheet { text-align: center; padding-top: 40px; padding-bottom: 40px; }
.offline-sheet .title { margin-bottom: 14px; }
.offline-sheet p { margin: 0 0 14px; font: 400 16px/1.6 var(--font-ui); }
.offline-sheet .muted { font-size: 14px; color: var(--muted); }
.offline-actions { display: flex; gap: 10px; justify-content: center; margin: 24px 0; }
