/* Mobile Responsiveness for Wordsmith Wordle Roguelike */

/* Base mobile styles and variables */
:root {
  --panel-transition-speed: 0.3s;
  --mobile-breakpoint: 768px;
  --panel-width: 80%;
  --toggle-button-size: 36px;
}

/* Hide desktop-specific elements on all screen sizes */
.desktop-control {
  display: none;
}

/* Hide mobile-only elements by default */
.panel-toggle,
.panel-overlay,
.swipe-hint {
  display: none;
}

/* Theme toggle container in right panel */
.theme-toggle-container {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  padding: 10px;
  border-radius: 8px;
}

.theme-label {
  margin-left: 10px;
  font-weight: bold;
}

/* Mobile layout adjustments */
@media (max-width: 768px) {
  body {
    /* Prevent overscroll effects */
    overscroll-behavior: contain;
  }

  .main-container {
    flex-direction: column;
    position: relative;
    overflow: hidden;
    height: 100dvh; /* Dynamic viewport height */
  }

  /* Hide side panels by default on mobile */
  .side-container {
    position: fixed;
    height: 100dvh; /* Dynamic viewport height */
    width: var(--panel-width);
    top: 0;
    z-index: 30; /* Above overlay */
    transition: transform var(--panel-transition-speed) ease-in-out;
  }

  /* Position left panel off-screen by default */
  #left-container {
    left: 0;
    transform: translateX(-100%);
  }

  /* Position right panel off-screen by default */
  #right-container {
    right: 0;
    transform: translateX(100%);
  }

  /* When panel is active */
  .side-container.active {
    transform: translateX(0) !important;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
  }

  /* Main game container takes full width */
  #game-container {
    width: 100%;
    max-width: 100%;
    height: 100dvh; /* Dynamic viewport height */
    padding: 5px;
    position: relative;
    z-index: 5; /* Lower than panels */
    display: flex;
    flex-direction: column;
  }

  /* Panel toggle buttons - show on mobile and position above board */
  .panel-toggle {
    position: static;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: var(--toggle-button-size);
    height: var(--toggle-button-size);
    background-color: rgba(0, 0, 0, 0.2);
    color: var(--color-text);
    border: none;
    border-radius: 50%;
    z-index: 40; /* Above side panels to ensure they're always clickable */
    font-size: 18px;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    transition: all 0.2s ease;
    margin: 0 10px;
  }

  /* Container for the toggle buttons */
  .panel-toggles-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 5px;
    position: relative;
    z-index: 40; /* Same as toggle buttons */
  }

  .panel-toggle:hover, .panel-toggle:focus {
    background-color: var(--color-present);
    color: white;
    outline: none;
  }

  .panel-toggle:active {
    transform: scale(0.95);
  }

  /* Adjust header for mobile to include toggle buttons */
  header {
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-bottom: 2px;
    border-bottom: 1px solid var(--header-border-bottom);
    margin-bottom: 2px; /* Reduced from 10px */
    z-index: 35; /* Same as toggles */
    flex-shrink: 0; /* Prevent header from shrinking */
  }

  header h1 {
    font-size: 24px;
    margin: 5px 0;
  }
  
  /* Move toggle buttons to beside the title */
  #left-panel-toggle {
    margin-right: 0;
  }

  #right-panel-toggle {
    margin-left: 0;
  }

  /* Overlay for closing panels when clicking outside */
  .panel-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100dvh; /* Dynamic viewport height */
    background-color: rgba(0, 0, 0, 0.3);
    z-index: 20; /* Just below side panels */
    pointer-events: auto;
  }
  
  .panel-overlay.active {
    display: block;
  }

  /* Make keyboard more touch-friendly */
  #keyboard-container {
    width: 100%;
    margin: 3px 0;
    padding: 0;
    flex-shrink: 0; /* Prevent keyboard from shrinking */
  }

  .keyboard-row {
    margin-bottom: 4px;
  }
  
  .keyboard-row button {
    margin: 2px;
    padding: 8px 4px; /* Reduced padding */
    font-size: 0.85rem; /* Slightly smaller font */
    min-width: 0;
    transition: transform 0.1s ease;
  }
  
  .keyboard-row button:active {
    transform: scale(0.95);
  }

  /* Game board adjustments for no scrolling and better vertical space usage */
  #board-container {
    height: 0; /* Reset height */
    min-height: 0; /* Allow it to shrink */
    flex-grow: 1; /* Take remaining space */
    width: 100%;
    overflow-y: auto; /* Keep this but we'll try to avoid needing it */
    -webkit-overflow-scrolling: touch;
    margin: 0;
    padding: 0; /* Remove all padding */
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  #board {
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%; /* Fill the entire container height */
    max-height: none; /* Remove max-height constraint */
  }
  
  /* Make each row flex to help distribute space */
  .row {
    display: flex;
    justify-content: center;
    width: 100%;
    max-width: 360px; /* Increased maximum width */
    margin: 0; /* Remove any margin */
  }

  /* Dynamic tile sizing that scales properly on all screens */
  .tile {
    /* Better scaling formula that allows growth on larger screens */
    width: min(
      /* On small screens, scale based on available height */
      clamp(25px, calc((100dvh - 165px) / 7), 62px),
      /* On larger screens, scale based on width */
      calc(100vw / 7)
    );
    height: min(
      clamp(25px, calc((100dvh - 165px) / 7), 62px),
      calc(100vw / 7)
    );
    margin: 1px; /* Reduce margin */
    font-size: min(
      clamp(0.9rem, calc((100dvh - 165px) / 25), 1.5rem),
      calc(100vw / 28)
    );
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  /* Smaller tile indicators */
  .tile .repeat, .tile .direction {
    width: 12px;
    height: 12px;
    font-size: 0.6rem;
    top: 1px;
    right: 1px;
  }
  
  /* Score Overlay adjustments */
  #score-overlay {
    /* z-index moved to shared declaration */
  }
  
  #score-box {
    width: 90%;
    max-width: 90%;
    max-height: 90dvh; /* Dynamic viewport height */
    overflow-y: auto;
    padding: 12px;
  }
  
  #score-box h2 {
    font-size: 1.4rem;
    margin: 0 0 12px 0;
  }
  
  .word-display {
    max-width: 100%;
    flex-wrap: wrap;
    justify-content: center;
    margin-bottom: 12px;
  }
  
  .word-display .letter {
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
  }
  
  .score-breakdown {
    width: 100%;
    margin-bottom: 12px;
  }
  
  .score-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
  }
  
  .score-row.total {
    margin-top: 6px;
    padding-top: 8px;
  }
  
  /* Upgrade Overlay adjustments for mobile */
  #upgrade-overlay {
    /* z-index moved to shared declaration */
  }
  
  .shop-container {
    width: 92%;
    max-width: 92%;
    max-height: 80dvh; /* Dynamic viewport height */
    overflow-y: auto;
    padding: 12px 10px; /* Reduced padding */
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px; /* Reduced gap */
  }
  
  .shop-abilities-header {
    font-size: 1.4rem; /* Smaller font */
    text-align: center;
    width: 100%;
    margin: 0 0 8px 0; /* Reduced margins */
    color: white !important;
    letter-spacing: 0.5px;
  }
  
  /* Ensure shop abilities container is properly laid out */
  #ability-shop-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 92%;
    padding: 10px; /* Reduced padding */
    gap: 8px; /* Add gap for spacing */
  }
  
  /* Completely redesign the upgrade cards for mobile */
  #shop-current-upgrades {
    display: flex;
    flex-direction: column;
    gap: 10px; /* Reduced from 15px */
    width: 100%;
    max-height: 60dvh; /* Dynamic viewport height */
    overflow-y: auto;
    padding-right: 5px;
  }
  
  .upgrade-card {
    width: 100%;
    min-width: 0;
    height: auto;
    min-height: auto;
    padding: 15px;
    margin: 0;
    flex-direction: row;
    align-items: stretch;
    border-radius: 12px;
  }
  
  /* Two-column layout for upgrade/debuff sections */
  .upgrade-section {
    width: 50%;
    padding-right: 10px;
    text-align: left;
    display: flex;
    flex-direction: column;
  }
  
  .debuff-section {
    width: 50%;
    padding-left: 10px;
    text-align: left;
    display: flex;
    flex-direction: column;
    border-left: 1px solid rgba(255, 255, 255, 0.3);
  }
  
  /* Hide horizontal divider for mobile */
  .card-divider {
    display: none;
  }
  
  .upgrade-card h3 {
    font-size: 18px;
    margin-bottom: 10px;
    text-align: left;
  }
  
  .upgrade-card p {
    font-size: 14px;
    line-height: 1.4;
    margin: 0;
    text-align: left;
  }
  
  /* Style for the ability shop in mobile */
  #shop-current-abilities {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px; /* Reduced gap */
    width: 100%;
    align-self: center;
  }
  
  .ability-card {
    width: 100%;
    height: auto;
    aspect-ratio: 1/1;
    padding: 8px; /* Reduced padding */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  
  .ability-card::before {
    font-size: 1.8rem; /* Smaller emoji */
    margin-bottom: 4px; /* Add some space below emoji */
  }
  
  .ability-card h3 {
    font-size: 0.9rem; /* Smaller title */
    margin: 3px 0;
  }
  
  .ability-card p {
    font-size: 0.8rem; /* Smaller text */
    margin: 4px 0;
    line-height: 1.2;
  }
  
  /* Bigger buttons for mobile with better contrast */
  #shop-close-btn, #next-btn, #restart-btn {
    width: 100%;
    height: auto;
    padding: 12px; /* Reduced padding */
    margin-top: 8px; /* Reduced margin */
    font-size: 16px; /* Smaller font */
    border-radius: 12px;
    font-weight: bold;
    background-color: var(--color-correct);
    color: white;
    border: none;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    align-self: center;
  }
  
  /* Game Over Overlay adjustments */
  #game-over-overlay {
    /* z-index moved to shared declaration */
  }
  
  #game-over-box {
    width: 90%;
    max-width: 90%;
    max-height: 90dvh; /* Dynamic viewport height */
    overflow-y: auto;
    padding: 15px;
  }
  
  .final-word-display {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .final-word-display .letter {
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
  }
  
  /* Enhanced abilities area in left panel for mobile */
  #current-abilities {
    width: 100%;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  
  #abilities-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 20px;
    width: 100%;
    justify-content: center;
    padding: 10px;
    justify-items: center;
  }
  
  .ability-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
  }
  
  .ability-square {
    width: 90px;
    height: 90px;
    max-width: 90px;
    max-height: 90px;
    background: linear-gradient(145deg, rgba(60, 60, 60, 0.4), rgba(30, 30, 30, 0.6));
    border: 2px solid var(--keyboard-tile-border);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-bottom: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    position: relative;
    overflow: hidden;
  }
  
  /* Add subtle glow effect based on ability type */
  .ability-square[data-ability="hint"] {
    border-color: rgba(255, 215, 0, 0.6);
  }
  
  .ability-square[data-ability="darken"] {
    border-color: rgba(138, 43, 226, 0.6);
  }
  
  .ability-square[data-ability="repeats"] {
    border-color: rgba(30, 144, 255, 0.6);
  }
  
  .ability-square[data-ability="accept-non-words"] {
    border-color: rgba(50, 205, 50, 0.6);
  }
  
  /* Add a subtle animated background pattern */
  .ability-square::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.08) 0%, transparent 70%);
    pointer-events: none;
  }
  
  .ability-square:active {
    transform: scale(0.95);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  }
  
  /* Fix emoji icons to appear at the correct size */
  .ability-square[data-ability="hint"]::before,
  .ability-square[data-ability="darken"]::before,
  .ability-square[data-ability="repeats"]::before,
  .ability-square[data-ability="accept-non-words"]::before {
    font-size: 2.5rem;
    position: relative;
    z-index: 2;
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.3));
  }
  
  .ability-count {
    font-size: 20px;
    font-weight: bold;
    margin-top: 8px;
  }
  
  /* Show swipe hints on mobile */
  .swipe-hint {
    display: block;
  }
  
  /* Better button focus states for touch */
  button {
    touch-action: manipulation;
  }
  
  button:focus {
    outline: 2px solid var(--color-present);
    outline-offset: 2px;
  }
  
  /* Prevent scrolling when panels are open */
  body.panel-open {
    overflow: hidden;
  }
  
  /* Ensure panels don't overlap content */
  body.panel-open #game-container {
    filter: blur(2px);
    pointer-events: none;
  }
  
  /* Style the currency display in right panel for better visibility */
  #score-info {
    background-color: rgba(0, 0, 0, 0.2);
    padding: 8px;
    border-radius: 8px;
    margin-bottom: 10px;
  }
  
  #score-info h2 {
    margin: 0;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    font-size: 1.1rem;
  }
  
  /* Round info and upgrades in left panel */
  #round-info {
    padding: 8px;
    margin-bottom: 10px;
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
  }
  
  #round-info h2 {
    margin: 0;
    font-size: 1.1rem;
    line-height: 1.3;
  }
  
  #current-upgrades {
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
  }
  
  .cur-upgrade {
    padding: 0.25em 0.4em;
    line-height: 1.1;
    font-size: 0.9rem;
  }

  /* Message container adjustments for mobile */
  .message-container {
    top: 80px; /* Positioned closer to the top */
    min-width: 240px;
    max-width: 92%;
    font-size: 16px;
    padding: 12px 15px;
    font-weight: 700;
    line-height: 1.3;
    border-width: 2px;
    z-index: 45; /* Just below game overlays */
  }
  
  /* Make error and warning messages even more visible */
  .message-error {
    background-color: rgba(248, 215, 218, 0.95);
  }
  
  .message-warning {
    background-color: rgba(255, 243, 205, 0.95);
  }
}

