:root {
  --bg: #111821;
  --card: #1b2532;
  --line: #2c3a4b;
  --text: #e8eef5;
  --muted: #93a3b5;
  --accent: #4fb3ff;
  --ok: #3ddc84;
  --maybe: #ffc94d;
  --bad: #ff6b6b;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font:
    16px / 1.45 system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
  -webkit-tap-highlight-color: transparent;
  /* Like a native app: the page scrolls but never zooms; only the photo
     viewer zooms (app.js). */
  touch-action: pan-x pan-y;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

canvas,
button {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  padding: calc(env(safe-area-inset-top) + 0.6rem) 1rem 0.6rem;
  background: #1d2a3a;
  border-bottom: 1px solid var(--line);
}

h1 {
  margin: 0;
  font-size: 1.2rem;
}

.version {
  font-size: 0.75rem;
  font-weight: normal;
  color: var(--muted);
}

.version.update {
  color: var(--ok);
  cursor: pointer;
  text-decoration: underline;
}

.engine {
  font-size: 0.8rem;
  color: var(--muted);
}

.engine.ready {
  color: var(--ok);
}

main {
  max-width: 900px;
  margin: 0 auto;
  /* room for the tab bar */
  padding: 0.8rem 0.8rem calc(env(safe-area-inset-bottom) + 4.8rem);
}

/* Two tabs at the bottom like a native app: Vorlage and Teile. */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: flex;
  background: #1d2a3a;
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.45rem 0 0.5rem;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
}

.tabbar button[aria-selected="true"] {
  color: var(--accent);
}

.tabbar button:disabled {
  opacity: 0.4;
}

.tabbar svg {
  width: 1.6rem;
  height: 1.6rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linejoin: round;
  stroke-linecap: round;
}

h3 {
  font-size: 1rem;
  margin: 1rem 0 0.4rem;
}

.hint {
  color: var(--muted);
  margin: 0 0 0.7rem;
  font-size: 0.92rem;
}

.muted {
  color: var(--muted);
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.6rem 0;
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #243245;
  color: var(--text);
  padding: 0.7rem 1rem;
  border-radius: 10px;
  font: inherit;
  cursor: pointer;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #06233a;
  font-weight: 600;
}

.btn.small {
  padding: 0.4rem 0.7rem;
  min-height: 36px;
  font-size: 0.9rem;
}

.btn:disabled {
  opacity: 0.5;
}

.mini {
  appearance: none;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #243245;
  color: var(--text);
  font-size: 1.1rem;
}

canvas {
  display: block;
  max-width: 100%;
}

.crop-wrap,
.ref-view,
.viewer-wrap {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  background: #0b1016;
}

#crop-canvas,
#viewer {
  width: 100%;
  touch-action: none;
}

#ref-canvas,
#map {
  width: 100%;
  height: auto;
}

.count {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.8rem;
}

.count input {
  width: 6.5rem;
  font: inherit;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #0f1620;
  color: var(--text);
}

.count.need input {
  outline: 2px solid var(--accent);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.chips button {
  appearance: none;
  border: 1px solid var(--line);
  background: #1f2b3a;
  color: var(--text);
  border-radius: 999px;
  padding: 0.3rem 0.65rem;
  font: inherit;
  font-size: 0.85rem;
}

.chips button.on {
  background: var(--accent);
  color: #06233a;
  border-color: var(--accent);
}

.grid-adjust {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.6rem;
}

.thumbs {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  padding-bottom: 0.2rem;
}

.thumb {
  position: relative;
  flex: none;
  width: 84px;
  height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  background: #0b1016;
  padding: 0;
}

.thumb.on {
  border-color: var(--accent);
}

.thumb canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb .x {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 0;
  background: #000a;
  color: #fff;
  font-size: 0.8rem;
  line-height: 22px;
}

.thumb .busy {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: #0008;
  font-size: 0.75rem;
}

.status {
  min-height: 1.4em;
  margin: 0.5rem 0;
}

/* Bar on the photo analysed now. */
.thumb .busy i {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 5px;
  width: 0;
  background: var(--accent);
  transition: width 0.3s linear;
}

/* Progress of all photos waiting, with the time left and a way to stop. */
.progress[hidden] {
  display: none;
}

.progress {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.3rem 0.8rem;
  margin: -0.2rem 0 0.6rem;
}

.progress .bar {
  flex: 1 0 100%;
  height: 8px;
  border-radius: 4px;
  background: var(--line);
  overflow: hidden;
}

.progress .bar i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.3s linear;
}

.progress span {
  flex: 1;
  font-size: 0.85rem;
}

/* Start screen: the three steps of a round. */
.howto {
  margin: 1.6rem 0 0;
  padding: 0.8rem 1rem 0.8rem 2.2rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--muted);
  font-size: 0.92rem;
}

