/* Soft Clay tokens, from the server's docs/bonsai-design-tokens.html. --muted
   is that page's reading gray: ink-dim is too light for a table's text. */
:root {
  color-scheme: light dark;
  --ground: #e7efe0;
  --surface: #fffefb;
  --tint: #eaf2e4;
  --track: #e3ecdc;
  --ink: #33463a;
  --dim: #93a595;
  --muted: #6b736c;
  --fern: #4f7d5c;
  --fern-text: #f2f8ef;
  --soil: #e8e4cf;
  --bark: #6e6749;
  --garnet: #a33b32;
  --garnet-line: #edd3ce;
  /* Not in the Soft Clay tokens: a count that needs a look, short of a problem. */
  --amber: #7a5a00;
  --amber-fill: #f3e6bf;
  /* The levels of a tree's graph, from its first steps to its last. */
  --level-0: #b86b00;
  --level-1: #17843f;
  --level-2: #0465af;
  --level-3: #8b306a;
  --shadow-raised: 0 2px 3px rgba(70, 96, 74, .06), 0 8px 20px rgba(70, 96, 74, .10);
  --shadow-chip: 0 1px 2px rgba(70, 96, 74, .10), 0 3px 8px rgba(70, 96, 74, .10);
  --shadow-pressed: inset 0 1px 2px rgba(0, 0, 0, .12);
  --radius-card: 20px;
  --radius-control: 14px;
  --rounded: ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  --rail: 208px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ground: #1a211b;
    --surface: #242c25;
    --tint: #2c362d;
    --track: #2c362d;
    --ink: #e8ece6;
    --dim: #8d9a8e;
    --muted: #9aa39b;
    --fern: #7fae8f;
    --fern-text: #152018;
    --soil: #302e20;
    --bark: #aba37e;
    --garnet: #de8078;
    --garnet-line: #4a322e;
    --amber: #e2b84f;
    --amber-fill: #3d3319;
    --level-0: #c5770f;
    --level-1: #54b66e;
    --level-2: #59aaf8;
    --level-3: #ef8bc5;
    --shadow-raised: 0 2px 3px rgba(0, 0, 0, .25), 0 8px 20px rgba(0, 0, 0, .30);
    --shadow-chip: 0 1px 2px rgba(0, 0, 0, .25), 0 3px 8px rgba(0, 0, 0, .28);
    --shadow-pressed: inset 0 1px 2px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* A double tap never zooms, and a tap acts at once. Pinch zoom stays. */
html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, sans-serif;
}

h1, h2, h3, button, .chip, .tag, .value, .wordmark, .row-title, #nav a, .parent, .top-link {
  font-family: var(--rounded);
}

h1 { font-size: 28px; font-weight: 800; line-height: 1.15; margin: 0; text-wrap: balance; }
h2 { font-size: 17px; font-weight: 800; margin: 10px 0 0; }
h3 { font-size: 15px; font-weight: 800; margin: 0; }
p { margin: 0; }
a { color: var(--fern); }
code, pre { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }
:focus-visible { outline: 2px solid var(--fern); outline-offset: 2px; }

/* Controls: a press shows, and a long press selects no text. */
button, summary, .chip, .row, a.tile, #nav a, .wordmark, .top-link, [role=button] {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button:active:not(:disabled), summary:active, .chip:active, .row:active, a.tile:active, #nav a:active, [role=button]:active {
  opacity: .65;
}

/* ------------------------------------------------------------ controls */

button {
  font-size: 15px;
  font-weight: 800;
  border: 0;
  border-radius: var(--radius-control);
  padding: 11px 18px;
  min-height: 44px;
  cursor: pointer;
}

button:disabled { opacity: .6; cursor: default; }
.primary { background: var(--fern); color: var(--fern-text); }
.secondary { background: var(--tint); color: var(--fern); }
/* tint never sits on the ground: a second action outside a card is raised. */
.raised { background: var(--surface); color: var(--fern); box-shadow: var(--shadow-chip); }
.destructive { background: var(--surface); color: var(--garnet); border: 1.5px solid var(--garnet-line); }
.ghost { background: none; color: var(--muted); font-weight: 600; }

input {
  font: inherit;
  /* iOS zooms the page when a field with smaller text gets focus. */
  font-size: 16px;
  color: var(--ink);
  background: var(--surface);
  border: 0;
  border-radius: var(--radius-control);
  padding: 11px 13px;
  min-height: 44px;
  box-shadow: var(--shadow-chip);
}

.chips, .actions { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-chip);
}

.chip[aria-current] { background: var(--fern); color: var(--fern-text); box-shadow: var(--shadow-pressed); }
.chip .arrow { margin-left: 6px; font-size: 15px; line-height: 1; }

.tag {
  display: inline-block;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  background: var(--soil);
  color: var(--bark);
}

.tag.good { background: var(--tint); color: var(--fern); }
.tag.bad, .tag.problem { background: var(--garnet-line); color: var(--garnet); }
.tag.watch { background: var(--amber-fill); color: var(--amber); }

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

