:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #111827;
  --mute: #6b7280;
  --line: #e5e7eb;
  --blue: #1e67ff;
  --blue-soft: #e8f0ff;
  --green: #16a34a;
  --warn-bg: #fff7e8;
  --warn-ink: #a16207;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
  --font: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  --nav-h: 64px;
  --top-h: 52px;
  --shell-max: 480px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }
html, body {
  min-height: 100%;
  overflow-x: hidden;
}
body {
  background:
    radial-gradient(1200px 480px at 50% -120px, rgba(30, 103, 255, 0.10), transparent 60%),
    radial-gradient(900px 420px at 100% 20%, rgba(59, 130, 246, 0.06), transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--ink);
  font: 400 14px/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}
button, input, select { font: inherit; color: inherit; }
button { border: 0; background: none; cursor: pointer; }
button:disabled { opacity: .45; cursor: not-allowed; }
input, select {
  width: 100%;
  border: 1px solid var(--line);
  background: #f9fafb;
  border-radius: 10px;
  padding: 12px 14px;
  outline: none;
}
input:focus, select:focus { border-color: var(--blue); background: #fff; }
a { color: inherit; text-decoration: none; }
.hidden { display: none !important; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: var(--top-h);
  padding: calc(10px + var(--safe-top)) max(16px, var(--safe-right)) 10px max(16px, var(--safe-left));
  background: rgba(255, 255, 255, 0.88);
  border-bottom: 1px solid rgba(229, 231, 235, 0.9);
  backdrop-filter: blur(12px);
}
.header-inner {
  width: min(var(--shell-max), 100%);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}
.top-nav {
  display: none;
  align-items: center;
  gap: 4px;
  flex: 1 1 auto;
  justify-content: center;
  min-width: 0;
}
.top-nav button {
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  color: var(--mute);
  font-weight: 650;
  font-size: 14px;
}
.top-nav button:hover { color: var(--ink); background: #f3f4f6; }
.top-nav button.active {
  color: var(--blue);
  background: var(--blue-soft);
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  color: #0f172a;
  min-width: 0;
}
.brand-logo {
  display: block;
  height: 36px;
  width: auto;
  max-width: min(160px, 46vw);
  object-fit: contain;
}
.login-brand {
  display: flex;
  justify-content: center;
  margin: 4px 0 14px;
}
.login-brand img {
  display: block;
  width: clamp(160px, 58vw, 240px);
  height: auto;
  object-fit: contain;
}
.header-user {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--mute);
  font-size: 13px;
  min-width: 0;
}
#headerUserName {
  display: inline-block;
  max-width: min(140px, 36vw);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 10px;
  font-weight: 600;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-secondary { background: #eef2f7; color: #334155; }
.btn-ghost {
  background: var(--blue-soft);
  color: var(--blue);
  min-height: 32px;
  padding: 0 12px;
  font-size: 13px;
}
.btn-block { width: 100%; }
.btn-row { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; }

.shell {
  width: min(var(--shell-max), 100%);
  max-width: 100%;
  margin: 0 auto;
  padding: 8px 16px calc(var(--nav-h) + var(--safe-bottom) + 24px);
}
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  max-width: 100%;
  min-width: 0;
}
.card + .card { margin-top: 14px; }
.card h2 { font-size: 20px; margin-bottom: 4px; }
.card .sub { color: var(--mute); margin-bottom: 14px; }
.section-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 12px;
}
.section-title strong { font-size: 18px; }
.section-title span { color: var(--mute); font-size: 13px; }
.muted { color: var(--mute); }
.invite-box {
  background: var(--warn-bg);
  color: var(--warn-ink);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 14px;
  font-size: 13px;
}
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: #eef2f7;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 14px;
}
.tabs button {
  min-height: 44px;
  color: var(--mute);
  font-weight: 600;
  font-size: clamp(12px, 3.4vw, 14px);
  padding: 0 4px;
}
.tabs button.active { color: var(--blue); background: transparent; }

.field { margin-bottom: 12px; }
.field input { min-height: 44px; }
.kv {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}
.kv:first-child { border-top: 0; }
.kv span { color: var(--mute); }
.kv strong.online { color: var(--green); }

