:root {
  --sky-a: #bfe6f2;
  --sky-b: #dff4dc;
  --ink: #1f2f1d;
  --panel: rgba(255, 252, 240, 0.88);
  --border: #355a38;
  --accent: #2e6e3e;
  --accent-hover: #245a32;
  --vv-top-inset: 0px;
  --vv-right-inset: 0px;
  --vv-bottom-inset: 0px;
  --vv-left-inset: 0px;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  font-family: 'Trebuchet MS', 'Avenir Next', 'Gill Sans', sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at 20% 18%, var(--sky-a), var(--sky-b) 50%, #acd998 100%);
}

html.join-active,
body.join-active {
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

html.join-active,
body.join-active {
  height: auto;
  min-height: 100%;
}

body.join-active #game-canvas {
  position: fixed;
  inset: 0;
}

body.join-active #ui {
  position: relative;
  min-height: 100dvh;
  display: block;
  padding:
    max(10px, calc(env(safe-area-inset-top, 0px) + 10px))
    10px
    max(16px, calc(env(safe-area-inset-bottom, 0px) + 16px));
  overflow: visible;
  pointer-events: auto;
}

body.join-active {
  touch-action: pan-y;
}

#game-canvas {
  width: 100vw;
  height: 100vh;
  display: block;
}

#ui {
  position: fixed;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 18px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.panel {
  width: min(560px, calc(100vw - 28px));
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 10px 28px rgba(14, 36, 17, 0.2);
  padding: 14px 16px;
  pointer-events: auto;
  backdrop-filter: blur(4px);
}

#join-screen h1 {
  margin: 0;
  letter-spacing: 0.04em;
  line-height: 0.98;
  font-size: clamp(34px, 4.5vw, 46px);
}

#join-screen {
  margin: 0 auto;
  width: min(720px, calc(100vw - 28px));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#join-screen p {
  margin: 0;
}

.join-hero {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.join-kicker {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: #305f36;
}

.join-lead {
  max-width: 52ch;
  font-size: 15px;
  line-height: 1.5;
  color: rgba(31, 47, 29, 0.88);
}

#join-message {
  min-height: 18px;
  font-weight: 700;
  color: #b1482d;
}

#join-form {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.join-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.join-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #355a38;
}

#join-form input {
  flex: 1;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 16px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
}

#join-form input::placeholder {
  color: rgba(31, 47, 29, 0.42);
}

.join-helper {
  font-size: 12px;
  line-height: 1.4;
  color: rgba(31, 47, 29, 0.7);
}

button {
  border: 0;
  border-radius: 10px;
  padding: 10px 13px;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

select {
  border: 1px solid rgba(53, 90, 56, 0.24);
  border-radius: 10px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.82);
  color: var(--ink);
  font: inherit;
}

button:hover {
  background: var(--accent-hover);
}

button:disabled,
#join-form input:disabled {
  cursor: wait;
  opacity: 0.72;
}

#hud {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: min(290px, calc(100vw - 28px));
  padding: 10px 12px;
}

.hud-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.hud-summary {
  flex: 1;
}

#hud-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.hud-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.hud-row.actions {
  margin-top: 2px;
}

#objective-title {
  font-size: 13px;
  line-height: 1.2;
}

#objective-meta {
  font-size: 12px;
  line-height: 1.3;
  opacity: 0.9;
}

#objective-panel {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: min(320px, calc(100vw - 28px));
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 11px 13px;
  background: rgba(255, 252, 240, 0.9);
  border-color: rgba(53, 90, 56, 0.88);
  box-shadow: 0 12px 28px rgba(14, 36, 17, 0.18);
  z-index: 5;
}

#tips-bubble {
  position: fixed;
  top: max(16px, env(safe-area-inset-top, 0px) + 10px);
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  width: min(520px, calc(100vw - 36px));
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(255, 251, 239, 0.9);
  border: 1px solid rgba(53, 90, 56, 0.2);
  box-shadow: 0 10px 24px rgba(14, 36, 17, 0.14);
  color: #2a412c;
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  backdrop-filter: blur(6px);
  transition: opacity 0.45s ease, transform 0.45s ease;
  z-index: 6;
}

