/* doom-one palette */
:root {
  --bg: #282c34;
  --bg-alt: #21242b;
  --base: #2f343f;
  --base-hi: #3a3f4b;
  --fg: #bbc2cf;
  --fg-dim: #5b6268;
  --blue: #51afef;
  --magenta: #c678dd;
  --green: #98be65;
  --orange: #da8548;
  --red: #ff6c6b;
  --yellow: #ecbe7b;
  --cyan: #46d9ff;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--mono);
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- stack ---------- */
#stack-wrap { position: relative; flex: 1 1 auto; min-height: 0; display: flex; }
#stack {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 12px 14px 6px;
  font-size: clamp(18px, 5.4vw, 26px);
}
#stack > :first-child { margin-top: auto; }

.row {
  display: flex;
  align-items: center;
  gap: 0.6em;
  padding: 3px 4px;
  border-radius: 6px;
  touch-action: pan-y;
  transition: background 0.15s;
}
.row:active { background: var(--base); }
.row.doomed { background: color-mix(in srgb, var(--red) 25%, transparent); }
.row.empty { color: var(--fg-dim); }
.lvl { color: var(--fg-dim); flex: none; }
.val { margin-left: auto; text-align: right; overflow-wrap: anywhere; }
.row:last-child .val { color: var(--fg); font-weight: 600; }

/* Big language (d B): typeset formulas */
.row.big { padding: 6px 4px; }
.row.big .val { font-family: "STIX Two Math", "Cambria Math", "Times New Roman", serif; font-size: 1.15em; font-weight: 400; line-height: 1.2; }
.row.big i { font-style: italic; }
.f-frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; margin: 0 0.15em; }
.f-frac > span { padding: 0 0.15em; }
.f-frac > span:first-child { border-bottom: 1px solid currentColor; }
.f-frac .f-frac { font-size: 0.9em; }
.f-sqrt { display: inline-flex; align-items: stretch; }
.f-rad { font-size: 1.1em; }
.f-under { border-top: 1px solid currentColor; padding: 0 0.1em; margin-top: 0.12em; }
.f-idx { font-size: 0.6em; margin-right: -0.3em; }
.f-fn { font-style: normal; }
.row.big sup { font-size: 0.65em; }

/* trail (t d) */
#trail {
  position: absolute; inset: 0;
  background: color-mix(in srgb, var(--bg-alt) 96%, transparent);
  overflow-y: auto;
  padding: 10px 14px;
  font-size: 15px;
  border-bottom: 1px solid var(--base-hi);
}
#trail[hidden] { display: none; }
.thead { color: var(--blue); font-weight: 700; margin-bottom: 6px; }
.thead span { color: var(--fg-dim); font-weight: 400; font-size: 12px; }
.trow { display: flex; gap: 1em; padding: 2px 0; }
.ttag { color: var(--magenta); width: 6em; text-align: right; flex: none; }
.tval { overflow-wrap: anywhere; }

/* ---------- mode line & echo area ---------- */
#modeline {
  display: flex;
  gap: 0.9em;
  align-items: center;
  padding: 5px 12px;
  background: var(--bg-alt);
  border-top: 1px solid var(--base-hi);
  font-size: 13px;
  color: var(--fg-dim);
  white-space: nowrap;
  overflow: hidden;
  cursor: pointer;
}
#modeline .buf { color: var(--blue); font-weight: 700; }
#flags { color: var(--orange); font-weight: 700; }
#arg { color: var(--yellow); }
#side { color: var(--magenta); margin-left: auto; }

#echo {
  min-height: 2.1em;
  max-height: 32dvh;
  overflow-y: auto;
  padding: 6px 12px;
  font-size: 16px;
  background: var(--bg-alt);
  overflow-wrap: anywhere;
}
#echo.error { color: var(--red); }
#echo .prompt { color: var(--magenta); }
#echo .hint { color: var(--fg-dim); font-size: 13px; margin-top: 4px; }
#echo .cursor {
  display: inline-block; width: 0.55em; height: 1.1em; margin-left: 1px;
  vertical-align: text-bottom; background: var(--blue);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
#echo.whichkey { font-size: 14px; line-height: 1.7; }
.wk { display: inline-block; margin-right: 1.1em; white-space: nowrap; }
.wk b { color: var(--yellow); }
.wk.dim { color: var(--fg-dim); }

