/* Ledger — read in a dark room at 11pm. Warm, low contrast against black,
   nothing that flashes, nothing that scores. No red and no green anywhere:
   both of them turn an entry into a verdict, which is the one thing the
   spec forbids. Sand and amber do all the work. */

:root {
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;

  --ground: #14120f;
  --raised: #1c1915;
  --raised-2: #241f19;
  --line: #2e2820;
  --line-soft: #241f19;

  --ink: #ece3d5;
  --ink-quiet: #9c9184;
  --ink-faint: #6f665b;

  --sand: #d9bb8b;
  --amber: #e0a45c;
  --sand-dim: rgba(217, 187, 139, 0.14);
  --sand-edge: rgba(217, 187, 139, 0.28);

  --r: 14px;
  --pad: 1.25rem;
  --max: 34rem;

  color-scheme: dark;
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 17px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden { display: none !important; }

/* ------------------------------------------------------------ shell */

#app {
  max-width: var(--max);
  margin: 0 auto;
  padding: 1.5rem var(--pad) 4rem;
}

.view { animation: fade 220ms ease-out; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

h1, h2 { font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 0.35rem; }

p { margin: 0 0 0.85rem; }

.quiet { color: var(--ink-quiet); }
.fine { font-size: 0.9rem; line-height: 1.5; }

/* Sentence case, not caps: one of these labels is a whole sentence, and
   set in caps it shouts. */
.label {
  font-size: 0.88rem;
  color: var(--ink-faint);
  margin: 1.6rem 0 0.6rem;
}

/* ------------------------------------------------------------- lock */

.lock {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: var(--pad);
}
.lock form {
  width: 100%;
  max-width: 20rem;
  display: grid;
  gap: 0.85rem;
}
.lock h1 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: 1.9rem;
  margin: 0 0 0.5rem;
  color: var(--sand);
}
.err { color: var(--amber); font-size: 0.9rem; min-height: 1.2rem; margin: 0; }

/* ----------------------------------------------------------- inputs */

input[type="password"], textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.85rem 0.9rem;
  resize: none;
}
textarea { line-height: 1.5; }
input:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--sand-edge);
  outline-offset: 2px;
}
::placeholder { color: var(--ink-faint); }

label {
  display: block;
  margin: 1.4rem 0;
  color: var(--ink-quiet);
  font-size: 0.95rem;
}
label textarea { margin-top: 0.5rem; color: var(--ink); font-size: 1rem; }

/* ---------------------------------------------------------- buttons */

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.primary {
  display: block;
  width: 100%;
  min-height: 3.25rem;
  margin-top: 1.5rem;
  padding: 0.9rem 1rem;
  border-radius: 12px;
  background: var(--sand);
  color: #1a1610;
  font-weight: 600;
  transition: opacity 160ms ease;
}
.primary:active { opacity: 0.82; }
.primary[disabled] { opacity: 0.45; cursor: default; }

.lock button {
  min-height: 3.25rem;
  border-radius: 12px;
  background: var(--sand);
  color: #1a1610;
  font-weight: 600;
}

.big {
  display: grid;
  gap: 0.4rem;
  width: 100%;
  text-align: left;
  min-height: 7.5rem;
  margin: 2rem 0 1rem;
  padding: 1.4rem 1.35rem;
  border-radius: 18px;
  background: linear-gradient(160deg, #2b2419, #201b14);
  border: 1px solid var(--sand-edge);
  transition: transform 160ms ease;
}
.big:active { transform: scale(0.985); }
.big-title { font-size: 1.35rem; font-weight: 600; color: var(--sand); }
.big-sub { font-size: 0.92rem; line-height: 1.45; color: var(--ink-quiet); }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }

.tile {
  min-height: 3.5rem;
  padding: 0.9rem;
  border-radius: 12px;
  background: var(--raised);
  border: 1px solid var(--line);
  color: var(--ink);
}
.tile:active { background: var(--raised-2); }