.profile-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
}
.avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: #f59e0b;
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 24px;
  font-weight: 700;
}
.tag {
  display: inline-flex;
  margin-top: 4px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: var(--blue);
  font-size: 12px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
.stat {
  background: #f3f7ff;
  border-radius: 12px;
  padding: 14px 6px;
  text-align: center;
  min-width: 0;
}
.stat strong {
  display: block;
  color: var(--blue);
  font-size: clamp(16px, 5vw, 22px);
  line-height: 1.2;
  overflow-wrap: anywhere;
}
.stat span { color: var(--mute); font-size: 11px; }

.invitee-list {
  display: grid;
  gap: 8px;
}
.invitee-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f9fafb;
}
.invitee-id {
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.invitee-meta {
  grid-column: 1 / -1;
  color: var(--mute);
  font-size: 12px;
}
.invitee-badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 650;
  white-space: normal;
  text-align: right;
  max-width: 46vw;
  line-height: 1.25;
}
.invitee-badge.rewarded {
  background: #e7f8ee;
  color: var(--green);
}
.invitee-badge.pending {
  background: var(--warn-bg);
  color: var(--warn-ink);
}
.invitee-badge.activated {
  background: var(--blue-soft);
  color: var(--blue);
}
.invitee-empty {
  padding: 16px 0;
  text-align: center;
  font-size: 12px;
}

.project-card {
  margin-bottom: 12px;
  background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
}
.project-status {
  color: var(--green);
  font-size: 12px;
  font-weight: 600;
}
.project-expiry {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
}
.project-expiry strong {
  color: var(--blue);
  font-size: 20px;
  font-weight: 700;
}
#modeSegment button.locked {
  opacity: .35;
  text-decoration: line-through;
}

.flip-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 12px 0;
}
.flip-card {
  aspect-ratio: 3/4;
  border-radius: 12px;
  background: linear-gradient(160deg, #3b82f6, #1d4ed8);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 700;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.25);
}
.flip-card.revealed {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: none;
}
.flip-card .mark { font-size: 28px; line-height: 1; }

.prize-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.prize-chips span {
  background: #f3f4f6;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  color: #374151;
}
.history-item {
  background: #f8fafc;
  border-radius: 12px;
  padding: 12px;
  margin-top: 8px;
}
.history-item strong { display: block; margin-bottom: 4px; }
.history-item small { color: var(--mute); }

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  min-height: var(--nav-h);
  background: #fff;
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
  box-shadow: 0 -6px 20px rgba(15, 23, 42, 0.04);
}
.bottom-nav button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: calc(var(--nav-h) - 4px);
  color: #9ca3af;
  font-size: 12px;
}
.bottom-nav button.active { color: var(--blue); }
.bottom-nav svg { width: 22px; height: 22px; flex: 0 0 auto; }

.empty-home {
  min-height: 42vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
}
.empty-home h2 { font-size: 24px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 18px);
  transform: translateX(-50%) translateY(20px);
  z-index: 50;
  max-width: min(90vw, 360px);
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(17, 24, 39, 0.92);
  color: #fff;
  opacity: 0;
  pointer-events: none;
  transition: .2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: rgba(185, 28, 28, 0.95); }

