/*
 * Token Tracker.
 *
 * The instrument, not the dashboard. This is a meter you glance at on a phone
 * to answer one question -- am I burning more than I meant to today -- so the
 * numbers are set like a gauge readout and everything else stays quiet.
 *
 * Two decisions worth naming:
 *
 *   Numerals are monospaced with tabular figures throughout. A cost that
 *   reflows its own digits as it updates is unreadable at a glance, and
 *   alignment down a column is the entire point of a readout.
 *
 *   Stale data is drawn as visibly stale -- hatched and desaturated -- rather
 *   than annotated with a small grey timestamp. A tracker whose collection has
 *   quietly stopped looks exactly like a tracker reporting no usage, and the
 *   second is reassuring, which is the problem.
 */

:root {
  --ground:     #EEF1F4;
  --surface:    #FFFFFF;
  --ink:        #16202B;
  --ink-soft:   #5A6B79;
  --rule:       #C9D3DC;
  --signal:     #0F6E6E;
  --signal-dim: #0F6E6E26;
  --warn:       #B4531B;
  --warn-dim:   #B4531B1F;

  --mono: ui-monospace, "SF Mono", SFMono-Regular, "JetBrains Mono", Menlo,
          Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto,
          Helvetica, Arial, sans-serif;

  --step: 4px;
  --radius: 3px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ground:     #0E1519;
    --surface:    #16202B;
    --ink:        #E4EBF0;
    --ink-soft:   #8397A5;
    --rule:       #2A3A46;
    --signal:     #3FB6A8;
    --signal-dim: #3FB6A82E;
    --warn:       #E08445;
    --warn-dim:   #E084452B;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}

.frame {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--step) * 5) calc(var(--step) * 4)
           calc(var(--step) * 12);
  padding-top: max(calc(var(--step) * 5), env(safe-area-inset-top));
  padding-bottom: max(calc(var(--step) * 12), env(safe-area-inset-bottom));
}

/* ------------------------------------------------------------- masthead */

.masthead {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: calc(var(--step) * 2);
  padding-bottom: calc(var(--step) * 3);
  border-bottom: 1px solid var(--rule);
  margin-bottom: calc(var(--step) * 6);
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: calc(var(--step) * 2);
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--ink);
  text-decoration: none;
}

/* A filled cell in an empty track: the mark is the meter itself, at 8px. */
.wordmark__glyph {
  width: 22px;
  height: 8px;
  border: 1px solid var(--ink);
  background: linear-gradient(to right,
              var(--signal) 0 40%, transparent 40% 100%);
}

.nav {
  display: flex;
  gap: calc(var(--step) * 4);
}

.nav__link {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  text-decoration: none;
  padding-bottom: 2px;
  border-bottom: 1px solid transparent;
}

.nav__link:hover { color: var(--ink); }
.nav__link.is-current {
  color: var(--ink);
  border-bottom-color: var(--signal);
}

/* ---------------------------------------------------------------- labels */

.label {
  display: block;
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin-bottom: calc(var(--step) * 2);
}

/* ------------------------------------------------------------- the meter */

.readout { margin-bottom: calc(var(--step) * 8); }

.readout__figure {
  display: flex;
  align-items: baseline;
  gap: calc(var(--step) * 3);
  flex-wrap: wrap;
}

.readout__value {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 52px;
  line-height: 1;
  font-weight: 600;
  letter-spacing: -0.03em;
  margin: 0;
}

.readout__value.is-over { color: var(--warn); }

.readout__aside {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--ink-soft);
}

/*
 * The allowance meter. The ticks are a real scale rather than decoration --
 * ten divisions of the daily limit -- and the inscribed marker sits at the
 * trailing seven-day average, so "am I over?" and "is this normal for me?"
 * are answered by the same glance at the same object.
 */
.meter {
  position: relative;
  margin-top: calc(var(--step) * 5);
}

.meter__track {
  position: relative;
  height: 26px;
  border: 1px solid var(--rule);
  background: var(--surface);
  overflow: hidden;
}

.meter__ticks {
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    to right,
    var(--rule) 0 1px,
    transparent 1px 10%
  );
  opacity: 0.85;
  pointer-events: none;
}

.meter__fill {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 0;
  background: var(--signal-dim);
  border-right: 2px solid var(--signal);
  transition: width 420ms cubic-bezier(0.22, 0.61, 0.36, 1);
}

.meter__fill.is-over {
  background: var(--warn-dim);
  border-right-color: var(--warn);
}

/* The average marker: a full-height hairline with a foot, like a gauge
   inscription. Drawn above the fill so it stays legible when passed. */
.meter__avg {
  position: absolute;
  top: -5px;
  bottom: -5px;
  width: 1px;
  background: var(--ink);
  opacity: 0.55;
  pointer-events: none;
}

.meter__scale {
  display: flex;
  justify-content: space-between;
  margin-top: calc(var(--step) * 2);
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

/* ----------------------------------------------------------------- cells */

.cells {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1px;
  background: var(--rule);
  border: 1px solid var(--rule);
  margin-bottom: calc(var(--step) * 8);
}

@media (min-width: 520px) {
  .cells { grid-template-columns: repeat(4, 1fr); }
}

.cell {
  background: var(--surface);
  padding: calc(var(--step) * 4) calc(var(--step) * 3);
}

.cell__value {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.cell__note {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--ink-soft);
  margin-top: 2px;
}

/* ------------------------------------------------------------- histogram */

.section { margin-bottom: calc(var(--step) * 8); }

.bars {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 84px;
  padding-top: calc(var(--step) * 2);
  border-bottom: 1px solid var(--rule);
}

.bars__col {
  flex: 1;
  min-height: 1px;
  background: var(--signal);
  opacity: 0.75;
  border-radius: 1px 1px 0 0;
  position: relative;
}

.bars__col.is-today { opacity: 1; }
.bars__col.is-empty { background: var(--rule); opacity: 0.6; }

.bars__axis {
  display: flex;
  justify-content: space-between;
  margin-top: calc(var(--step) * 2);
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

/* ------------------------------------------------------------------ list */

.rows {
  border-top: 1px solid var(--rule);
}

.row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: calc(var(--step) * 3);
  padding: calc(var(--step) * 3) 0;
  border-bottom: 1px solid var(--rule);
}

.row__name {
  font-family: var(--mono);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row__value {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--ink-soft);
  flex: none;
}

.empty {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--ink-soft);
  padding: calc(var(--step) * 4) 0;
  border-bottom: 1px solid var(--rule);
}