.link {
  padding: 0.7rem 0;
  color: var(--sand);
  text-align: left;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.link.quiet { color: var(--ink-quiet); display: block; width: 100%; text-align: center; margin-top: 0.75rem; }

.back {
  display: inline-block;
  margin: -0.4rem 0 1.25rem -0.5rem;
  padding: 0.6rem 0.5rem;
  color: var(--ink-quiet);
  font-size: 0.95rem;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
  margin-top: 2.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line-soft);
}
.tabs button {
  padding: 0.85rem 0.25rem;
  border-radius: 10px;
  font-size: 0.85rem;
  color: var(--ink-quiet);
}
.tabs button:active { background: var(--raised); }

/* ------------------------------------------------------------- home */

.home-head { margin-bottom: 0.5rem; }
.home-head .quiet { margin-bottom: 0.25rem; }

.count {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin: 0 0 0.5rem;
  font-size: 3.6rem;
  font-weight: 300;
  line-height: 1;
  letter-spacing: -0.03em;
  color: var(--sand);
}
.count small {
  font-size: 0.85rem;
  font-weight: 400;
  letter-spacing: 0.02em;
  color: var(--ink-quiet);
}

.nudge {
  margin: 1.5rem 0;
  padding: 1rem 1.1rem;
  border-radius: 12px;
  background: var(--raised);
  border: 1px solid var(--line);
}
.nudge p { margin: 0 0 0.25rem; font-size: 0.95rem; color: var(--ink-quiet); }

.card {
  margin: 2rem 0 0;
  padding: 1.1rem 1.15rem 0.5rem;
  border-radius: 14px;
  background: var(--raised);
  border: 1px solid var(--line-soft);
}
.card h2 { color: var(--ink-quiet); font-size: 0.82rem; letter-spacing: 0.06em; text-transform: uppercase; font-weight: 500; }

.empty {
  margin: 2rem 0 0;
  padding: 1.1rem 1.15rem;
  border-radius: 14px;
  border: 1px dashed var(--line);
  color: var(--ink-quiet);
  font-size: 0.92rem;
  line-height: 1.5;
}

/* His own words. Serif so they read as testimony, not as chrome. */
.testimony, .morning-note, .plan-quote {
  font-family: var(--serif);
  font-size: 1.05rem;
  line-height: 1.55;
  color: var(--ink);
}

.morning { padding: 0.9rem 0; border-top: 1px solid var(--line-soft); }
.morning:first-child { border-top: none; padding-top: 0.2rem; }
.morning-head {
  font-family: var(--sans);
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  margin-bottom: 0.35rem;
}
.morning-note { margin: 0; }
.morning-note::before { content: "\201C"; }
.morning-note::after { content: "\201D"; }

.plan-quote {
  margin: 1.25rem 0;
  padding: 1rem 1.1rem;
  border-left: 2px solid var(--sand-edge);
  border-radius: 0 10px 10px 0;
  background: var(--sand-dim);
}
.plan-quote p { margin: 0; }
.plan-quote .plan-head {
  display: block;
  font-family: var(--sans);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 0.35rem;
}

.testimony { margin-top: 1.5rem; }
.testimony .t-block { margin: 0 0 1.1rem; }
.testimony .t-when {
  display: block;
  font-family: var(--sans);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  margin-bottom: 0.3rem;
}

/* --------------------------------------------------- scales & chips */

.scale { display: grid; grid-template-columns: repeat(5, 1fr); gap: 0.5rem; }
.step {
  min-height: 3.25rem;
  border-radius: 12px;
  background: var(--raised);
  border: 1px solid var(--line);
  color: var(--ink-quiet);
  font-size: 1.05rem;
  transition: background 140ms ease, color 140ms ease;
}
/* Selection is the only state. Every step looks identical otherwise —
   a 5 must not look heavier than a 1. */
