:root {
  --bg: #0e1116;
  --panel: #141a22;
  --text: #e7eefc;
  --muted: #9cb0cf;
  --accent: #c9a24a;
  --danger: #ff5c6a;
  --ok: #46d39a;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, "PingFang SC", "Noto Sans CJK SC",
    "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.home-mode #topbar {
  display: none !important;
}

.level-mode #home-topbar {
  display: none !important;
}

.home-topbar {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(20, 26, 34, 0.85);
  backdrop-filter: blur(10px);
}

.home-topbar-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.home-menu {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.home-screen {
  display: grid;
  place-items: center;
  padding: 32px 16px;
  width: 100%;
  min-height: 100%;
}

.home-screen-inner {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: min(720px, 90vw);
}

.home-title {
  text-align: center;
  font-size: clamp(32px, 4.2vw, 42px);
  font-weight: 800;
  letter-spacing: 2px;
  color: #f6d58f;
  line-height: 1.1;
  margin-bottom: 8px;
}

.home-build {
  text-align: center;
  font-size: clamp(11px, 2.2vw, 13px);
  color: rgba(231, 238, 252, 0.6);
  margin-top: -6px;
  margin-bottom: 10px;
}

.home-card {
  text-align: left;
  padding: 26px 26px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.home-card:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.2);
}

.home-card-title {
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
}

.home-card-desc {
  margin-top: 6px;
  font-size: 15px;
  color: var(--muted);
}

.home-card.disabled {
  opacity: 0.5;
}

.hidden {
  display: none;
}

.pvp-main {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 20px;
  padding: 24px;
  width: 100%;
  box-sizing: border-box;
}

.pvp-room-only {
  grid-template-columns: 1fr;
}

.pvp-list-only {
  grid-template-columns: 1fr;
  max-width: 1200px;
  margin: 0 auto;
}

@media (max-width: 768px) {
  .pvp-main {
    padding: 16px;
    gap: 16px;
  }

  .pvp-list-only {
    max-width: 100%;
  }
}

.pvp-list,
.pvp-room {
  border-radius: 12px;
  min-height: 520px;
}

.pvp-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.pvp-list-title {
  font-weight: 700;
  font-size: 18px;
  color: #fff;
}

.pvp-list-actions {
  display: flex;
  gap: 8px;
}

.pvp-room-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pvp-room-card {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 12px;
  text-align: left;
  color: #fff;
  cursor: pointer;
}

.pvp-room-card:hover {
  border-color: rgba(255, 255, 255, 0.2);
}

.pvp-room-card-title {
  font-weight: 600;
  margin-bottom: 6px;
}

.pvp-room-card-sub {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.75);
}

.pvp-room-empty {
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
}

.pvp-room-header {
  margin-bottom: 12px;
}

.pvp-room-title {
  font-weight: 700;
  font-size: 20px;
  color: #fff;
}

.pvp-room-sub {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
}

.pvp-info-grid {
  display: flex;
  flex-wrap: nowrap;
  gap: clamp(4px, 1.2vw, 8px);
  align-items: center;
  white-space: nowrap;
}

.pvp-info-item {
  display: inline-flex;
  align-items: baseline;
  gap: clamp(3px, 0.8vw, 6px);
  background: rgba(255, 255, 255, 0.06);
  border-radius: 999px;
  padding: clamp(3px, 0.8vw, 6px) clamp(6px, 1.6vw, 12px);
  flex: 0 1 auto;
}

.pvp-info-label {
  font-size: clamp(9px, 1.8vw, 12px);
  color: rgba(255, 255, 255, 0.6);
}

.pvp-info-value {
  font-size: clamp(10px, 2vw, 13px);
  font-weight: 600;
  color: #fff;
}

.pvp-invite {
  margin-top: 16px;
}

.pvp-invite-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
  white-space: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.pvp-input {
  flex: 1;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(9, 14, 20, 0.8);
  color: #fff;
}

