:root {
  --map-bg: #1a1a1a;
  --panel: #242424;
  --panel-edge: #333;
  --text: #e8e8e8;
  --muted: #999;
  --money: #5fd068;
  --hp: #4a9fd4;
  --hp-bg: #1e2a36;
  --ally: #2a9d8f;
  --enemy: #e05a4f;
  --ct: #5a9fd4;
  --t: #d4a832;
  --scheme: #826bd4;
  --topbar-h: 48px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--map-bg);
  color: var(--text);
  font: 13px/1.35 "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  background: var(--map-bg);
  padding:
    var(--safe-top)
    var(--safe-right)
    var(--safe-bottom)
    var(--safe-left);
}

body.fs-active {
  overflow: hidden;
  width: 100%;
  height: 100%;
  height: 100dvh;
  margin: 0;
  background: var(--map-bg);
}

html.fs-active {
  overflow: hidden;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background: var(--map-bg);
}

#app.pseudo-fullscreen {
  position: fixed;
  inset: 0;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  z-index: 2147483647;
  margin: 0;
  padding:
    var(--safe-top)
    var(--safe-right)
    var(--safe-bottom)
    var(--safe-left);
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  min-height: var(--topbar-h);
  padding: 8px 14px;
  background: #141414;
  border-bottom: 1px solid #2a2a2a;
  flex-shrink: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-self: start;
  min-width: 0;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-self: end;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-watermark {
  justify-self: center;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.02em;
  white-space: nowrap;
  pointer-events: auto;
  user-select: none;
}

.topbar-watermark a {
  color: var(--scheme);
  text-decoration: none;
  font-weight: 600;
}

.topbar-watermark a:hover {
  text-decoration: underline;
  color: #9d8ae8;
}

.topbar-title {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  flex-shrink: 0;
}

.follow-label {
  color: var(--muted);
  font-size: 12px;
  flex-shrink: 0;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.dot.live { background: #2ecc71; }
.dot.wait { background: #d4a017; }
.dot.off { background: var(--enemy); }

#conn-label {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.btn, select {
  background: #2a2a2a;
  border: 1px solid #3a3a3a;
  color: var(--text);
  border-radius: 6px;
  font: inherit;
  font-size: 12px;
  padding: 6px 10px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover, select:hover { border-color: var(--scheme); }
.btn:active { transform: scale(0.98); }
.btn.small { padding: 4px 8px; }

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  min-height: 36px;
  padding: 6px;
}
.btn-icon svg { display: block; }
.btn-icon .hidden { display: none; }

select {
  min-width: 120px;
  max-width: 180px;
  appearance: none;
  padding-right: 24px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='5'%3E%3Cpath fill='%23999' d='M0 0l4 5 4-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

.workspace {
  flex: 1;
  display: flex;
  background: var(--map-bg);
  min-height: 0;
}

.board-col {
  width: 208px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 8px;
  background: var(--map-bg);
  min-height: 0;
}
.board-col.hidden { display: none; }

.slots {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.slot {
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 5px;
  padding: 8px;
  min-height: 76px;
  display: grid;
  grid-template-columns: 46px 1fr;
  gap: 8px;
  transition: opacity 0.15s;
}
.slot.hidden { display: none; }
.slot.dead { opacity: 0.42; }
.slot.enemy-side {
  grid-template-columns: 1fr 46px;
}
.slot.enemy-side .slot-avatar-wrap { order: 2; }
.slot.enemy-side .slot-body { order: 1; }
.slot.enemy-side.ct { border-left: 3px solid var(--ct); }
.slot.enemy-side.t { border-left: 3px solid var(--t); }

.slot-avatar-wrap {
  position: relative;
  width: 46px;
  height: 54px;
  border-radius: 4px;
  overflow: hidden;
  background: #333;
  border: 1px solid var(--panel-edge);
}
.slot-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.slot-avatar.hidden { display: none; }
.slot-initials {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.35);
}
.slot-initials.hidden { display: none; }

.slot-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.slot-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 6px;
}

.slot-name {
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.slot-money {
  color: var(--money);
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

.slot-loadout {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  min-height: 18px;
}
.slot-loadout img {
  width: 17px;
  height: 17px;
  object-fit: contain;
  filter: brightness(1.1);
}

.slot-hp {
  position: relative;
  height: 15px;
  background: var(--hp-bg);
  border-radius: 3px;
  overflow: hidden;
}
.slot-hp-fill {
  height: 100%;
  background: var(--hp);
  border-radius: 3px;
  transition: width 0.08s linear;
}
.slot-hp-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.45);
}

.map-panel {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background: var(--map-bg);
  min-height: 0;
}

#radar-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.map-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #888;
  font-size: 14px;
  letter-spacing: 0.02em;
  pointer-events: none;
  padding: 16px;
  text-align: center;
}
.map-hint.hidden { display: none; }

.idle-screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--map-bg);
  color: #9a9a9a;
  font-size: 15px;
  letter-spacing: 0.04em;
  z-index: 2;
  pointer-events: none;
  padding: 16px;
  text-align: center;
}
.idle-screen #idle-message {
  max-width: 20rem;
  line-height: 1.5;
}
.idle-screen.hidden { display: none; }

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 19;
}
.drawer-backdrop.hidden { display: none; }

