:root {
  --bg: #f7f8f9;
  --surface: #ffffff;
  --surface-secondary: #f0f2f5;
  --border: #e0e3e7;
  --border-light: #eceef1;
  --text: #232e3c;
  --text-secondary: #8e99a4;
  --accent: #2aabee;
  --accent-hover: #229ed9;
  --accent-light: #e8f5fd;
  --warning: #d5a100;
  --warning-light: #fff8dc;
  --danger: #e5545e;
  --danger-light: #fdeaec;
  --success: #33b571;
  --success-light: #e8f8ef;
  --radius-lg: 14px;
  --radius-md: 12px;
  --radius-sm: 10px;
  --radius-xs: 8px;
  --tg-viewport-height: 100vh;
  --tg-safe-top: env(safe-area-inset-top, 0px);
  --tg-safe-right: env(safe-area-inset-right, 0px);
  --tg-safe-bottom: env(safe-area-inset-bottom, 0px);
  --tg-safe-left: env(safe-area-inset-left, 0px);
  --tg-content-safe-top: env(safe-area-inset-top, 0px);
  --tg-content-safe-right: env(safe-area-inset-right, 0px);
  --tg-content-safe-bottom: env(safe-area-inset-bottom, 0px);
  --tg-content-safe-left: env(safe-area-inset-left, 0px);
  --font: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue",
    sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  -webkit-font-smoothing: antialiased;
}

body {
  overflow-x: hidden;
}

button,
input,
select {
  font: inherit;
}

/* ─── Layout ─── */
.app-shell {
  height: var(--tg-viewport-height);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.app {
  width: min(100%, 480px);
  margin: 0 auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
  padding: max(72px, var(--tg-content-safe-top)) max(16px, var(--tg-content-safe-right)) max(40px, var(--tg-content-safe-bottom)) max(16px, var(--tg-content-safe-left));
}

.top-strip {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  align-items: stretch;
}

.top-stat-card {
  background: linear-gradient(180deg, #ffffff, #eef8fe);
  border: 1px solid #d9eaf7;
  border-radius: var(--radius-md);
  padding: 5px 6px;
  display: grid;
  gap: 1px;
  min-height: 42px;
}

.top-stat-card strong {
  font-size: 0.7rem;
  line-height: 1.15;
}

.top-rank-button {
  min-height: 42px;
  background: linear-gradient(135deg, #2aabee, #1c85bb);
  color: #fff;
  font-size: 0.62rem;
  padding: 5px 6px;
}

.top-rank-button:hover {
  background: linear-gradient(135deg, #229ed9, #176f9d);
}

.scene {
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  animation: fadeIn 180ms ease;
}

/* ─── Panel ─── */
.panel {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: 20px;
}

.hero-panel,
.step-panel,
.battle-panel,
.center-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  min-height: 0;
  padding: 14px;
}

/* ─── Hero (Lobby) ─── */
.hero-visual {
  width: 72px;
  height: 72px;
  margin: 0 auto 4px;
  border-radius: 20px;
  background: var(--accent-light);
  display: flex;
  align-items: center;
  justify-content: center;
}

.hero-icon {
  font-size: 2.5rem;
  line-height: 1;
}

.hero-text {
  text-align: center;
  margin-bottom: 8px;
}

.hero-text h1 {
  font-size: 1.65rem;
  margin-bottom: 6px;
}

.feature-grid {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.feature-item {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--surface-secondary);
  border-radius: var(--radius-sm);
}

.feature-icon {
  font-size: 1.5rem;
  line-height: 1;
}

.feature-item strong {
  display: block;
  font-size: 0.875rem;
  color: var(--text);
  margin-bottom: 2px;
}

.feature-item p {
  color: var(--text-secondary);
  font-size: 0.813rem;
  margin: 0;
}

.mt-auto {
  margin-top: auto;
}

.center-text {
  text-align: center;
}

.hero-button {
  padding: 14px 18px;
  font-size: 1rem;
}

/* ─── Typography ─── */
h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 6px;
  font-size: 1.55rem;
  font-weight: 700;
  line-height: 1.15;
}

h2 {
  margin-bottom: 0;
  font-size: 1.15rem;
  font-weight: 600;
}

h3 {
  margin-bottom: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.lead {
  color: var(--text-secondary);
  line-height: 1.55;
  margin-bottom: 0;
  font-size: 0.938rem;
}

.lead.small {
  font-size: 0.875rem;
}

.eyebrow,
.mini-label,
label {
  margin: 0 0 6px;
  font-size: 0.52rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

/* ─── Badges & Pills ─── */
.badge,
.status-pill,
.mini-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 5px 12px;
  border-radius: 20px;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
}

.badge.soft,
.mini-pill.ghost {
  background: var(--surface-secondary);
  color: var(--text-secondary);
}

/* ─── Forms ─── */
.form-block {
  display: grid;
  gap: 10px;
}

.secret-form button {
  width: 100%;
  margin-top: 8px;
}

input,
select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  outline: none;
  transition: border-color 150ms ease;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

/* ─── Buttons ─── */
button {
  border: 0;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.875rem;
  transition: background 150ms ease, opacity 150ms ease;
}

button:active {
  opacity: 0.85;
}

.primary-button {
  background: var(--accent);
  color: #fff;
}

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

.icon-button {
  padding: 8px 14px;
  background: var(--surface-secondary);
  color: var(--text);
  border-radius: var(--radius-xs);
  font-size: 0.813rem;
}

.icon-button:hover {
  background: var(--border-light);
}

.chat-trigger {
  position: relative;
}

.chat-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--danger);
  color: #fff;
  font-size: 0.688rem;
  font-weight: 700;
  box-shadow: 0 4px 10px rgba(229, 84, 94, 0.28);
}

/* ─── Waiting Loader ─── */
.pulse-orb {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  margin: 0 auto;
}

.pulse-orb span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #fff;
  animation: pulseDots 1.1s infinite ease-in-out;
}

