/* ==========================================================================
   SŁOWIANIE: 536 – ŚWIT POPIOŁÓW | Responsywny Interfejs PWA & Smartfony
   Perfekcyjne Dopasowanie pod Ekran Pionowy (Portrait) i Poziomy (Landscape)
   ========================================================================== */

/* Kontener Wirtualnego Joysticka i Przycisków Dotykowych (Wewnątrz HUD) */
#touch-controls {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 40;
  display: block;
}

/* Wirtualny Joystick (ZAWSZE W LEWYM DOLNYM ROGU) */
#virtual-joystick-zone {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: max(16px, env(safe-area-inset-left));
  width: 110px;
  height: 110px;
  pointer-events: auto;
  touch-action: none;
  z-index: 50;
}

.joystick-base {
  width: 96px;
  height: 96px;
  background: radial-gradient(circle, rgba(212, 155, 66, 0.25) 0%, rgba(20, 12, 8, 0.85) 100%);
  border: 2px solid rgba(212, 155, 66, 0.7);
  border-radius: 50%;
  position: absolute;
  top: 7px;
  left: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.8);
}

.joystick-stick {
  width: 44px;
  height: 44px;
  background: radial-gradient(circle, #ffe17d 0%, #d49b42 60%, #52311c 100%);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 12px rgba(212, 155, 66, 0.95);
  position: absolute;
  top: 26px;
  left: 26px;
  pointer-events: none;
  transition: transform 0.03s ease-out;
}

/* Mobilne Przyciski Akcji Dotykowej (Prawy Dół) */
#touch-actions {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  right: max(14px, env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  pointer-events: auto;
  z-index: 45;
}

.touch-btn-cluster {
  display: flex;
  gap: 8px;
  align-items: center;
}

.touch-action-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: radial-gradient(circle, #4a2916 0%, #201109 100%);
  border: 2px solid var(--gold-primary);
  color: #fff;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(0,0,0,0.8);
  touch-action: none;
  cursor: pointer;
  transition: transform 0.1s, background 0.1s;
}

.touch-action-btn:active {
  transform: scale(0.92);
  background: radial-gradient(circle, #8c3614 0%, #3d1405 100%);
  border-color: #ffe17d;
}

.touch-action-btn.btn-attack {
  width: 56px;
  height: 56px;
  border-color: var(--red-fire);
  background: radial-gradient(circle, #8c2815 0%, #380f07 100%);
  font-size: 1.4rem;
}

.touch-action-btn.btn-spell {
  border-color: var(--blue-perun);
  background: radial-gradient(circle, #0e5a80 0%, #062333 100%);
}

/* ==========================================================================
   1. WSPÓLNE ZASADY MOBILNE (Ekrany dotykowe i urządzenia <= 900px)
   ========================================================================== */

@media (max-width: 900px) {
  /* Globalne panele i modale */
  .modal-window {
    width: 95vw;
    max-width: 95vw;
    padding: 12px 10px;
    max-height: 88vh;
    box-sizing: border-box;
    overflow-y: auto;
  }

  .modal-title {
    font-size: 1.1rem;
    letter-spacing: 1px;
  }

  /* Karta Bohatera w HUD */
  #hero-card {
    top: calc(42px + max(0px, env(safe-area-inset-top)));
    left: max(6px, env(safe-area-inset-left));
    bottom: auto;
    padding: 3px 6px;
    transform: scale(0.8);
    transform-origin: top left;
    background: rgba(22, 14, 10, 0.92);
    z-index: 34;
  }

  /* Pasek Zadań (Quest Tracker) */
  #quest-tracker {
    top: calc(42px + max(0px, env(safe-area-inset-top)));
    right: max(6px, env(safe-area-inset-right));
    width: auto;
    max-width: 52vw;
    z-index: 35;
  }

  .quest-pill {
    padding: 3px 8px;
    font-size: 0.72rem;
  }

  /* Pasek Górny (Top Bar) */
  #hud-topbar {
    padding-top: max(4px, env(safe-area-inset-top));
    padding-bottom: 3px;
    padding-left: max(6px, env(safe-area-inset-left));
    padding-right: max(6px, env(safe-area-inset-right));
    font-size: 0.76rem;
    height: calc(38px + max(0px, env(safe-area-inset-top)));
    gap: 4px;
    box-sizing: border-box;
  }

  .time-season {
    display: none; /* Zwolnij miejsce dla surowców */
  }

  .topbar-center {
    flex: 1;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .resources-list {
    gap: 3px;
    flex-wrap: nowrap;
  }

  .res-item {
    padding: 2px 4px;
    font-size: 0.72rem;
    white-space: nowrap;
  }

  /* Dolny Pasek Budowania */
  #action-bar {
    bottom: max(6px, env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    padding: 2px 4px;
    gap: 3px;
    max-width: 55vw;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 32;
  }

  .action-btn {
    width: 36px;
    height: 36px;
    min-width: 36px;
    border-radius: 3px;
  }

  .action-btn-icon {
    font-size: 0.95rem;
  }

  .action-btn-label {
    font-size: 0.48rem;
  }

  #minimap-card {
    display: none; /* Na małych ekranach minimapa ustępuje miejsca joystickowi */
  }
}

/* ==========================================================================
   2. TRYB PIONOWY NA SMARTFONIE (Portrait Mode: height >= width)
   ========================================================================== */

