:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #0f172a);
  --surface: var(--tg-theme-secondary-bg-color, #172033);
  --surface-2: var(--tg-theme-section-bg-color, #1d2940);
  --text: var(--tg-theme-text-color, #f8fafc);
  --muted: var(--tg-theme-hint-color, #94a3b8);
  --accent: var(--tg-theme-button-color, #3b82f6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #ef4444);
  --border: color-mix(in srgb, var(--text) 14%, transparent);
  --success: #22c55e;
  --blue: #2563eb;
  --nav-height: 70px;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Tahoma, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: var(--tg-viewport-stable-height, 100vh);
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

button:disabled {
  cursor: default;
}

.app-shell {
  width: min(100%, 560px);
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px
    calc(22px + env(safe-area-inset-bottom));
}

body.has-bottom-nav .app-shell {
  padding-bottom: calc(
    var(--nav-height) + 22px + env(safe-area-inset-bottom)
  );
}

.screen { width: 100%; }
.hidden { display: none !important; }

.center-screen {
  min-height: 72vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 12px;
}

h1,
h2,
p {
  margin: 0;
}

h1 { font-size: 1.42rem; }
h2 { font-size: 1rem; }
small { color: var(--muted); }
.muted { color: var(--muted); }

.eyebrow {
  color: var(--muted);
  font-size: .8rem;
  margin-bottom: 5px;
}

.page-description {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.8;
}

.logo-mark {
  width: 72px;
  height: 72px;
  display: grid;
  place-items: center;
  border-radius: 22px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 2rem;
  font-weight: 700;
}

.loader {
  width: 28px;
  height: 28px;
  margin-top: 8px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.app-header,
.page-header,
.game-header,
.section-heading,
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.app-header,
.page-header,
.game-header {
  margin-bottom: 18px;
}

.page-header {
  display: block;
}

.game-header {
  align-items: flex-start;
}

.hero-card {
  display: grid;
  gap: 18px;
  padding: 18px;
  border-radius: 24px;
  background:
    linear-gradient(
      135deg,
      color-mix(in srgb, var(--accent) 26%, var(--surface)),
      var(--surface)
    );
  border: 1px solid var(--border);
}

.hero-card h2 {
  margin-top: 11px;
  font-size: 1.18rem;
}

.hero-card p {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.8;
}

.status-badge,
.soon-badge {
  display: inline-flex;
  width: fit-content;
  padding: 5px 9px;
  border-radius: 999px;
  font-size: .72rem;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  color: var(--text);
}

.section-block {
  margin-top: 20px;
}

.section-title {
  margin-bottom: 12px;
}

.stats-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.stats-strip > div,
.profile-stats > div {
  padding: 12px 8px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  text-align: center;
}

.stats-strip span,
.profile-stats span {
  display: block;
  color: var(--muted);
  font-size: .7rem;
}

.stats-strip strong,
.profile-stats strong {
  display: block;
  margin-top: 5px;
}

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

.wide-mode-card {
  min-height: 88px;
  padding: 14px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 12px;
  text-align: right;
}

.wide-mode-card.available:active,
.level-card:active {
  transform: scale(.985);
}

.wide-mode-card.unavailable {
  grid-template-columns: auto 1fr auto;
  opacity: .48;
}

.wide-mode-card span:nth-child(2) {
  display: grid;
  gap: 6px;
}

.mode-icon {
  font-size: 1.45rem;
}

.soon-badge {
  white-space: nowrap;
  background: color-mix(in srgb, var(--text) 9%, transparent);
  color: var(--muted);
}

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

.level-card {
  min-height: 94px;
  padding: 14px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 7px;
  text-align: right;
}

.level-card strong {
  font-size: 1.08rem;
}

.level-card span {
  color: var(--muted);
  font-size: .76rem;
  line-height: 1.65;
}

.empty-state {
  min-height: 54vh;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
}

.empty-state > span {
  font-size: 2.6rem;
}

.empty-state p {
  max-width: 340px;
  color: var(--muted);
  line-height: 1.9;
}

.profile-card {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 16px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.avatar {
  width: 56px;
  height: 56px;
  flex: 0 0 56px;
  display: grid;
  place-items: center;
  border-radius: 18px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1.35rem;
  font-weight: 700;
}

.profile-card h2 {
  font-size: 1.05rem;
  margin-bottom: 5px;
}

.profile-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  margin-top: 12px;
}

.game-title {
  flex: 1;
}

.score-box {
  min-width: 76px;
  padding: 9px 11px;
  border-radius: 16px;
  background: var(--surface);
  text-align: center;
}

.score-box span {
  display: block;
  color: var(--muted);
  font-size: .72rem;
}

.score-box strong {
  display: block;
  margin-top: 3px;
  font-size: 1.2rem;
}

.word-card,
.targets-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 16px;
}

.word-card { margin-bottom: 12px; }
.targets-card { margin-bottom: 14px; }

.section-heading span {
  color: var(--muted);
  font-size: .85rem;
}

.input-slots {
  direction: rtl;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  min-height: 58px;
  margin-top: 14px;
}

.input-slot,
.target-cell {
  display: grid;
  place-items: center;
  width: 42px;
  height: 46px;
  border-radius: 13px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 1.2rem;
  font-weight: 700;
}

.targets-board {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 14px;
}

.target-row {
  display: flex;
  justify-content: center;
  gap: 5px;
  min-height: 34px;
}

.target-row.found {
  padding: 8px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--success) 18%, transparent);
  color: var(--text);
  font-weight: 700;
}

.target-cell {
  width: 28px;
  height: 32px;
  border-radius: 9px;
  font-size: .95rem;
}

.bonus-row {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  line-height: 1.7;
}

.keyboard-area {
  padding-bottom: 8px;
}

.letters-keyboard {
  direction: rtl;
  display: grid;
  grid-template-columns: repeat(var(--letter-count, 4), minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}

.letter-button {
  min-height: 55px;
  border-radius: 15px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 1.3rem;
  font-weight: 700;
}

.letter-button:active {
  transform: scale(.94);
}

.letter-button.used {
  opacity: .32;
}

.utility-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  margin-bottom: 9px;
}

.soft-button,
.blue-button,
.submit-button,
.danger-button,
.primary-button {
  min-height: 48px;
  border-radius: 14px;
  padding: 10px 14px;
  font-weight: 700;
}

.soft-button {
  background: var(--surface);
  border: 1px solid var(--border);
}

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

.submit-button {
  width: 100%;
  margin-bottom: 9px;
  background: var(--success);
  color: #fff;
}

.danger-button {
  width: 100%;
  background: color-mix(in srgb, var(--danger) 76%, #7f1d1d);
  color: #fff;
}

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

.finish-secondary {
  min-width: 180px;
}

.finish-icon {
  font-size: 3rem;
}

.bottom-nav {
  position: fixed;
  z-index: 20;
  right: 50%;
  bottom: 0;
  transform: translateX(50%);
  width: min(100%, 560px);
  min-height: var(--nav-height);
  padding: 8px 12px max(8px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(16px);
}

.nav-item {
  min-height: 48px;
  border-radius: 14px;
  background: transparent;
  display: grid;
  place-items: center;
  gap: 2px;
  color: var(--muted);
}

.nav-item span {
  font-size: 1.05rem;
}

.nav-item small {
  font-size: .69rem;
  color: inherit;
}

.nav-item.active {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
}

.popup-backdrop {
  position: fixed;
  z-index: 50;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, .52);
}

.popup-card {
  width: min(100%, 360px);
  padding: 20px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  text-align: center;
}

.popup-card p {
  margin: 10px 0 18px;
  color: var(--muted);
  line-height: 1.8;
}

@media (max-width: 370px) {
  .stats-strip {
    grid-template-columns: 1fr;
  }

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

  .input-slot {
    width: 38px;
    height: 43px;
  }
}


.finish-result {
  line-height: 1.9;
  font-weight: 700;
}


.history-section {
  margin-top: 18px;
  padding: 16px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.history-status {
  padding: 18px 4px 4px;
  text-align: center;
  line-height: 1.8;
}

.history-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.history-item {
  display: grid;
  gap: 6px;
  padding: 12px;
  border-radius: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.history-item span,
.history-item small {
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.65;
}

.text-button {
  background: transparent;
  color: var(--accent);
  padding: 6px;
}

.toast {
  position: fixed;
  z-index: 30;
  left: 16px;
  right: 16px;
  bottom: calc(var(--nav-height) + 18px + env(safe-area-inset-bottom));
  width: min(calc(100% - 32px), 528px);
  margin: 0 auto;
  padding: 13px 15px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--danger) 25%, var(--surface));
  border: 1px solid var(--border);
  text-align: center;
  line-height: 1.7;
}

.toast.success {
  background: color-mix(in srgb, var(--success) 22%, var(--surface));
}

button[aria-busy="true"] {
  opacity: .65;
}

/* Patch 010.1: Friend invite and waiting room */
.friend-option-block {
  margin-bottom: 18px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
}

.choice-grid {
  display: grid;
  gap: 10px;
}

.four-choice {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.three-choice {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.two-choice {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.choice-button {
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

.choice-button:active {
  transform: scale(0.98);
}

.choice-button.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, var(--surface));
  color: var(--accent);
}

.friend-status-card {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
}

.friend-status-card p {
  margin: 5px 0 0;
  color: var(--muted);
  line-height: 1.7;
}

.friend-status-icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  border-radius: 16px;
  background: var(--surface-2);
  font-size: 24px;
}

.friend-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}

.friend-summary-grid > div {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-height: 76px;
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
}

.friend-summary-grid span {
  color: var(--muted);
  font-size: 12px;
}

.friend-summary-grid strong {
  font-size: 15px;
  line-height: 1.5;
}

#friend-waiting-screen > button,
#friend-settings-screen > .primary-button {
  width: 100%;
  margin-top: 10px;
}

.inline-status {
  margin: 12px 0 0;
  text-align: center;
  line-height: 1.7;
}

.inline-status.error {
  color: var(--danger);
}

.friend-ready-summary {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 10px 0 16px;
}

.friend-ready-summary > * {
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 13px;
}

.friend-next-note {
  max-width: 440px;
  margin: 0 auto 18px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.9;
}

@media (max-width: 380px) {
  .three-choice {
    grid-template-columns: 1fr;
  }

  .two-choice {
    grid-template-columns: 1fr;
  }
}

/* Patch 010.2: live friend match */
.friend-ready-action {
  width: min(100%, 390px);
  margin-top: 10px;
}

.friend-lobby-presence {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 2px 0 12px;
  color: var(--muted);
  font-size: 13px;
}

.presence-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--muted) 15%, transparent);
}