#tips-bubble.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.objective-panel-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #355a38;
}

#objective-panel #objective-title {
  font-size: 14px;
}

#objective-panel #objective-meta {
  font-size: 12px;
  line-height: 1.4;
  opacity: 0.92;
}

#hud button {
  padding: 7px 10px;
  font-size: 13px;
}

.hud-toggle-btn {
  min-width: 30px;
  padding: 4px 7px;
  line-height: 1;
  font-size: 18px;
  border-radius: 999px;
}

#hud button.active {
  background: var(--accent-hover);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
}

#hud.minimized {
  width: min(214px, calc(100vw - 28px));
  gap: 0;
}

#hud.minimized #hud-body {
  display: none;
}

#hud.minimized .hud-toggle-btn {
  transform: rotate(-90deg);
}

#hud #message {
  min-height: 16px;
  font-size: 13px;
}

#debug-readout {
  min-height: 16px;
  font-size: 12px;
  line-height: 1.35;
  color: #294030;
  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', monospace;
}

.hint {
  font-size: 13px;
  opacity: 0.85;
  margin-top: 10px;
}

.controls-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.38), rgba(239, 247, 232, 0.62));
  border: 1px solid rgba(53, 90, 56, 0.18);
}

.quality-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.42), rgba(236, 244, 230, 0.7));
  border: 1px solid rgba(53, 90, 56, 0.18);
}

.quality-card-header {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  align-items: baseline;
}

.quality-card-header strong {
  font-size: 14px;
}

.quality-card-header span,
.quality-status {
  font-size: 12px;
  line-height: 1.4;
  color: rgba(31, 47, 29, 0.72);
}

.quality-card-row,
.quality-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.quality-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #355a38;
}

.quality-row select,
.quality-card-row select {
  min-width: 140px;
}

.controls-header {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  align-items: baseline;
}

.controls-header strong {
  font-size: 14px;
}

.controls-header span {
  font-size: 12px;
  line-height: 1.4;
  color: rgba(31, 47, 29, 0.72);
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(255, 252, 240, 0.72);
  border: 1px solid rgba(53, 90, 56, 0.12);
}

.control-group h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #355a38;
}

.control-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  font-size: 13px;
  line-height: 1.35;
}

.control-keys {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 5px;
}

.control-note {
  font-size: 12px;
  color: rgba(31, 47, 29, 0.72);
  text-align: right;
}

kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  padding: 4px 7px;
  border-radius: 8px;
  border: 1px solid rgba(53, 90, 56, 0.22);
  background: rgba(255, 255, 255, 0.82);
  box-shadow: inset 0 -1px 0 rgba(53, 90, 56, 0.12);
  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', monospace;
  font-size: 12px;
  font-weight: 700;
  color: #27402c;
}

#mobile-controls {
  position: fixed;
  inset: 0;
  display: none;
  overflow: visible;
  pointer-events: none;
  z-index: 6;
}

#mobile-controls:not(.hidden) {
  display: block;
}

#touch-move-wrap {
  position: absolute;
  left: 50%;
  bottom: max(26px, calc(env(safe-area-inset-bottom, 0px) + 22px), calc(var(--vv-bottom-inset) + 22px));
  width: clamp(120px, 30vw, 164px);
  height: clamp(120px, 30vw, 164px);
  padding: 8px;
  box-sizing: border-box;
  overflow: visible;
  pointer-events: none;
  transform: translateX(-50%);
}

#touch-move-base {
  width: 100%;
  height: 100%;
  border: 2px solid rgba(35, 60, 30, 0.55);
  border-radius: 50%;
  background: rgba(250, 248, 238, 0.38);
  position: relative;
  touch-action: none;
  box-sizing: border-box;
}

#touch-move-thumb {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(34, 85, 52, 0.62);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

