:root {
  --bb-ink: #10233b;
  --bb-cream: #fff8df;
  --bb-gold: #ffc857;
  --bb-coral: #ef6f61;
  --bb-teal: #168f9d;
  --bb-glass: rgba(255, 255, 255, 0.88);
}

html.balloon-game-active-root,
body.balloon-game-active {
  width: 100% !important;
  height: 100% !important;
  min-height: 100% !important;
  max-height: 100% !important;
  overflow: hidden !important;
  overflow-x: hidden !important;
  overflow-y: hidden !important;
  overscroll-behavior: none !important;
  background: #74c9ee !important;
}

body.balloon-game-active {
  position: fixed !important;
  inset: 0 !important;
}

body.balloon-game-active.forwardBg,
body.balloon-game-active.reverseBg {
  background: #74c9ee !important;
}

body.balloon-game-active > .wrap,
body.balloon-game-active > #training-lang-toggle,
body.balloon-game-active > #localMediaSection {
  display: none !important;
}

body.balloon-game-active > #backBtn,
body.balloon-game-active > #helpDlg,
body.balloon-game-active > #sessionsDlg {
  display: none !important;
}

#balloonGame {
  position: fixed !important;
  inset: 0 !important;
  z-index: 100000;
  display: grid;
  grid-template-rows: auto 1fr;
  width: 100% !important;
  height: 100dvh !important;
  min-height: 0 !important;
  max-height: 100dvh !important;
  color: var(--bb-ink);
  background: #8ad6f4;
  font-family: Arial, Tahoma, "Segoe UI", "Noto Sans", sans-serif;
  overflow: hidden !important;
  overflow-x: hidden !important;
  overflow-y: hidden !important;
  user-select: none;
  -webkit-user-select: none;
}

.bb-topbar {
  position: relative;
  z-index: 4;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  min-height: 62px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: rgba(255, 250, 231, 0.94);
  border-bottom: 1px solid rgba(16, 35, 59, 0.14);
  box-shadow: 0 4px 18px rgba(16, 35, 59, 0.12);
}

.bb-top-left,
.bb-top-right,
.bb-mode,
.bb-connection,
.bb-game-stats {
  display: flex;
  align-items: center;
  gap: 8px;
}

.bb-top-right { justify-content: flex-end; }
.bb-mode { justify-content: center; }

.bb-btn,
.bb-mode-btn {
  min-height: 42px;
  border: 1px solid rgba(16, 35, 59, 0.18);
  border-radius: 8px;
  padding: 8px 13px;
  color: var(--bb-ink);
  background: #fffdf5;
  font: 700 14px/1 Arial, Tahoma, sans-serif;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(16, 35, 59, 0.08);
}

.bb-btn:hover,
.bb-btn:focus-visible,
.bb-mode-btn:hover,
.bb-mode-btn:focus-visible {
  outline: 3px solid rgba(22, 143, 157, 0.22);
  outline-offset: 1px;
}

.bb-finish {
  color: #fff;
  background: #b63a31;
  border-color: #922d27;
}

.bb-mode {
  padding: 4px;
  border: 1px solid rgba(16, 35, 59, 0.13);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.8);
}

.bb-mode-btn {
  min-width: 92px;
  box-shadow: none;
  background: transparent;
}

.bb-mode-btn.active {
  color: #fff;
  background: var(--bb-teal);
  border-color: var(--bb-teal);
}

.bb-connection {
  min-height: 38px;
  padding: 4px 10px;
  border: 1px solid rgba(16, 35, 59, 0.13);
  border-radius: 999px;
  background: #fffdf5;
  font-weight: 800;
  white-space: nowrap;
}

.bb-led {
  width: 13px;
  height: 13px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #d9273e;
  box-shadow: 0 0 0 4px rgba(217, 39, 62, 0.14), 0 0 12px rgba(217, 39, 62, 0.45);
}

.bb-connection.connected .bb-led {
  background: #20b15a;
  box-shadow: 0 0 0 4px rgba(32, 177, 90, 0.14), 0 0 12px rgba(32, 177, 90, 0.45);
}

