/* ========================================
   MOBILE RESPONSIVE - KEYBOARD FIX
   ======================================== */

@media (max-width: 768px) {

  /* ===== VIEWPORT FIX ===== */
  html {
    height: 100vh;
    height: -webkit-fill-available;
  }

  body {
    height: 100vh;
    height: -webkit-fill-available;
    overflow: hidden;
    position: fixed;
    width: 100%;
  }

  /* Allow scrolling on results page */
  body.results-open {
    position: static;
    overflow: auto;
    overflow-x: hidden;
  }

  /* Hide title text on mobile - show only logo */
  .logo-wrapper h1 {
    display: none !important;
  }

  /* Reset Desktop auto-margin */
  /* Push Profile/Logout to right side on Mobile - REDUCED GAP */
  .logo-wrapper {
    margin-right: 0 !important;
    gap: 0 !important;
    /* Reduce internal gap */
  }

  /* Don't push leaderboard button so far */
  #leaderboardBtn {
    margin-right: 0 !important;
  }

  /* Use flex-grow spacer instead or just justify-content */
  .header-row {
    justify-content: space-between !important;
    gap: 4px !important;
    /* Tighter gap for mobile */
  }

  /* ===== MENU BUTTON ===== */
  .menu-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    position: fixed !important;
    top: 10px !important;
    right: 15px !important;
    z-index: 9999 !important;
    background: var(--bg-secondary) !important;
    box-shadow: 0 2px 8px var(--shadow) !important;
    transition: opacity 0.3s ease, transform 0.3s ease !important;
  }

  /* Hide menu button when sidebar is open */
  .menu-btn.hidden {
    opacity: 0;
    transform: scale(0.9);
    pointer-events: none;
  }

  /* ===== RIGHT PANEL ANIMATION ===== */
  @keyframes slideInPanel {
    from {
      right: -100%;
      opacity: 0;
    }

    to {
      right: 0;
      opacity: 1;
    }
  }

  @keyframes slideOutPanel {
    from {
      right: 0;
      opacity: 1;
    }

    to {
      right: -100%;
      opacity: 0;
    }
  }

  /* ===== TYPING SCREEN LAYOUT ===== */
  .main-layout {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: -webkit-fill-available;
    overflow: hidden;
    position: relative;
  }

  /* ===== LEFT PANEL - FIXED HEIGHT ===== */
  .left-panel {
    width: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex: 1;
    min-height: 0;
  }

  /* ===== PARAGRAPH DISPLAY - SHRUNK MORE ===== */
  .paragraph-display {
    flex: 1;
    min-height: 0;
    padding: 55px 20px 15px !important;
    font-size: 22px !important;
    line-height: 1.9 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    background: var(--bg-primary) !important;
    -webkit-overflow-scrolling: touch !important;
    position: relative;
    transition: all 0.3s ease !important;
  }

  /* CHANGED: Shrunk paragraph more (0.5 instead of 0.6) */
  .paragraph-display.keyboard-open {
    flex: 0.5 !important;
    padding: 45px 18px 10px !important;
    font-size: 21px !important;
    line-height: 1.8 !important;
  }

  /* ===== SEPARATOR ===== */
  .separator {
    display: block !important;
    height: 1px;
    margin: 0 !important;
    flex-shrink: 0;
  }

  /* Hide separator when keyboard open to save space */
  .separator.keyboard-open {
    display: none !important;
  }

  /* ===== TYPING INPUT AREA - MOVED UP SLIGHTLY ===== */
  .typed-wrapper {
    flex-shrink: 0;
    width: 100% !important;
    padding: 12px 20px 16px !important;
    background: var(--bg-secondary) !important;
    border-top: 2px solid var(--border-color) !important;
    box-sizing: border-box !important;
    transition: all 0.3s ease !important;
    /* CHANGED: Added negative margin to move up */
    margin-top: -5px !important;
  }

  /* CHANGED: More negative margin when keyboard open */
  .typed-wrapper.keyboard-open {
    padding: 8px 18px 10px !important;
    margin-top: -8px !important;
  }

  .typed-input {
    width: 100% !important;
    font-size: 16px !important;
    height: 110px !important;
    padding: 12px !important;
    resize: none !important;
    box-sizing: border-box !important;
    transition: all 0.3s ease !important;
  }

  /* Smaller input when keyboard is open */
  .typed-input.keyboard-open {
    height: 75px !important;
    font-size: 15px !important;
    padding: 10px !important;
  }

  /* ===== SIDEBAR ===== */
  .right-panel {
    position: fixed !important;
    top: 0 !important;
    right: -100% !important;
    width: 85% !important;
    max-width: 320px !important;
    height: 100vh !important;
    z-index: 2000 !important;
    transition: right 0.3s ease !important;
    box-shadow: -8px 0 30px var(--shadow-dark) !important;
    overflow-y: auto !important;
    display: flex !important;
    flex-direction: column !important;
    animation: slideOutPanel 0.3s ease forwards;
    background: var(--bg-secondary) !important;
    flex-wrap: nowrap !important;
  }

  .right-panel.open {
    right: 0 !important;
    animation: slideInPanel 0.3s ease forwards;
  }

  /* ===== CLOSE BUTTON ===== */
  .close-btn {
    display: flex !important;
    padding: 0 !important;
    width: 36px !important;
    height: 36px !important;
    align-items: center !important;
    justify-content: center !important;
    position: fixed !important;
    top: 10px !important;
    right: 15px !important;
    z-index: 10000 !important;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    cursor: pointer !important;
    -webkit-appearance: none !important;
    appearance: none !important;
    transition: opacity 0.3s ease !important;
    opacity: 0;
    pointer-events: none;
  }

  /* Add Menu Button Reset */
  .menu-btn {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
    cursor: pointer !important;
    -webkit-appearance: none !important;
    appearance: none !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  /* Force SVG Sizing */
  .menu-btn svg,
  .close-btn svg {
    width: 24px !important;
    height: 24px !important;
    display: block !important;
    stroke: var(--text-primary) !important;
  }

  .time-section {
    margin-top: 40px !important;
  }


  .close-btn.open {
    opacity: 1;
    pointer-events: auto;
  }

  /* ===== OVERLAY ===== */
  .overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1500;
  }

  .overlay.show {
    display: block !important;
  }

  /* ===== RESULTS LAYOUT ===== */
  .results-layout {
    flex-direction: column;
    padding: 15px;
    overflow-y: auto !important;
    height: auto !important;
    min-height: 100vh;
    padding-bottom: 80px;
  }

  .results-left {
    padding: 22px 20px;
  }

  .results-right {
    width: 100%;
    padding: 22px 20px;
    padding-bottom: 40px;
  }

  /* Fix results screen to allow scroll */
  #resultsScreen.active {
    overflow-y: auto !important;
    height: 100vh !important;
    position: relative !important;
  }

  /* ===== CHART CONTAINER FIX ===== */
  .chart-container {
    max-height: 200px !important;
    overflow-y: auto !important;
    margin-bottom: 20px !important;
    padding: 15px !important;
  }

  /* ===== CONTAINER ===== */
  .container {
    padding: 30px 25px;
  }
}

