/* Only the open workbench gets an edge-to-edge page. The desk, album and
   letters keep their folio margins and their own return button. */
body:has(main > #bench:not([hidden])) { min-height: 100dvh; }
main:has(> #bench:not([hidden])) {
  display: flex;
  flex-direction: column;
  width: min(100%, 52rem);
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  padding: max(4px, env(safe-area-inset-top)) env(safe-area-inset-right)
           max(4px, env(safe-area-inset-bottom)) env(safe-area-inset-left);
}
#bench:not([hidden]) { flex: 1; min-height: 0; }
#bench:not([hidden]) ~ #deskHome,
#bench:not([hidden]) ~ #error:empty { display: none; }

#bench .mineral-bench {
  --bench-gap: clamp(6px, 1.2vh, 12px);
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: 'head' 'order' 'tray' 'summary' 'actions';
  grid-template-rows: auto auto minmax(0, max-content) auto auto;
  align-content: center;
  gap: var(--bench-gap);
  height: 100%;
  max-width: none;
  padding: var(--bench-gap) 4px;
  container-type: inline-size;
}
.mineral-bench .mb-head { grid-area: head; padding: 0 .35rem .35rem; }
.mineral-bench .mb-head small { display: block; }
.mineral-bench .mb-order { grid-area: order; margin: 0; padding: .35rem .5rem; }
.mineral-bench .mb-order #recipe {
  display: flex;
  flex-wrap: wrap;
  gap: .18rem .65rem;
  min-width: 0;
  font-size: .82rem;
  font-variant-numeric: tabular-nums;
}
.mineral-bench .mb-turns { flex: none; font-variant-numeric: tabular-nums; }

/* The in-flow sizer asks for the full-width height at this order's inline
   aspect ratio. The track can shrink; the tray then derives its width from
   the actual track height. Canvas and touch grid still share one rectangle. */
.mineral-bench .mb-tray {
  grid-area: tray;
  justify-self: center;
  width: auto;
  height: 100%;
  min-width: 0;
  min-height: 0;
  max-width: 100%;
  max-height: none;
  margin: 0;
}
.mineral-bench .mb-tray::before {
  content: '';
  display: block;
  width: 100cqw;
  aspect-ratio: inherit;
  pointer-events: none;
}
.mineral-bench .mb-status {
  grid-area: summary;
  min-height: 2.5em;
  margin: 0;
  padding: 0 .35rem;
  align-content: center;
}
.mineral-bench .mb-actions {
  grid-area: actions;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.mineral-bench .mb-actions button { min-height: 44px; }

/* Anchor the result to the board row, without contributing to its size. */
.mineral-bench #mineralEnd {
  position: absolute;
  grid-area: tray;
  inset: auto 0 4px;
  z-index: 3;
  width: min(calc(100% - 8px), 25rem);
  max-height: calc(100% - 8px);
  overflow: auto;
  margin: 0 auto;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: center;
  column-gap: .5rem;
  border-top: 3px solid var(--ribbon);
  box-shadow: 0 -3px 12px #302d2740;
}
#benchResultTitle { grid-area: 1 / 1; font: 600 1.05rem/1.15 Georgia, serif; }
#benchResultNote { grid-area: 2 / 1; margin: 0; font-size: .8rem; line-height: 1.25; }
#resultMount { grid-area: 1 / 2 / span 2; justify-self: center; width: min(100%, 8rem); height: auto; }
#mineralEnd #benchRetry[hidden] ~ #benchFinish { grid-column: 1 / -1; }

#benchMoreDialog {
  position: fixed;
  inset: auto 0 0;
  margin: 0 auto;
  width: min(100%, 28rem);
  max-width: 100%;
  max-height: 94dvh;
  overflow: auto;
  overscroll-behavior: contain;
  padding: .85rem max(.75rem, env(safe-area-inset-right))
           max(.85rem, env(safe-area-inset-bottom)) max(.75rem, env(safe-area-inset-left));
  border: 1px solid var(--rule);
  border-top: 3px solid var(--ribbon);
  border-radius: 10px 10px 0 0;
  color: var(--ink);
  background: var(--paper);
}
#benchMoreDialog[open] { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .4rem; }
#benchMoreDialog h3, #benchMoreDialog #setting, #benchMoreDialog details,
#benchMoreDialog [data-loupe], #benchMoreClose { grid-column: 1 / -1; }
#benchMoreDialog #setting { display: block; width: min(100%, 12rem); height: auto; justify-self: center; }
#benchMoreDialog details { margin: 0; border-block: 1px solid var(--rule); }
#benchMoreDialog summary { min-height: 44px; padding: .75rem .25rem; }
#benchMoreDialog::backdrop { background: #302d279e; }

@media (max-height: 650px) {
  #bench .mineral-bench { --bench-gap: 4px; }
  .mineral-bench .mb-head { padding-block-end: 2px; }
  .mineral-bench .mb-head h2 { font-size: 1.05rem; }
  .mineral-bench .mb-order { padding-block: 2px; }
  .mineral-bench .mb-turns { display: flex; align-items: baseline; gap: .28rem; }
  #resultMount { width: min(100%, 6rem); }
}

/* On a rotated phone, share the top and bottom rows rather than reducing
   the stones to tiny tap targets between five stacked rows. */
@media (orientation: landscape) and (max-height: 450px) and (min-width: 500px) {
  #bench .mineral-bench {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas: 'head order' 'tray tray' 'summary actions';
    grid-template-rows: auto minmax(0, max-content) auto;
  }
}
