:root {
  --bg: #000;
  --num: #333336;
  --num-down: #737376;
  --fn: #5c5c5f;
  --fn-down: #8f8f93;
  --op: #ff9f0a;
  --op-down: #fcc78e;
  --text: #fff;
  --dim: #8e8e93;
  --line: #38383a;
  --name: #64d2ff;
  --kh: clamp(40px, calc(var(--app-h, 100vh) * 0.058), 54px); /* key height */
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: var(--app-h, 100%); background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none; }

body {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }

#app {
  height: var(--app-h, 100%);
  max-width: 520px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}

/* ---------- top bar ---------- */
.topbar { flex: none; display: flex; align-items: center; gap: 10px; height: 56px; padding: 0 14px; }
.spacer { flex: 1; }
.tb-btn { position: relative; width: 42px; height: 42px; border-radius: 50%; background: #1c1c1e; display: grid; place-items: center; color: var(--text); }
.tb-btn svg { width: 21px; height: 21px; }
.tb-btn.off { color: #5c5c5f; }
.tb-text { width: auto; padding: 0 16px; border-radius: 21px; font-size: 15px; font-weight: 500; }
@media (max-width: 389px) {    /* narrow phones: the bar's buttons sit a little closer */
  .topbar { gap: 7px; padding: 0 10px; }
  .tb-text { padding: 0 12px; }
}
.tb-btn.accent { background: var(--op); }
/* ‹ › history: one pill, two halves */
.hist-nav { display: inline-flex; border-radius: 21px; background: #1c1c1e; }
.hist-nav .tb-btn { background: none; width: 44px; }
.hist-nav .tb-btn svg { width: 19px; height: 19px; }
.textmode .hist-nav { display: none; }
/* on an old page: when it was last edited, which one, and Delete */
.hist-bar { flex: none; display: flex; align-items: center; gap: 10px; margin: -2px 14px 4px; padding: 6px 12px; border-radius: 12px; background: rgba(100, 210, 255, 0.12); font-size: 13px; }
.hist-when { color: var(--name); font-weight: 600; white-space: nowrap; }
.hist-cloud { color: var(--dim); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.hist-delete { flex: none; color: #ff453a; font-weight: 600; padding: 2px 0 2px 8px; }
#keypadBtn { display: none; }
.textmode #keypadBtn { display: grid; }
.dot { position: absolute; top: 3px; right: 3px; width: 10px; height: 10px; border-radius: 50%; background: #ff453a; border: 2px solid #000; }

/* ---------- the page ---------- */
.page { --fs: 22px; position: relative; flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.note {
  min-height: 100%;
  padding: 4px 20px 28px;
  font-size: var(--fs); /* set by the app: large on a short page, down to 22px as it fills */
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  outline: none;
  caret-color: var(--op);
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
  touch-action: manipulation;
}
.ln { min-height: 1.5em; }
/* names stand out from numbers: blue and bolder, both where they're set and where they're used */
.v, .def { color: var(--name); font-weight: 600; }
.arrow { color: var(--dim); }
.divider { color: var(--dim); font-size: 0.68em; letter-spacing: 0.04em; }

/* answers sit in a column on the right, outside the editable text, so the cursor never lands beside one */
.answers { position: absolute; top: 0; left: 0; right: 0; height: 0; pointer-events: none; }
.ans {
  position: absolute;
  right: 14px;
  max-width: 55%;
  height: calc(var(--fs) * 1.5);
  padding: 0 8px;
  border-radius: 9px;
  background: rgba(255, 159, 10, 0.14);
  color: var(--op);
  font-size: var(--fs);
  line-height: calc(var(--fs) * 1.5);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: auto;
}
.ans.active { background: rgba(255, 159, 10, 0.32); }
.ans.dim { background: #1c1c1e; color: #636366; }
.ans.live { opacity: 0.7; } /* still being typed: 30% dimmer than a finished answer */

.vars { flex: none; display: flex; gap: 8px; overflow-x: auto; padding: 2px 14px 4px; scrollbar-width: none; touch-action: pan-x; }
.vars::-webkit-scrollbar { display: none; }
.textmode .vars { display: none; }
.chip { flex: none; display: inline-flex; align-items: baseline; gap: 6px; height: 34px; padding: 0 13px; border-radius: 17px; background: #1c1c1e; font-size: 16px; line-height: 34px; white-space: nowrap; }
.chip-name { color: var(--name); font-weight: 600; }
.chip-val { color: var(--dim); font-size: 14px; }
.var-chips { display: contents; }
.name-chip { color: var(--op); font-weight: 600; }

.fxbar { flex: none; align-self: flex-end; margin: 0 18px 2px; padding: 4px 0; font-size: 12px; color: var(--dim); }
.fxbar.stale { color: var(--op); }

/* ---------- keypad ---------- */
.keypad {
  flex: none;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: var(--kh);
  gap: 8px;
  padding: 6px 14px calc(env(safe-area-inset-bottom) + 8px);
  touch-action: none;
}
.textmode .keypad { display: none; }
.key {
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--num);
  font-size: calc(var(--kh) * 0.5);
  font-weight: 400;
  transition: background-color 0.35s ease-out;
}
.key.down { transition: none; background: var(--num-down); }
.key.fn { background: var(--fn); font-size: calc(var(--kh) * 0.4); font-weight: 500; }
.key.fn.down { background: var(--fn-down); }
.key.op { background: var(--op); font-size: calc(var(--kh) * 0.6); }
.key.op.down { background: var(--op-down); }
.key svg { height: 44%; width: auto; }
/* new line: a lighter grey than the other grey keys so it's easy to find */
.key.fn.enter { background: #9c9ca1; }
.key.fn.enter.down { background: #c4c4c9; }

/* ---------- backup sheet ---------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: flex-end;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s, visibility 0s linear 0.25s;
}
.sheet.open { opacity: 1; visibility: visible; transition: opacity 0.25s; }
.sheet-card {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 0 20px calc(env(safe-area-inset-bottom) + 20px);
  background: #1c1c1e;
  border-radius: 16px 16px 0 0;
  transform: translateY(40px);
  transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet.open .sheet-card { transform: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; height: 56px; }
.sheet-head h2 { font-size: 17px; font-weight: 600; }
.done { color: var(--op); font-size: 17px; font-weight: 600; padding: 8px 0 8px 16px; }
.backup { font-size: 15px; color: var(--text); margin-bottom: 6px; }
.backup.warn { color: var(--op); }
.hint { font-size: 13px; line-height: 1.4; color: var(--dim); margin-bottom: 16px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sheet-actions button { height: 48px; border-radius: 12px; background: #2c2c2e; color: var(--op); font-size: 17px; font-weight: 500; }
.diag { margin-top: 16px; font-size: 11px; color: #636366; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom) + 6 * var(--kh) + 112px); /* above the keypad and name chips, clear of the page text */
  left: 50%;
  z-index: 20;
  padding: 9px 16px;
  border-radius: 999px;
  background: rgba(58, 58, 60, 0.96);
  font-size: 15px;
  line-height: 1.35;
  width: max-content;
  max-width: calc(100vw - 40px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.tappable { pointer-events: auto; cursor: pointer; }
.toast-act { margin-left: 14px; color: var(--op); font-weight: 600; }
.textmode ~ .toast { bottom: auto; top: calc(env(safe-area-inset-top) + 62px); }

.help-btn { font-size: 21px; font-weight: 600; }

/* ---------- answer menu ---------- */
.menu {
  position: fixed;
  z-index: 15;
  display: flex;
  border-radius: 12px;
  overflow: hidden;
  background: #3a3a3c;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
}
.menu button { padding: 11px 17px; font-size: 16px; color: var(--text); }
.menu-full { align-self: center; padding: 0 12px 0 14px; font-size: 14px; color: var(--op); border-right: 0.5px solid #545458; }
.menu button + button { border-left: 0.5px solid #545458; }
.menu button:active { background: #4a4a4e; }

/* ---------- name box (sits at the top, clear of the iPhone keyboard) ---------- */
.namebox { position: fixed; inset: 0; z-index: 12; background: rgba(0, 0, 0, 0.55); }
.nb-card {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 64px);
  left: 14px;
  right: 14px;
  max-width: 492px;
  margin: 0 auto;
  padding: 16px;
  border-radius: 16px;
  background: #1c1c1e;
}
.nb-label { font-size: 13px; color: var(--dim); }
.nb-value { margin: 2px 0 12px; font-size: 30px; color: var(--op); overflow-wrap: anywhere; }
.nb-input {
  width: 100%;
  height: 46px;
  padding: 0 12px;
  border: 0;
  border-radius: 10px;
  outline: none;
  background: #2c2c2e;
  color: var(--name);
  font: 600 19px -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  caret-color: var(--op);
  -webkit-user-select: text;
  user-select: text;
}
.nb-input::placeholder { color: #636366; font-weight: 400; }
.nb-error { margin-top: 8px; font-size: 13px; color: #ff453a; }
.nb-actions { display: flex; align-items: center; gap: 6px; margin-top: 14px; }
.nb-actions .grow { flex: 1; }
.nb-actions button { height: 40px; padding: 0 14px; border-radius: 10px; font-size: 16px; }
.nb-remove { color: #ff453a; padding-left: 0 !important; }
.nb-cancel { color: var(--dim); }
.nb-save { background: var(--op); color: #000; font-weight: 600; }

/* ---------- help ---------- */
.help {
  position: fixed;
  inset: calc(env(safe-area-inset-top) + 8px) 0 0 0;
  z-index: 11;
  display: flex;
  flex-direction: column;
  border-radius: 16px 16px 0 0;
  background: #1c1c1e;
  transform: translateY(105%);
  visibility: hidden;
  transition: transform 0.32s cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s linear 0.32s;
}
.help.open { transform: none; visibility: visible; transition: transform 0.32s cubic-bezier(0.2, 0.8, 0.2, 1); }
.help-head { flex: none; display: flex; align-items: center; justify-content: space-between; height: 56px; padding: 0 20px; }
.help-head h2 { font-size: 17px; font-weight: 600; }
.help-body { flex: 1; overflow-y: auto; padding: 0 20px calc(env(safe-area-inset-bottom) + 24px); }
/* help cards: a tiny copy of the page with one caption */
.hc { margin: 14px 0 0; }
.hc-page { position: relative; padding: 10px 14px; border-radius: 12px; background: #000; font-size: 18px; line-height: 1.5; }
.hc-line { display: flex; align-items: center; gap: 10px; min-height: 27px; }
.hc-text { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hc-caret { display: inline-block; width: 2px; height: 1.05em; margin: 0 1px; vertical-align: -3px; background: var(--op); }
.hc-ans { flex: none; padding: 0 7px; border-radius: 8px; background: rgba(255, 159, 10, 0.14); color: var(--op); }
.hc-ans.live { opacity: 0.7; }
.hc-menu { display: inline-flex; margin: 6px 0 2px auto; float: right; border-radius: 10px; overflow: hidden; background: #3a3a3c; font-size: 14px; }
.hc-menu span { padding: 7px 13px; }
.hc-menu span + span { border-left: 0.5px solid #545458; }
.hc-page::after { content: ''; display: block; clear: both; }
.hc-chips { display: flex; gap: 6px; margin-top: 8px; font-size: 13px; }
.hc-chip { padding: 4px 10px; border-radius: 12px; background: #1c1c1e; }
.hc-bar { display: flex; align-items: center; gap: 8px; padding: 2px 0 4px; }
.hc-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 17px; background: #1c1c1e; font-size: 15px; font-weight: 600; }
.hc-btn-text { width: auto; padding: 0 12px; font-size: 13px; }
.hc-spacer { flex: 1; }
.hc-cap { padding: 8px 4px 0; font-size: 14px; line-height: 1.4; color: #d1d1d6; }
.help-about { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 6px 0 4px; }
.help-intro { color: var(--op); font-size: 15px; font-weight: 600; padding: 2px 10px; }
.help-chip { display: inline-flex; align-items: baseline; gap: 7px; padding: 7px 16px; border-radius: 999px; background: rgba(100, 210, 255, 0.14); font-size: 14px; }
.help-app { font-size: 17px; font-weight: 700; color: var(--text); }
.help-ver { color: var(--dim); }
.help-dot { color: var(--dim); }
.help-maker { color: var(--name); font-weight: 600; }

/* ---------- conversions sheet ---------- */
.conv-body { padding-top: 4px; }
.c-title { margin: 18px 0 6px; font-size: 13px; font-weight: 600; letter-spacing: 0.03em; text-transform: uppercase; color: var(--dim); }
.c-row { display: flex; align-items: center; gap: 6px; padding: 6px 0; }
.c-side { display: flex; align-items: center; gap: 5px; flex: 1; min-width: 0; }
.c-in {
  width: 100%;
  min-width: 0;
  height: 42px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  outline: none;
  background: #2c2c2e;
  color: var(--op);
  font: 500 19px -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  text-align: right;
  caret-color: var(--op);
  -webkit-user-select: text;
  user-select: text;
}
.c-in.narrow { width: 74px; flex: none; padding: 0 7px; font-size: 17px; }
.c-in:focus { box-shadow: 0 0 0 1.5px var(--op); }
.c-in::placeholder { color: #48484a; }
.c-in[type="time"] { text-align: left; color-scheme: dark; }
.c-unit { flex: none; min-width: 34px; font-size: 15px; color: #d1d1d6; }
.c-sel {
  flex: none;
  max-width: 96px;
  height: 42px;
  padding: 0 4px;
  border: 0;
  border-radius: 10px;
  background: #3a3a3c;
  color: var(--text);
  font: 500 15px -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
}
.c-arrow { flex: none; width: 22px; text-align: center; color: var(--dim); font-size: 17px; }
.c-add { flex: none; width: 34px; height: 34px; border-radius: 17px; background: #3a3a3c; color: var(--op); font-size: 22px; line-height: 34px; text-align: center; }
.c-note { font-size: 12px; color: var(--dim); padding: 0 0 4px; }
.c-note.stale { color: var(--op); }
.c-shift { flex: none; font-size: 13px; color: var(--op); width: 22px; }
.c-block { display: flex; flex-direction: column; gap: 8px; padding: 10px 10px 8px; margin: 4px 0; border-radius: 14px; background: #262628; }
.c-block .c-side { gap: 8px; }
.c-block .c-sel { max-width: none; min-width: 84px; }
.c-arrow-v { flex: none; width: 34px; text-align: center; color: var(--dim); font-size: 17px; }
.c-money { background: rgba(255, 159, 10, 0.12); box-shadow: inset 0 0 0 1px rgba(255, 159, 10, 0.35); }
.c-money .c-in { background: rgba(0, 0, 0, 0.35); }
.c-money .c-note { padding: 2px 4px 0; }

/* ---------- first-launch guide (intro.js) ---------- */
/* Each page: a short title over a phone (drawn here) showing real screenshots, its foot fading into the page.
   Everything on the phone is sized in cqw = hundredths of the phone's width, so it scales as one. */
.intro {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: none;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 6px) 0 calc(env(safe-area-inset-bottom) + 14px);
  background: linear-gradient(180deg, #402606 0%, #1d1203 28%, #000 60%) #000;   /* warm light from above */
}
.intro.open { display: flex; }
.intro-top { flex: none; display: flex; justify-content: flex-end; height: 36px; padding: 0 22px; }
.intro-skip { color: var(--dim); font-size: 17px; font-weight: 500; padding: 4px 0 4px 16px; }
.intro-pages { flex: 1; min-height: 0; display: flex; overflow-x: auto; overflow-y: hidden; scroll-snap-type: x mandatory; scrollbar-width: none; overscroll-behavior: contain; }
.intro-pages::-webkit-scrollbar { display: none; }
.intro-page { flex: none; width: 100%; min-height: 0; display: flex; flex-direction: column; align-items: center; scroll-snap-align: center; scroll-snap-stop: always; }
.intro-page h2 { flex: none; margin: 4px 20px 18px; font-size: clamp(25px, 7.6vw, 33px); font-weight: 800; letter-spacing: -0.025em; line-height: 1.1; text-align: center; }
.intro-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 520px;
  display: flex;
  justify-content: center;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(#000 calc(100% - 120px), transparent calc(100% - 6px));
  mask-image: linear-gradient(#000 calc(100% - 120px), transparent calc(100% - 6px));
}
/* as wide as fits; on a short screen, small enough that most of the phone still shows */
.intro-phone { position: relative; flex: none; align-self: flex-start; width: min(80%, 372px, calc((100vh - 90px) / 2.174)); margin-top: 10px; container-type: inline-size; }
.intro-body {
  position: relative;
  border: 2.6cqw solid #1a1a1c;
  border-radius: 16.5cqw;
  background: #000;
  box-shadow: 0 0 0 0.55cqw #55555a, 0 0 0 0.9cqw #101012;
  overflow: hidden;
}
.intro-screen { position: relative; border-radius: 13.9cqw; overflow: hidden; }
.intro-shot { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.5s; -webkit-user-drag: none; pointer-events: none; }
.intro-shot.was { opacity: 1; z-index: 1; transition: none; }
.intro-shot.on { opacity: 1; z-index: 2; }
/* a page whose phone must show whole (Safari's buttons are at its foot): sized to the room there is, no fade */
.intro-stage { container-type: size; }
.intro-stage.whole { -webkit-mask-image: none; mask-image: none; align-items: flex-start; }
.intro-stage.whole .intro-phone { width: min(80cqw, 372px, calc((100cqh - 18px) / 2.2)); }
.intro-icon { align-self: center; width: min(46cqw, 190px); border-radius: 22.5%; box-shadow: 0 0 0 1px #3a3a3c, 0 18px 60px rgba(255, 159, 10, 0.28); margin-bottom: 12cqh; }
.intro-island { position: absolute; z-index: 3; top: 2.9cqw; left: 50%; width: 29cqw; height: 8.6cqw; margin-left: -14.5cqw; border-radius: 4.3cqw; background: #0a0a0b; box-shadow: inset 0 0 0 0.3cqw #17171a; }
/* over the screen (not clipped by the phone, so a label may overhang its edge) */
.intro-over { position: absolute; top: 2.6cqw; left: 2.6cqw; right: 2.6cqw; z-index: 4; pointer-events: none; }
.intro-lines { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.intro-mark, .intro-ring { opacity: 0; transition: opacity 0.3s; }
.intro-mark.show, .intro-ring.show { opacity: 1; transition: opacity 0.4s 0.2s; }
.intro-mark line { stroke: #fff; stroke-width: 1.5px; vector-effect: non-scaling-stroke; }
.intro-mark circle { fill: #fff; }
.intro-pulse { transform-box: fill-box; transform-origin: center; animation: intro-pulse 1.6s ease-out infinite; }
@keyframes intro-pulse { from { transform: scale(1); opacity: 0.55; } to { transform: scale(3.4); opacity: 0; } }
.intro-ring { fill: none; stroke: #fff; stroke-width: 2px; vector-effect: non-scaling-stroke; filter: drop-shadow(0 0 1.2cqw rgba(255, 255, 255, 0.75)); }
.intro-ring.show { animation: intro-ring 1.6s ease-in-out infinite; }
/* a "tap" on the ringed button: late in the picture's time, just before the next picture shows what the tap did */
.intro-press { fill: #fff; opacity: 0; transform-box: fill-box; transform-origin: center; }
.intro-press.go { animation: intro-press 0.6s ease-out 0.8s; }
@keyframes intro-press { 0% { opacity: 0.75; transform: scale(0.5); } 60% { opacity: 0.45; } 100% { opacity: 0; transform: scale(1.12); } }
@keyframes intro-ring { 0%, 100% { stroke-opacity: 1; } 50% { stroke-opacity: 0.45; } }
.intro-chip {
  position: absolute;
  padding: 0.42em 0.85em;
  border-radius: 999px;
  background: #fff;
  color: #000;
  font-size: 4.6cqw;
  font-weight: 650;
  letter-spacing: -0.01em;
  line-height: 1.15;
  white-space: nowrap;
  box-shadow: 0 0.6cqw 2.6cqw rgba(0, 0, 0, 0.55);
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.86);
  transition: opacity 0.25s, transform 0.25s;
}
.intro-chip.show { opacity: 1; transform: translate(-50%, -50%); transition: opacity 0.35s 0.2s, transform 0.45s 0.2s cubic-bezier(0.2, 0.9, 0.3, 1.25); }
.intro-foot { flex: none; display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 4px 22px 0; }
.intro-dots { display: flex; gap: 8px; }
.intro-dots span { width: 7px; height: 7px; border-radius: 50%; background: #48484a; transition: background 0.2s, width 0.2s; }
.intro-dots span.on { width: 20px; border-radius: 4px; background: #fff; }
.intro-next { width: 100%; max-width: 440px; height: 52px; border-radius: 26px; background: var(--op); color: #fff; font-size: 19px; font-weight: 600; }
