:root {
  --stack-paper: #f3f1ea;
  --stack-ink: #24231f;
  --stack-accent: #ef5b2a;
  --stack-inset: clamp(20px, 4.5vw, 64px);
  --stack-ui: 13px;
  --stack-support: 12px;
  --stack-label: 11px;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--stack-paper);
  color: var(--stack-ink);
}

body {
  font-family: 'Urbanist', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
a,
input {
  -webkit-tap-highlight-color: transparent;
}

.stack-study {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--stack-paper);
  user-select: none;
  -webkit-user-select: none;
}

/* Same SuDu site header geometry, without a divider on STACK. */
.stack-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 120;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: 34px var(--stack-inset) 30px;
  background: rgba(243, 241, 234, .94);
  pointer-events: none;
}

.stack-mark {
  display: flex;
  align-items: flex-end;
  pointer-events: auto;
}

.stack-mark img {
  display: block;
  width: auto;
  height: 38px;
}

.stack-header h1 {
  margin: 0;
  padding-bottom: 3px;
  font-size: var(--stack-ui);
  font-weight: 500;
  line-height: 1;
  letter-spacing: 0;
}

/* STACK uses the same practical scale as the SuDu site: 13px controls,
   12px supporting copy and 11px tertiary labels. No micro-type. */
.stack-instruction,
.stack-note,
.stack-loading,
.stack-players {
  position: fixed;
  z-index: 20;
  font-size: var(--stack-support);
  font-weight: 500;
  line-height: 1.35;
  letter-spacing: .02em;
  color: rgba(36, 35, 31, .72);
}

.stack-instruction {
  left: var(--stack-inset);
  bottom: 25px;
  margin: 0;
}

.stack-note {
  right: var(--stack-inset);
  bottom: 24px;
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: var(--stack-support);
}

#move-count:empty {
  display: none;
}

#again {
  padding: 0;
  border: 0;
  border-bottom: 1px solid rgba(36, 35, 31, .45);
  background: transparent;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  cursor: pointer;
}

#again:hover,
#again:focus-visible {
  color: #111;
  border-bottom-color: #111;
  outline: none;
}

.stack-help {
  width: 20px;
  height: 20px;
  padding: 1px;
  border: 0;
  background: transparent;
  color: rgba(36, 35, 31, .58);
  cursor: pointer;
}

.stack-help svg {
  display: block;
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.15;
  stroke-linecap: square;
}

.stack-help .stack-help-accent {
  stroke: var(--stack-accent);
  stroke-width: 1.65;
}

.stack-help:hover,
.stack-help:focus-visible {
  color: var(--stack-ink);
  outline: none;
}

#stack-stage {
  position: absolute;
  inset: 0;
  cursor: grab;
  touch-action: none;
}

#stack-stage.is-hovering {
  cursor: pointer;
}

#stack-stage.is-dragging,
#stack-stage.is-orbiting {
  cursor: grabbing;
}

#stack-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.stack-loading {
  top: 50%;
  left: 50%;
  margin: 0;
  transform: translate(-50%, -50%);
}

/* Player ledger. It is a quiet turn ledger, not a scoreboard. */
.stack-players {
  top: 50%;
  left: var(--stack-inset);
  width: 180px;
  transform: translateY(-50%);
  z-index: 130;
  pointer-events: none;
  user-select: text;
  -webkit-user-select: text;
}

.stack-player-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 22px;
  align-items: end;
  gap: 10px;
  width: 100%;
  pointer-events: auto;
  transition: opacity 180ms ease;
}

.stack-player-form.is-full {
  display: none;
}

.stack-player-form.is-locked {
  opacity: .32;
  pointer-events: none;
}

.stack-player-input {
  width: 100%;
  min-width: 0;
  padding: 0 0 5px;
  border: 0;
  border-bottom: 1px solid rgba(36, 35, 31, .26);
  border-radius: 0;
  outline: 0;
  background: transparent;
  color: var(--stack-ink);
  font: inherit;
  font-size: var(--stack-support);
  font-weight: 500;
  line-height: 1.3;
  letter-spacing: .02em;
  appearance: none;
}

.stack-player-input::placeholder {
  color: rgba(36, 35, 31, .34);
  opacity: 1;
}

.stack-player-input:focus {
  border-bottom-color: rgba(36, 35, 31, .66);
}

.stack-player-add {
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--stack-ink);
  font: 300 20px/20px 'Urbanist', Arial, sans-serif;
  cursor: pointer;
}

.stack-player-add:hover,
.stack-player-add:focus-visible {
  color: var(--stack-accent);
  outline: none;
}

.stack-player-list {
  display: grid;
  gap: 8px;
  margin: 14px 0 0;
  padding: 0;
  list-style: none;
}

.stack-player {
  position: relative;
  min-height: 15px;
  padding-left: 15px;
  overflow: hidden;
  color: rgba(36, 35, 31, .42);
  font-size: var(--stack-support);
  font-weight: 500;
  line-height: 1.3;
  letter-spacing: .02em;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: color 180ms ease, opacity 180ms ease;
}

.stack-player.is-active,
.stack-player.is-winner {
  color: var(--stack-ink);
}

.stack-player.is-active::before,
.stack-player.is-winner::before {
  content: '';
  position: absolute;
  left: 0;
  top: 4px;
  width: 6px;
  height: 6px;
  border-top: 1.5px solid var(--stack-accent);
  border-right: 1.5px solid var(--stack-accent);
  transform: rotate(45deg);
}

.stack-player.is-loser {
  opacity: .28;
}

/* The result belongs to the player ledger, never over the tower. */
.stack-result {
  display: grid;
  justify-items: start;
  gap: 5px;
  margin-top: 18px;
  padding-left: 15px;
  text-align: left;
  pointer-events: none;
  animation: stack-ledger-result-in 220ms ease both;
}