/* Swipe area indicators - subtle hints for swipe gestures */
@media (max-width: 768px) {
  .swipe-hint {
    position: fixed;
    top: 50%;
    width: 10px;
    height: 100px;
    transform: translateY(-50%);
    background: linear-gradient(
      to right,
      rgba(255, 255, 255, 0.3),
      transparent
    );
    border-radius: 5px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.5s ease;
  }
  
  .swipe-hint.left {
    left: 0;
  }
  
  .swipe-hint.right {
    right: 0;
    background: linear-gradient(
      to left,
      rgba(255, 255, 255, 0.3),
      transparent
    );
  }
  
  .swipe-hint.visible {
    opacity: 1;
    animation: pulse 2s infinite;
  }
  
  @keyframes pulse {
    0% { opacity: 0.2; }
    50% { opacity: 0.5; }
    100% { opacity: 0.2; }
  }
}

/* Additional adjustments for narrow aspect ratios (like iPhone) */
@media (max-width: 400px) {
  /* Optimize layout for narrow devices */
  .keyboard-row button {
    height: 32px;
    padding: 2px 1px;
    margin: 1px;
  }
  
  .tile {
    /* Adjusted scaling for narrow screens */
    width: min(
      clamp(22px, calc((100dvh - 150px) / 7), 45px),
      calc(100vw / 7)
    );
    height: min(
      clamp(22px, calc((100dvh - 150px) / 7), 45px),
      calc(100vw / 7)
    );
    margin: 1px;
    font-size: min(
      clamp(0.8rem, calc((100dvh - 150px) / 28), 1.1rem),
      calc(100vw / 30)
    );
    border-width: 1px;
  }
  
  /* Smaller tile indicators */
  .tile .repeat, .tile .direction {
    width: 10px;
    height: 10px;
    font-size: 0.5rem;
  }
  
  header h1 {
    font-size: 18px;
    margin: 3px 0;
  }
  
  /* Compact game progress bar */
  #game-progress {
    height: 4px;
    margin: 0 0 2px 0;
  }
  
  /* More compact header with smaller margins */
  header {
    padding: 3px 0;
    margin-bottom: 3px;
  }
  
  /* Make board container more efficient */
  #board-container {
    padding: 3px 0;
  }
  
  /* Smaller overlay elements for narrow screens */
  .word-display .letter,
  .final-word-display .letter {
    width: 35px;
    height: 35px;
    font-size: 1rem;
  }
  
  #score-box h2,
  #game-over-box h2,
  .shop-container h2 {
    font-size: 1.2rem;
  }
  
  /* Adjusted ability squares for very small screens */
  .ability-square {
    width: 70px;
    height: 70px;
    max-width: 70px;
    max-height: 70px;
  }
  
  #abilities-grid {
    grid-gap: 12px;
    padding: 6px;
  }
  
  /* Scale down the emoji for very small screens */
  .ability-square[data-ability="hint"]::before,
  .ability-square[data-ability="darken"]::before,
  .ability-square[data-ability="repeats"]::before,
  .ability-square[data-ability="accept-non-words"]::before {
    font-size: 2rem;
  }
  
  .ability-count {
    font-size: 16px;
    margin-top: 4px;
  }
  
  /* Further reduce text size on very small screens */
  .upgrade-card h3 {
    font-size: 14px;
  }
  
  .upgrade-card p {
    font-size: 11px;
  }
  
  /* Message container adjustments for very small screens */
  .message-container {
    top: 70px;
    min-width: 220px;
    max-width: 95%;
    font-size: 15px;
    padding: 10px 12px;
  }
  
  /* Improve the distribution of space in the game container */
  #game-container {
    display: flex;
    flex-direction: column;
  }
  
  #keyboard-container {
    margin-top: 5px;
  }
  
  .shop-abilities-header {
    font-size: 1.2rem;
    margin: 0 0 5px 0;
  }
  
  .ability-card::before {
    font-size: 1.6rem;
  }
  
  #ability-shop-container {
    padding: 8px;
    gap: 5px;
  }
  
  #shop-close-btn, #next-btn, #restart-btn {
    padding: 10px;
    font-size: 14px;
  }
  
  /* Cash display in ability shop */
  #cash-number {
    font-size: 1.1rem;
  }
}

