/* Tournament HUD overlay: waiting lobby (time gate), in-match countdown and finished podium.
   Paper + blue ink (--ink) + red accents (--red) hand-drawn language, shared with style.css /
   account.css / coop.css; no external fonts or assets. Only buttons are interactive
   (pointer-events: auto); everything else lets canvas clicks through so pointer lock still works
   while waiting. Safe-area aware and single-column on phones. */

#hud .tour-root {
  position: absolute;
  inset: 0;
  z-index: 20; /* above the game HUD (.board is 5), below #touch (30) and fullscreen modals */
  color: var(--ink);
  font-family: inherit;
  pointer-events: none;
  user-select: none;
}

/* hidden must beat the display:flex / block declarations below */
.tour-root [hidden] { display: none !important; }

.tour-root button { pointer-events: auto; cursor: pointer; }
.tour-root button:focus-visible { outline: 2px dashed var(--red); outline-offset: 3px; }

/* ---------------- top connection / error lines ---------------- */

.tour-status,
.tour-error {
  position: absolute;
  left: 50%;
  top: calc(8px + env(safe-area-inset-top));
  box-sizing: border-box;
  max-width: min(78vw, 520px);
  padding: 2px 16px;
  transform: translateX(-50%) rotate(-.6deg);
  font-size: 19px;
  line-height: 1.3;
  text-align: center;
  color: var(--ink);
  background: rgba(246, 243, 230, .88);
  border: 2px dashed var(--ink);
  border-radius: 8px 12px 7px 11px / 12px 7px 11px 8px;
  box-shadow: 2px 2px 0 rgba(26, 48, 192, .14);
  mix-blend-mode: multiply;
}

.tour-status.reconnect {
  color: var(--red);
  border-color: var(--red);
  animation: tour-blink 1.1s infinite;
}

.tour-error {
  top: calc(46px + env(safe-area-inset-top));
  color: var(--red);
  border-style: solid;
  border-color: var(--red);
  background: rgba(250, 248, 238, .94);
}

/* ---------------- WAITING lobby card ---------------- */

.tour-lobby {
  position: absolute;
  left: 50%;
  top: calc(56px + env(safe-area-inset-top));
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(660px, calc(100vw - 24px - env(safe-area-inset-left) - env(safe-area-inset-right)));
  max-height: min(56vh, 440px);
  padding: 12px 16px 12px 18px;
  transform: translateX(-50%) rotate(-.7deg);
  background: rgba(250, 248, 238, .93);
  border: 3px solid var(--ink);
  border-radius: 12px 18px 10px 16px / 16px 10px 18px 12px;
  box-shadow: 5px 5px 0 rgba(26, 48, 192, .15);
  mix-blend-mode: multiply;
  animation: tour-in .22s ease-out;
}

/* Title gets the full card width (so the exact English line fits on one row); below it the info
   column and the scrollable roster sit side by side. */
.tour-lobby-body {
  display: flex;
  flex: 1;
  gap: 16px;
  align-items: stretch;
  min-height: 0;
}

.tour-lobby-main {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  text-align: left;
}

.tour-title {
  font-size: 29px;
  line-height: 1.05;
  letter-spacing: 1px;
  white-space: nowrap;
  transform: rotate(-.5deg);
}

/* exact English countdown line from the spec: Match starts in: MM:SS */
.tour-countdown {
  margin-top: 2px;
  font-size: 33px;
  line-height: 1.1;
  color: var(--red);
  font-variant-numeric: tabular-nums;
}

.tour-countdown.urgent { animation: tour-pulse .8s infinite; }

.tour-meta {
  font-size: 16px;
  opacity: .75;
}

.tour-lockhint {
  align-self: flex-start;
  margin-top: 2px;
  padding: 1px 9px;
  font-size: 15px;
  line-height: 1.35;
  color: var(--ink);
  border: 2px dashed rgba(26, 48, 192, .5);
  border-radius: 6px 9px 5px 8px / 9px 5px 8px 6px;
  opacity: .9;
}

.tour-lobby-foot {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-top: auto;
  padding-top: 6px;
}

.tour-badge {
  display: inline-block;
  padding: 1px 12px;
  font-size: 17px;
  white-space: nowrap;
  color: var(--ink);
  background: rgba(246, 243, 230, .6);
  border: 2px solid currentColor;
  border-radius: 8px 11px 7px 10px / 11px 7px 10px 8px;
  transform: rotate(-1deg);
}

