:root {
  --bg: #07090f;
  --surface: rgba(16, 22, 34, 0.92);
  --surface-2: #1a2332;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --accent: #5ec8ff;
  --border: rgba(255, 255, 255, 0.1);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --vv-top: 0px;
  --vv-left: 0px;
  --vv-w: 100%;
  --vv-h: 100%;
  --pwa-fill-h: 100%;
  --pwa-extra-b: 0px;
  /* Element tile size — on :root so tray/sheet --rail-h can see it */
  --cell: 3.4304rem; /* +20% from 2.8587 */
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation;
}

/* Bug B: PWA must grow layout viewport to screen fillH (in flow — not fixed html/body) */
html.pwa-standalone,
html.pwa-standalone body {
  height: calc(var(--pwa-fill-h, 100%) + var(--pwa-extra-b, 0px));
  min-height: calc(var(--pwa-fill-h, 100%) + var(--pwa-extra-b, 0px));
  max-height: none;
}

/* Pin shell to visual viewport (Safari tab) / PWA fillH (HyperMiler pattern) */
#app {
  position: fixed;
  top: var(--vv-top);
  left: var(--vv-left);
  width: var(--vv-w);
  height: var(--vv-h);
  right: auto;
  bottom: auto;
  overflow: hidden;
  background: var(--bg);
}

#stage {
  position: absolute;
  inset: 0;
  z-index: 0;
  touch-action: none;
}

#stage canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.45rem;
  padding: max(0.12rem, var(--safe-t)) calc(0.75rem + var(--safe-r)) 0.55rem
    calc(0.75rem + var(--safe-l));
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(7, 9, 15, 0.9), transparent);
}

.top > * {
  pointer-events: auto;
}

.app-title {
  margin: 0;
  font-family: "Michroma", system-ui, sans-serif;
  font-size: clamp(1.05rem, 3.6vw, 1.35rem);
  font-weight: 400;
  letter-spacing: 0.06em;
  line-height: 1.1;
  text-align: center;
  color: var(--text);
}

.top-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  justify-content: center;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 0.42rem 0.78rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.btn[aria-pressed="true"] {
  border-color: rgba(94, 200, 255, 0.55);
  color: var(--accent);
  box-shadow: 0 0 0 1px rgba(94, 200, 255, 0.2);
}

.btn.ghost {
  background: transparent;
}

.hud {
  position: absolute;
  left: calc(0.75rem + var(--safe-l));
  right: calc(0.75rem + var(--safe-r));
  bottom: calc(3.6rem + var(--safe-b) + var(--rail-h, 0px));
  z-index: 4;
  text-align: center;
  pointer-events: none;
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.65);
}

.hud-formula {
  font-family: Michroma, system-ui, sans-serif;
  font-size: clamp(1.15rem, 4.5vw, 1.6rem);
  letter-spacing: 0.06em;
}

.hud-name {
  margin-top: 0.25rem;
  font-size: clamp(0.95rem, 3.2vw, 1.15rem);
  font-weight: 600;
}

.hud-hint {
  margin-top: 0.35rem;
  color: var(--muted);
  font-size: 0.68rem;
}

.tray {
  position: absolute;
  left: 50%;
  /* Above home indicator + rail height (rail-h has no safe-area in it) */
  bottom: calc(0.45rem + var(--safe-b) + var(--rail-h, 0px));
  transform: translateX(-50%);
  z-index: 7; /* above element rail so chips stay visible/tappable */
  display: flex;
  gap: 0.35rem;
  max-width: calc(100% - 1.5rem);
  overflow-x: auto;
  padding: 0.35rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  min-height: 2.4rem;
  min-width: 2.4rem;
}

.tray:empty {
  display: none;
}

.chip {
  flex: 0 0 auto;
  min-width: 2rem;
  height: 1.7rem;
  padding: 0 0.55rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.78rem;
  color: #0b0d12;
}

.chip-undo {
  appearance: none;
  cursor: pointer;
  box-shadow: 0 0 0 1px rgba(94, 200, 255, 0.45), 0 0 12px rgba(94, 200, 255, 0.25);
  border-color: rgba(94, 200, 255, 0.55);
}

.chip-undo::after {
  content: "↩";
  margin-left: 0.28rem;
  font-size: 0.72rem;
  font-weight: 600;
  opacity: 0.9;
}

