/* ════════════════════════════════════════════════════════════════════
   TypeArena — FINAL MOBILE UI v3
   Полная переделка интерфейса для мобильных
   ════════════════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
  /* ─────── BODY & APP ─────── */
  body {
    --safe-top: max(env(safe-area-inset-top), 8px);
    --safe-bottom: max(env(safe-area-inset-bottom), 8px);
  }

  html, body {
    position: fixed;
    width: 100%;
    height: 100vh;
    overflow: hidden;
  }

  #app {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    overflow: hidden;
  }

  /* ─────── HIDE OLD TOPBAR & PROFILE ─────── */
  .topbar,
  .profile-pill,
  .topbar-spacer {
    display: none !important;
  }

  /* ─────── MAIN CONTENT AREA ─────── */
  .main-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding-top: 0;
    padding-bottom: 0 !important;
    position: relative;
  }

  /* ─────── PAGES (Absolute Positioning) ─────── */
  .page {
    position: absolute !important;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px;
    padding-bottom: 80px;
    display: none;
  }

  .page.visible {
    display: block;
  }

  .page::-webkit-scrollbar {
    width: 4px;
  }

  .page::-webkit-scrollbar-thumb {
    background: rgba(88, 166, 255, 0.3);
    border-radius: 2px;
  }

  /* ─────── BOTTOM NAVIGATION BAR ─────── */
  .bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    padding-bottom: var(--safe-bottom);
    background: linear-gradient(
      to top,
      rgba(13, 17, 23, 0.98),
      rgba(13, 17, 23, 0.92)
    );
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
    z-index: 200;
    gap: 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.5);
  }

  /* ─────── NAVIGATION BUTTONS ─────── */
  .bottom-nav-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    flex: 1;
    height: 60px;
    background: none;
    border: none;
    color: var(--text2);
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
    font-size: 0;
    padding: 0;
    position: relative;
  }

  .bottom-nav-btn:active {
    background: rgba(88, 166, 255, 0.06);
  }

  /* Icon */
  .bottom-nav-btn .icon {
    font-size: 22px;
    line-height: 1;
    display: block;
  }

  /* Label */
  .bottom-nav-btn .label {
    font-size: 0.6rem;
    font-weight: 700;
    color: var(--text2);
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

  /* Active state */
  .bottom-nav-btn.active {
    color: var(--accent);
  }

  .bottom-nav-btn.active .icon {
    color: var(--accent);
  }

  .bottom-nav-btn.active .label {
    color: var(--accent);
  }

  .bottom-nav-btn.active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: var(--accent);
  }

  /* ─────── GAME PAGE FULLSCREEN ─────── */
  #page-game {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100vh;
    z-index: 150;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    padding: 0 !important;
    margin: 0 !important;
  }

  /* All game wrappers */
  .classic-wrapper,
  .falling-wrapper,
  .zombie-wrapper,
  .osu-wrapper,
  .sapper-wrapper,
  .detective-wrapper,
  .boss-wrapper,
  .royale-wrapper,
  .hacker-wrapper {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    z-index: 151;
  }

  /* ─────── HUD (SCORE, WPM, ETC) ─────── */
  .classic-hud,
  .falling-hud,
  .zombie-hud,
  .osu-hud,
  .sapper-hud,
  .detective-hud,
  .boss-hud,
  .royale-hud,
  .hacker-hud {
    order: 1;
    flex-shrink: 0;
    padding: 6px 8px !important;
    gap: 6px !important;
    font-size: 0.65rem !important;
    background: rgba(13, 17, 23, 0.85);
    border-bottom: 1px solid rgba(88, 166, 255, 0.1);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    min-height: auto;
    line-height: 1.2;
  }

  .hud-item {
    font-size: 0.65rem !important;
    padding: 2px 4px !important;
    white-space: nowrap;
    display: flex;
    gap: 2px;
    align-items: center;
  }

  .hud-item strong {
    font-weight: 700;
    color: var(--accent);
  }

  /* Hide some HUD items on very small screens */
  @media (max-width: 480px) {
    .hud-item:nth-child(n + 6) {
      display: none;
    }
  }

  /* ─────── GAME CONTENT (scrollable area) ─────── */
  .classic-text-area,
  .falling-field,
  .zombie-list,
  .zombie-text-area,
  .osu-arena,
  .sapper-board,
  .detective-board,
  .boss-arena,
  .boss-spell-area,
  .royale-leaderboard,
  .royale-text-area,
  .hacker-arena {
    order: 2;
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
  }

  /* ─────── KEYBOARD SECTION (fixed at bottom) ─────── */
  #global-keyboard-section {
    position: fixed;
    bottom: 60px; /* Above bottom nav */
    left: 0;
    right: 0;
    width: 100%;
    z-index: 160;
    max-height: 280px;
    overflow: hidden;
    background: var(--bg);
    border-top: 1px solid var(--border);
  }

  .keyboard-section {
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 0 !important;
    max-height: 280px;
    width: 100%;
  }

  /* Virtual keyboard styling */
  #virtual-keyboard {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(32px, 1fr));
    gap: 2px;
    padding: 4px;
    background: var(--bg2);
  }

  .key {
    padding: 8px 4px !important;
    font-size: 0.7rem !important;
    border-radius: 4px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.1s, box-shadow 0.1s;
    border: 1px solid var(--border);
    background: var(--bg3);
    color: var(--text);
  }

  .key:active {
    background: var(--accent);
    color: white;
    box-shadow: none;
  }

  /* ─────── GAME-SPECIFIC LAYOUTS ─────── */

  /* Classic Mode */
  .classic-wrapper .classic-hud {
    order: 1;
  }

  .classic-wrapper .classic-text-area {
    order: 2;
    padding: 12px 10px !important;
    font-size: 1.2rem !important;
    line-height: 1.9 !important;
    text-align: center;
    word-break: break-word;
  }

  /* Falling Mode */
  .falling-wrapper .falling-field {
    order: 2;
    min-height: 220px;
  }

  /* Zombie Mode */
  .zombie-wrapper .zombie-list {
    order: 2;
    flex-shrink: 0;
    max-height: 60px;
    margin-bottom: 4px;
  }

  .zombie-wrapper .zombie-text-area {
    order: 3;
    padding: 10px 8px !important;
    font-size: 1.1rem !important;
  }

  /* OSU Mode */
  .osu-wrapper .osu-arena {
    order: 2;
    justify-content: center;
    align-items: center;
    padding: 12px 8px !important;
    gap: 16px;
  }

  .osu-circle {
    width: 54px !important;
    height: 54px !important;
    font-size: 1.6rem !important;
    flex-shrink: 0;
  }

  /* Sapper Mode */
  .sapper-wrapper .sapper-hud {
    order: 1;
  }

  .sapper-wrapper .sapper-board {
    order: 2;
    flex: 1;
  }

  .sapper-wrapper .sb-code-display {
    order: 3;
    flex-shrink: 0;
    padding: 8px !important;
    font-size: 0.95rem !important;
    border-top: 1px solid var(--border);
  }

  /* Detective Mode */
  .detective-wrapper .detective-board {
    order: 2;
    padding: 16px 10px !important;
    font-size: 1.1rem !important;
    text-align: center;
  }

  .det-sentence {
    line-height: 1.8 !important;
  }

  /* Boss Mode */
  .boss-wrapper .boss-hud {
    order: 1;
  }

  .boss-wrapper .boss-arena {
    order: 2;
    flex-shrink: 0;
    max-height: 100px;
    padding: 6px 8px !important;
    overflow: hidden;
  }

  .boss-wrapper .boss-spell-area {
    order: 3;
    flex: 1;
    padding: 8px 6px !important;
  }

  .bs-spell-text {
    font-size: 1rem !important;
  }

  /* Royale Mode */
  .royale-wrapper .royale-leaderboard {
    order: 2;
    flex-shrink: 0;
    max-height: 80px;
  }

  .royale-wrapper .royale-text-area {
    order: 3;
    flex: 1;
    padding: 8px !important;
  }

  /* Hacker Mode */
  .hacker-wrapper .hacker-arena {
    order: 2;
    flex-direction: column;
    padding: 12px 8px !important;
    gap: 12px;
  }

  .hacker-display {
    padding: 12px 16px !important;
  }

  .hk-word {
    font-size: 1.3rem !important;
  }

  /* ─────── HOME PAGE RESPONSIVE ─────── */
  #page-home {
    padding: 0 !important;
  }

  .home-layout {
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .home-hero {
    padding: 12px 10px;
    border-bottom: 1px solid var(--border);
  }

  .home-hero-left {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .hero-title {
    font-size: 1.4rem !important;
    font-weight: 800;
    line-height: 1.2;
    margin: 0 !important;
  }

  .hero-subtitle {
    font-size: 0.85rem;
    color: var(--text2);
    margin: 0 !important;
    line-height: 1.5;
  }

  .quick-stats {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin-top: 8px;
  }

  .qs-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px;
    background: var(--bg2);
    border-radius: 8px;
    text-align: center;
  }

  .qs-val {
    font-size: 1.6rem;
    font-weight: 800;
    color: var(--accent);
  }

  .qs-lbl {
    font-size: 0.65rem;
    color: var(--text2);
    font-weight: 600;
  }

  .home-hero-vip {
    display: none !important;
  }

  .home-ui-toggle {
    display: none !important;
  }

  .section-label {
    padding: 10px;
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text2);
    letter-spacing: 0.05em;
  }

  .mode-cards {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 0 8px 12px;
  }

  .mode-card {
    padding: 12px;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    display: grid;
    grid-template-columns: 40px 1fr;
    gap: 10px;
    align-items: start;
  }

  .mode-card:active {
    background: rgba(88, 166, 255, 0.15);
    border-color: var(--accent);
  }

  .mc-icon {
    font-size: 1.8rem;
    grid-column: 1 / 2;
    grid-row: 1 / 3;
  }

  .mc-name {
    font-weight: 700;
    font-size: 0.95rem;
    color: var(--text);
    grid-column: 2;
  }

  .mc-desc {
    font-size: 0.75rem;
    color: var(--text2);
    line-height: 1.4;
    grid-column: 2;
  }

  .mc-badge {
    display: none;
  }

  .mode-card.ui-advanced {
    display: none;
  }

  /* ─────── OTHER PAGES ─────── */
  .page {
    padding-bottom: 80px !important;
  }

  /* ─────── SCROLLBAR STYLING ─────── */
  .main-content::-webkit-scrollbar,
  .classic-text-area::-webkit-scrollbar,
  .falling-field::-webkit-scrollbar,
  .zombie-list::-webkit-scrollbar,
  .sapper-board::-webkit-scrollbar,
  .boss-spell-area::-webkit-scrollbar {
    width: 4px;
  }

  .main-content::-webkit-scrollbar-track,
  .classic-text-area::-webkit-scrollbar-track,
  .falling-field::-webkit-scrollbar-track,
  .zombie-list::-webkit-scrollbar-track,
  .sapper-board::-webkit-scrollbar-track,
  .boss-spell-area::-webkit-scrollbar-track {
    background: transparent;
  }

  .main-content::-webkit-scrollbar-thumb,
  .classic-text-area::-webkit-scrollbar-thumb,
  .falling-field::-webkit-scrollbar-thumb,
  .zombie-list::-webkit-scrollbar-thumb,
  .sapper-board::-webkit-scrollbar-thumb,
  .boss-spell-area::-webkit-scrollbar-thumb {
    background: rgba(88, 166, 255, 0.3);
    border-radius: 2px;
  }
}

