/* SN Pro, self-hosted as variable-weight WOFF2s (200–900, normal + italic —
   same files and declarations as ann). swap: text renders in the system
   stack until the font arrives; the SW precaches both, so that's first
   visit only. */
@font-face {
  font-display: swap;
  font-family: "SN Pro";
  font-style: normal;
  font-weight: 200 900;
  src: url("./SNPro-VariableFont_wght.woff2") format("woff2");
}
@font-face {
  font-display: swap;
  font-family: "SN Pro";
  font-style: italic;
  font-weight: 200 900;
  src: url("./SNPro-Italic-VariableFont_wght.woff2") format("woff2");
}

:root {
  --lh: 1.8rem;
  --bg: #ffffff;
  --fg: #1b1b1f;
  --muted: #8a8a93;
  --accent: #3b6ef6;
  --header-bg: #f5f5f7;
  --result-number: #3b6ef6;
  --result-money: #1a9950;
  --result-unit: #b07d00;
  --result-date: #0e8a9e;
  --result-percent: #a44bc7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1b1f;
    --fg: #e6e6ea;
    --muted: #77777f;
    --header-bg: #232329;
    --result-number: #7ea2ff;
    --result-money: #5fd28f;
    --result-unit: #e0b34d;
    --result-date: #59c2d4;
    --result-percent: #cf8de8;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  /* dvh tracks the collapsing mobile URL bar; 100% (above) is the fallback.
     main.ts pins this further to visualViewport.height while the software
     keyboard is up. */
  height: 100dvh;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--fg);
  font-family: "SN Pro", system-ui, -apple-system, "Segoe UI", Roboto,
    "Noto Sans", sans-serif;
}

header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  /* Safe areas: the app runs edge-to-edge (viewport-fit=cover) with a
     translucent status bar on installed iOS. */
  padding-top: max(0.4rem, env(safe-area-inset-top, 0px));
  padding-left: calc(0.75rem + env(safe-area-inset-left, 0px));
  padding-right: calc(0.75rem + env(safe-area-inset-right, 0px));
  background: var(--header-bg);
  font-size: 0.85rem;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.05em;
}

#title {
  flex: 0 1 14rem;
  background: transparent;
  border: none;
  border-bottom: 1px solid transparent;
  color: var(--fg);
  font: inherit;
  padding: 0.15rem 0.25rem;
}

#title:focus {
  outline: none;
  border-bottom-color: var(--accent);
}

select {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--muted);
  border-radius: 4px;
  font: inherit;
  padding: 0.1rem 0.25rem;
}

#docs {
  max-width: 12rem;
}

button {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--muted);
  border-radius: 4px;
  font: inherit;
  cursor: pointer;
  padding: 0.1rem 0.5rem;
}

button:hover {
  border-color: var(--accent);
}

#status {
  margin-left: auto;
  color: var(--muted);
  font-size: 0.75rem;
}

/* The menu: a plain <details> wearing a button face; its panel is the home
   for settings, export/import, and the about lines. */
#menu {
  position: relative;
}

#menu > summary {
  list-style: none;
  cursor: pointer;
  border: 1px solid var(--muted);
  border-radius: 4px;
  padding: 0.1rem 0.5rem;
}

#menu > summary::-webkit-details-marker {
  display: none;
}

#menu > summary:hover,
#menu[open] > summary {
  border-color: var(--accent);
}

#menu .panel {
  position: absolute;
  right: 0;
  top: calc(100% + 0.4rem);
  z-index: 10;
  min-width: 16rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
  background: var(--bg);
  border: 1px solid rgba(128, 128, 140, 0.35);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

#menu .panel section {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#menu .panel h3 {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

#menu .panel label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

#menu .panel button {
  text-align: left;
  padding: 0.3rem 0.5rem;
}

#menu .panel .shortcuts {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.8;
}

#menu .panel kbd {
  font-family: inherit;
  border: 1px solid var(--muted);
  border-radius: 3px;
  padding: 0 0.3em;
}

#menu .panel footer {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  border-top: 1px solid rgba(128, 128, 140, 0.25);
  padding-top: 0.75rem;
  color: var(--muted);
  font-size: 0.75rem;
}

#menu .panel footer a {
  color: inherit;
}

#menu .panel footer a:hover {
  color: var(--accent);
}

/* The one scroll container: both columns move together. */
#scroll {
  flex: 1;
  overflow: auto;
}

#sheet {
  display: flex;
  align-items: flex-start;
  position: relative;
  min-height: 100%;
}

#editor,
#results .row {
  font: inherit;
  font-size: 1.125rem;
  line-height: var(--lh);
}

#editor {
  flex: 1;
  min-width: 0;
  outline: none;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  padding: 0.75rem 0.25rem 0.75rem 1rem;
  color: var(--fg);
  caret-color: var(--accent);
}