.pvp-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.pvp-log {
  background: rgba(7, 10, 15, 0.7);
  border-radius: 10px;
  padding: 10px;
  font-size: 12px;
  max-height: 160px;
  overflow: auto;
  color: rgba(255, 255, 255, 0.7);
}

.pvp-log-drawer .pvp-log {
  margin-top: 0;
  max-height: calc(100% - 32px);
}

.toast {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(8, 12, 18, 0.92);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: clamp(10px, 2vw, 16px) clamp(16px, 3vw, 24px);
  font-size: clamp(13px, 2.2vw, 18px);
  font-weight: 600;
  letter-spacing: 0.5px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 1000;
  text-align: center;
  max-width: 80vw;
}

.toast.show {
  opacity: 1;
}

.pvp-setup {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

.pvp-board {
  background: rgba(7, 10, 15, 0.55);
  border-radius: 12px;
  width: min(900px, 100%);
  margin: 0 auto;
  display: flex;
  justify-content: center;
}

.pvp-board canvas {
  width: 100%;
  height: auto;
  display: block;
}

.pvp-board canvas,
.pvp-piece-panel {
  max-width: 900px;
}

.pvp-piece-panel {
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  padding: 10px;
  max-height: 520px;
  overflow: auto;
  width: min(900px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.pvp-piece-title {
  font-weight: 700;
  margin-bottom: clamp(4px, 1.2vw, 8px);
  color: #fff;
  font-size: clamp(12px, 2.2vw, 16px);
  letter-spacing: clamp(0px, 0.2vw, 0.6px);
}

.pvp-piece-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  --pvp-cell-w: 64px;
  --pvp-cell-h: 42px;
  justify-content: center;
}

.pvp-piece-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: var(--pvp-cell-w);
  height: var(--pvp-cell-h);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  cursor: pointer;
  color: #fff;
  font-size: clamp(9px, 1.6vw, 13px);
  text-align: center;
  line-height: 1.1;
  padding: 4px;
  box-sizing: border-box;
}

.pvp-piece-item.active {
  border-color: rgba(255, 255, 255, 0.6);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.15);
}

.pvp-piece-item.placed {
  opacity: 0.6;
}

.pvp-piece-item.side-red {
  background: rgba(192, 60, 60, 0.25);
  border-color: rgba(255, 120, 120, 0.5);
  color: #ffe6e6;
}

.pvp-piece-item.side-blue {
  background: rgba(60, 90, 200, 0.25);
  border-color: rgba(120, 150, 255, 0.5);
  color: #e6efff;
}

.pvp-piece-name {
  font-weight: 600;
}

.pvp-piece-cell {
  margin-top: 2px;
  font-size: clamp(8px, 1.3vw, 11px);
  color: rgba(255, 255, 255, 0.7);
}

.home-mode .main {
  grid-template-columns: 1fr;
  place-items: center;
  padding: 0;
}

.home-mode .panel {
  display: none;
}

.level-mode .main {
  height: 100%;
}

.level-mode .panel {
  align-self: stretch;
}

.home-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.home-list-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.home-back {
  width: auto;
  padding: 6px 10px;
  border-radius: 10px;
}

.home-list-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(20, 26, 34, 0.85);
  backdrop-filter: blur(10px);
  gap: 8px;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.5px;
}

.current-level {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}

.btn-back {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 16px;
}