.step[aria-pressed="true"] {
  background: var(--sand);
  border-color: var(--sand);
  color: #1a1610;
  font-weight: 600;
}

.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chip {
  min-height: 2.9rem;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  background: var(--raised);
  border: 1px solid var(--line);
  color: var(--ink-quiet);
  font-size: 0.95rem;
  transition: background 140ms ease, color 140ms ease;
}
.chip[aria-pressed="true"] {
  background: var(--sand);
  border-color: var(--sand);
  color: #1a1610;
}

/* -------------------------------------------------------- urge dial */

.timer-wrap { display: block; }

.dial {
  position: relative;
  width: min(15rem, 68vw);
  margin: 0.5rem auto 1.25rem;
  aspect-ratio: 1;
}
.dial svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.dial-bg { fill: none; stroke: var(--line); stroke-width: 6; }
.dial-arc {
  fill: none;
  stroke: var(--sand);
  stroke-width: 6;
  stroke-linecap: round;
  opacity: 0.9;
}
.dial-time {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 2.5rem;
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: var(--ink);
}
#urge-msg { text-align: center; }

/* ------------------------------------------------------------- chat */

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  min-height: 40svh;
  max-height: calc(100svh - 14rem);
  overflow-y: auto;
  padding: 0.25rem 0 1rem;
  overscroll-behavior: contain;
}
.msg {
  max-width: 88%;
  padding: 0.8rem 1rem;
  border-radius: 14px;
  font-size: 1rem;
  line-height: 1.55;
}
.msg p { margin: 0 0 0.7rem; }
.msg p:last-child { margin-bottom: 0; }
.msg.user {
  align-self: flex-end;
  background: var(--raised-2);
  border: 1px solid var(--line);
  border-bottom-right-radius: 5px;
}
.msg.coach {
  align-self: flex-start;
  background: var(--sand-dim);
  border: 1px solid var(--line-soft);
  border-bottom-left-radius: 5px;
}
.msg.coach.waiting::after {
  content: "";
  display: inline-block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: var(--sand);
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 0.25; } 50% { opacity: 0.9; } }

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.6rem;
  align-items: end;
  position: sticky;
  bottom: 0;
  padding: 0.75rem 0 0.25rem;
  background: linear-gradient(to top, var(--ground) 70%, transparent);
}
.composer textarea { max-height: 9rem; overflow-y: auto; }
.composer button {
  min-height: 3rem;
  padding: 0 1.1rem;
  border-radius: 10px;
  background: var(--sand);
  color: #1a1610;
  font-weight: 600;
}
.composer button[disabled] { opacity: 0.4; cursor: default; }

/* -------------------------------------------------------------- log */

.counter {
  display: grid;
  gap: 1rem;
  justify-items: center;
  margin: 1.5rem 0 0.5rem;
}
#drink-count {
  font-size: 4.5rem;
  font-weight: 300;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--sand);
}
.counter .primary { margin-top: 0; max-width: 16rem; }

/* ------------------------------------------------------------ curve */

.chart { width: 100%; height: auto; display: block; margin: 1.25rem 0 0.5rem; }
.chart .frame { stroke: var(--line); stroke-width: 1; fill: none; }
.chart .grid { stroke: var(--line-soft); stroke-width: 1; }
.chart .tick { fill: var(--ink-faint); font-size: 9px; font-family: var(--sans); }
.chart .attempt { fill: none; stroke: var(--sand); stroke-width: 1.5; opacity: 0.3; }
/* Every point renders identically. Marking outcomes differently here would
   turn the plot into a pass/fail scorecard. */
.chart .pt { fill: var(--sand); opacity: 0.85; }
.chart .daymark { stroke: var(--amber); stroke-width: 1; stroke-dasharray: 3 4; opacity: 0.55; }
.chart .daymark-label { fill: var(--amber); font-size: 9px; font-family: var(--sans); opacity: 0.8; }

/* --------------------------------------------------------- patterns */