/* Extreme small screen adjustments (like itch.io iframe) */
@media (max-width: 320px) {
  /* Make ability squares tiny for extremely small screens */
  .ability-square {
    width: 55px;
    height: 55px;
    max-width: 55px;
    max-height: 55px;
  }
  
  #abilities-grid {
    grid-gap: 8px;
    padding: 5px;
  }
  
  /* Further reduce emoji size */
  .ability-square[data-ability="hint"]::before,
  .ability-square[data-ability="darken"]::before,
  .ability-square[data-ability="repeats"]::before,
  .ability-square[data-ability="accept-non-words"]::before {
    font-size: 1.5rem;
  }
  
  .ability-count {
    font-size: 14px;
    margin-top: 2px;
  }
  
  /* Reduce padding on shop containers */
  .shop-container {
    padding: 8px 5px;
  }
  
  /* Make upgrade cards even more compact */
  .upgrade-card {
    padding: 8px;
  }
  
  .upgrade-card h3 {
    font-size: 12px;
    margin-bottom: 5px;
  }
  
  .upgrade-card p {
    font-size: 10px;
    line-height: 1.2;
  }
  
  /* Ultra tiny tiles for extreme narrow screens */
  .tile {
    width: min(
      clamp(18px, calc((100dvh - 130px) / 7), 35px),
      calc(100vw / 8)
    );
    height: min(
      clamp(18px, calc((100dvh - 130px) / 7), 35px),
      calc(100vw / 8)
    );
    margin: 1px;
    font-size: min(
      clamp(0.7rem, calc((100dvh - 130px) / 30), 0.9rem),
      calc(100vw / 35)
    );
    border-width: 1px;
  }
  
  /* Ultra compact keyboard */
  .keyboard-row button {
    margin: 1px;
    padding: 2px 1px;
    height: 28px;
    font-size: 0.7rem;
  }
  
  /* Absolutely minimal header */
  header h1 {
    font-size: 14px;
    margin: 2px 0;
  }
  
  /* Single pixel progress bar */
  #game-progress {
    height: 2px;
  }
}