.presence-dot.online {
  background: var(--success);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--success) 18%, transparent);
}

.friend-game-topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px minmax(0, 1fr);
  gap: 8px;
  margin: -10px -4px 14px;
  padding: 8px 4px 12px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(14px);
}

.friend-player-card,
.friend-clock-card {
  min-height: 78px;
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: 17px;
  background: var(--surface);
  text-align: center;
}

.friend-player-card {
  display: grid;
  gap: 3px;
}

.friend-player-card span,
.friend-player-card small,
.friend-clock-card span,
.friend-clock-card small {
  color: var(--muted);
  font-size: 11px;
}

.friend-player-card strong {
  font-size: 1.45rem;
  color: var(--accent);
}

.friend-player-card.opponent strong {
  color: var(--blue);
}

.friend-clock-card {
  display: grid;
  align-content: center;
  gap: 2px;
}

.friend-clock-card strong {
  font-size: 1.55rem;
  font-variant-numeric: tabular-nums;
}

.friend-clock-card.countdown {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
}

.friend-clock-card.urgent strong {
  color: var(--danger);
}

.friend-word-card {
  margin-bottom: 14px;
}

#friend-game-screen .keyboard-area {
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
}

#friend-game-status {
  min-height: 24px;
}

.friend-result-score {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  width: min(100%, 390px);
  margin: 8px auto 18px;
}