#shell {
  padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
  display: grid;
  grid-template: "top top" auto "nav screen" 1fr / var(--rail) minmax(0, 1fr);
  min-height: 100dvh;
}

#top {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(12px + env(safe-area-inset-top)) 22px 4px;
}

.top-link {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--radius-control);
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}

.top-link svg { width: 20px; height: 20px; }
.top-link, .wordmark { white-space: nowrap; }

.wordmark {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-right: auto;
  font-size: 17px;
  font-weight: 800;
  color: var(--ink);
  text-decoration: none;
}

.wordmark img { border-radius: 9px; }

#nav {
  grid-area: nav;
  align-self: start;
  position: sticky;
  top: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 6px 22px 16px;
}

#nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--radius-control);
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}

#nav svg { width: 22px; height: 22px; flex: none; }

#nav a[aria-current] {
  background: var(--surface);
  box-shadow: var(--shadow-chip);
  color: var(--fern);
  font-weight: 800;
}

#screen { grid-area: screen; padding: 12px 32px 48px 16px; }
/* Loading shows as a bar on the top edge, so it moves nothing. It stays
   clear for a moment, so a fast answer shows no bar at all. */
#screen.busy::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  z-index: 20;
  height: 3px;
  background: var(--fern);
  opacity: 0;
  animation: loading .8s .2s ease-in-out infinite alternate;
}

@keyframes loading {
  from { opacity: .3; }
  to { opacity: 1; }
}

.stack { display: flex; flex-direction: column; gap: 12px; }
.stack > * { min-width: 0; }

.screen-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }

.parent { font-size: 13px; font-weight: 700; text-decoration: none; align-self: flex-start; }

/* ------------------------------------------------------------- content */

.card {
  background: var(--surface);
  border-radius: var(--radius-card);
  padding: 14px 16px;
  box-shadow: var(--shadow-raised);
}

article.card { display: flex; flex-direction: column; gap: 8px; }

.banner {
  background: var(--surface);
  border-radius: var(--radius-control);
  border-left: 4px solid var(--dim);
  padding: 10px 13px;
  box-shadow: var(--shadow-chip);
}

.banner.error { border-left-color: var(--garnet); }
.banner button { display: block; margin-top: 10px; }

.empty { color: var(--muted); padding: 22px 0; }
.meta { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.prose { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 76ch; }
.reasons { margin: 0; padding-left: 18px; font-size: 13.5px; }

.rows { display: flex; flex-direction: column; gap: 8px; }

.row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
  border-radius: var(--radius-control);
  padding: 11px 13px;
  box-shadow: var(--shadow-chip);
  color: var(--ink);
  text-decoration: none;
}

.row-title { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 16px; font-weight: 800; }

.clamp {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
/* The attention tiles: rows of four, or of two on a phone. */
.tiles.attention { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.tile { position: relative; display: flex; flex-direction: column; color: var(--ink); text-decoration: none; }
.tile > .tag { position: absolute; top: 12px; right: 12px; }
.value { font-size: 28px; font-weight: 800; line-height: 1.2; font-variant-numeric: tabular-nums; white-space: nowrap; }
.label { font-size: 13px; color: var(--muted); }
.tile.quiet .value { color: var(--dim); }
.tile.watch .value { color: var(--amber); }
.tile.problem .value { color: var(--garnet); }

.facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 22px; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }

.photo {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: var(--radius-control);
  background: var(--track);
  color: var(--muted);
  font-size: 13px;
}

.photo img { width: 100%; height: 100%; object-fit: cover; }
.thumb { width: 140px; }

.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-raised);
}

table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 12px 14px 8px; white-space: nowrap; }
td { padding: 10px 14px; border-top: 1px solid var(--track); vertical-align: top; white-space: nowrap; }
.long { white-space: normal; min-width: 200px; }
.number { text-align: right; font-variant-numeric: tabular-nums; }

details > summary { cursor: pointer; color: var(--fern); font-weight: 600; min-height: 44px; align-content: center; }

/* A table's rows as cards, on a phone. A tap opens the rest of the row. */
.cards { display: none; flex-direction: column; gap: 8px; }

.row-card {
  background: var(--surface);
  border-radius: var(--radius-control);
  padding: 4px 13px;
  box-shadow: var(--shadow-chip);
}

.row-card > .face {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 8px 22px 8px 0;
  position: relative;
  list-style: none;
  color: var(--ink);
  font-weight: 400;
}

.row-card > .face::-webkit-details-marker { display: none; }

.row-card > .face::after {
  content: "›";
  position: absolute;
  right: 2px;
  top: 8px;
  font-size: 20px;
  line-height: 1;
  color: var(--dim);
}

.row-card[open] > .face::after { rotate: 90deg; }
.row-card[role=button], tr[role=button] { cursor: pointer; }
.row-card .facts { grid-template-columns: 9.5em minmax(0, 1fr); }
.row-card > .facts { padding: 8px 0 10px; border-top: 1px solid var(--track); }
.row-card .facts:empty { display: none; }
pre { margin: 0; padding: 12px; overflow-x: auto; background: var(--tint); border-radius: var(--radius-control); }