/* ---------- pad: keypad and keyboard share this area ---------- */
#pad {
  flex: none;
  position: relative;
  height: min(58dvh, 470px);
  overflow: hidden;
  background: var(--bg-alt);
  touch-action: none;   /* sideways swipes go to the app, taps still click */
}
#keypad, #kbd {
  position: absolute; inset: 0;
  display: grid;
  gap: 6px;
  padding: 8px 8px 10px;
  transition: transform 0.22s ease, opacity 0.22s ease;
}
#keypad { grid-template-columns: repeat(5, minmax(0, 1fr)); }
#kbd {
  grid-template-columns: repeat(20, minmax(0, 1fr));
  grid-template-rows: repeat(6, 1fr);
  gap: 5px 4px;
  padding: 6px 4px 8px;
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}
#pad.kbd-on #kbd { transform: translateX(0); opacity: 1; pointer-events: auto; }
#pad.kbd-on #keypad { transform: translateX(35%); opacity: 0; pointer-events: none; }

.key {
  font-family: var(--mono);
  font-size: clamp(15px, 4.6vw, 21px);
  border: none;
  border-radius: 10px;
  background: var(--base);
  color: var(--fg);
  touch-action: manipulation;
  cursor: pointer;
  padding: 0 2px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
  transition: filter 0.06s, transform 0.06s;
}
.key:active { filter: brightness(1.4); transform: scale(0.96); }
.key.num    { background: var(--base-hi); font-size: clamp(19px, 5.8vw, 26px); }
.key.op     { color: var(--blue); font-size: clamp(14px, 4.2vw, 19px); }
.key.fn     { color: var(--fg); font-size: clamp(13px, 3.8vw, 17px); }
.key.prefix { color: var(--magenta); }
.key.flag   { color: var(--orange); font-weight: 700; }
.key.flag.on { background: var(--orange); color: var(--bg); }
.key.var    { color: var(--green); font-style: italic; font-size: clamp(19px, 5.8vw, 26px); }
.key.enter  { background: var(--blue); color: var(--bg); font-weight: 700; letter-spacing: 0.08em; }
.key.danger { color: var(--red); font-size: clamp(13px, 3.8vw, 17px); }
.key.hint   {
  background: none; box-shadow: none; color: var(--fg-dim); cursor: default;
  font-size: 12px; display: flex; align-items: center; justify-content: center; text-align: center;
}
#keypad:not([data-layer="main"]) { border-top: 2px solid var(--magenta); }

/* full keyboard keys */
.kb {
  font-family: var(--mono);
  border: none;
  border-radius: 7px;
  background: var(--base-hi);
  color: var(--fg);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
  touch-action: manipulation;
  padding: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 0; overflow: hidden;
  transition: filter 0.05s;
}
.kb:active { filter: brightness(1.5); }
.kb .kl { font-size: clamp(15px, 4.6vw, 20px); line-height: 1.1; }
.kb .kc { font-size: clamp(7.5px, 2.15vw, 10px); line-height: 1.1; margin-top: 1px; color: var(--blue);
          max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: clip; }
.kb.is-prefix .kc { color: var(--magenta); }
.kb.mod { background: var(--base); font-size: 13px; }
.kb.mod .kl { font-size: clamp(12px, 3.6vw, 15px); }
.kb.on { background: var(--orange); color: var(--bg); }
.kb.on.lock { background: var(--yellow); }
.kb.ret { background: var(--blue); color: var(--bg); font-weight: 700; }
.kb.ret .kc { color: var(--bg); }

/* plot (g f) */
#plot { position: absolute; inset: 0; background: var(--bg-alt); display: flex; flex-direction: column; z-index: 2; }
#plot[hidden] { display: none; }
.plot-head { height: 34px; flex: none; display: flex; align-items: center; gap: 6px; padding: 0 6px 0 12px;
             border-bottom: 1px solid var(--base-hi); }
.plot-title { flex: 1; color: var(--fg); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.plot-btn { font: inherit; font-size: 16px; background: var(--base); color: var(--fg); border: none; border-radius: 6px;
            width: 32px; height: 26px; }
#plot canvas { display: block; flex: 1; }