/* ===== SMALLER PHONES ===== */
@media (max-width: 480px) {
  .container {
    padding: 25px 20px;
  }

  .container h1 {
    font-size: 24px;
  }

  .paragraph-display {
    padding: 50px 16px 12px !important;
    font-size: 17.6px !important;
    line-height: 1.65 !important;
  }

  /* CHANGED: Shrunk more (0.45 instead of higher) */
  .paragraph-display.keyboard-open {
    padding: 40px 14px 8px !important;
    font-size: 16.5px !important;
    line-height: 1.55 !important;
    flex: 0.45 !important;
  }

  .menu-btn {
    width: 32px !important;
    height: 32px !important;
    font-size: 18px !important;
    top: 8px !important;
    right: 12px !important;
  }

  .menu-btn svg {
    width: 20px !important;
    height: 20px !important;
  }

  .close-btn svg {
    width: 18px !important;
    height: 18px !important;
  }

  .typed-wrapper {
    padding: 10px 16px 14px !important;
    /* CHANGED: Move up slightly */
    margin-top: -4px !important;
  }

  .typed-wrapper.keyboard-open {
    padding: 6px 14px 8px !important;
    /* CHANGED: More negative margin */
    margin-top: -7px !important;
  }

  .typed-input {
    font-size: 15px !important;
    height: 100px !important;
    padding: 10px !important;
  }

  .typed-input.keyboard-open {
    height: 70px !important;
    font-size: 14px !important;
    padding: 8px !important;
  }

  .right-panel {
    max-width: 300px !important;
    width: 90% !important;
  }
}

