/* Palette: Inkwell #2C3639, Lunar Eclipse #3F4E4F, Creme Brulee #A27B5B,
   Au Lait #DCD7C9, plus a few tints and shades derived from them.

   One contrast rule runs through all of this: Creme Brulee measures 3.3:1 on
   Inkwell and 2.6:1 on Au Lait, so it is never a button background with text on
   it. It fills progress bars, marks accents and sets large numerals. Buttons use
   Inkwell or Au Lait, which pair at 8.4:1 in either direction. */

:root {
  --bg: #DCD7C9;
  --surface: #F4F2EC;
  --surface-2: #E7E3D8;
  --text: #2C3639;
  --text-muted: #5C6B6C;
  --accent: #A27B5B;
  --accent-text: #8A6647;
  --accent-soft: rgba(162, 123, 91, 0.16);
  --border: rgba(44, 54, 57, 0.14);
  --btn-bg: #2C3639;
  --btn-text: #DCD7C9;
  --shadow: 0 1px 2px rgba(44, 54, 57, 0.08), 0 8px 24px rgba(44, 54, 57, 0.06);
  --scrim: rgba(44, 54, 57, 0.44);

  --ahead: #4F6F52;
  --ontrack: #3F4E4F;
  --behind: #9C5B3C;
}

/* The bare :root block above is the complete light palette. These two blocks
   only re-point the same tokens, so nothing has its sole definition inside a
   media query. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #2C3639;
    --surface: #3F4E4F;
    --surface-2: #4A5A5B;
    --text: #DCD7C9;
    --text-muted: #A9AFA6;
    --accent: #A27B5B;
    --accent-text: #C39B77;
    --accent-soft: rgba(162, 123, 91, 0.22);
    --border: rgba(220, 215, 201, 0.14);
    --btn-bg: #DCD7C9;
    --btn-text: #2C3639;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.28), 0 8px 24px rgba(0, 0, 0, 0.22);
    --scrim: rgba(20, 26, 28, 0.6);

    --ahead: #8FB694;
    --ontrack: #C7CFC6;
    --behind: #D89B7B;
  }
}

:root[data-theme="dark"] {
  --bg: #2C3639;
  --surface: #3F4E4F;
  --surface-2: #4A5A5B;
  --text: #DCD7C9;
  --text-muted: #A9AFA6;
  --accent: #A27B5B;
  --accent-text: #C39B77;
  --accent-soft: rgba(162, 123, 91, 0.22);
  --border: rgba(220, 215, 201, 0.14);
  --btn-bg: #DCD7C9;
  --btn-text: #2C3639;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.28), 0 8px 24px rgba(0, 0, 0, 0.22);
  --scrim: rgba(20, 26, 28, 0.6);

  --ahead: #8FB694;
  --ontrack: #C7CFC6;
  --behind: #D89B7B;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  /* viewport-fit=cover puts content under the notch and home indicator unless
     the safe-area insets are paid back here. */
  padding:
    env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

main { max-width: 620px; margin: 0 auto; padding: 0 16px 96px; }