.pulse-orb span:nth-child(2) {
  animation-delay: 0.16s;
}

.pulse-orb span:nth-child(3) {
  animation-delay: 0.32s;
}

.small-pulse {
  width: 48px;
  height: 48px;
  margin: 0;
}

/* ─── Step / Battle Header ─── */
.step-header,
.battle-header,
.mini-topline,
.board-meta,
.result-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.timer-pill {
  min-width: 72px;
  font-variant-numeric: tabular-nums;
}

.hud-side {
  display: flex;
  align-items: center;
  gap: 6px;
}

.player-chip,
.player-heading {
  display: flex;
  align-items: center;
  gap: 12px;
}

.player-chip {
  padding: 12px 14px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  background: var(--surface-secondary);
}

.setup-wait-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px dashed #b3ddf5;
  background: linear-gradient(135deg, #f4fbff, #e8f5fd);
}

.player-meta {
  display: grid;
  gap: 2px;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #dff2fd, #b9e4fa);
  color: var(--accent);
  font-weight: 700;
}

.small-avatar {
  width: 40px;
  height: 40px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar span {
  font-size: 0.938rem;
}

/* ─── Digit / Board Grids ─── */
.digit-grid,
.board-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}

.digit-grid input {
  text-align: center;
  padding: 10px 0;
  font-weight: 700;
  font-size: 1rem;
}

/* ─── Board Section ─── */
.board-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-height: 0;
}

.board-card {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-height: 0;
}

.own-side {
  background: var(--surface);
}

.enemy-side {
  background: var(--surface);
}

.board-meta span {
  color: var(--text-secondary);
  font-size: 0.813rem;
}

/* ─── Board Cells ─── */
.board-cell {
  position: relative;
  border-radius: var(--radius-xs);
  background: var(--surface-secondary);
  border: 1px solid var(--border-light);
  display: grid;
  place-items: center;
  overflow: hidden;
  height: 100%;
  min-height: 48px;
}

.board-cell.hit {
  background: var(--danger-light);
  color: var(--danger);
  border-color: #f0c6ca;
}

.board-cell.own {
  background: var(--surface-secondary);
}

.enemy-side .board-cell.hit {
  background: var(--accent-light);
  color: var(--accent);
  border-color: #b3ddf5;
}

.enemy-side .board-cell.misplaced {
  background: var(--warning-light);
  color: var(--warning);
  border-width: 2px;
  border-color: #efc94c;
}

.cell-index {
  position: absolute;
  top: 5px;
  left: 7px;
  color: var(--text-secondary);
  font-size: 0.688rem;
  font-weight: 500;
}

.board-cell strong {
  font-size: 1.25rem;
}

.board-input {
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
  text-align: center;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent);
  padding: 10px 0 0;
  border-radius: 0;
}

.board-input:focus {
  border: 0;
  box-shadow: none;
}

.board-cell.guessable {
  border-color: var(--accent);
  border-style: dashed;
}

.board-cell.selected {
  border-color: var(--accent);
  border-style: solid;
  background: var(--accent-light);
}

.board-cell.misplaced.selected {
  border-color: #efc94c;
  background: var(--warning-light);
}

.board-cell.misplaced .board-input {
  color: var(--warning);
}

/* ─── Turn Strip ─── */
.turn-strip {
  padding: 8px 10px;
  border-radius: var(--radius-xs);
  background: var(--accent-light);
  color: var(--text);
  font-weight: 600;
  font-size: 0.813rem;
  text-align: center;
}

/* ─── Guess Form ─── */
.guess-form {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.battle-legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--surface-secondary);
  border: 1px solid var(--border-light);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.72rem;
  color: var(--text-secondary);
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 2px solid transparent;
  flex: 0 0 auto;
}