/* ===== VERY SMALL PHONES (iPhone SE) ===== */
@media (max-width: 360px) {
  .paragraph-display {
    padding: 48px 14px 10px !important;
    font-size: 16.5px !important;
  }

  /* CHANGED: Shrunk more */
  .paragraph-display.keyboard-open {
    padding: 38px 12px 6px !important;
    font-size: 15.4px !important;
    line-height: 1.5 !important;
    flex: 0.4 !important;
  }

  .typed-input {
    font-size: 14px !important;
    height: 90px !important;
  }

  .typed-input.keyboard-open {
    height: 65px !important;
    font-size: 13px !important;
  }

  .right-panel {
    max-width: 280px !important;
    padding: 15px 12px !important;
    gap: 10px !important;
    display: flex !important;
    flex-direction: column !important;
    flex-wrap: nowrap !important;
  }

  /* FORCE BUTTONS TO STACK */
  .right-panel>button,
  .right-panel .btn {
    width: 100% !important;
    flex: 0 0 auto !important;
    margin: 5px 0 !important;
    max-width: 100% !important;
  }

  .typed-wrapper {
    margin-top: -4px !important;
  }

  .typed-wrapper.keyboard-open {
    margin-top: -6px !important;
  }

  /* Fix buttons visibility on very small screens */
  .right-panel .btn {
    padding: 10px !important;
    font-size: 12px !important;
    margin-top: 4px !important;
  }

  .time-section {
    padding: 12px !important;
  }

  .time-section .value {
    font-size: 28px !important;
  }

  .live-stats-card {
    padding: 10px !important;
  }

  .stat-row {
    padding: 6px 0 !important;
  }

  .progress-circle {
    width: 120px !important;
    height: 120px !important;
  }

  .control-row {
    padding: 8px 10px !important;
  }

  /* Hide "Typing Test" title on very small screens */
  .logo-wrapper h1 {
    display: none !important;
  }

  /* Compact badges for small screens */
  .status-install-row .xp-level-inline,
  .status-install-row .xp-points-inline {
    padding: 4px 8px !important;
    font-size: 10px !important;
    gap: 4px !important;
  }
}

/* ===== SMALLER PHONES BUTTON FIXES ===== */
@media (max-width: 480px) {

  /* Ensure buttons are always visible */
  .right-panel .btn {
    padding: 10px !important;
    font-size: 13px !important;
    flex-shrink: 0 !important;
  }

  /* Hide "Typing Test" text on mobile - show only logo */
  .logo-wrapper h1 {
    display: none !important;
  }
}



/* ===== LANDSCAPE MODE & TABLET ===== */
@media (max-width: 768px) and (orientation: landscape),
(max-width: 1024px) and (orientation: landscape) {

  /* Horizontal layout: Left 72% (paragraph + input), Right 28% (stats) */
  .main-layout {
    flex-direction: row !important;
    height: 100vh !important;
  }

  .left-panel {
    flex: 0 0 72% !important;
    display: flex !important;
    flex-direction: column !important;
    width: 72% !important;
    max-width: 72% !important;
  }

  .paragraph-display {
    flex: 1 !important;
    min-height: 0 !important;
    padding: 15px 20px 10px !important;
    font-size: 15px !important;
    line-height: 1.6 !important;
    overflow-y: auto !important;
  }

  .paragraph-display.keyboard-open {
    flex: 1 !important;
    padding: 15px 20px 10px !important;
    font-size: 15px !important;
    line-height: 1.6 !important;
  }

  .separator {
    display: block !important;
    height: 1px !important;
    margin: 0 !important;
  }

  .separator.keyboard-open {
    display: block !important;
  }

  .typed-wrapper {
    flex-shrink: 0 !important;
    padding: 8px 20px 10px !important;
    margin-top: 0 !important;
  }

  .typed-wrapper.keyboard-open {
    padding: 8px 20px 10px !important;
    margin-top: 0 !important;
  }

  .typed-input {
    height: 40px !important;
    font-size: 14px !important;
    padding: 8px 12px !important;
    line-height: 1.4 !important;
  }

  .typed-input.keyboard-open {
    height: 40px !important;
    font-size: 14px !important;
    padding: 8px 12px !important;
  }

  /* Stats Panel - Visible and Slim */
  .right-panel {
    position: static !important;
    flex: 0 0 28% !important;
    width: 28% !important;
    max-width: 28% !important;
    height: 100vh !important;
    right: auto !important;
    animation: none !important;
    box-shadow: none !important;
    border-left: 1px solid var(--border-color) !important;
    z-index: auto !important;
    gap: 10px !important;
    padding: 15px 12px !important;
  }

  .right-panel.open {
    right: auto !important;
    animation: none !important;
  }

  .menu-btn {
    display: none !important;
  }

  .close-btn {
    display: none !important;
  }

  .overlay {
    display: none !important;
  }
}