#jump-btn {
  position: absolute;
  right: max(14px, calc(env(safe-area-inset-right, 0px) + 10px), calc(var(--vv-right-inset) + 10px));
  bottom: max(28px, calc(env(safe-area-inset-bottom, 0px) + 24px), calc(var(--vv-bottom-inset) + 24px));
  width: clamp(60px, 17vw, 72px);
  height: clamp(60px, 17vw, 72px);
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.65);
  font-weight: 700;
  background: rgba(36, 86, 36, 0.85);
  color: #fff;
  font-size: 13px;
  letter-spacing: 0.03em;
  pointer-events: auto;
  touch-action: none;
  backdrop-filter: blur(2px);
}

#jump-btn:active {
  background: rgba(25, 68, 25, 0.95);
}

#mobile-controls.hidden {
  display: none;
}

#fps-overlay {
  position: fixed;
  top: max(14px, env(safe-area-inset-top, 0px) + 8px);
  right: max(14px, env(safe-area-inset-right, 0px) + 8px);
  min-width: 70px;
  padding: 7px 10px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: 999px;
  background: rgba(16, 29, 19, 0.74);
  color: #f4f8ec;
  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', monospace;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.45;
  text-align: left;
  white-space: pre-line;
  pointer-events: none;
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.18);
  z-index: 7;
}

#fps-overlay.mobile-simple {
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: #ffffff;
  font-family: 'Trebuchet MS', 'Avenir Next', 'Gill Sans', sans-serif;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  text-align: right;
  white-space: nowrap;
  backdrop-filter: none;
  box-shadow: none;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

#players-overlay {
  position: fixed;
  top: max(54px, env(safe-area-inset-top, 0px) + 48px);
  right: max(14px, env(safe-area-inset-right, 0px) + 8px);
  min-width: 210px;
  max-width: min(280px, calc(100vw - 28px));
  padding: 10px 12px 11px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: 14px;
  background: rgba(16, 29, 19, 0.82);
  color: #f4f8ec;
  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', monospace;
  font-size: 12px;
  line-height: 1.35;
  pointer-events: none;
  backdrop-filter: blur(6px);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.22);
  z-index: 7;
}

#connection-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(14, 24, 15, 0.22);
  pointer-events: none;
  z-index: 8;
}

.connection-overlay-card {
  width: min(420px, calc(100vw - 40px));
  padding: 18px 20px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  background: rgba(19, 31, 21, 0.84);
  color: #f4f8ec;
  text-align: center;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(8px);
}

.connection-overlay-card strong {
  display: block;
  margin-bottom: 6px;
  font-size: 18px;
  letter-spacing: 0.02em;
}

.connection-overlay-card div {
  font-size: 14px;
  line-height: 1.45;
  color: rgba(244, 248, 236, 0.9);
}

#level-change-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.26s ease;
  z-index: 7;
}

#level-change-overlay.visible {
  opacity: 1;
}

.level-change-card {
  width: min(380px, calc(100vw - 40px));
  padding: 20px 22px;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.74);
  background: rgba(19, 31, 21, 0.78);
  color: #f4f8ec;
  text-align: center;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(10px);
}

.level-change-label {
  margin-bottom: 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: rgba(215, 232, 198, 0.76);
}

#level-change-title {
  display: block;
  margin-bottom: 6px;
  font-size: 28px;
  line-height: 1;
  letter-spacing: 0.02em;
}

#level-change-subtitle {
  font-size: 14px;
  line-height: 1.45;
  color: rgba(244, 248, 236, 0.9);
}

#chat-overlay {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom, 0px) + 10px);
  transform: translateX(-50%);
  width: min(560px, calc(100vw - 28px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  z-index: 8;
}

#chat-messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: stretch;
}

.chat-message {
  align-self: stretch;
  padding: 9px 12px;
  border-radius: 12px;
  background: rgba(18, 29, 19, 0.8);
  color: #f4f8ec;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.18);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  opacity: 1;
  transition: opacity 1s ease;
}

.chat-message.fading {
  opacity: 0;
}

.chat-author {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(215, 232, 198, 0.88);
  margin-bottom: 3px;
}

.chat-body {
  font-size: 14px;
  line-height: 1.4;
  color: #f7fbef;
  word-break: break-word;
}