.chip-undo:active {
  transform: scale(0.96);
}

.sheet {
  position: absolute;
  left: calc(0.5rem + var(--safe-l));
  right: calc(0.5rem + var(--safe-r));
  /* Fill unused space: under top actions, above element rail */
  top: calc(4.55rem + var(--safe-t));
  bottom: calc(0.4rem + var(--safe-b) + var(--rail-h, 0px));
  z-index: 8;
  max-height: none;
  overflow: auto;
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1rem;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

/* Title + search stay pinned; list never scrolls above the search */
.sheet-sticky {
  position: sticky;
  top: 0;
  z-index: 3;
  padding: 0.75rem 0.85rem 0.45rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.sheet-toolbar {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.sheet-note {
  color: var(--muted);
  font-size: 0.72rem;
  margin: 0.45rem 0 0;
}

.sheet-title {
  margin: 0;
  flex: 1;
  min-width: 0;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--text);
}

.about-body {
  padding: 0.35rem 0.85rem calc(0.85rem + var(--safe-b));
}

.about-section {
  margin: 0.85rem 0 1.1rem;
}

.about-section h3 {
  margin: 0 0 0.45rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: #ffb45c;
}

.about-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 0.55rem;
}

.about-list li {
  padding: 0.65rem 0.75rem;
  border-radius: 0.65rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: #c5d0e0;
  font-size: 0.82rem;
  line-height: 1.45;
}

.about-list strong {
  color: var(--text);
  font-weight: 600;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.recipe-search-wrap {
  display: block;
  position: relative;
  flex: 1;
  min-width: 0;
  margin: 0;
}

.recipe-search {
  width: 100%;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 0.65rem;
  padding: 0.65rem 2.1rem 0.65rem 0.75rem;
  font: inherit;
  font-size: 0.88rem;
}

/* Hide native clear so our button is the one way back to the full list */
.recipe-search::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.recipe-search::placeholder {
  color: var(--muted);
}

.recipe-search:focus {
  outline: none;
  border-color: rgba(94, 200, 255, 0.55);
}

.recipe-search-clear {
  position: absolute;
  right: 0.25rem;
  top: 50%;
  transform: translateY(-50%);
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.35rem 0.45rem;
  border-radius: 0.4rem;
}

.recipe-search-clear:hover,
.recipe-search-clear:focus-visible {
  color: var(--text);
  outline: none;
}

.icon-close {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  flex: 0 0 auto;
}

/*
 * Horizontal element strip — equal-size cells (no scale morph).
 * Scale(2) on rail cells overlapped neighbors and blocked taps on iOS PWA.
 */
.element-rail {
  --rail-gap: 0.14rem;
  position: absolute;
  left: 0;
  right: 0;
  /* Sit above the home indicator; don’t double-count safe-area in height */
  bottom: var(--safe-b);
  z-index: 6;
  height: calc(var(--cell) + 0.4rem);
  padding: 0.12rem 0;
  overflow: hidden;
  background: linear-gradient(
    to top,
    rgba(7, 9, 15, 0.96) 40%,
    rgba(7, 9, 15, 0.72) 78%,
    transparent
  );
  pointer-events: none;
}

.element-rail.is-open {
  pointer-events: auto;
}

.element-rail:not(.is-open) {
  visibility: hidden;
}

.element-rail-inner {
  position: relative;
  height: 100%;
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: none;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--rail-gap);
  padding: 0 max(0.2rem, var(--safe-r)) 0 max(0.15rem, var(--safe-l));
  scrollbar-width: none;
  touch-action: pan-x;
  /* Promote a stable scroll layer — cuts tile flicker on iOS */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

.element-rail-inner::-webkit-scrollbar {
  display: none;
}

.el-cell {
  appearance: none;
  -webkit-appearance: none;
  flex: 0 0 auto;
  width: var(--cell);
  height: var(--cell);
  margin: 0;
  padding: 0.14rem 0.1rem 0.1rem;
  border-radius: 0.2rem;
  border: 1px solid rgba(0, 0, 0, 0.35);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
  display: grid;
  grid-template-rows: auto 1fr auto;
  align-items: center;
  justify-items: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  text-align: center;
  line-height: 1;
  position: relative;
  z-index: 1;
  /* Keep tile colors — never flash to black on tap */
  -webkit-tap-highlight-color: transparent;
  /* Isolate paint so scrolling neighbors don’t repaint the whole rail */
  contain: layout paint;
}

.el-cell:active:not(.is-blocked):not(:disabled) {
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.22),
    0 0 0 1px rgba(255, 255, 255, 0.2);
}

.el-cell.is-blocked,
.el-cell:disabled {
  /* No CSS filter — filters on ~350 scroll tiles caused intermittent flicker */
  opacity: 0.55;
  cursor: not-allowed;
  position: relative;
}

.el-cell.is-blocked::after,
.el-cell:disabled::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: rgba(7, 9, 15, 0.32);
  pointer-events: none;
}