/* ════════════════════════════════════════════════════════════════════
   GRAPHICS QUALITY SETTINGS — ULTRA AGGRESSIVE OPTIMIZATION
   ════════════════════════════════════════════════════════════════════ */

/* LOW GRAPHICS — MAXIMUM PERFORMANCE */
body[data-graphics="low"] {
  --transition: none;
  --animation: none;
}

body[data-graphics="low"] * {
  animation: none !important;
  transition: none !important;
  animation-duration: 0s !important;
  transition-duration: 0s !important;
}

/* Remove all blur effects */
body[data-graphics="low"] .topbar,
body[data-graphics="low"] .bottom-nav,
body[data-graphics="low"] .keyboard-section,
body[data-graphics="low"] #virtual-keyboard {
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

/* Remove shadows */
body[data-graphics="low"] * {
  box-shadow: none !important;
  text-shadow: none !important;
  filter: none !important;
}

/* Disable fancy effects */
body[data-graphics="low"] .key:active {
  background: var(--accent);
  color: white;
  box-shadow: none !important;
  filter: none !important;
}

body[data-graphics="low"] .mode-card:active {
  background: rgba(88, 166, 255, 0.1);
  box-shadow: none !important;
}

/* Remove gradient backgrounds */
body[data-graphics="low"] .bottom-nav {
  background: var(--bg) !important;
  border-top: 1px solid var(--border);
}