/* Room stage */
.room-stage .hero {
  text-align: center;
  padding: 10px 0 16px;
}
.room-stage .hero-label { color: var(--mute); font-size: 12px; }
.room-stage .hero strong {
  display: block;
  font-size: clamp(28px, 10vw, 40px);
  letter-spacing: 2px;
  margin: 6px 0;
  overflow-wrap: anywhere;
  word-break: break-all;
}
.queue-list { display: grid; gap: 8px; margin: 10px 0 14px; }
.queue-item {
  display: flex;
  justify-content: space-between;
  background: #f8fafc;
  border-radius: 10px;
  padding: 10px 12px;
}
.queue-item .pending { color: var(--mute); font-weight: 500; }
.timer {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}
.timer strong { font-size: 22px; color: var(--blue); }
.status-row {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  color: var(--mute);
}
.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #cbd5e1;
  margin-right: 6px;
}
.dot.ok { background: var(--green); }
.dot.warn { background: #f59e0b; }
.choice {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}
.choice-2 { grid-template-columns: 1fr 1fr; }
.choice-4 { grid-template-columns: 1fr 1fr; }
.choice button {
  min-height: 44px;
  border-radius: 10px;
  background: #f3f4f6;
  color: #475569;
  font-weight: 600;
  padding: 0 8px;
}
.choice button.active { background: var(--blue-soft); color: var(--blue); }
.collision-field {
  width: 100%;
  max-width: 100%;
  margin: 4px 0 14px;
}
.toggle-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f9fafb;
  box-sizing: border-box;
  cursor: pointer;
  writing-mode: horizontal-tb;
  text-orientation: mixed;
}
.collision-toggle-copy {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  writing-mode: horizontal-tb;
  text-orientation: mixed;
  white-space: normal;
  word-break: keep-all;
  overflow-wrap: anywhere;
  line-height: 1.4;
}
.collision-toggle-copy strong {
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
}
.collision-toggle-copy small {
  font-size: 12px;
  line-height: 1.35;
}
.toggle-row input[type="checkbox"] {
  flex: 0 0 auto;
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--blue);
  writing-mode: horizontal-tb;
}
.formation-progress { margin: 12px 0; }
.formation-track {
  height: 8px;
  background: #e5e7eb;
  border-radius: 999px;
  overflow: hidden;
  margin: 8px 0;
}
.formation-track i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--blue);
}
.ops-actions { display: grid; gap: 10px; }
.ops-actions-primary { margin: 4px 0 8px; }
.btn-open {
  min-height: 50px;
  font-size: 16px;
  letter-spacing: 0.02em;
  box-shadow: 0 10px 24px rgba(30, 103, 255, 0.22);
}
.control-block { margin-bottom: 12px; }
.control-label {
  color: var(--mute);
  font-size: 12px;
  font-weight: 650;
  margin-bottom: 8px;
}
.route-hint { display: block; margin: 8px 0 0; font-size: 12px; line-height: 1.4; }
.card-meta {
  margin-top: 12px;
  font-size: 12px;
  display: grid;
  gap: 4px;
}
.more-options {
  margin-top: 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f9fafb;
  overflow: hidden;
}
.more-options > summary {
  list-style: none;
  cursor: pointer;
  min-height: 44px;
  display: flex;
  align-items: center;
  padding: 0 14px;
  color: var(--secondary, #5d5b55);
  color: var(--mute);
  font-size: 13px;
  font-weight: 650;
  user-select: none;
}
.more-options > summary::-webkit-details-marker { display: none; }
.more-options > summary::after {
  content: "▾";
  margin-left: auto;
  color: #9ca3af;
  font-size: 12px;
}
.more-options[open] > summary {
  border-bottom: 1px solid var(--line);
  background: #fff;
}
.more-options[open] > summary::after { content: "▴"; }
.more-options-body { padding: 12px 14px 14px; }

.home-project {
  display: grid;
  gap: 0;
  min-width: 0;
}
.room-controls { min-width: 0; }

.login-only .bottom-nav,
.login-only .app-header { display: none !important; }
.login-only .shell {
  width: min(420px, 100%);
  padding: 24px 16px calc(24px + var(--safe-bottom));
  min-height: 100svh;
  display: grid;
  align-content: center;
}
.login-only #page-login {
  width: 100%;
}
.login-only #page-login h2 { text-align: center; }
.login-only #page-login .sub { text-align: center; }

@media (max-width: 480px) {
  .shell { padding-left: 12px; padding-right: 12px; }
  .card { padding: 16px; }
  .section-title { flex-wrap: wrap; }
  .invitee-row { grid-template-columns: minmax(0, 1fr); }
  .invitee-badge { max-width: 100%; text-align: left; justify-self: start; }
  #headerUserName { max-width: min(96px, 28vw); }
}

@media (min-width: 768px) {
  :root {
    --shell-max: 920px;
    --top-h: 64px;
  }
  .shell {
    padding: 20px 24px 40px;
  }
  .header-inner { width: min(var(--shell-max), 100%); }
  .top-nav { display: flex; }
  .bottom-nav { display: none !important; }
  .choice-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .collision-field { max-width: none; }
  .collision-toggle-copy strong { font-size: 15px; }
  .login-only .shell { width: min(460px, 100%); }
  .login-only #page-login { box-shadow: 0 16px 48px rgba(15, 23, 42, 0.08); }
  .stat span { font-size: 12px; }
  .invitee-badge { white-space: nowrap; max-width: none; }
  /* Desktop: keep more options open by default for mouse users */
  .more-options { border-color: transparent; background: transparent; }
  .more-options > summary { display: none; }
  .more-options-body { display: block !important; padding: 0; }
  .more-options #routeGroupField,
  .more-options .collision-field,
  .more-options .card-meta {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #f9fafb;
    padding: 12px 14px;
    margin-bottom: 12px;
  }
  .flip-grid { grid-template-columns: repeat(3, 1fr); max-width: 560px; }
  #page-lottery:not(.hidden) {
    display: grid;
    grid-template-columns: minmax(0, 1.25fr) minmax(280px, 0.75fr);
    gap: 14px;
    align-items: start;
  }
  #page-lottery .card:first-child { grid-row: 1 / span 2; }
}

@media (min-width: 1024px) {
  :root { --shell-max: 1080px; }
  .home-project {
    grid-template-columns: minmax(280px, 0.85fr) minmax(0, 1.35fr);
    gap: 16px;
    align-items: start;
  }
  .home-project > .project-card {
    margin-bottom: 0;
    position: sticky;
    top: calc(var(--top-h) + var(--safe-top) + 16px);
  }
  .home-project > .room-stage { margin-top: 0; }
  .brand-logo { height: 42px; max-width: 190px; }
  .room-stage .hero strong { font-size: 48px; }
  .btn-open { min-height: 52px; font-size: 17px; }
}