.el-cell.is-pending:not(.is-blocked) {
  outline: 2px solid rgba(94, 200, 255, 0.9);
  outline-offset: 1px;
  box-shadow:
    inset 0 0 0 1px rgba(94, 200, 255, 0.35),
    0 0 10px rgba(94, 200, 255, 0.25);
}

.el-cell .el-top {
  display: flex;
  width: 100%;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.08rem;
  line-height: 1.25; /* room for descenders (g in gas) */
  padding-bottom: 0.06rem;
}

.el-cell .el-z {
  font-size: 0.62rem;
  font-weight: 600;
  opacity: 0.85;
  flex: 0 0 auto;
  line-height: 1.25;
}

.el-cell .el-state {
  font-size: 0.48rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  text-transform: lowercase;
  opacity: 0.88;
  text-align: right;
  max-width: 62%;
  /* no overflow:hidden — it clipped the g/y descenders */
  white-space: nowrap;
  flex: 0 1 auto;
  line-height: 1.25;
}

.el-cell .el-sym {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.el-cell .el-name {
  font-size: 0.4252rem;
  font-weight: 500;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.9;
}

body.rail-open {
  /* Rail bar height only — safe-area applied separately via bottom offsets */
  --rail-h: calc(var(--cell) + 0.4rem);
}

@media (min-width: 700px) {
  :root {
    --cell: 3.9618rem; /* +20% from 3.3015 */
  }
  body.rail-open {
    --rail-h: calc(var(--cell) + 0.4rem);
  }
  .el-cell .el-sym {
    font-size: 1.33rem;
  }
  .el-cell .el-name {
    font-size: 0.4637rem;
  }
  .el-cell .el-z {
    font-size: 0.7rem;
  }
  .el-cell .el-state {
    font-size: 0.54rem;
  }
}

.recipe-list {
  list-style: none;
  display: grid;
  gap: 0.4rem;
  padding: 0.55rem 0.85rem calc(0.85rem + var(--safe-b));
}

.recipe-cat {
  margin: 0.55rem 0 0.1rem;
  padding: 0.28rem 0.45rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  /* Warm amber — distinct from cyan meta / muted card text while scrolling */
  color: #ffb45c;
  background: rgba(255, 180, 92, 0.1);
  border-left: 3px solid #ffb45c;
  border-radius: 0.25rem;
}

.recipe-cat:first-child {
  margin-top: 0.15rem;
}

.recipe-empty {
  color: var(--muted);
  font-size: 0.8rem;
  padding: 0.85rem 0.25rem;
}

.recipe {
  width: 100%;
  text-align: left;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 0.7rem;
  padding: 0.7rem 0.8rem;
  cursor: pointer;
}

.recipe strong {
  display: block;
  font-size: 0.95rem;
}

.recipe .recipe-line,
.recipe .recipe-meta,
.recipe .recipe-hook {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  margin-top: 0.18rem;
  line-height: 1.35;
}

.recipe .recipe-meta {
  color: rgba(94, 200, 255, 0.85);
  font-size: 0.68rem;
  letter-spacing: 0.02em;
}

.recipe .recipe-hook {
  color: #c5d0e0;
  font-size: 0.74rem;
  margin-top: 0.28rem;
}

@media (min-width: 900px) {
  .sheet {
    left: auto;
    right: calc(0.75rem + var(--safe-r));
    width: min(400px, 42vw);
    top: calc(4.55rem + var(--safe-t));
    bottom: calc(0.75rem + var(--safe-b) + var(--rail-h, 0px));
  }

  .hud {
    bottom: calc(5rem + var(--safe-b) + var(--rail-h, 0px));
  }
}