/* --- Header ---------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 20px 0 12px;
}
.topbar h1 { font-size: 30px; letter-spacing: -0.02em; margin: 0; font-weight: 700; }
.topbar .sub { font-size: 13px; color: var(--text-muted); margin: 2px 0 0; }

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 40px; height: 40px;
  border-radius: 12px;
  font-size: 17px;
  display: grid; place-items: center;
  cursor: pointer;
  flex: none;
}
.icon-btn:active { background: var(--surface-2); }

.back-link {
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--accent-text);
  text-decoration: none;
  font-size: 15px; font-weight: 600;
  padding: 18px 0 4px;
}

/* --- Tabs ------------------------------------------------------------ */

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 18px;
}
.tabs button {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 9px 4px;
  border-radius: 9px;
  cursor: pointer;
  white-space: nowrap;
}
.tabs button[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.tabs .count { opacity: 0.55; font-weight: 500; }

/* --- Cards ----------------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.card-head { display: flex; gap: 12px; align-items: flex-start; }

.cover {
  width: 44px; height: 58px;
  border-radius: 6px;
  display: grid; place-items: center;
  font-size: 22px;
  flex: none;
  box-shadow: inset -3px 0 6px rgba(0, 0, 0, 0.18);
}

.card-title { font-size: 17px; font-weight: 650; margin: 0; line-height: 1.25; }
.card-author { font-size: 13px; color: var(--text-muted); margin: 3px 0 0; }

.meta-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-top: 14px;
}

/* The daily loop starts here: this chip is the tap target that opens the update
   sheet, so it is deliberately oversized and reads as the loudest thing on the
   card. */
.page-chip {
  appearance: none;
  border: 1px solid var(--accent-soft);
  background: var(--accent-soft);
  color: var(--text);
  border-radius: 12px;
  padding: 8px 14px;
  font: inherit;
  cursor: pointer;
  text-align: left;
  min-height: 48px;
  display: flex; flex-direction: column; justify-content: center;
}
.page-chip:active { transform: scale(0.98); }
.page-chip .big {
  font-size: 21px; font-weight: 700;
  color: var(--accent-text);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.page-chip .small { font-size: 11px; color: var(--text-muted); margin-top: 1px; }

.target { text-align: right; font-size: 12px; color: var(--text-muted); line-height: 1.5; }
.target strong { display: block; font-size: 15px; color: var(--text); font-variant-numeric: tabular-nums; }

.progress {
  height: 6px;
  background: var(--surface-2);
  border-radius: 99px;
  overflow: hidden;
  margin-top: 14px;
}
.progress > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }

.badge {
  display: inline-block;
  font-size: 11px; font-weight: 700;
  letter-spacing: 0.03em; text-transform: uppercase;
  padding: 4px 9px;
  border-radius: 99px;
  border: 1px solid currentColor;
}
.badge.ahead { color: var(--ahead); }
.badge.ontrack { color: var(--ontrack); }
.badge.behind { color: var(--behind); }
.badge.done, .badge.neutral { color: var(--text-muted); }

.badge-row { display: flex; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.badge-note { font-size: 12px; color: var(--text-muted); }

/* --- Lists ----------------------------------------------------------- */

.row {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  appearance: none;
}
.row:active { background: var(--surface-2); }
.row .grow { flex: 1; min-width: 0; }
.row .r-title { font-size: 15px; font-weight: 600; }
.row .r-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.row .chev { color: var(--text-muted); font-size: 18px; flex: none; }

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

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 13px;
  padding: 13px 18px;
  font: inherit;
  font-size: 15px;
  font-weight: 650;
  cursor: pointer;
  width: 100%;
  min-height: 48px;
}
.btn-primary { background: var(--btn-bg); color: var(--btn-text); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-quiet { background: transparent; color: var(--text-muted); border-color: var(--border); }
.btn-danger { background: transparent; color: var(--behind); border-color: var(--border); }
.btn:active { opacity: 0.85; }
.btn:disabled { opacity: 0.45; cursor: default; }

.btn-stack { display: grid; gap: 8px; margin-top: 20px; }

/* --- Forms ----------------------------------------------------------- */

label.field { display: block; margin-bottom: 16px; }
label.field > span {
  display: block;
  font-size: 13px; font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}
input[type="text"], input[type="number"], input[type="date"], textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 13px 14px;
  font: inherit;
  font-size: 16px; /* under 16px, iOS zooms the page on focus */
  color: var(--text);
  min-height: 48px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { min-height: 120px; resize: vertical; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }

.chip-row { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 11px;
  padding: 10px 0;
  font: inherit;
  font-size: 13px; font-weight: 600;
  cursor: pointer;
  flex: 1;
  min-width: 40px;
  min-height: 44px;
}
.chip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-text);
}