.howto li {
  margin: 0.3rem 0;
}

.howto b {
  color: var(--text);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1rem;
  font-size: 0.85rem;
  margin-top: 0.4rem;
}

.sw {
  display: inline-block;
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 3px;
  margin-right: 0.3rem;
  vertical-align: -1px;
}

.sw.bad {
  background: #7d8996;
}

dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  max-width: min(94vw, 480px);
  padding: 1rem;
}

dialog::backdrop {
  background: #000a;
}

dialog h3 {
  margin-top: 0;
}

.detail-pair {
  display: flex;
  gap: 0.8rem;
  justify-content: center;
}

.detail-pair figure {
  margin: 0;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
}

.detail-pair canvas {
  width: 130px;
  height: 130px;
  border-radius: 8px;
  background: #0b1016;
  margin-bottom: 0.3rem;
}

.alts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.8rem;
  font-size: 0.85rem;
}

.alts span {
  background: #243245;
  border-radius: 6px;
  padding: 0.2rem 0.5rem;
}

/* Step by step: one piece, its candidate places, put in or skip. */
.step {
  margin: 0.6rem 0;
  padding: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
}

.step-head {
  display: flex;
  flex-wrap: wrap;
  gap: 0.2rem 0.6rem;
  align-items: baseline;
  margin-bottom: 0.5rem;
}

/* Title and buttons on one line, the counter on its own line below. */
#step-title {
  white-space: nowrap;
}

#step-sub {
  order: 3;
  flex-basis: 100%;
}

/* Gap and piece side by side at the same scale, where the piece lies below:
   everything for one try on one screen, no zooming. */
.step-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.step figure {
  margin: 0;
}

.step figcaption {
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
  margin-top: 2px;
}

.step-pair canvas,
.step-where canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  background: #0b1016;
}

.step figure.step-where {
  margin-top: 0.5rem;
}

.step-where canvas {
  aspect-ratio: 2;
}

.step-buttons .btn {
  flex: 1 1 auto;
  min-height: 3rem;
  font-size: 1.05rem;
}

details.more {
  margin: 0.8rem 0;
}

details.more summary {
  cursor: pointer;
  font-weight: 600;
  padding: 0.4rem 0;
}

.cands {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.cand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 2px solid transparent;
  border-radius: 8px;
  background: #0b1016;
  color: var(--text);
  font-size: 0.75rem;
}

.cand canvas {
  width: 56px;
  height: 56px;
  border-radius: 6px;
}

.cand.on {
  border-color: var(--accent);
}

/* Unsure: still there to try, but not inviting. */
.cand.weak:not(.on) {
  opacity: 0.55;
}

.step-text {
  margin: 0.5rem 0;
}

.piece-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.piece-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  text-align: left;
}

.piece-row.on {
  border-color: var(--accent);
}

.piece-row canvas {
  width: 52px;
  height: 52px;
  border-radius: 6px;
  flex: none;
}

.piece-row .no {
  min-width: 1.6rem;
  font-weight: bold;
}

/* Search list: the motifs of the open places without a sure piece, two
   across, big enough to tell at a glance on the phone by the pieces. */
.search-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  align-content: start;
  /* A tile opened across both columns leaves no hole above it. */
  grid-auto-flow: dense;
}

.search-list p {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 0.85rem;
}

.search-tile {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.8rem;
  text-align: center;
  cursor: pointer;
}

.search-tile canvas {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
}