.friend-result-score > div {
  display: grid;
  gap: 6px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
}

.friend-result-score span {
  color: var(--muted);
  font-size: 12px;
}

.friend-result-score strong {
  font-size: 1.55rem;
}

#friend-result-home-button {
  width: min(100%, 390px);
}

@media (max-width: 380px) {
  .friend-game-topbar {
    grid-template-columns: minmax(0, 1fr) 82px minmax(0, 1fr);
  }

  .friend-player-card,
  .friend-clock-card {
    padding: 8px 5px;
  }

  .friend-player-card small {
    font-size: 10px;
  }
}


/* Patch 010.2.1: one-tap friend entry UX */
.friend-quick-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 15px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
}

.friend-quick-card > div {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.friend-quick-card span,
.friend-entry-note,
.friend-chat-note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.8;
}

.friend-quick-card strong {
  font-size: 14px;
  line-height: 1.7;
}

.compact-button {
  width: auto;
  min-width: max-content;
  padding: 9px 12px;
  margin: 0;
}

.friend-advanced-settings {
  animation: friend-settings-open 160ms ease-out;
}

.friend-entry-note,
.friend-chat-note {
  margin: 8px 2px 12px;
  text-align: center;
}

.friend-chat-button {
  width: 100%;
  margin-top: 10px;
}

@keyframes friend-settings-open {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 380px) {
  .friend-quick-card {
    align-items: stretch;
    flex-direction: column;
  }

  .compact-button {
    width: 100%;
  }
}

/* Phase 3: player progression, history and leaderboard */
.progress-card {
  margin-top: 12px;
  padding: 14px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.progress-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.progress-card-head > div {
  display: grid;
  gap: 4px;
}

.progress-card-head span,
.progress-card small {
  color: var(--muted);
  font-size: .74rem;
}

.progress-track {
  height: 9px;
  margin: 12px 0 8px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.progress-track > span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .25s ease;
}

.leaderboard-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 8px 0 10px;
}

.leaderboard-tab {
  min-height: 42px;
  border-radius: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  font-weight: 700;
}

.leaderboard-tab.active {
  background: color-mix(in srgb, var(--accent) 18%, var(--surface));
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}

.leaderboard-me {
  padding: 8px 2px 4px;
  text-align: center;
  font-weight: 700;
}

.leaderboard-list {
  display: grid;
  gap: 9px;
  margin-top: 12px;
}

.leaderboard-item {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.leaderboard-item.is-viewer {
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}

.leaderboard-rank {
  text-align: center;
  font-size: 1.05rem;
}

.leaderboard-identity {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.leaderboard-identity strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-identity small {
  color: var(--muted);
  font-size: .72rem;
}

.leaderboard-value {
  font-size: 1.05rem;
}

.history-item.match-win {
  border-color: color-mix(in srgb, var(--success) 40%, var(--border));
}

.history-item.match-loss {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--border));
}