/* Disable hover effects */
body[data-graphics="low"] .bottom-nav-btn:hover,
body[data-graphics="low"] .key:hover,
body[data-graphics="low"] .mode-card:hover {
  opacity: 1;
  background: unchanged;
}

/* Remove transform animations */
body[data-graphics="low"] .confetti,
body[data-graphics="low"] .particle,
body[data-graphics="low"] [class*="animate"],
body[data-graphics="low"] [class*="bounce"],
body[data-graphics="low"] [class*="pulse"] {
  animation: none !important;
  transform: none !important;
}

/* Simplify game elements */
body[data-graphics="low"] .osu-circle {
  box-shadow: none !important;
  filter: none !important;
}

body[data-graphics="low"] .boss-avatar {
  filter: none !important;
  box-shadow: none !important;
}

body[data-graphics="low"] .zombie-item {
  filter: none !important;
  box-shadow: none !important;
}

body[data-graphics="low"] .falling-word {
  text-shadow: none !important;
  box-shadow: none !important;
}

/* Reduce motion requests */
@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

/* MEDIUM GRAPHICS (default) */
body[data-graphics="medium"] {
  --transition: 0.15s ease;
}

body[data-graphics="medium"] .bottom-nav {
  background: linear-gradient(
    to top,
    rgba(13, 17, 23, 0.98),
    rgba(13, 17, 23, 0.92)
  );
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

body[data-graphics="medium"] .key:active {
  box-shadow: 0 0 8px rgba(88, 166, 255, 0.3);
}

/* HIGH GRAPHICS */
body[data-graphics="high"] {
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

body[data-graphics="high"] .bottom-nav {
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.6);
}

body[data-graphics="high"] .key:active {
  box-shadow: 0 0 12px rgba(88, 166, 255, 0.5);
}

body[data-graphics="high"] .boss-avatar {
  filter: drop-shadow(0 0 16px rgba(220, 38, 38, 0.7));
}

/* ════════════════════════════════════════════════════════════════════
   EXTRA SMALL SCREENS (<480px)
   ════════════════════════════════════════════════════════════════════ */

@media (max-width: 480px) {
  .bottom-nav-btn .icon {
    font-size: 20px;
  }

  .bottom-nav-btn .label {
    font-size: 0.55rem;
  }

  .bottom-nav {
    height: 56px;
  }

  .hero-title {
    font-size: 1.2rem !important;
  }

  .quick-stats {
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
  }

  .qs-val {
    font-size: 1.4rem;
  }

  .classic-text-area {
    font-size: 1rem !important;
    padding: 10px 8px !important;
  }

  .mode-card {
    grid-template-columns: 36px 1fr;
    gap: 8px;
    padding: 10px;
  }

  .mc-icon {
    font-size: 1.6rem;
  }

  .mc-name {
    font-size: 0.9rem;
  }

  .mc-desc {
    font-size: 0.7rem;
  }
}

/* ════════════════════════════════════════════════════════════════════
   ZOMBIE MODE ENHANCEMENTS
   ════════════════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
  /* Zombie элементы */
  .zombie-item {
    position: absolute;
    width: auto;
    min-width: 60px;
    padding: 6px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 600;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    transition: opacity 0.3s ease;
    will-change: left, top;
  }

  /* Zombie word styling */
  .zombie-word {
    display: flex;
    align-items: center;
    gap: 0;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.9rem;
    font-weight: 700;
    letter-spacing: 0.05em;
  }

  .zw-typed {
    color: var(--accent);
  }

  .zw-rest {
    color: var(--text2);
    opacity: 0.7;
  }

  /* Zombie типы - разные цвета */
  .zombie-item.zm-basic {
    background: rgba(100, 150, 200, 0.85);
    border: 1px solid rgba(100, 150, 200, 1);
  }

  .zombie-item.zm-runner {
    background: rgba(200, 120, 80, 0.85);
    border: 1px solid rgba(200, 120, 80, 1);
  }

  .zombie-item.zm-brute {
    background: rgba(180, 60, 60, 0.85);
    border: 1px solid rgba(180, 60, 60, 1);
  }

  .zombie-item.zm-ghost {
    background: rgba(150, 100, 200, 0.7);
    border: 1px dashed rgba(150, 100, 200, 1);
  }

  .zombie-item.zm-archer {
    background: rgba(180, 140, 60, 0.85);
    border: 1px solid rgba(180, 140, 60, 1);
  }

  .zombie-item.zm-explosive {
    background: rgba(220, 100, 60, 0.85);
    border: 1px solid rgba(220, 100, 60, 1);
  }

  .zombie-item.zm-screamer {
    background: rgba(200, 80, 80, 0.85);
    border: 1px solid rgba(200, 80, 80, 1);
  }

  .zombie-item.zm-shield {
    background: rgba(100, 180, 100, 0.85);
    border: 2px solid rgba(100, 180, 100, 1);
  }

  .zombie-item.zm-commander {
    background: rgba(220, 180, 60, 0.85);
    border: 2px solid rgba(220, 180, 60, 1);
  }

  .zombie-item.zm-necro {
    background: rgba(100, 50, 150, 0.85);
    border: 2px solid rgba(100, 50, 150, 1);
  }

  .zombie-item.zm-leaper {
    background: rgba(150, 100, 200, 0.85);
    border: 1px solid rgba(150, 100, 200, 1);
  }

  .zombie-item.zm-boss {
    background: rgba(220, 50, 50, 0.95);
    border: 3px solid rgba(220, 50, 50, 1);
  }

  /* Zombie ошибка */
  .zombie-item.zombie-error {
    background: rgba(220, 60, 60, 1) !important;
    animation: shake 0.3s ease;
  }

  @keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-3px); }
    75% { transform: translateX(3px); }
  }

  /* Zombie мёртв */
  .zombie-item.zombie-dead {
    opacity: 0;
    pointer-events: none;
  }

  /* На LOW графике - без анимаций, плоский дизайн */
  body[data-graphics="low"] .zombie-item {
    animation: none !important;
    transition: opacity 0.2s linear !important;
    box-shadow: none !important;
    filter: none !important;
  }

  body[data-graphics="low"] .zombie-item.zombie-error {
    animation: none !important;
  }

  body[data-graphics="low"] .zombie-word {
    text-shadow: none !important;
    filter: none !important;
  }

  /* На LOW графике - убираем сложные стили */
  body[data-graphics="low"] .zombie-item.zm-ghost {
    border: 1px solid rgba(150, 100, 200, 1) !important;
  }

  body[data-graphics="low"] .zombie-item.zm-commander {
    border: 1px solid rgba(220, 180, 60, 1) !important;
  }

  body[data-graphics="low"] .zombie-item.zm-shield {
    border: 1px solid rgba(100, 180, 100, 1) !important;
  }

  body[data-graphics="low"] .zombie-item.zm-necro {
    border: 1px solid rgba(100, 50, 150, 1) !important;
  }

  body[data-graphics="low"] .zombie-item.zm-boss {
    border: 2px solid rgba(220, 50, 50, 1) !important;
  }
}

/* ════════════════════════════════════════════════════════════════════
   ZOMBIE HUD FIX
   ════════════════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
  .zombie-hud {
    display: flex !important;
    flex-wrap: wrap;
    gap: 6px !important;
    align-items: center;
  }

  .zombie-list {
    display: flex !important;
    gap: 4px !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    padding: 4px 0 !important;
  }

  .zombie-list::-webkit-scrollbar {
    height: 2px;
  }

  .zombie-list::-webkit-scrollbar-thumb {
    background: rgba(88, 166, 255, 0.3);
  }

  /* Фокусированный зомби */
  .zombie-item.typing {
    box-shadow: 0 0 8px rgba(88, 166, 255, 0.4) !important;
    border-color: var(--accent) !important;
  }

  body[data-graphics="low"] .zombie-item.typing {
    box-shadow: none !important;
    border-color: var(--accent) !important;
  }
}