.seg { display: flex; gap: 4px; background: var(--surface-2); border-radius: 12px; padding: 4px; margin-bottom: 16px; }
.seg button {
  flex: 1; appearance: none; border: 0; background: transparent;
  color: var(--text-muted); font: inherit; font-size: 13px; font-weight: 600;
  padding: 10px 6px; border-radius: 9px; cursor: pointer; min-height: 44px;
}
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.derived {
  background: var(--accent-soft);
  border-radius: 12px;
  padding: 13px 15px;
  font-size: 14px;
  color: var(--text);
  margin-bottom: 16px;
}
.derived strong { color: var(--accent-text); font-size: 17px; }
.derived.error { background: transparent; border: 1px solid var(--behind); color: var(--behind); }

.emoji-grid { display: flex; gap: 6px; flex-wrap: wrap; }
.emoji-grid button {
  appearance: none; border: 1px solid var(--border); background: var(--surface);
  width: 46px; height: 46px; border-radius: 11px; font-size: 21px; cursor: pointer;
}
.emoji-grid button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.swatch { width: 46px; height: 46px; border-radius: 11px; border: 2px solid transparent; cursor: pointer; appearance: none; }
.swatch[aria-pressed="true"] { border-color: var(--text); }

/* --- Sections -------------------------------------------------------- */

h2.section {
  font-size: 12px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 28px 0 10px;
}

.sched {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
}
.sched-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 14px;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.sched-row:last-child { border-bottom: 0; }
.sched-row.past { color: var(--text-muted); }
.sched-row.today { background: var(--accent-soft); font-weight: 650; }
.sched-row .pages { font-variant-numeric: tabular-nums; color: var(--text-muted); }
.sched-row.today .pages { color: var(--accent-text); font-weight: 700; }

.sched details > summary {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 14px; font-size: 14px; cursor: pointer; list-style: none;
  border-bottom: 1px solid var(--border);
}
.sched details > summary::-webkit-details-marker { display: none; }
.sched details[open] > summary { font-weight: 650; }
.sched details .sched-row { padding-left: 28px; background: var(--surface-2); }

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 56px 24px;
  font-size: 15px;
  line-height: 1.6;
}
.empty .big { font-size: 34px; display: block; margin-bottom: 12px; }

/* --- Sheet ----------------------------------------------------------- */

.scrim {
  position: fixed; inset: 0;
  background: var(--scrim);
  z-index: 40;
  animation: fade 0.16s ease;
}
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 50;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.24);
  animation: rise 0.22s cubic-bezier(0.2, 0.9, 0.3, 1);
  max-width: 620px;
  margin: 0 auto;
  max-height: 88vh;
  overflow-y: auto;
}
@keyframes rise { from { transform: translateY(100%); } }

.sheet h3 { margin: 0 0 2px; font-size: 18px; }
.sheet .sheet-sub { font-size: 13px; color: var(--text-muted); margin: 0 0 18px; }

/* The page field: big, centred, and pre-filled with today's target so the
   common case is open-and-save with no typing at all. */
.page-input {
  width: 100%;
  font-size: 40px !important;
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
  padding: 14px !important;
  letter-spacing: -0.02em;
}

.quick-row { display: flex; gap: 6px; margin: 12px 0 18px; }
.quick-row button {
  flex: 1; appearance: none;
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); border-radius: 11px; padding: 11px 4px;
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  min-height: 44px;
}
.quick-row button:active { background: var(--accent-soft); }

.sheet-actions { display: grid; grid-template-columns: 1fr 2fr; gap: 8px; }

/* --- Toast ----------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%; transform: translateX(-50%) translateY(-140%);
  top: calc(10px + env(safe-area-inset-top));
  z-index: 90;
  background: var(--btn-bg); color: var(--btn-text);
  padding: 11px 18px; border-radius: 12px;
  font-size: 14px; font-weight: 600;
  box-shadow: var(--shadow);
  transition: transform 0.24s cubic-bezier(0.2, 0.9, 0.3, 1);
  max-width: 90vw;
}
.toast.show { transform: translateX(-50%) translateY(0); }

.log-row {
  display: flex; justify-content: space-between;
  padding: 10px 14px; font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.log-row:last-child { border-bottom: 0; }
.log-row .delta { color: var(--accent-text); font-weight: 600; font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