/* Buttons: ink-filled paper text by default (same look as .online button); the lobby "leave"
   button is a small red-outlined variant. */
.tour-btn {
  font: inherit;
  font-size: 19px;
  line-height: 1.2;
  color: var(--paper);
  background: var(--ink);
  border: 2.5px solid var(--ink);
  border-radius: 7px 10px 6px 9px / 10px 6px 9px 7px;
  padding: 4px 20px;
  transform: rotate(.6deg);
}

.tour-btn:hover { filter: brightness(1.15); }

.tour-lobby .tour-leave {
  font-size: 17px;
  padding: 2px 14px;
  color: var(--red);
  background: transparent;
  border-color: var(--red);
}

.tour-lobby .tour-leave:hover {
  color: var(--paper);
  background: var(--red);
  filter: none;
}

/* Roster side panel: the server roster; the local player's row is accented, offline rows struck. */
.tour-roster {
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  width: 186px;
  min-height: 0;
  padding-left: 12px;
  border-left: 2px solid var(--ink);
}

.tour-roster-head {
  display: flex;
  gap: 6px;
  align-items: baseline;
  padding-bottom: 2px;
  margin-bottom: 3px;
  border-bottom: 2px solid var(--ink);
  font-size: 15px;
}

.tour-roster-title { white-space: nowrap; }

.tour-roster-hint {
  font-size: 11px;
  opacity: .55;
  white-space: nowrap;
}

.tour-roster-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1px;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.tour-rrow {
  display: flex;
  gap: 6px;
  align-items: baseline;
  padding: 1px 4px;
  font-size: 16px;
  border-bottom: 1px dashed rgba(26, 48, 192, .25);
}

.tour-rrow:last-child { border-bottom: 0; }

.tour-rrank {
  width: 18px;
  flex: 0 0 auto;
  font-size: 12px;
  text-align: right;
  opacity: .6;
}