@media (orientation: portrait) and (max-width: 600px) {
  .screen {
    justify-content: center;
    padding-top: max(52px, env(safe-area-inset-top, 52px));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  /* Bezpieczne marginesy pod iPhone Notch w HUD */
  #hud-topbar {
    padding-top: max(46px, env(safe-area-inset-top, 46px)) !important;
    height: calc(36px + max(46px, env(safe-area-inset-top, 46px))) !important;
  }

  #hero-card {
    top: calc(42px + max(46px, env(safe-area-inset-top, 46px))) !important;
  }

  #quest-tracker {
    top: calc(42px + max(46px, env(safe-area-inset-top, 46px))) !important;
  }

  #mobile-chat-toggle-btn {
    top: calc(42px + max(46px, env(safe-area-inset-top, 46px))) !important;
  }

  .modal-window {
    margin-top: max(46px, env(safe-area-inset-top, 46px));
  }

  .game-title-box {
    margin-bottom: 12px;
    padding: 0 4px;
  }

  .game-title-sub {
    font-size: 0.75rem;
    letter-spacing: 2px;
  }

  .game-title-main {
    font-size: clamp(1.8rem, 7.5vw, 2.4rem);
    letter-spacing: 1.5px;
    line-height: 1.1;
    margin: 2px 0;
  }

  .game-title-desc {
    font-size: 0.78rem;
    line-height: 1.3;
    margin-top: 4px;
    max-width: 90vw;
  }

  /* Menu Główne w Pionie: 1 Zgrabna Kolumna */
  .menu-buttons {
    width: 100%;
    max-width: min(92vw, 340px);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
  }

  .menu-buttons .btn-slavic {
    font-size: clamp(0.82rem, 3.8vw, 0.92rem);
    padding: 9px 12px;
    margin: 1px 0;
    width: 100%;
    letter-spacing: 0.5px;
    white-space: normal;
    text-align: center;
    line-height: 1.2;
    box-sizing: border-box;
    min-height: 42px;
  }

  .menu-buttons .btn-slavic.btn-primary {
    font-size: clamp(0.88rem, 4vw, 1rem);
    padding: 11px 16px;
    margin-bottom: 2px;
  }

  /* Karta Kreacji Bohatera */
  #char-create-screen .slavic-panel {
    padding: 12px 10px;
    width: 96%;
    max-width: 380px;
  }

  .char-class-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
  }

  /* In-Game Czat w Pionie */
  #hud-minecraft-chat {
    bottom: 56px;
    left: 8px;
    width: min(320px, calc(100vw - 16px));
    z-index: 35;
    pointer-events: none;
  }

  #hud-chat-history {
    max-height: 75px;
    font-size: 0.72rem;
    padding: 3px 6px;
  }

  #mobile-chat-toggle-btn {
    top: calc(42px + max(0px, env(safe-area-inset-top)));
    left: max(6px, env(safe-area-inset-left));
    padding: 3px 8px;
    font-size: 0.7rem;
    z-index: 45;
  }
}

/* ==========================================================================
   3. TRYB POZIOMY NA SMARTFONIE (Landscape Mode: height < width)
   ========================================================================== */

@media (orientation: landscape) and (max-height: 520px) {
  #touch-controls {
    display: block;
  }

  /* Menu Główne w Poziomie: Tytuł po lewej, Przyciski 2x3 po prawej (Zero Przewijania!) */
  #main-menu-screen {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: 24px;
    padding: 8px 16px;
  }

  #main-menu-screen .game-title-box {
    text-align: left;
    max-width: 250px;
    margin-bottom: 0;
  }

  #main-menu-screen .game-title-main {
    font-size: 1.7rem;
    letter-spacing: 1px;
    margin: 0;
  }

  #main-menu-screen .game-title-sub {
    font-size: 0.65rem;
    letter-spacing: 2px;
  }

  #main-menu-screen .game-title-desc {
    display: none; /* Ukryj w landscape by zachować czysty widok */
  }

  #main-menu-screen .menu-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px;
    width: 360px;
    max-width: 55vw;
  }

  #main-menu-screen .btn-slavic {
    margin: 0;
    padding: 7px 10px;
    font-size: 0.78rem;
    min-height: 36px;
    letter-spacing: 0.3px;
  }

  #main-menu-screen .btn-slavic.btn-primary {
    font-size: 0.84rem;
    padding: 8px 10px;
  }

  /* Pasek Górny w Poziomie */
  #hud-topbar {
    height: 32px;
    padding: 2px 10px;
    font-size: 0.72rem;
  }

  /* Elementy HUD w Poziomie */
  #hero-card {
    top: 36px;
    left: 8px;
    transform: scale(0.72);
    transform-origin: top left;
  }

  #quest-tracker {
    top: 36px;
    right: 8px;
    max-height: 90px;
    transform: scale(0.85);
    transform-origin: top right;
  }

  #virtual-joystick-zone {
    bottom: 8px;
    left: max(10px, env(safe-area-inset-left));
    transform: scale(0.82);
    transform-origin: bottom left;
  }

  #touch-actions {
    bottom: 8px;
    right: max(10px, env(safe-area-inset-right));
    transform: scale(0.82);
    transform-origin: bottom right;
  }

  #action-bar {
    bottom: 4px;
    max-width: 48vw;
    transform: translateX(-50%) scale(0.92);
    transform-origin: bottom center;
  }

  #hud-minecraft-chat {
    bottom: 44px;
    left: 100px;
    width: 250px;
    max-height: 70px;
    transform: scale(0.85);
    transform-origin: bottom left;
  }

  #mobile-chat-toggle-btn {
    top: 36px;
    left: 80px;
    font-size: 0.68rem;
    padding: 2px 6px;
  }

  /* Modale w trybie poziomym */
  .modal-window {
    max-height: 92vh;
    padding: 8px 12px;
  }
}
