/* ---------- tokens ---------- */
:root {
  --bg: #fbfaf7;
  --fg: #1d1d1b;
  --muted: #8a877f;
  --accent: #2f6fde;
  --accent-soft: #2f6fde1a;
  --slot: #a9b8d4;
  --kb-bg: #e4e2dc;
  --key: #ffffff;
  --key-fg: #1d1d1b;
  --key-alt: #cfccc4;
  --key-down: #bdbab2;
  --shadow: rgba(0, 0, 0, .18);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --fg: #ecebe7;
    --muted: #8d8a83;
    --accent: #6a9bff;
    --accent-soft: #6a9bff22;
    --slot: #4b5a78;
    --kb-bg: #232321;
    --key: #3a3a37;
    --key-fg: #ecebe7;
    --key-alt: #2c2c2a;
    --key-down: #51514d;
    --shadow: rgba(0, 0, 0, .5);
  }
}

/* ---------- page: fixed frame, nothing moves ---------- */
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--fg);
  font: 19px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

/* ---------- chain ---------- */
#stage {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
}
#chain {
  display: grid;
  gap: .4rem 1rem;
  align-items: start;
  align-content: end; /* bottom-anchored: focus sits just above the keyboard */
  justify-items: start;
  width: max-content;
  min-width: 100%;
  min-height: 100%;
  box-sizing: border-box;
  padding: 16px 16px 16px;
}

.cell {
  box-sizing: border-box;
  max-width: min(60vw, 28rem);
  min-height: calc(1.4em + .6rem);
  padding: .3rem .5rem;
  border-radius: .4rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.term { cursor: pointer; }
.term.current {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1.5px var(--accent);
}

.slot {
  min-width: 4.5rem;
  border: 1.5px dashed var(--slot);
  cursor: pointer;
}
.slot:hover, .slot:active { border-color: var(--accent); }

.edit {
  display: flex;
  align-items: center;
  min-width: 4.5rem;
  box-shadow: inset 0 0 0 1.5px var(--accent);
}
.edit .text { min-width: 0; }
.caret {
  flex: none;
  width: 2px;
  height: 1.2em;
  margin-left: 1px;
  background: var(--accent);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.set {
  flex: none;
  margin-left: .5rem;
  padding: .05rem .45rem;
  border: 0;
  border-radius: .35rem;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: .85em;
  cursor: pointer;
}

/* ---------- keyboard ---------- */
#keyboard {
  flex: none;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  background: var(--kb-bg);
  touch-action: none;
}
.kb-row {
  display: flex;
  gap: 5px;
  max-width: 680px;
  margin: 0 auto 6px;
}
.kb-row:last-child { margin-bottom: 0; }
.key {
  flex: var(--w, 1) 1 0;
  min-width: 0;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: var(--key);
  color: var(--key-fg);
  box-shadow: 0 1px 0 var(--shadow);
  font-size: 19px;
  cursor: pointer;
}
.key.fn { background: var(--key-alt); font-size: 15px; }
.key.enter { background: var(--accent); color: #fff; font-size: 19px; }
.key.on { background: var(--fg); color: var(--bg); }
.key.down { background: var(--key-down); }
.key.spacer { visibility: hidden; }
#keyboard.idle .key:not([data-k="{menu}"]) { opacity: .45; }

/* ---------- menu ---------- */
#menu {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, .3);
}
#menu[hidden] { display: none; }
.sheet {
  width: min(100%, 420px);
  box-sizing: border-box;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  border-radius: 14px 14px 0 0;
  background: var(--bg);
}
.sheet button {
  display: block;
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--fg);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.sheet button:hover, .sheet button:active { background: var(--accent-soft); }
.sheet .danger { color: #d64545; }