.tour-rname {
  flex: 1;
  max-width: 118px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.tour-rtag {
  flex: 0 0 auto;
  font-size: 12px;
  opacity: .7;
  white-space: nowrap;
}

.tour-rrow.me {
  background: rgba(26, 48, 192, .1);
  border-radius: 4px;
}

.tour-rrow.me .tour-rname { font-weight: bold; }
.tour-rrow.me .tour-rtag { color: var(--red); opacity: 1; }

.tour-rrow.off { opacity: .45; }
.tour-rrow.off .tour-rname { text-decoration: line-through; }
.tour-rrow.off .tour-rtag { color: var(--red); }

.tour-rempty {
  padding: 8px 4px;
  font-size: 15px;
  text-align: center;
  opacity: .6;
}

/* ---------------- IN_PROGRESS match clock ---------------- */

.tour-clock {
  position: absolute;
  left: 50%;
  top: calc(8px + env(safe-area-inset-top));
  box-sizing: border-box;
  width: min(320px, calc(100vw - 24px - env(safe-area-inset-left) - env(safe-area-inset-right)));
  padding: 4px 14px 6px;
  text-align: center;
  transform: translateX(-50%) rotate(-.8deg);
  background: rgba(250, 248, 238, .9);
  border: 2.5px solid var(--ink);
  border-radius: 10px 14px 9px 13px / 13px 9px 14px 10px;
  box-shadow: 4px 4px 0 rgba(26, 48, 192, .14);
  mix-blend-mode: multiply;
}

.tour-clock-row {
  display: flex;
  gap: 10px;
  align-items: baseline;
  justify-content: center;
}

.tour-clock-label {
  font-size: 15px;
  letter-spacing: 1px;
  opacity: .75;
}

.tour-clock-time {
  font-size: 34px;
  line-height: 1;
  color: var(--red);
  font-variant-numeric: tabular-nums;
}

.tour-clock.urgent .tour-clock-time { animation: tour-pulse .8s infinite; }

.tour-clock-bar {
  height: 10px;
  margin-top: 5px;
  overflow: hidden;
  border: 2.5px solid var(--ink);
  border-radius: 6px 8px 5px 7px / 8px 5px 7px 6px;
}

.tour-clock-fill {
  display: block;
  height: 100%;
  width: 100%;
  background: repeating-linear-gradient(-55deg, var(--ink) 0 2.5px, transparent 2.5px 6px);
  transition: width .3s linear;
}

.tour-clock.urgent .tour-clock-bar { border-color: var(--red); }
.tour-clock.urgent .tour-clock-fill { background: repeating-linear-gradient(-55deg, var(--red) 0 2.5px, transparent 2.5px 6px); }

/* ---------------- FINISHED podium ---------------- */

.tour-podium {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(14px + env(safe-area-inset-top))
    calc(12px + env(safe-area-inset-right))
    calc(14px + env(safe-area-inset-bottom))
    calc(12px + env(safe-area-inset-left));
  background: rgba(246, 243, 230, .5);
  overflow-y: auto;
  overscroll-behavior: contain;
  pointer-events: none;
}

.tour-podium-card {
  box-sizing: border-box;
  width: min(560px, 100%);
  max-height: 100%;
  padding: 16px 26px 14px;
  overflow-y: auto;
  overscroll-behavior: contain;
  text-align: center;
  pointer-events: auto;
  background: rgba(250, 248, 238, .96);
  border: 3px solid var(--ink);
  border-radius: 14px 20px 12px 18px / 18px 12px 20px 14px;
  box-shadow: 7px 7px 0 rgba(26, 48, 192, .16);
  transform: rotate(-.5deg);
  animation: tour-pop .25s ease-out;
  scrollbar-width: thin;
}

.tour-podium-title {
  font-size: 44px;
  line-height: 1;
  letter-spacing: 3px;
}

.tour-podium-sub {
  margin: 5px 0 10px;
  font-size: 18px;
  opacity: .75;
}

.tour-board {
  width: 100%;
  border-collapse: collapse;
  font-size: 19px;
  color: var(--ink);
}

.tour-board th {
  padding: 2px 6px;
  font-size: 14px;
  font-weight: normal;
  letter-spacing: 1px;
  text-align: right;
  opacity: .65;
  border-bottom: 2px solid var(--ink);
}

.tour-board td {
  padding: 3px 6px;
  text-align: right;
  border-bottom: 1px dashed rgba(26, 48, 192, .28);
}

.tour-board .tour-c-rank {
  width: 42px;
  text-align: center;
  opacity: .75;
}

.tour-board .tour-c-name {
  max-width: 220px;
  overflow: hidden;
  text-align: left;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.tour-board th.tour-c-rank,
.tour-board th.tour-c-name { text-align: center; }

.tour-board th.tour-c-name { text-align: left; }

/* Top three: pale red wash + bold rank; the local player's row is blue with a red edge. */
.tour-brow.top { background: rgba(208, 32, 48, .06); }
.tour-brow.top .tour-c-rank { font-weight: bold; color: var(--red); opacity: 1; }
.tour-brow.top .tour-c-name { font-weight: bold; }

.tour-brow.me { background: rgba(26, 48, 192, .12); }
.tour-brow.me .tour-c-name { color: var(--red); }

.tour-brow.tour-bempty td {
  padding: 12px;
  text-align: center;
  opacity: .6;
  border-bottom: 0;
}

.tour-podium-foot {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  margin-top: 12px;
}

.tour-podium-hint { font-size: 13px; opacity: .6; }

.tour-podium-foot .tour-btn {
  font-size: 21px;
  padding: 6px 28px;
}

/* Two-stage events: merged preliminary standings + the qualified player's advance action. */
.tour-standings {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 2px dashed rgba(32, 28, 24, .25);
  max-height: 210px;
  overflow-y: auto;
}

.tour-standings-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 1px;
  opacity: .75;
  margin-bottom: 6px;
}

.tour-srow {
  font-size: 15px;
  line-height: 1.55;
  padding: 1px 8px;
  border-radius: 4px;
  opacity: .85;
}

.tour-srow.qualified { color: var(--red, #d02030); font-weight: 700; }
.tour-srow.me { background: rgba(32, 90, 200, .12); }

.tour-btn.tour-advance {
  background: var(--red, #d02030);
  color: #fff;
  border-color: #fff;
}

/* ---------------- animations ---------------- */

@keyframes tour-in {
  from { opacity: 0; transform: translateX(-50%) translateY(-8px) rotate(-.7deg); }
  to { opacity: 1; transform: translateX(-50%) rotate(-.7deg); }
}

@keyframes tour-pop {
  from { opacity: 0; transform: rotate(-.5deg) scale(.94); }
  to { opacity: 1; transform: rotate(-.5deg) scale(1); }
}

@keyframes tour-pulse { 50% { opacity: .35; } }
@keyframes tour-blink { 50% { opacity: .4; } }

@media (prefers-reduced-motion: reduce) {
  .tour-lobby, .tour-podium-card { animation: none; }
  .tour-countdown.urgent, .tour-clock.urgent .tour-clock-time, .tour-status.reconnect { animation: none; }
}

/* ---------------- phones / narrow screens: lobby stacks, roster moves under the card ---------------- */

body.touch .tour-lobby { top: calc(74px + env(safe-area-inset-top)); }

@media (max-width: 900px) {
  .tour-lobby {
    top: calc(48px + env(safe-area-inset-top));
    width: min(92vw, calc(100vw - 20px - env(safe-area-inset-left) - env(safe-area-inset-right)));
    max-height: min(58vh, 400px);
    padding: 10px 12px 10px 14px;
  }

  /* phone layout: roster moves under the join column */
  .tour-lobby-body { flex-direction: column; gap: 8px; }

  .tour-title { font-size: 23px; }
  .tour-countdown { font-size: 27px; }
  .tour-meta { font-size: 14px; }
  .tour-lockhint { font-size: 13px; }
  .tour-lobby-foot { padding-top: 4px; }

  .tour-roster {
    width: 100%;
    max-height: 30vh;
    padding: 6px 0 0;
    border-top: 2px solid var(--ink);
    border-left: 0;
  }

  .tour-rname { max-width: none; }
  .tour-roster-hint { display: none; }

  .tour-clock { width: min(280px, calc(100vw - 20px)); }
  .tour-clock-time { font-size: 29px; }
  .tour-clock-label { font-size: 13px; }

  .tour-podium { padding: 10px 8px; }
  .tour-podium-card { padding: 12px 14px 12px; }
  .tour-podium-title { font-size: 35px; letter-spacing: 2px; }
  .tour-podium-sub { font-size: 16px; }
  .tour-board { font-size: 17px; }
  .tour-board .tour-c-name { max-width: 150px; }
  .tour-podium-foot .tour-btn { font-size: 19px; padding: 5px 22px; }

  .tour-status, .tour-error { max-width: 90vw; padding: 2px 10px; font-size: 15px; }
  .tour-error { top: calc(40px + env(safe-area-inset-top)); }
}

@media (max-width: 640px) {
  .tour-lobby { width: calc(100vw - 16px - env(safe-area-inset-left) - env(safe-area-inset-right)); }
  .tour-title { font-size: 19px; letter-spacing: 0; white-space: normal; }
  .tour-countdown { font-size: 23px; }
  .tour-badge { font-size: 15px; }
  .tour-lobby .tour-leave { font-size: 15px; }

  .tour-podium-title { font-size: 29px; }
  .tour-board { font-size: 15px; }
  .tour-board th { font-size: 12px; }
  .tour-board .tour-c-name { max-width: 108px; }
  .tour-board .tour-c-rank { width: 30px; }
}

/* Short landscape screens: keep the lobby and podium inside the viewport and scroll inside. */
@media (max-height: 480px) {
  .tour-lobby { top: calc(6px + env(safe-area-inset-top)); max-height: 82vh; }
  .tour-podium-card { padding: 10px 16px; }
  .tour-podium-title { font-size: 28px; }
  .tour-podium-sub { margin: 2px 0 6px; }
}

/* ---------------- main-menu join card (tournament screen) ----------------
   main.js builds this card in front of the `online` panel; keep it in the same paper/ink voice. */
.tcard {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(560px, calc(100vw - 32px));
  margin: 2px auto 4px;
  padding: 12px 18px;
  transform: rotate(-.35deg);
  color: var(--ink);
  background: rgba(246, 243, 230, .8);
  border: 2px solid var(--ink);
  border-radius: 10px 14px 9px 13px / 13px 9px 14px 10px;
  box-shadow: 3px 3px 0 rgba(26, 48, 192, .14);
  mix-blend-mode: multiply;
}
.tcard .tl { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.tcard .tl b { font-size: 22px; letter-spacing: .08em; }
.tcard .tl span { font-size: 15px; opacity: .78; }
.tcard .ts { display: flex; justify-content: space-between; gap: 12px; font-size: 15px; opacity: .85; }
.tcard .ts b { font-weight: 600; opacity: 1; }
#tournament .ticketbox { width: min(320px, 62vw); letter-spacing: .06em; text-transform: uppercase; }
@media (max-width: 640px) {
  .tcard { width: calc(100vw - 20px); padding: 9px 12px; }
  .tcard .tl b { font-size: 18px; }
  .tcard .ts { font-size: 13px; }
  #tournament .ticketbox { width: 100%; max-width: 320px; }
}