.pat-group { margin: 2rem 0; }
.pat-group h3 {
  font-size: 0.8rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 0.75rem;
}
.pat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 1.25rem;
  padding: 0.85rem 0;
  border-top: 1px solid var(--line-soft);
}
.pat-row:first-of-type { border-top: none; }
.pat-name { flex-basis: 100%; color: var(--ink); }
.pat-stat { font-size: 0.85rem; color: var(--ink-quiet); font-variant-numeric: tabular-nums; }
.pat-stat b { font-weight: 600; color: var(--ink); }

/* Density, not severity: one hue, opacity carries the count. */
.density {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  gap: 4px;
  margin-top: 0.5rem;
}
.dcell {
  aspect-ratio: 1;
  border-radius: 3px;
  background: var(--sand);
  opacity: 0.06;
}
.density-key { margin-top: 0.75rem; font-size: 0.8rem; color: var(--ink-faint); }

/* ------------------------------------------------------------ misc */

.inline-err {
  margin: 1rem 0;
  padding: 0.75rem 0.9rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--raised);
  color: var(--ink-quiet);
  font-size: 0.9rem;
}

/* Stays sans even inside .testimony — UI chrome must not borrow his voice. */
.note { font-family: var(--sans); color: var(--ink-quiet); font-size: 0.92rem; }
.inline-err { font-family: var(--sans); }

/* ------------------------------------------------------ wider screens */

@media (min-width: 40rem) {
  body { font-size: 18px; }
  #app { padding-top: 3rem; }
  .count { font-size: 4.2rem; }
}

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

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .view { animation: none; }
}

/* ---------------------------------------------------------- daylight */

/* An explicit choice always wins. A small inline script in <head> stamps
   data-theme before first paint, so switching never flashes the old palette. */