/* Extra small screen adaptation */
@media (max-height: 600px) {
  /* Extreme space optimization for short screens */
  .tile {
    width: min(
      clamp(20px, calc((100dvh - 130px) / 7), 40px),
      calc(100vw / 7.5)
    );
    height: min(
      clamp(20px, calc((100dvh - 130px) / 7), 40px),
      calc(100vw / 7.5)
    );
    margin: 0.5px;
    font-size: min(
      clamp(0.7rem, calc((100dvh - 130px) / 30), 1rem),
      calc(100vw / 32)
    );
    border-width: 1px;
  }
  
  .keyboard-row button {
    height: 30px;
    font-size: 0.7rem;
  }
  
  header h1 {
    font-size: 16px;
    margin: 2px 0;
  }
  
  #game-progress {
    height: 3px;
    margin: 0 0 1px 0;
  }
}

/* Game overlays share the highest z-index */
#score-overlay,
#upgrade-overlay,
#game-over-overlay {
  z-index: 50; /* Highest z-index for game overlays */
  display: none; /* Ensure overlays are hidden by default */
  opacity: 0; /* Start with zero opacity for fade-in transition */
  height: 100dvh; /* Dynamic viewport height */
}

/* Message container sits above panels but below overlays */
.message-container {
  z-index: 45; 
}

/* Additional spacing adjustments for very small screens */
@media (max-width: 370px) {
  /* Make abilities fit better */
  #ability-shop-container, .shop-container {
    padding: 6px;
  }
  
  /* Score display */
  .score-row {
    font-size: 0.9rem;
    padding: 6px 0;
  }
  
  /* Game over stats */
  .game-over-stats .stat-row {
    font-size: 0.9rem;
  }
  
  .stat-label, .stat-value {
    margin: 0;
  }
}