.drawer {
  position: fixed;
  top: calc(var(--topbar-h) + var(--safe-top) + 6px);
  right: calc(14px + var(--safe-right));
  width: min(280px, calc(100vw - 28px - var(--safe-left) - var(--safe-right)));
  max-height: calc(100dvh - var(--topbar-h) - var(--safe-top) - var(--safe-bottom) - 24px);
  overflow-y: auto;
  background: #242424;
  border: 1px solid #333;
  border-radius: 8px;
  z-index: 20;
  box-shadow: 0 10px 28px rgba(0,0,0,0.45);
  color: var(--text);
  -webkit-overflow-scrolling: touch;
}
.drawer.hidden { display: none; }

.drawer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid #333;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  position: sticky;
  top: 0;
  background: #242424;
  z-index: 1;
}

.drawer-body { padding: 12px; }

.opt {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  cursor: pointer;
  font-size: 13px;
  min-height: 36px;
}
.opt input {
  accent-color: var(--scheme);
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.zoom-opt {
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  margin-top: 8px;
  min-height: auto;
}
.zoom-opt input[type=range] {
  width: 100%;
  accent-color: var(--scheme);
  min-height: 28px;
}
.zoom-opt span:last-child { text-align: right; color: var(--muted); font-size: 11px; }

/* Tablet — narrower side panels */
@media (max-width: 900px) {
  .board-col { width: 170px; }
  .topbar-watermark { font-size: 11px; }
}

/* Phone portrait — map first, compact player strips */
@media (max-width: 768px) {
  :root { --topbar-h: auto; }

  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    padding: 8px 10px;
    padding-left: max(10px, var(--safe-left));
    padding-right: max(10px, var(--safe-right));
  }

  .topbar-left {
    grid-column: 1;
    grid-row: 1;
  }

  .topbar-watermark {
    display: none;
  }

  .topbar-right {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: stretch;
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    align-items: center;
    gap: 8px;
  }

  .follow-label {
    display: none;
  }

  #follow-select {
    min-width: 0;
    max-width: none;
    width: 100%;
    min-height: 40px;
    font-size: 13px;
  }

  .btn, .btn-icon {
    min-height: 40px;
  }

  #btn-settings {
    padding-left: 12px;
    padding-right: 12px;
  }

  .workspace {
    flex-direction: column;
  }

  .map-panel {
    order: 1;
    flex: 1 1 auto;
    min-height: 45vh;
    min-height: 45dvh;
  }

  #ally-board {
    order: 0;
    width: 100%;
    max-height: 108px;
    padding: 6px 8px 4px;
  }

  #enemy-board {
    order: 2;
    width: 100%;
    max-height: 108px;
    padding: 4px 8px 6px;
  }

  .slots {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 6px;
    scroll-snap-type: x proximity;
    padding-bottom: 2px;
  }

  .slot {
    min-width: 168px;
    max-width: 200px;
    flex-shrink: 0;
    scroll-snap-align: start;
    min-height: 68px;
    padding: 6px;
  }

  .drawer {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: auto;
    max-height: min(75dvh, 520px);
    border-radius: 14px 14px 0 0;
    border-bottom: none;
    padding-bottom: var(--safe-bottom);
  }
}

/* Narrow phone */
@media (max-width: 420px) {
  #conn-label { display: none; }

  .topbar-title { font-size: 13px; }

  .topbar-right {
    grid-template-columns: 1fr auto auto;
    grid-template-rows: auto auto;
  }

  #follow-select {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  #btn-fullscreen {
    grid-column: 2;
    grid-row: 2;
    justify-self: end;
  }

  #btn-settings {
    grid-column: 3;
    grid-row: 2;
  }

  .slot { min-width: 150px; }
}

/* Phone landscape — maximize map, thin strips top/bottom */
@media (max-height: 500px) and (orientation: landscape) {
  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto;
    padding: 4px 10px;
  }

  .topbar-right {
    grid-column: 2;
    grid-row: 1;
    display: flex;
    flex-wrap: nowrap;
  }

  .topbar-watermark { display: none; }

  .workspace {
    flex-direction: row;
  }

  .map-panel {
    order: 1;
    min-height: 0;
  }

  #ally-board,
  #enemy-board {
    width: 140px;
    max-height: none;
    order: unset;
    padding: 4px;
  }

  #enemy-board { order: 2; }

  .slots {
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .slot {
    min-width: 0;
    max-width: none;
  }
}