#chat-form {
  pointer-events: auto;
}

#chat-input {
  width: 100%;
  box-sizing: border-box;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.44);
  background: rgba(14, 24, 15, 0.86);
  color: #f7fbef;
  font-size: 15px;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.18);
  backdrop-filter: blur(8px);
}

#chat-input::placeholder {
  color: rgba(247, 251, 239, 0.58);
}

#controls-overlay {
  position: fixed;
  inset: 0;
  z-index: 28;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(16px, calc(env(safe-area-inset-top, 0px) + 16px))
    16px
    max(16px, calc(env(safe-area-inset-bottom, 0px) + 16px));
  background: rgba(12, 24, 15, 0.48);
  backdrop-filter: blur(10px);
  pointer-events: auto;
}

.controls-overlay-card {
  width: min(720px, calc(100vw - 32px));
  max-height: min(78vh, 860px);
  overflow-y: auto;
}

.controls-overlay-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.controls-overlay-kicker {
  margin-bottom: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: rgba(48, 95, 54, 0.86);
}

#controls-overlay-title {
  display: block;
  font-size: 28px;
  line-height: 1;
}

.players-overlay-header {
  margin-bottom: 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.78;
}

.players-overlay-table {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px 10px;
  align-items: center;
}

.players-overlay-head {
  font-size: 11px;
  opacity: 0.72;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.players-overlay-body {
  display: contents;
}

.players-overlay-cell {
  min-width: 0;
}

.players-overlay-cell.name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.players-overlay-cell.ping {
  text-align: right;
}

#message {
  min-height: 18px;
  font-weight: 700;
  color: #b1482d;
}

.hidden {
  display: none !important;
}

@media (max-width: 700px) {
  #ui {
    align-items: flex-start;
    justify-content: center;
    padding:
      max(10px, calc(env(safe-area-inset-top, 0px) + 10px))
      10px
      max(10px, calc(env(safe-area-inset-bottom, 0px) + 10px));
  }

  body.join-active #ui {
    padding:
      max(10px, calc(env(safe-area-inset-top, 0px) + 10px))
      10px
      max(24px, calc(env(safe-area-inset-bottom, 0px) + 24px));
  }

  .panel {
    width: calc(100vw - 20px);
    border-radius: 12px;
    padding: 10px 12px;
  }

  #join-screen {
    margin: 0 auto;
    max-width: 100%;
  }

  #join-form {
    flex-direction: column;
  }

  .controls-grid {
    grid-template-columns: 1fr;
  }

  .controls-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .controls-overlay-card {
    width: calc(100vw - 20px);
    max-height: calc(100dvh - 20px);
  }

  .controls-overlay-header {
    flex-direction: column;
    align-items: stretch;
  }

  #objective-panel {
    right: 10px;
    bottom: max(96px, env(safe-area-inset-bottom, 0px) + 92px);
    width: min(280px, calc(100vw - 20px));
    padding: 10px 12px;
  }

  #tips-bubble {
    top: max(10px, env(safe-area-inset-top, 0px) + 6px);
    width: calc(100vw - 20px);
    border-radius: 18px;
    font-size: 12px;
  }

  #players-overlay {
    top: max(48px, env(safe-area-inset-top, 0px) + 42px);
    min-width: 180px;
    max-width: min(240px, calc(100vw - 20px));
    padding: 8px 10px 9px;
    font-size: 11px;
  }

  #touch-move-wrap {
    left: 50%;
    bottom: max(34px, calc(env(safe-area-inset-bottom, 0px) + 30px), calc(var(--vv-bottom-inset) + 30px));
    width: clamp(118px, 32vw, 150px);
    height: clamp(118px, 32vw, 150px);
  }

  #jump-btn {
    right: max(12px, calc(env(safe-area-inset-right, 0px) + 8px), calc(var(--vv-right-inset) + 8px));
    bottom: max(36px, calc(env(safe-area-inset-bottom, 0px) + 32px), calc(var(--vv-bottom-inset) + 32px));
  }
}