.stack-result[hidden] {
  display: none;
}

.stack-result-kicker {
  color: rgba(36, 35, 31, .40);
  font-size: var(--stack-label);
  font-weight: 500;
  line-height: 1;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.stack-result-line {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
}

.stack-result-name {
  display: block;
  max-width: 130px;
  overflow: hidden;
  color: var(--stack-ink);
  font-size: var(--stack-ui);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: .01em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stack-result-state {
  color: var(--stack-accent);
  font-size: var(--stack-label);
  font-weight: 700;
  line-height: 1;
  letter-spacing: .04em;
  text-transform: uppercase;
}

@keyframes stack-ledger-result-in {
  from { opacity: 0; transform: translateY(3px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Tutorial colour and game-state graphics are visual only. They never sit
   between the pointer and the Rapier/Three interaction surface. */
.stack-cue-shield {
  position: fixed;
  inset: 0;
  z-index: 15;
  pointer-events: none !important;
  background: transparent;
}

body.stack-cue-open .stack-instruction,
body.stack-game-over .stack-instruction {
  opacity: 0;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  .stack-player,
  .stack-player-form,
  .stack-result {
    transition: none;
    animation: none;
  }
}

/* Mobile is a different interface architecture, not a compressed desktop.
   Portrait reserves the bottom edge for thumb controls and turns the player
   ledger into a horizontal rail. The tower remains visually dominant. */
@media (max-width: 760px) {
  :root {
    --stack-mobile-left: max(20px, env(safe-area-inset-left));
    --stack-mobile-right: max(20px, env(safe-area-inset-right));
    --stack-mobile-bottom: env(safe-area-inset-bottom);
  }

  .stack-header {
    padding-top: max(24px, calc(env(safe-area-inset-top) + 12px));
    padding-right: var(--stack-mobile-right);
    padding-bottom: 18px;
    padding-left: var(--stack-mobile-left);
  }

  .stack-mark img {
    height: 38px;
  }

  .stack-players {
    top: auto;
    right: var(--stack-mobile-right);
    bottom: calc(70px + var(--stack-mobile-bottom));
    left: var(--stack-mobile-left);
    width: auto;
    transform: none;
    display: grid;
    grid-template-columns: minmax(118px, 154px) minmax(0, 1fr);
    grid-template-areas:
      'form players'
      '. result';
    column-gap: 14px;
    row-gap: 8px;
    align-items: end;
  }

  .stack-player-form {
    grid-area: form;
    grid-template-columns: minmax(0, 1fr) 44px;
    gap: 0;
    min-height: 44px;
    align-items: center;
  }

  /* 16px prevents iOS from auto-zooming the viewport while entering a name.
     All persistent STACK typography remains on the SuDu 11/12/13px scale. */
  .stack-player-input {
    height: 44px;
    padding: 14px 0 8px;
    font-size: 16px;
    line-height: 1;
  }

  .stack-player-add {
    width: 44px;
    height: 44px;
    font-size: 21px;
    line-height: 44px;
  }

  .stack-player-list {
    grid-area: players;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 7px 14px;
    margin: 0;
  }

  .stack-player {
    min-height: 16px;
    padding-left: 14px;
    font-size: var(--stack-support);
  }

  .stack-result {
    grid-area: result;
    justify-self: end;
    margin: 0;
    padding-left: 0;
  }

  .stack-result-name {
    max-width: 150px;
  }

  .stack-instruction {
    left: var(--stack-mobile-left);
    bottom: calc(18px + var(--stack-mobile-bottom));
    margin: 0;
    font-size: var(--stack-support);
  }

  .stack-note {
    right: var(--stack-mobile-right);
    bottom: calc(6px + var(--stack-mobile-bottom));
    min-height: 44px;
    gap: 2px;
  }

  #move-count {
    margin-right: 8px;
    white-space: nowrap;
  }

  .stack-help {
    width: 44px;
    height: 44px;
    padding: 12px;
  }

  #again {
    min-width: 54px;
    min-height: 44px;
    padding: 0 0 0 10px;
    border-bottom: 0;
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 3px;
  }

  body.stack-name-entry .stack-instruction,
  body.stack-name-entry .stack-note {
    opacity: 0;
    pointer-events: none;
  }

  body.stack-name-entry .stack-players {
    bottom: calc(18px + var(--stack-mobile-bottom));
  }
}

/* Short landscape phones use the side-ledger architecture again. This leaves
   the narrow vertical dimension almost entirely to the tower. */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 560px) {
  :root {
    --stack-mobile-left: max(20px, env(safe-area-inset-left));
    --stack-mobile-right: max(20px, env(safe-area-inset-right));
    --stack-mobile-bottom: env(safe-area-inset-bottom);
  }

  .stack-header {
    padding-top: max(18px, calc(env(safe-area-inset-top) + 8px));
    padding-right: var(--stack-mobile-right);
    padding-bottom: 14px;
    padding-left: var(--stack-mobile-left);
  }

  .stack-players {
    top: 50%;
    right: auto;
    bottom: auto;
    left: var(--stack-mobile-left);
    width: 160px;
    transform: translateY(-50%);
    display: block;
  }

  .stack-player-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 44px;
  }

  .stack-player-list {
    display: grid;
    justify-content: stretch;
    gap: 7px;
    margin-top: 10px;
  }

  .stack-result {
    margin-top: 12px;
  }

  .stack-instruction {
    bottom: calc(14px + var(--stack-mobile-bottom));
  }

  .stack-note {
    bottom: calc(2px + var(--stack-mobile-bottom));
  }
}