/* No piece to try: bigger, the gap as the photos show it alongside. */
.search-tile.open {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.search-tile.open span,
.search-tile.open canvas:only-of-type {
  grid-column: 1 / -1;
}

/* Buttons and rows set their own display; hidden must still hide them. */
[hidden],
.btn[hidden],
.row[hidden] {
  display: none;
}

/* Sweep over the table: live camera, full screen. */
.sweep[hidden] {
  display: none;
}

.sweep {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: #000;
}

.sweep-view {
  position: relative;
  flex: 1;
  min-height: 0;
}

.sweep video,
#sweep-over {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* What is photographed so far, growing with every photo. */
.sweep-map {
  position: absolute;
  top: calc(0.5rem + env(safe-area-inset-top));
  right: 0.5rem;
  width: 40%;
  max-width: 220px;
  aspect-ratio: 1;
  pointer-events: none;
}

.sweep-flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.sweep-flash.on {
  opacity: 0.6;
  transition: none;
}

.sweep-bar {
  padding: 0.6rem 0.8rem calc(0.8rem + env(safe-area-inset-bottom));
  background: var(--card);
}

.sweep-bar p {
  margin: 0 0 0.5rem;
  min-height: 2.6em;
}

.sweep-bar .btn {
  flex: 1;
  min-height: 3rem;
}

/* How to hold the phone, before the first sweep. */
.sweep-hint[hidden] {
  display: none;
}

.sweep-hint {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: #000c;
}

.sweep-hint-box {
  max-width: 26rem;
  padding: 1rem 1.2rem;
  border-radius: 12px;
  background: var(--card);
}

.sweep-hint-box h3 {
  margin-top: 0;
}

.sweep-hint-box ol {
  margin: 0 0 1rem;
  padding-left: 1.3rem;
}

.sweep-hint-box li {
  margin: 0.4rem 0;
}

.sweep-hint-box .btn {
  width: 100%;
  justify-content: center;
}

.thumb .unplaced {
  position: absolute;
  left: 2px;
  bottom: 2px;
  padding: 0 4px;
  border-radius: 4px;
  background: #e0a030;
  color: #000;
  font-size: 0.7rem;
  font-weight: 600;
}

.step-tools {
  margin-left: auto;
  display: flex;
  gap: 0.4rem;
}

.mini.wide {
  width: auto;
  padding: 0 0.6rem;
  font-size: 0.85rem;
}

.mini.on {
  border-color: var(--accent);
  color: var(--accent);
}

/* At the table: the phone lies by the pieces, the hands are busy. One try
   fills the screen: where the piece lies and where it goes on top, piece
   and gap below, big buttons for a knuckle. */
.step.table {
  position: fixed;
  inset: 0;
  z-index: 40;
  margin: 0;
  border: 0;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: calc(0.5rem + env(safe-area-inset-top)) 0.6rem calc(0.6rem + env(safe-area-inset-bottom));
  overflow: hidden;
}

.step.table .step-head {
  margin: 0;
  font-size: 1.15rem;
  align-items: center;
}

.step.table .cands {
  display: none;
}

.step.table figure.step-where {
  order: 1;
  flex: 1;
  min-height: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
}

.step.table .step-where canvas {
  flex: 1;
  min-height: 0;
  aspect-ratio: auto;
}

.step.table .step-pair {
  order: 2;
  grid-template-columns: repeat(2, minmax(0, 24dvh));
  justify-content: center;
}

.step.table .step-text {
  order: 3;
  margin: 0;
  font-size: 1.25rem;
  text-align: center;
}

.step.table .step-buttons {
  order: 4;
  flex-wrap: nowrap;
}

.step.table .step-buttons .btn {
  flex: 1 1 0;
  min-height: 4.5rem;
  font-size: 1.2rem;
  padding: 0 0.3rem;
}

.step.table #step-placed {
  flex-grow: 2;
  font-size: 1.5rem;
}

.step.table #step-removed {
  display: none;
}

.step:not(.table) #step-undo {
  display: none;
}

.step.table #step-mode {
  margin: 0;
}

.step:not(.search) .search-list,
.step.search .step-pair,
.step.search .cands,
.step.search figure.step-where,
.step.search .step-text,
.step.search .step-buttons {
  display: none;
}

.step.table .search-list {
  order: 1;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

/* Capture buttons: short labels in the compact row. */
.capture .short {
  display: none;
}

.scene-head.compact {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.scene-head.compact .hint {
  display: none;
}

.scene-head.compact .capture {
  flex: none;
  flex-wrap: nowrap;
  gap: 0.35rem;
  margin: 0;
}

.scene-head.compact .capture .btn {
  padding: 0.3rem 0.55rem;
  min-height: 40px;
  font-size: 0.88rem;
}

.scene-head.compact .capture .long {
  display: none;
}

.scene-head.compact .capture .short {
  display: inline;
}

.scene-head.compact .thumbs {
  flex: 1;
  min-width: 0;
  gap: 0.3rem;
  padding: 0;
}

.scene-head.compact .thumb {
  width: 56px;
  height: 42px;
}

.scene-head.compact .thumb .x {
  width: 18px;
  height: 18px;
  font-size: 0.65rem;
  line-height: 18px;
}

.scene-head.compact .thumb .unplaced {
  font-size: 0;
  width: 10px;
  height: 10px;
  padding: 0;
  border-radius: 50%;
}

.scene-head.compact + .status {
  font-size: 0.88rem;
  color: var(--muted);
}

/* With results the progress sits above the tab bar: nothing on the page
   moves while the step card is in use. */
#progress-text {
  display: none;
}

.scene-head.compact ~ .progress {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(3.9rem + env(safe-area-inset-bottom));
  z-index: 4;
  margin: 0;
  padding: 0.5rem 0.8rem;
  background: #1d2a3a;
  border-top: 1px solid var(--line);
}

.scene-head.compact ~ .progress #progress-text {
  display: block;
  flex: 1 0 100%;
  font-size: 0.88rem;
}