/* ---------------------------------------------------------- status banner */

.banner {
  display: block;
  padding: calc(var(--step) * 3) calc(var(--step) * 4);
  margin-bottom: calc(var(--step) * 6);
  border: 1px solid var(--warn);
  border-left-width: 3px;
  background: var(--warn-dim);
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--ink);
}

.banner--ok {
  border-color: var(--signal);
  background: var(--signal-dim);
}

.banner a { color: inherit; }

/*
 * The stale treatment. Hatching the figures makes an out-of-date reading
 * unmistakable at arm's length, which a timestamp is not.
 */
.is-stale .readout__value,
.is-stale .cell__value {
  position: relative;
  opacity: 0.45;
}

.is-stale .readout__value::after,
.is-stale .cell__value::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    -45deg,
    var(--ink) 0 1px,
    transparent 1px 6px
  );
  opacity: 0.35;
  pointer-events: none;
}

/* ----------------------------------------------------------------- forms */

.field {
  margin-bottom: calc(var(--step) * 5);
}

.field__hint {
  font-family: var(--mono);
  font-size: 10.5px;
  line-height: 1.6;
  color: var(--ink-soft);
  margin-top: calc(var(--step) * 1.5);
}

input[type="text"],
input[type="number"],
input[type="password"],
select {
  width: 100%;
  padding: calc(var(--step) * 3);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 16px; /* 16px or iOS zooms the whole page on focus */
}

input:focus-visible,
select:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--signal);
  outline-offset: 2px;
}

.row-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: calc(var(--step) * 3);
}

.check {
  display: flex;
  align-items: center;
  gap: calc(var(--step) * 2);
  font-family: var(--mono);
  font-size: 12px;
}

.check input { width: auto; }

.button {
  display: inline-block;
  padding: calc(var(--step) * 3) calc(var(--step) * 5);
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  background: var(--ink);
  color: var(--ground);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.06em;
  cursor: pointer;
  text-decoration: none;
}

.button:hover { opacity: 0.88; }

.button--quiet {
  background: transparent;
  color: var(--ink);
  border-color: var(--rule);
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: calc(var(--step) * 2);
  margin-top: calc(var(--step) * 2);
}

.note {
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.6;
  color: var(--ink-soft);
  margin-top: calc(var(--step) * 3);
  min-height: 1.6em;
}

.note.is-good { color: var(--signal); }
.note.is-bad  { color: var(--warn); }

pre.snippet {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: calc(var(--step) * 3);
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.6;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.steps {
  padding-left: 1.2em;
  font-size: 13.5px;
  line-height: 1.75;
}

.steps li { margin-bottom: calc(var(--step) * 2); }

/* ----------------------------------------------------------------- login */

.gate {
  max-width: 320px;
  margin: 18vh auto 0;
}

.gate__title {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  margin: 0 0 calc(var(--step) * 6);
  display: flex;
  align-items: center;
  gap: calc(var(--step) * 2);
}

/* ------------------------------------------------------------- colophon */

.colophon {
  margin-top: calc(var(--step) * 10);
  padding-top: calc(var(--step) * 4);
  border-top: 1px solid var(--rule);
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.7;
  color: var(--ink-soft);
}

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

/* --------------------------------------------------- windows & projection */

.readout--secondary {
  margin-bottom: calc(var(--step) * 7);
  padding-top: calc(var(--step) * 5);
  border-top: 1px solid var(--rule);
}

.readout__value--small { font-size: 30px; }
.readout__value.is-warn { color: var(--warn); }

.meter__track--slim { height: 14px; }

.verdict {
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--ink-soft);
  margin: calc(var(--step) * 4) 0 0;
  min-height: 1.6em;
}

.verdict.is-bad  { color: var(--warn); }
.verdict.is-good { color: var(--signal); }

.meter__fill.is-warn {
  background: var(--warn-dim);
  border-right-color: var(--warn);
}

/* The projection marker is dashed where the average marker was solid: one is
   a measurement, the other is a guess, and they should not look alike. */
#session-projection {
  width: 0;
  border-left: 1px dashed var(--ink);
  background: none;
  opacity: 0.5;
}

/* ------------------------------------------------------------------ trace */

.trace {
  height: 96px;
  border-bottom: 1px solid var(--rule);
  background:
    repeating-linear-gradient(to bottom,
      var(--rule) 0 1px, transparent 1px 25%);
}

.trace__svg { width: 100%; height: 100%; display: block; }

.trace__line {
  fill: none;
  vector-effect: non-scaling-stroke;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.trace__line--session { stroke: var(--signal); stroke-width: 2; }
.trace__line--week    { stroke: var(--ink-soft); stroke-width: 1; stroke-dasharray: 3 3; }

.trace__key {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.swatch {
  display: inline-block;
  width: 10px;
  height: 2px;
  margin-left: 6px;
}

.swatch--session { background: var(--signal); }
.swatch--week    { background: var(--ink-soft); }
