/* ════════════════════════════════════════════════════════════════════
   TypeArena — MEGA PERFORMANCE & UX v5
   Супер-оптимизация производительности, кэширование, быстрая загрузка
   ════════════════════════════════════════════════════════════════════ */

@media (max-width: 767px) {
  /* ─────── PERFORMANCE OPTIMIZATION ─────── */
  * {
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }

  body {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  /* ─────── WILL-CHANGE OPTIMIZATION ─────── */
  .page {
    will-change: opacity, transform;
  }

  .main-content {
    will-change: scroll-position;
  }

  .progress-fill,
  .achievement-popup,
  .fab {
    will-change: transform, box-shadow;
  }

  /* ─────── GPU ACCELERATION ─────── */
  button,
  .stat-card,
  .home-mode-card,
  .mode-card-large,
  .shop-item,
  .profile-section {
    transform: translateZ(0);
  }

  /* ─────── FONT OPTIMIZATION ─────── */
  body {
    font-family: 'Manrope', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    line-height: 1.5;
  }

  h1, .title-1 {
    font-size: 1.8rem;
    line-height: 1.2;
  }

  h2, .title-2 {
    font-size: 1.4rem;
    line-height: 1.3;
  }

  h3, .title-3 {
    font-size: 1.1rem;
    line-height: 1.4;
  }

  small, .text-small {
    font-size: 0.75rem;
  }

  /* ─────── IMAGE OPTIMIZATION ─────── */
  img {
    max-width: 100%;
    height: auto;
    display: block;
  }

  /* ─────── SCROLL PERFORMANCE ─────── */
  .main-content {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
  }

  /* ─────── TOUCH OPTIMIZATION ─────── */
  input[type="text"],
  input[type="password"],
  input[type="email"],
  button,
  a {
    -webkit-touch-callout: none;
  }

  button,
  a,
  .clickable {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* ─────── BATTERY SAVER MODE ─────── */
  @media (prefers-color-scheme: dark) {
    body[data-graphics="low"] {
      background-color: #000;
    }
  }

  /* ─────── NETWORK INFO ─────── */
  @media (prefers-reduced-motion: reduce) {
    * {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
  }

  /* ─────── LAZY LOADING PLACEHOLDER ─────── */
  .lazy-placeholder {
    background: var(--bg2);
    border-radius: 8px;
    min-height: 60px;
    animation: pulse 2s ease-in-out infinite;
  }

  /* ─────── CONTENT VISIBILITY ─────── */
  .off-screen {
    content-visibility: auto;
  }

  /* ─────── SKELETON SCREEN ─────── */
  .skeleton {
    background: linear-gradient(
      90deg,
      var(--bg2) 0%,
      var(--bg3) 50%,
      var(--bg2) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s ease-in-out infinite;
    border-radius: 4px;
  }

  /* ─────── CONTAINER QUERIES ─────── */
  .responsive-container {
    container-type: inline-size;
  }

  @supports (container-type: inline-size) {
    @container (max-width: 400px) {
      .mode-card-large {
        padding: 8px;
      }

      .stat-badge {
        font-size: 0.6rem;
      }
    }

    @container (min-width: 401px) {
      .mode-card-large {
        padding: 12px;
      }
    }
  }

  /* ─────── PREFERS COLOR SCHEME ─────── */
  @media (prefers-color-scheme: dark) {
    :root {
      color-scheme: dark;
    }
  }

  @media (prefers-color-scheme: light) {
    :root {
      color-scheme: light;
    }
  }

  /* ─────── HIGH DPI OPTIMIZATION ─────── */
  @media (min-resolution: 150dpi) {
    button,
    .stat-card {
      border-width: 0.5px;
    }
  }

  /* ─────── VISIBILITY API ─────── */
  .page {
    display: none !important;
  }

  .page.visible {
    display: block !important;
  }

  /* ─────── SCROLL SNAP ─────── */
  .main-content {
    scroll-snap-type: y proximity;
  }

  .page {
    scroll-snap-align: start;
  }

  /* ─────── FOCUS VISIBLE ─────── */
  button:focus-visible,
  a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  /* ─────── CUSTOM SCROLLBAR ─────── */
  ::-webkit-scrollbar {
    width: 6px;
  }

  ::-webkit-scrollbar-track {
    background: var(--bg);
  }

  ::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
  }

  ::-webkit-scrollbar-thumb:hover {
    background: var(--text2);
  }

  /* ─────── INPUT OPTIMIZATION ─────── */
  input[type="text"],
  input[type="password"],
  input[type="email"],
  textarea {
    font-size: 16px; /* Prevents zoom on iOS */
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg2);
    color: var(--text);
  }

  input::placeholder {
    color: var(--text2);
  }

  /* ─────── LOADING STATE ─────── */
  .is-loading {
    pointer-events: none;
    opacity: 0.6;
  }

  .loader {
    width: 32px;
    height: 32px;
    border: 3px solid rgba(88, 166, 255, 0.2);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }

  /* ─────── EMPTY STATE ─────── */
  .empty-state-improved {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
  }

  .empty-state-improved-icon {
    font-size: 4rem;
    margin-bottom: 12px;
  }

  .empty-state-improved-title {
    font-size: 1rem;
    font-weight: 800;
    color: var(--text);
    margin-bottom: 6px;
  }

  .empty-state-improved-desc {
    font-size: 0.85rem;
    color: var(--text2);
    line-height: 1.4;
  }

  /* ─────── ERROR STATE ─────── */
  .error-state {
    background: rgba(255, 100, 100, 0.1);
    border: 1px solid rgba(255, 100, 100, 0.3);
    border-radius: 8px;
    padding: 12px;
    margin: 12px 0;
    text-align: center;
  }

  .error-icon {
    font-size: 2rem;
    margin-bottom: 8px;
  }

  .error-text {
    font-size: 0.9rem;
    color: #ff6464;
    font-weight: 600;
  }

  /* ─────── SUCCESS TOAST ─────── */
  .toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 16px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text);
    z-index: 200;
    animation: slideUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  .toast.success {
    border-color: #4ade80;
    background: rgba(74, 222, 128, 0.1);
    color: #4ade80;
  }

  .toast.error {
    border-color: #ff6464;
    background: rgba(255, 100, 100, 0.1);
    color: #ff6464;
  }

  /* ─────── RESPONSIVE TEXT ─────── */
  @media (max-width: 480px) {
    h1, .title-1 { font-size: 1.4rem; }
    h2, .title-2 { font-size: 1.1rem; }
    h3, .title-3 { font-size: 0.95rem; }
    body { font-size: 13px; }
  }

  /* ─────── IMPROVED TRANSITIONS ─────── */
  body[data-graphics="low"] {
    scroll-behavior: auto;
  }

  body[data-graphics="medium"],
  body[data-graphics="high"] {
    scroll-behavior: smooth;
  }

  /* ─────── BUTTON STATES ─────── */
  button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  button:disabled:active {
    transform: none;
  }

  /* ─────── CARD IMPROVEMENTS ─────── */
  .card {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    transition: all 0.2s;
  }

  .card:hover {
    border-color: var(--text2);
  }

  .card:active {
    transform: translateY(-2px);
  }

  /* ─────── BADGE IMPROVEMENTS ─────── */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: rgba(88, 166, 255, 0.2);
    border: 1px solid rgba(88, 166, 255, 0.4);
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--accent);
  }

  /* ─────── DIVIDER ─────── */
  .divider {
    height: 1px;
    background: linear-gradient(
      90deg,
      transparent,
      var(--border),
      transparent
    );
    margin: 16px 0;
  }

  /* ─────── SECTION SPACING ─────── */
  .section {
    margin-bottom: 16px;
  }

  .section:last-child {
    margin-bottom: 0;
  }

  /* ─────── SMOOTH COLOR TRANSITIONS ─────── */
  body[data-graphics="high"] * {
    transition: color 0.2s ease, background-color 0.2s ease;
  }

  /* ─────── SAFE AREA INSET USAGE ─────── */
  .safe-area-top {
    padding-top: env(safe-area-inset-top, 0);
  }

  .safe-area-bottom {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .safe-area-left {
    padding-left: env(safe-area-inset-left, 0);
  }

  .safe-area-right {
    padding-right: env(safe-area-inset-right, 0);
  }
}