#balloonGameLanguageDock {
  display: flex;
  align-items: center;
}

#balloonGameLanguageDock #training-lang-toggle {
  position: static !important;
  inset: auto !important;
  visibility: visible !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  box-shadow: none !important;
  background: transparent !important;
}

#balloonGameLanguageDock #training-language-select {
  min-height: 42px;
}

.bb-stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  isolation: isolate;
  background: #78cef0 url("assets/balloon-biofeedback/sky-valley.png") center / cover no-repeat;
}

#balloonCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  direction: ltr !important;
}

.bb-game-stats {
  position: absolute;
  z-index: 3;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  min-height: 42px;
  padding: 7px 15px;
  border: 1px solid rgba(255, 255, 255, 0.62);
  border-radius: 999px;
  color: #fff;
  background: rgba(16, 35, 59, 0.72);
  box-shadow: 0 7px 18px rgba(16, 35, 59, 0.18);
  backdrop-filter: blur(7px);
  font-weight: 900;
  white-space: nowrap;
}

.bb-gauges {
  position: absolute;
  z-index: 3;
  top: 14px;
  right: max(14px, env(safe-area-inset-right));
  display: flex;
  gap: 10px;
  direction: ltr;
}

.bb-gauge {
  --gauge-low: #20b15a;
  --gauge-mid: #ffc857;
  --gauge-high: #ef6f61;
  box-sizing: border-box;
  position: relative;
  width: 110px;
  height: 110px;
  padding: 9px;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  background: var(--bb-glass);
  box-shadow: 0 7px 18px rgba(16, 35, 59, 0.14);
}

.bb-gauge-hrv {
  --gauge-low: #ef6f61;
  --gauge-high: #20b15a;
}

.bb-gauge-label {
  position: absolute;
  z-index: 3;
  inset: 56px 0 auto;
  display: grid;
  justify-items: center;
  gap: 0;
  color: #18324d;
  font-size: 11px;
  font-weight: 900;
  line-height: 1;
}