.legend-swatch.hit {
  background: var(--accent-light);
  border-color: #b3ddf5;
}

.legend-swatch.misplaced {
  background: var(--warning-light);
  border-color: #efc94c;
}

/* ─── Feed / Lists ─── */
.feed-compact {
  display: grid;
  gap: 10px;
}

.compact-list {
  display: grid;
  gap: 3px;
}

.list-item {
  padding: 5px 6px;
  border-radius: var(--radius-xs);
  background: var(--surface-secondary);
  border: 1px solid var(--border-light);
  display: grid;
  gap: 1px;
  font-size: 0.68rem;
}

.list-item strong {
  font-size: 0.63rem;
}

.list-item.hit {
  color: var(--danger);
  background: var(--danger-light);
  border-color: #f0c6ca;
}

.list-item.misplaced {
  color: var(--warning);
  background: var(--warning-light);
  border-color: #efc94c;
}

.list-item.success {
  color: var(--success);
  background: var(--success-light);
  border-color: #b3e6cc;
}

.list-item.error {
  color: var(--danger);
  background: var(--danger-light);
  border-color: #f0c6ca;
}

/* ─── Center / Result ─── */
.center-panel {
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px 16px;
}

.result-panel {
  gap: 14px;
}

.rank-summary-card {
  padding: 6px 8px;
  border-radius: var(--radius-md);
  border: 1px solid #d9eaf7;
  background: linear-gradient(135deg, #f8fcff, #eaf6fd);
  display: grid;
  gap: 1px;
  margin-bottom: 6px;
}

.rank-summary-card strong {
  font-size: 0.8rem;
  color: var(--accent);
}

.rank-item {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.rank-item.you {
  background: var(--accent-light);
  border-color: #b3ddf5;
}

/* ─── Overlay ─── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
}

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

.overlay-sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: min(62vh, 440px);
  overflow: auto;
  background: var(--surface);
  border-top: 1px solid var(--border-light);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 10px;
}

.chat-sheet {
  display: grid;
  gap: 14px;
}

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

.chat-messages {
  max-height: min(42vh, 360px);
  overflow: auto;
  display: grid;
  gap: 8px;
}

.chat-message {
  display: grid;
  gap: 4px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-secondary);
  border: 1px solid var(--border-light);
  font-size: 0.875rem;
}

.chat-message.mine {
  background: var(--accent-light);
  border-color: #b3ddf5;
}

.chat-message strong {
  font-size: 0.813rem;
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

.quick-chat-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.quick-chat-button {
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 0.813rem;
}

.quick-chat-button:hover {
  background: #d8eefb;
}

.chat-form button {
  min-width: 92px;
}

.countdown-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(35, 46, 60, 0.58);
  backdrop-filter: blur(6px);
}

.countdown-badge {
  min-width: min(100%, 240px);
  padding: 28px 24px;
  border-radius: 24px;
  background: linear-gradient(180deg, #ffffff, #eef8fe);
  border: 1px solid #d5ebf8;
  text-align: center;
  box-shadow: 0 18px 48px rgba(18, 48, 74, 0.2);
}

.countdown-badge strong {
  display: block;
  font-size: clamp(4rem, 18vw, 5.75rem);
  line-height: 1;
  color: var(--accent);
  margin: 8px 0 10px;
}

.countdown-pop {
  animation: countdownPop 420ms ease;
}

/* ─── Utility ─── */
.hidden {
  display: none !important;
}

.empty-state {
  color: var(--text-secondary);
  font-size: 0.875rem;
}

/* ─── Animations ─── */
@keyframes pulseDots {
  0%,
  80%,
  100% {
    transform: scale(0.7);
    opacity: 0.55;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes countdownPop {
  0% {
    transform: scale(0.68);
    opacity: 0.2;
  }
  70% {
    transform: scale(1.08);
    opacity: 1;
  }
  100% {
    transform: scale(1);
  }
}

/* ─── Responsive ─── */
@media (max-width: 420px) {
  .app {
    padding-left: 12px;
    padding-right: 12px;
    padding-top: max(72px, var(--tg-content-safe-top));
    padding-bottom: max(40px, var(--tg-content-safe-bottom));
  }

  .top-strip {
    grid-template-columns: 1fr 1fr;
  }

  .top-rank-button {
    grid-column: 1 / -1;
  }

  .panel {
    padding: 14px;
  }

  .hero-panel,
  .step-panel,
  .battle-panel,
  .center-panel {
    padding: 14px;
    gap: 12px;
  }

  .step-header,
  .battle-header,
  .mini-topline,
  .board-meta,
  .result-actions,
  .overlay-header {
    flex-wrap: wrap;
  }

  .chat-form {
    grid-template-columns: 1fr;
  }
}