/* ===== PAUSE UI - MOBILE FIXES ===== */
@media (max-width: 768px) {
  .pause-content {
    padding: 30px 25px !important;
    border-radius: 16px !important;
    max-width: 90vw !important;
    max-height: 85vh !important;
    overflow-y: auto !important;
  }

  .pause-stats {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
    margin-bottom: 20px !important;
    padding: 15px !important;
  }

  .pause-stat-item {
    padding: 10px !important;
    gap: 6px !important;
  }

  .pause-stat-label {
    font-size: 10px !important;
  }

  .pause-stat-value {
    font-size: 22px !important;
  }

  .pause-icon {
    width: 70px !important;
    height: 70px !important;
    margin-bottom: 15px !important;
  }

  .pause-icon svg {
    width: 40px !important;
    height: 40px !important;
  }

  .pause-content h2 {
    font-size: 24px !important;
    margin-bottom: 10px !important;
  }

  .pause-content p {
    font-size: 14px !important;
    margin-bottom: 20px !important;
  }
}

/* ===== PAUSE UI - LANDSCAPE MODE ===== */
@media (max-width: 768px) and (orientation: landscape),
(max-width: 1024px) and (orientation: landscape) {
  .pause-content {
    padding: 25px 30px !important;
    border-radius: 16px !important;
    max-width: 70vw !important;
    max-height: 80vh !important;
  }

  .pause-stats {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
    margin-bottom: 15px !important;
    padding: 12px !important;
  }

  .pause-stat-item {
    padding: 8px !important;
    gap: 4px !important;
  }

  .pause-stat-label {
    font-size: 9px !important;
  }

  .pause-stat-value {
    font-size: 18px !important;
  }

  .pause-icon {
    width: 60px !important;
    height: 60px !important;
    margin-bottom: 12px !important;
  }

  .pause-icon svg {
    width: 36px !important;
    height: 36px !important;
  }

  .pause-content h2 {
    font-size: 20px !important;
    margin-bottom: 8px !important;
  }

  .pause-content p {
    font-size: 13px !important;
    margin-bottom: 15px !important;
  }
}

/* ===== PROFILE MODAL - MOBILE RESPONSIVE ===== */
@media (max-width: 768px) {
  #profileModal {
    padding: 15px;
  }

  .profile-card {
    padding: 18px;
    border-radius: 16px;
    max-height: 85vh;
  }

  .close-profile-btn {
    top: 12px;
    right: 12px;
    width: 28px;
    height: 28px;
    font-size: 20px;
  }

  .profile-avatar-large {
    width: 70px;
    height: 70px;
  }

  .profile-avatar-large #profileInitials {
    font-size: 28px;
  }

  #profileName {
    font-size: 18px;
  }

  .profile-stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }

  .profile-stat {
    padding: 12px 8px;
  }

  .profile-stat .stat-value {
    font-size: 18px;
  }

  .profile-stat .stat-icon {
    width: 24px;
    height: 24px;
  }

  .personal-bests {
    flex-direction: column;
    gap: 8px;
  }

  .improvement-cards {
    flex-direction: column;
    gap: 10px;
  }

  .badges-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
  }

  .badge-item {
    padding: 10px 6px;
  }

  .badge-icon {
    width: 30px;
    height: 30px;
  }

  .badge-icon svg {
    width: 16px;
    height: 16px;
  }

  .badge-name {
    font-size: 8px;
  }

  .profile-actions {
    flex-direction: column;
    gap: 10px;
  }

  .profile-btn {
    padding: 12px;
  }
}

@media (max-width: 480px) {
  .profile-card {
    padding: 15px;
  }

  .profile-avatar-large {
    width: 60px;
    height: 60px;
  }

  .profile-avatar-large #profileInitials {
    font-size: 24px;
  }

  #profileName {
    font-size: 16px;
  }

  .section-title {
    font-size: 13px;
  }

  .badges-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .badge-item {
    padding: 8px 4px;
  }

  .badge-icon {
    width: 26px;
    height: 26px;
    margin-bottom: 4px;
  }

  .badge-name {
    font-size: 7px;
  }

  .recent-tests-list {
    max-height: 150px;
  }
}

/* ===== STATUS & INSTALL ROW ===== */
.status-install-row {
  display: flex !important;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-bottom: 20px;
  gap: 10px;
  position: relative;
  min-height: 48px;
  padding: 0 4px;
  /* Slight padding */
}

/* Ensure Level is always Left */
.status-install-row .xp-level-inline {
  margin-right: auto;
  flex-shrink: 0;
}

/* Ensure Points is always Right */
.status-install-row .xp-points-inline {
  margin-left: auto;
  flex-shrink: 0;
}

/* Install Button stays Centered if present */
.status-install-row #installBtn {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  margin: 0 !important;
  /* Override current auto margins */
  width: auto !important;
  white-space: nowrap;
  top: 50%;
  transform: translate(-50%, -50%);
}

/* Ensure it stacks below header row properly */
.header-row {
  margin-bottom: 0 !important;
  /* Remove bottom margin if present */
}

/* PWA Button Override for this position */
.pwa-btn[hidden] {
  display: none !important;
}