/* --------------------------------------------------------------- search */

.search { display: flex; gap: 8px; }
.search input { flex: 1; min-width: 0; }

/* ----------------------------------------------------------- tree graph */

.graph { position: relative; }
.graph > svg { position: absolute; top: 0; left: 0; overflow: visible; pointer-events: none; }
.graph path { fill: none; stroke: var(--level); stroke-width: 2; }
.graph circle { fill: var(--level); stroke: var(--ground); stroke-width: 2; }
.graph svg.dimmed path:not(.on), .graph svg.dimmed circle:not(.on) { opacity: .25; }
.graph path.on { stroke-width: 4; }

.level-0 { --level: var(--level-0); }
.level-1 { --level: var(--level-1); }
.level-2 { --level: var(--level-2); }
.level-3 { --level: var(--level-3); }

.legend { display: flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 50%; background: var(--level); }

.steps { display: flex; flex-direction: column; gap: 6px; }

.step {
  background: var(--surface);
  border-radius: var(--radius-control);
  box-shadow: var(--shadow-chip);
  padding: 0 13px;
}

.step > summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  min-height: 44px;
  padding: 6px 0;
  list-style: none;
  color: var(--ink);
}

.step > summary::-webkit-details-marker { display: none; }
.step .row-title { font-size: 15px; }
.step > summary .meta { margin-left: auto; white-space: nowrap; }
.step > .stack { padding: 6px 0 13px; border-top: 1px solid var(--track); }
.step h3 { font-size: 13px; color: var(--muted); font-weight: 600; }
.step ol, .step ul { margin: 0; padding-left: 20px; }

/* -------------------------------------------------------------- charts */

.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.chart header { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.readout { font-variant-numeric: tabular-nums; }
.scale, .axis { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.axis { display: flex; justify-content: space-between; margin-top: 4px; }

.plot {
  display: flex;
  justify-content: space-around;
  gap: 2px;
  height: 132px;
  border-top: 1px solid var(--track);
  border-bottom: 1px solid var(--dim);
}

.plot.dense { gap: 1px; }
.bar { flex: 1 1 0; min-width: 0; max-width: 24px; display: flex; align-items: flex-end; }
.bar i { display: block; width: 100%; background: var(--fern); border-radius: 4px 4px 0 0; }
.dense .bar i { border-radius: 2px 2px 0 0; }
@media (hover: hover) {
  .bar:hover { background: var(--track); }
}

.line-plot {
  position: relative;
  height: 160px;
  border-top: 1px solid var(--track);
  border-bottom: 1px solid var(--dim);
  touch-action: pan-y;
}

.line-plot svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.line { fill: none; stroke: var(--fern); stroke-width: 2; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.area { fill: var(--fern); opacity: .1; }
.cursor { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--dim); }

.dot {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  translate: -50% -50%;
  background: var(--fern);
  box-shadow: 0 0 0 2px var(--surface);
}

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

dialog {
  border: 0;
  border-radius: var(--radius-card);
  padding: 16px;
  width: min(400px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-raised);
}

dialog::backdrop { background: rgba(20, 26, 22, .45); }
dialog form { display: flex; flex-direction: column; gap: 8px; }
dialog button { width: 100%; }
dialog h2 { margin: 0; }
dialog p { margin-bottom: 6px; }

/* A popover: the browser shows it above an open dialog. */
#toast {
  inset: auto auto 22px 50%;
  translate: -50% 0;
  margin: 0;
  border: 0;
  border-left: 4px solid var(--dim);
  padding: 10px 13px;
  overflow: visible;
  color: var(--ink);
  width: max-content;
  max-width: calc(100vw - 32px);
  box-shadow: var(--shadow-raised);
}

/* --------------------------------------------------------------- phone */

@media (max-width: 760px) {
  #shell { grid-template: "top" auto "screen" 1fr / minmax(0, 1fr); }
  #top { padding: calc(8px + env(safe-area-inset-top)) 8px 0 16px; }
  #screen { padding: 8px 16px calc(104px + env(safe-area-inset-bottom)); }
  h1 { font-size: 24px; }
  .charts { grid-template-columns: minmax(0, 1fr); }
  .tiles.attention { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .table-wrap { display: none; }
  .cards { display: flex; }

  /* The app's floating tab bar. */
  #nav {
    position: fixed;
    top: auto;
    left: 10px;
    right: 10px;
    bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 5;
    flex-direction: row;
    gap: 0;
    padding: 6px 4px;
    background: var(--surface);
    border-radius: 22px;
    box-shadow: var(--shadow-raised);
  }

  /* Eight tabs: the bar scrolls sideways on a narrow phone. */
  #nav { overflow-x: auto; scrollbar-width: none; }

  #nav a {
    flex: 1 0 52px;
    flex-direction: column;
    justify-content: center;
    gap: 2px;
    padding: 4px 0;
    font-size: 10px;
  }

  #nav a[aria-current] { background: none; box-shadow: none; }
  #toast { bottom: calc(96px + env(safe-area-inset-bottom)); }
  dialog { margin-bottom: 16px; }
}