.battle-top {
  justify-self: center;
  font-size: 14px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.home-title {
  justify-self: center;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.battle-top .dot {
  opacity: 0.6;
}

.actions button {
  margin-left: 8px;
}

.actions {
  justify-self: end;
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
}

@media (max-width: 820px) {
  .actions button {
    padding: 6px 8px;
    font-size: 12px;
  }
}

button {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
}

.btn-primary {
  background: linear-gradient(135deg, #e0b75a, #c3922e);
  border-color: rgba(255, 255, 255, 0.2);
  color: #1a1307;
  font-weight: 700;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.main {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 12px;
  padding: 12px;
  height: auto;
  min-height: 100%;
}

.board-wrap {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 0;
  overflow: auto;
  border-radius: 16px;
  background: radial-gradient(1000px 600px at 20% 0%, rgba(201, 162, 74, 0.08), transparent 55%),
    radial-gradient(900px 500px at 80% 100%, rgba(70, 211, 154, 0.06), transparent 60%),
    rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

canvas {
  max-width: 100%;
  height: auto;
  margin: 12px;
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(201, 162, 74, 0.12), rgba(255, 255, 255, 0.02));
  border: 1px solid rgba(255, 255, 255, 0.10);
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  align-self: start;
}

.bottom-actions {
  margin: 10px 12px 6px;
  display: flex;
  align-items: center;
  gap: clamp(4px, 1vw, 10px);
  flex-wrap: nowrap;
  width: calc(100% - 24px);
}

.bottom-actions button {
  flex: 1 1 0;
  min-width: 0;
  font-size: clamp(10px, 2.2vw, 14px);
  padding: clamp(4px, 1vw, 8px) clamp(6px, 2vw, 12px);
  white-space: nowrap;
}

.help-bottom {
  margin: 0 12px 12px;
  padding: 4px 10px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: var(--panel);
}

.help-bottom .panel-body {
  font-size: 12px;
  min-height: auto;
  line-height: 1.4;
  max-height: calc(1.4em * 2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel-card {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  padding: 12px;
  min-height: 0;
}

.status-drawer {
  position: fixed;
  top: 56px;
  right: 0;
  height: calc(100% - 56px);
  width: 300px;
  max-width: 80vw;
  transform: translateX(100%);
  transition: transform 0.2s ease;
  z-index: 30;
  background: rgba(20, 26, 34, 0.42);
  backdrop-filter: blur(6px);
}

.status-drawer.open {
  transform: translateX(0);
}

.status-tab {
  position: fixed;
  right: 0;
  top: 140px;
  width: clamp(32px, 6vw, 44px);
  height: clamp(88px, 18vw, 120px);
  border-radius: 12px 0 0 12px;
  border-right: none;
  background: rgba(20, 26, 34, 0.9);
  writing-mode: vertical-rl;
  text-orientation: upright;
  letter-spacing: clamp(0px, 0.6vw, 2px);
  font-size: clamp(10px, 2.2vw, 14px);
  z-index: 40;
}

.status-drawer .panel-title {
  margin-top: 4px;
}

.chat-drawer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 240px;
  max-height: 40vh;
  transform: translateY(100%);
  transition: transform 0.2s ease;
  z-index: 35;
  background: rgba(16, 20, 28, 0.15);
  backdrop-filter: blur(6px);
  border-radius: 16px 16px 0 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 12px;
}

.chat-drawer.open {
  transform: translateY(0);
}

.chat-drawer.open + .chat-tab {
  display: none;
}

.chat-tab {
  position: fixed;
  right: 12px;
  bottom: 12px;
  height: clamp(34px, 6vw, 44px);
  padding: 0 clamp(10px, 2.2vw, 16px);
  border-radius: 999px;
  background: rgba(20, 26, 34, 0.9);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-size: clamp(10px, 2.2vw, 14px);
  z-index: 40;
}

.pvp-chat-list {
  flex: 1;
  overflow: auto;
  background: rgba(7, 10, 15, 0.1);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
}

.pvp-chat-item {
  margin-bottom: 6px;
}

.pvp-chat-item .name {
  font-weight: 600;
  margin-right: 6px;
}

.pvp-chat-item .role {
  color: rgba(255, 255, 255, 0.6);
  margin-right: 6px;
}

.pvp-chat-input {
  display: flex;
  gap: 8px;
  align-items: center;
}

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

.chat-drawer .pvp-input {
  background: rgba(9, 14, 20, 0.15);
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.modal-card {
  position: relative;
  z-index: 1;
  width: min(320px, 86vw);
  background: rgba(16, 20, 28, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.invite-copy {
  min-height: 84px;
  resize: none;
  line-height: 1.4;
}

.modal-title {
  font-weight: 700;
  color: #fff;
  font-size: 16px;
}

.modal-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.modal-hint {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.65);
}

#chat-input {
  flex: 1;
}

#danmaku-layer {
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 100%;
  height: 80px;
  pointer-events: none;
  z-index: 200;
  overflow: hidden;
}

.conn-panel {
  position: fixed;
  left: 12px;
  bottom: 12px;
  width: min(320px, 90vw);
  z-index: 201;
  padding: 10px;
  background: rgba(10, 14, 20, 0.55);
  backdrop-filter: blur(6px);
  border-radius: 10px;
  font-size: 12px;
  color: #fff;
}

.conn-tab {
  position: fixed;
  left: 12px;
  bottom: 12px;
  z-index: 202;
  border: none;
  background: rgba(10, 14, 20, 0.65);
  color: #fff;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}

.pvp-log-footer {
  margin-top: 8px;
  display: flex;
  justify-content: flex-end;
}

.conn-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.conn-panel .panel-title {
  font-size: 12px;
}

.conn-text {
  min-height: 120px;
  font-size: 11px;
  line-height: 1.4;
  resize: none;
}

.danmaku-item {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  white-space: nowrap;
  font-size: clamp(14px, 2.4vw, 20px);
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
  animation-name: danmaku-move;
  animation-timing-function: linear;
  will-change: transform;
}

@keyframes danmaku-move {
  from {
    transform: translate(160%, -50%);
  }
  to {
    transform: translate(-320%, -50%);
  }
}

.battle-status .panel-body {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
}

.battle-status .ok {
  color: var(--ok);
}

.battle-status .warn {
  color: var(--accent);
}

.panel-title {
  font-weight: 700;
  margin-bottom: 8px;
}

.panel-body {
  color: var(--muted);
  line-height: 1.5;
  font-size: 14px;
  min-height: 72px;
}

#battle-rate {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.panel-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.level-list {
  display: grid;
  gap: 10px;
}

.level-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  transition: border-color 0.2s ease, transform 0.2s ease, background 0.2s ease;
}

.level-meta {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.level-title {
  font-weight: 700;
}

.level-status {
  font-size: 12px;
  color: var(--muted);
}

.level-item:hover {
  border-color: rgba(201, 162, 74, 0.5);
  background: rgba(255, 255, 255, 0.05);
}

.level-item.expanded {
  border-color: rgba(201, 162, 74, 0.7);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
}

.level-pill {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.06);
}

.level-pill.tutorial {
  color: #7ee6c5;
  border-color: rgba(70, 211, 154, 0.4);
}

.level-pill.battle {
  color: #f0c06c;
  border-color: rgba(201, 162, 74, 0.5);
}

.level-details {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.25s ease, opacity 0.25s ease;
  opacity: 0;
  width: 100%;
}

.level-details.expanded {
  max-height: 240px;
  opacity: 1;
}

.level-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

@media (max-width: 640px) {
  .level-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }
  .level-meta,
  .level-details {
    width: 100%;
  }
  .level-title {
    white-space: normal;
    word-break: break-word;
  }
  .level-actions {
    flex-wrap: wrap;
  }
}

.debug {
  margin: 0;
  color: #cfe0ff;
  font-size: 12px;
  overflow: auto;
  max-height: 360px;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.dialog {
  min-width: min(720px, 92vw);
  background: var(--panel);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  padding: 14px;
}

.dialog-title {
  font-weight: 800;
  margin-bottom: 8px;
}

.dialog-body {
  margin: 0;
  max-height: 60vh;
  overflow: auto;
  font-size: 12px;
  color: #cfe0ff;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}

@media (max-width: 960px) {
  .main {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 820px) {
  .main {
    grid-template-columns: 1fr;
    padding: 8px;
    gap: 8px;
  }

  .level-mode .panel {
    display: none;
  }

  .board-wrap {
    border-radius: 12px;
  }

  canvas {
    margin: 8px;
    width: 100%;
    height: auto;
    max-height: calc(100vh - 160px);
  }
}