:root[data-theme="light"] {
    --ground: #f7f2e8;
    --raised: #fffbf3;
    --raised-2: #f2ebdd;
    --line: #e0d7c4;
    --line-soft: #ebe3d3;

    --ink: #26211a;
    --ink-quiet: #6d6455;
    --ink-faint: #948a78;

    --sand: #8a6524;
    --amber: #a06b1f;
    --sand-dim: rgba(138, 101, 36, 0.08);
    --sand-edge: rgba(138, 101, 36, 0.25);

    color-scheme: light;
}
:root[data-theme="light"] .big { background: linear-gradient(160deg, #fdf6e7, #f6efdf); }
:root[data-theme="light"] .primary,
:root[data-theme="light"] .lock button,
:root[data-theme="light"] .composer button { color: #fffdf7; }
:root[data-theme="light"] .step[aria-pressed="true"],
:root[data-theme="light"] .chip[aria-pressed="true"] { color: #fffdf7; }
:root[data-theme="light"] .dcell { opacity: 0.08; }

/* Fallback only for the instant before that script runs, and for no-JS.
   Guarded on :not([data-theme]) so it can never override a real choice. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --ground: #f7f2e8;
    --raised: #fffbf3;
    --raised-2: #f2ebdd;
    --line: #e0d7c4;
    --line-soft: #ebe3d3;

    --ink: #26211a;
    --ink-quiet: #6d6455;
    --ink-faint: #948a78;

    --sand: #8a6524;
    --amber: #a06b1f;
    --sand-dim: rgba(138, 101, 36, 0.08);
    --sand-edge: rgba(138, 101, 36, 0.25);

    color-scheme: light;
  }
  .big { background: linear-gradient(160deg, #fdf6e7, #f6efdf); }
  .primary, .lock button, .composer button { color: #fffdf7; }
  .step[aria-pressed="true"], .chip[aria-pressed="true"] { color: #fffdf7; }
  .dcell { opacity: 0.08; }
}

/* ------------------------------------------------------- chat sessions
   A conversation from a bad Tuesday shouldn't be buried under a calm
   Sunday's, and the two shouldn't share a thread. Past ones stay
   reachable; nothing here deletes anything. */

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

.chat-head .back { margin-bottom: 0; }

.chat-head-actions {
  display: flex;
  gap: 1.25rem;
  flex-shrink: 0;
}

.chat-head-actions .link {
  font-size: 0.92rem;
  /* Text-width tap targets are miserable on a phone. Pad out horizontally and
     pull the margin back so the row still looks tight. */
  padding: 0.7rem 0.4rem;
  margin-right: -0.4rem;
}

.sessions {
  margin: 0.75rem 0 0.25rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  max-height: 17rem;
  overflow-y: auto;
}

.session-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  width: 100%;
  padding: 0.7rem 0.85rem;
  background: none;
  border: 0;
  border-bottom: 1px solid var(--line-soft);
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.session-row:last-child { border-bottom: 0; }
.session-row:hover { background: var(--sand-dim); }

/* Marks where he is, without ranking anything. */
.session-row.current { box-shadow: inset 2px 0 0 var(--sand); }

.session-when {
  font-family: var(--sans);
  font-size: 0.76rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.session-title {
  font-family: var(--sans);
  font-size: 0.95rem;
  line-height: 1.35;
  color: var(--ink);
}

/* Not a warning — just a way to find that night again. Same sand as
   everything else; nothing on this screen is colour-coded by severity. */
.session-tag {
  margin-top: 0.2rem;
  padding: 0.1rem 0.45rem;
  border: 1px solid var(--sand-edge);
  border-radius: 999px;
  font-family: var(--sans);
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  color: var(--ink-quiet);
}

/* ------------------------------------------------- theme, med card, checkpoint */

.theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 2rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line-soft);
}

.theme-label {
  font-family: var(--sans);
  font-size: 0.8rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.theme-pick { display: flex; gap: 0.4rem; }
.theme-pick .chip { padding: 0.45rem 0.7rem; font-size: 0.85rem; }

/* The medication card. Persistent but never nagging — it states a fact and
   offers words to use, and it takes "not now" for an answer. */
.med {
  margin-top: 1.5rem;
  padding: var(--pad);
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: var(--r);
}
.med p { font-family: var(--sans); font-size: 0.95rem; color: var(--ink-quiet); }
.med summary {
  margin-top: 0.75rem;
  font-family: var(--sans);
  font-size: 0.92rem;
  color: var(--sand);
  cursor: pointer;
}
.med blockquote {
  margin: 0.75rem 0 0;
  padding-left: 0.9rem;
  border-left: 2px solid var(--sand-edge);
  font-family: var(--serif);
  font-size: 1.02rem;
  line-height: 1.55;
  color: var(--ink);
}

.stat {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--line-soft);
}
.stat:last-child { border-bottom: 0; }
.stat-label { font-family: var(--sans); font-size: 0.92rem; color: var(--ink-quiet); }
/* Same weight for every value. Nothing here is a score. */
.stat-value { font-family: var(--sans); font-size: 0.95rem; color: var(--ink); }

.daylist { margin-top: 0.75rem; }
.dayrow { display: flex; justify-content: space-between; padding: 0.3rem 0; }

/* recorder */
.rec { margin-top: 2rem; padding-top: 1.25rem; border-top: 1px solid var(--line-soft); }
.rec-row { display: flex; align-items: center; gap: 0.75rem; margin: 0.5rem 0; }
.rec-time {
  font-family: var(--sans);
  font-variant-numeric: tabular-nums;
  color: var(--ink-quiet);
}
.rec audio, .testimony audio { width: 100%; margin-top: 0.6rem; }

.fs-actions { display: flex; align-items: center; gap: 1rem; margin-top: 0.4rem; }
.fs-actions .link { width: auto; margin: 0; padding: 0.45rem 0; text-align: left; }
.fs-warn { font-family: var(--sans); font-size: 0.85rem; color: var(--ink-faint); }