.bb-gauge-value {
  color: var(--bb-teal);
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

.bb-gauge-track {
  position: absolute;
  inset: 8px;
  border-radius: 50%;
  background: conic-gradient(from 225deg, var(--gauge-low) 0deg 90deg, var(--gauge-mid) 90deg 180deg, var(--gauge-high) 180deg 270deg, rgba(209, 222, 226, 0.42) 270deg 360deg);
  box-shadow: inset 0 0 0 1px rgba(16, 35, 59, 0.12);
}

.bb-gauge-track::before {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 50%;
  background: #f8fcfd;
  box-shadow: inset 0 2px 7px rgba(16, 35, 59, 0.13);
}

.bb-gauge-track::after {
  content: "0                                      100";
  position: absolute;
  inset: auto -1px 12px;
  color: #496075;
  font-size: 8px;
  font-weight: 800;
  white-space: pre;
}

.bb-gauge-needle {
  position: absolute;
  z-index: 2;
  left: calc(50% - 2px);
  bottom: 50%;
  width: 4px;
  height: 34px;
  transform: rotate(var(--gauge-angle));
  transform-origin: 50% 100%;
  border-radius: 4px 4px 2px 2px;
  background: #10233b;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 220ms ease-out;
}

.bb-gauge-hub {
  position: absolute;
  z-index: 4;
  top: 50%;
  left: 50%;
  width: 11px;
  height: 11px;
  transform: translate(-50%, -50%);
  border: 2px solid #eef7f7;
  border-radius: 50%;
  background: #10233b;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}

.bb-center-card {
  box-sizing: border-box;
  position: absolute;
  z-index: 5;
  top: 50%;
  left: 50%;
  width: min(520px, calc(100% - 32px));
  transform: translate(-50%, -50%);
  padding: 25px 24px;
  border: 2px solid rgba(255, 255, 255, 0.76);
  border-radius: 8px;
  color: var(--bb-ink);
  background: rgba(255, 250, 231, 0.96);
  box-shadow: 0 20px 55px rgba(16, 35, 59, 0.3);
  text-align: center;
}

.bb-center-card.hidden { display: none; }
.bb-center-card h1 { margin: 0 0 8px; font-size: clamp(26px, 4vw, 42px); }
.bb-center-card p { margin: 0 auto 18px; max-width: 430px; font-size: clamp(15px, 2vw, 19px); line-height: 1.45; }

.bb-player-field {
  display: grid;
  gap: 6px;
  max-width: 300px;
  margin: 0 auto 18px;
  text-align: start;
  font-size: 14px;
  font-weight: 900;
}

.bb-player-field input {
  box-sizing: border-box;
  width: 100%;
  min-height: 48px;
  border: 2px solid rgba(16, 35, 59, 0.22);
  border-radius: 8px;
  padding: 9px 12px;
  color: var(--bb-ink);
  background: #fff;
  font: 800 18px/1.2 Arial, Tahoma, sans-serif;
  text-align: start;
}

.bb-player-field input:focus {
  border-color: var(--bb-teal);
  outline: 3px solid rgba(22, 143, 157, 0.2);
}

.bb-primary {
  min-width: 190px;
  min-height: 52px;
  border: 0;
  border-radius: 8px;
  padding: 12px 22px;
  color: #10233b;
  background: linear-gradient(180deg, #ffd96f, #f4a72b);
  box-shadow: 0 8px 0 #bd6a18, 0 12px 22px rgba(90, 45, 7, 0.2);
  font: 900 18px/1 Arial, Tahoma, sans-serif;
  cursor: pointer;
}

.bb-primary:active { transform: translateY(5px); box-shadow: 0 3px 0 #bd6a18; }
.bb-primary:disabled { filter: grayscale(0.75); opacity: 0.62; cursor: not-allowed; }

.bb-calibration {
  position: absolute;
  z-index: 6;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(37, 104, 147, 0.33);
  backdrop-filter: blur(3px);
}

.bb-calibration.show { display: grid; }
.bb-calibration-box {
  width: min(520px, calc(100% - 32px));
  padding: 25px;
  border: 2px solid rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  background: rgba(255, 250, 231, 0.97);
  text-align: center;
  box-shadow: 0 20px 55px rgba(16, 35, 59, 0.28);
}

.bb-calibration-title { margin: 0 0 6px; font-size: clamp(25px, 5vw, 44px); }
.bb-countdown { color: var(--bb-coral); font-size: clamp(54px, 12vw, 102px); font-weight: 900; line-height: 1; }
.bb-calibration-note { margin-top: 8px; font-size: 16px; font-weight: 700; }

.bb-message {
  position: absolute;
  z-index: 4;
  bottom: max(18px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(620px, calc(100% - 30px));
  padding: 9px 16px;
  border-radius: 999px;
  color: #fff;
  background: rgba(16, 35, 59, 0.74);
  text-align: center;
  font-size: 15px;
  font-weight: 800;
  opacity: 0;
  transition: opacity 180ms ease;
  pointer-events: none;
}

.bb-message.show { opacity: 1; }

.bb-dialog {
  width: min(620px, calc(100% - 24px));
  max-height: min(78dvh, 700px);
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 8px;
  color: var(--bb-ink);
  background: #fffdf5;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.34);
}

.bb-dialog::backdrop { background: rgba(5, 20, 36, 0.68); backdrop-filter: blur(4px); }
.bb-dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 16px 18px; background: #e6f7f5; border-bottom: 1px solid #c5e4e0; }
.bb-dialog-head h2 { margin: 0; font-size: 21px; }
.bb-dialog-body { padding: 18px; overflow: auto; max-height: calc(78dvh - 70px); line-height: 1.55; }
.bb-dialog-body ol { margin: 0; padding-inline-start: 24px; }
.bb-dialog-body li + li { margin-top: 9px; }
.bb-help-dialog { width: min(780px, calc(100vw - 24px)); }
.bb-help-sections { display: grid; gap: 10px; }
.bb-help-sections section { padding: 12px 14px; border: 1px solid #d5e6e3; border-radius: 7px; background: #f8fcfb; }
.bb-help-sections h3 { margin: 0; color: #174d5a; font-size: 16px; }
.bb-help-sections p { margin: 5px 0 0; color: #23384a; }

.bb-results-dialog { width: min(900px, calc(100% - 24px)); }
.bb-results-body { display: grid; gap: 22px; }
.bb-results-body h3 { margin: 0 0 9px; font-size: 18px; }
.bb-results-body section { min-width: 0; }
.bb-results-body section > div { overflow-x: auto; }
.bb-results-table { width: 100%; border-collapse: collapse; background: #fff; font-size: 14px; }
.bb-results-table th, .bb-results-table td { padding: 9px 10px; border-bottom: 1px solid #dce7e9; text-align: start; white-space: nowrap; }
.bb-results-table th { color: #fff; background: #315d73; font-weight: 900; }
.bb-results-table tbody tr:nth-child(even) { background: #f3f8f8; }
.bb-results-table .bb-rank-first { background: #fff1a8; font-weight: 900; }
.bb-results-table .bb-rank-last { background: #ffe1dc; }
.bb-empty { margin: 0; padding: 18px; border: 1px dashed #9cb4bc; border-radius: 8px; background: #f6fbfb; text-align: center; }

@media (max-width: 760px) {
  .bb-topbar {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "left mode language" "connection connection connection";
    padding-bottom: 6px;
  }
  .bb-top-left { grid-area: left; }
  .bb-mode { grid-area: mode; }
  .bb-top-right { display: contents; }
  #balloonGameLanguageDock { grid-area: language; justify-self: end; }
  .bb-connection { grid-area: connection; justify-self: center; min-height: 30px; padding-block: 2px; }
  .bb-btn { min-width: 44px; padding-inline: 10px; }
  .bb-btn-text { display: none; }
  .bb-mode-btn { min-width: 72px; min-height: 38px; padding: 6px 9px; }
  .bb-gauges { top: 8px; right: 8px; gap: 5px; }
  .bb-gauge { width: 86px; height: 86px; padding: 5px; }
  .bb-gauge-track { inset: 6px; }
  .bb-gauge-track::before { inset: 6px; }
  .bb-gauge-track::after { inset: auto -1px 8px; font-size: 6px; }
  .bb-gauge-label { inset: 44px 0 auto; font-size: 9px; }
  .bb-gauge-value { font-size: 15px; }
  .bb-gauge-needle { height: 26px; }
  .bb-gauge-hub { width: 9px; height: 9px; }
  .bb-game-stats { top: auto; bottom: max(12px, env(safe-area-inset-bottom)); font-size: 13px; min-height: 35px; padding: 5px 11px; }
  .bb-message { bottom: 58px; font-size: 13px; }
  .bb-center-card { padding: 20px 18px; }
  .bb-player-field { margin-bottom: 14px; }
  .bb-player-field input { min-height: 44px; font-size: 16px; }
  .bb-results-table { font-size: 12px; }
  .bb-results-table th, .bb-results-table td { padding: 7px 8px; }
}

@media (max-width: 520px) {
  #balloonGameLanguageDock #training-language-select { max-width: 92px !important; min-height: 38px; padding-inline: 4px !important; }
  .bb-topbar { gap: 5px; }
  .bb-top-left, .bb-top-right { gap: 4px; }
  .bb-mode { gap: 2px; }
  .bb-mode-btn { min-width: 60px; font-size: 12px; }
  .bb-gauges { flex-direction: column; }
  .bb-gauge { width: 76px; height: 76px; }
  .bb-gauge-label { inset: 38px 0 auto; }
  .bb-gauge-needle { height: 23px; }
}

@media (max-height: 500px) and (orientation: landscape) {
  .bb-topbar { min-height: 48px; padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .bb-btn, .bb-mode-btn, #balloonGameLanguageDock #training-language-select { min-height: 36px; }
  .bb-connection { min-height: 32px; }
  .bb-gauge { width: 76px; height: 76px; padding: 5px; }
  .bb-center-card { padding: 16px; }
  .bb-center-card h1 { font-size: 28px; }
  .bb-center-card p { margin-bottom: 12px; font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  .bb-gauge-needle { transition: none; }
}