/* One div per logical line; results rows mirror these heights. Layout
 * containment makes each line an independent layout boundary, so editing
 * one line relayouts one line — without it, the caret restore after every
 * keystroke flushes layout for the whole document (O(document) per key,
 * the dominant cost at a few hundred lines). Height still follows content:
 * containment fixes nothing unless `size` is contained. */
#editor .ln {
  min-height: var(--lh);
  contain: layout paint;
  /* Off-screen lines drop out of layout and paint entirely, so the layout
   * flush a keystroke's caret restore forces scales with the viewport, not
   * the document. The intrinsic size keeps scroll geometry stable while
   * they're skipped. */
  content-visibility: auto;
  contain-intrinsic-block-size: auto var(--lh);
}

#results .row {
  contain: layout paint;
  content-visibility: auto;
  contain-intrinsic-block-size: auto var(--lh);
}

/* WebKit's content-visibility relevancy tracking loses a line the moment
   the caret leaves it (iOS: press Enter → the line you just typed blanks
   until the caret returns — the known caret-in-skipped-content bug class).
   Trade the viewport-scaled layout optimization for correctness on WebKit
   only; `contain: layout paint` (the bigger win) stays on. The feature
   query hits every WebKit port and nothing else — verified true in
   Playwright WebKit (so the e2e matrix runs this configuration) and false
   in Chromium/Firefox. */
@supports (background: -webkit-named-image(apple-pay-logo-black)) {
  #editor .ln,
  #results .row {
    content-visibility: visible;
  }
}

/* Token colors, from the core recognizer's span kinds. */
#editor .tk-number { color: var(--result-number); }
#editor .tk-money { color: var(--result-money); }
#editor .tk-unit { color: var(--result-unit); }
#editor .tk-date { color: var(--result-date); }
#editor .tk-percent { color: var(--result-percent); }
#editor .tk-keyword,
#editor .tk-operator { color: var(--muted); }
#editor .tk-variable { font-weight: 600; }
#editor .tk-ref {
  color: var(--accent);
  background: rgba(128, 128, 140, 0.12);
  border-radius: 0.3em;
}

/* A collapsed line reference: shows the referenced line's value; expands to
   raw "line N" text whenever the caret touches it. */
#editor .pill {
  background: rgba(128, 128, 140, 0.16);
  border-radius: 0.4em;
  padding: 0 0.35em;
  margin: 0 1px;
  white-space: nowrap;
  cursor: default;
  color: var(--result-number);
}

#editor .pill.kind-money { color: var(--result-money); }
#editor .pill.kind-unit,
#editor .pill.kind-duration { color: var(--result-unit); }
#editor .pill.kind-date { color: var(--result-date); }
#editor .pill.kind-percent { color: var(--result-percent); }

/* Full-bleed column: stretches to the sheet's full height and shares the
   header's background so the column runs to the window edges. */
#results {
  flex: 0 0 auto;
  align-self: stretch;
  min-width: 11ch;
  max-width: 40vw;
  padding: 0.75rem 1rem 0 0.75rem;
  background: var(--header-bg);
  border-left: 1px solid rgba(128, 128, 140, 0.3);
  text-align: left;
  user-select: none;
}

#results .row {
  height: var(--lh);
  white-space: nowrap;
  cursor: pointer;
  color: var(--result-number);
  transition: opacity 0.15s;
}

#results .row:hover {
  opacity: 0.7;
}

#results .row.copied {
  opacity: 0.3;
}

#results .kind-money,
#results .kind-rate {
  color: var(--result-money);
}
#results .kind-unit,
#results .kind-duration {
  color: var(--result-unit);
}
#results .kind-date {
  color: var(--result-date);
}
#results .kind-percent {
  color: var(--result-percent);
}

/* Narrow screens: keep the two-column sheet (syncHeights already mirrors
   wrapped-line heights at any column width) but compact the results rail.
   Never touch the .ln/.row containment rules here — they are load-bearing
   for keystroke latency. */
@media (max-width: 600px) {
  #editor,
  #results .row {
    font-size: 1rem;
  }

  #editor {
    padding-left: 0.75rem;
  }

  #results {
    min-width: 7ch;
    max-width: 34vw;
    padding: 0.75rem 0.5rem 0;
    padding-right: max(0.5rem, env(safe-area-inset-right, 0px));
  }

  #results .row {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  header {
    flex-wrap: wrap;
  }

  #title {
    flex: 1 1 8rem;
  }

  #docs {
    max-width: 40vw;
  }
}

/* Touch: taller lines make the result rows (tap targets for inserting
   references) and the header controls comfortably hittable. */
@media (pointer: coarse) {
  :root {
    --lh: 2rem;
  }

  /* Pills are the tap target for "edit this reference" — give the finger
     something to hit (syncHeights mirrors any extra height into the rows). */
  #editor .pill {
    padding: 0.25em 0.5em;
  }

  header button,
  header select,
  #menu > summary {
    min-height: 2.25rem;
    min-width: 2.25rem;
  }

  #menu .panel button {
    min-height: 2.25rem;
  }
}
