/* =========================================================
   Perbaikan teks keluar bingkai + responsif HP (portrait & landscape)
   Dimuat paling akhir agar menimpa aturan sebelumnya.
   ========================================================= */

/* ---------- Anti teks keluar bingkai (semua ukuran layar) ---------- */
.announcement-banner {
  max-width: min(640px, 92vw);
  width: max-content;
  white-space: normal;
  text-align: center;
  line-height: 1.25;
  border-radius: 24px;
}
.prompt-bubble {
  max-width: min(520px, 92vw);
  white-space: normal;
  text-align: center;
  line-height: 1.25;
  border-radius: 24px;
}
.prompt-bubble span:last-child { min-width: 0; overflow-wrap: anywhere; }

.mission-label { min-width: 0; overflow-wrap: anywhere; line-height: 1.25; }
.mission-item { align-items: flex-start; }
.mission-title { overflow-wrap: anywhere; }

.kiki-text, .edu-card p, .edu-card h3, .modal-header h2, .victory-subtitle,
.btn-primary-large, .btn-secondary { overflow-wrap: anywhere; }
.btn-primary-large, .btn-secondary { max-width: 100%; white-space: normal; line-height: 1.2; }
.modal-card { max-height: 94vh; overflow-y: auto; }

.game-logo, .map-selector-group, .btn-badge-action, #connection-status, .hud-btn {
  max-width: 100%;
}
.btn-badge-action span:last-child,
#connection-text {
  min-width: 0;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn-map { white-space: nowrap; }

.action-circle-btn { padding: 4px; overflow: hidden; }
.btn-inner { width: 100%; }
.btn-title {
  display: block;
  max-width: 100%;
  font-size: 0.6rem;
  line-height: 1.05;
  word-break: normal;
  overflow-wrap: anywhere;
}
.joystick-helper { max-width: 160px; text-align: center; white-space: normal; line-height: 1.2; }
.cam-btn { white-space: nowrap; }

.intro-panel { max-height: 55vh; overflow-y: auto; }
.intro-title, .intro-text { overflow-wrap: anywhere; }

/* Area aman layar berponi/notch dan batas kanan */
#top-hud {
  left: max(12px, env(safe-area-inset-left));
  right: max(18px, env(safe-area-inset-right));
}

.touch-controls-layer {
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  padding-bottom: max(20px, env(safe-area-inset-bottom));
}

/* ---------- Layar Desktop Sedang & Tablet Landscape (max-width: 1440px) ----------
   Cegah tombol bantuan di kanan atas terpotong atau mepet ke pinggir layar */
@media (max-width: 1440px) {
  #top-hud {
    right: max(18px, env(safe-area-inset-right));
    gap: 8px;
  }

  .hud-right {
    gap: 4px;
    margin-right: 4px;
  }

  /* Ringkaskan tombol kanan atas menjadi tombol ikon bulat bergaya modern */
  .hud-btn-text {
    display: none;
  }

  .hud-btn {
    width: 36px;
    height: 36px;
    padding: 0;
    border-radius: 50%;
    justify-content: center;
  }

  .map-selector-group {
    gap: 3px;
    padding: 2px 4px;
  }

  .btn-map {
    padding: 4px 8px;
    font-size: 0.78rem;
  }

  .hud-center {
    padding: 4px 10px;
    gap: 6px;
  }

  .score-card {
    font-size: 1.15rem;
  }

  .stars-card {
    font-size: 1.15rem;
  }
}

/* ---------- Tablet / layar sempit (portrait) ---------- */
@media (max-width: 860px) and (min-height: 521px) {
  #top-hud {
    flex-wrap: wrap;
    gap: 8px;
  }

  .hud-btn-text {
    display: none;
  }

  .hud-btn {
    width: 34px;
    height: 34px;
    padding: 0;
    border-radius: 50%;
  }

  .btn-map .map-title {
    font-size: 0.75rem;
  }

  .btn-badge-action span:last-child,
  #connection-text {
    max-width: 110px;
  }

  #mission-tracker {
    position: static;
    max-width: 260px;
  }

  .camera-toolbar {
    bottom: 195px;
  }
}

/* ---------- HP MODE LANDSCAPE (Layar Pendek / HP Orientasi Landscape) ---------- */
@media (max-height: 560px), (orientation: landscape) and (max-width: 960px) {
  html, body {
    font-size: 13px;
  }

  /* HUD atas: satu baris ringkas, aman notch & sisi kanan */
  #top-hud {
    top: 4px;
    left: max(6px, env(safe-area-inset-left));
    right: max(14px, env(safe-area-inset-right));
    gap: 4px;
    flex-wrap: nowrap;
    align-items: flex-start;
  }

  .hud-left {
    gap: 2px;
    min-width: 0;
    flex: 1 1 auto;
  }

  .logo-and-map {
    gap: 3px;
    flex-wrap: nowrap;
    align-items: center;
  }

  .game-logo {
    padding: 2px 6px;
    border-width: 2px;
    border-radius: 12px;
  }

  .logo-icon.icon-svg {
    width: 18px;
    height: 18px;
  }

  .logo-text {
    display: none; /* Sembunyikan teks EcoShield di HP landscape agar hemat ruang */
  }

  .map-selector-group {
    padding: 2px;
    border-width: 2px;
    gap: 2px;
    border-radius: 14px;
  }

  .btn-map {
    padding: 2px 6px;
    font-size: 0.68rem;
    gap: 3px;
    border-radius: 10px;
  }

  .btn-map .map-title {
    font-size: 0.68rem;
  }

  .btn-map .icon-svg {
    width: 14px;
    height: 14px;
  }

  .multiplayer-bar {
    gap: 3px;
    flex-wrap: nowrap;
  }

  #connection-status {
    padding: 2px 6px;
    font-size: 0.62rem;
    border-radius: 12px;
  }

  .btn-badge-action {
    padding: 2px 6px;
    font-size: 0.62rem;
    border-width: 1.5px;
    border-radius: 12px;
  }

  .btn-badge-action span:last-child,
  #connection-text {
    max-width: 75px;
  }

  /* Skor & Bintang di tengah */
  .hud-center {
    padding: 2px 8px;
    gap: 4px;
    border-width: 2px;
    border-radius: 18px;
    flex: none;
    flex-shrink: 0;
  }

  .score-card {
    font-size: 0.95rem;
    gap: 4px;
  }

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

  .stars-card {
    font-size: 1rem;
    gap: 2px;
  }

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

  /* Tombol Aksesibilitas di Kanan: Ikon Bulat Ringkas, Tidak Pernah Terpotong */
  .hud-right {
    gap: 3px;
    flex: none;
    flex-shrink: 0;
    margin-right: 4px;
  }

  .hud-btn {
    width: 28px;
    height: 28px;
    padding: 0;
    border-width: 2px;
    border-radius: 50%;
    justify-content: center;
  }

  .hud-btn .icon-svg {
    width: 14px;
    height: 14px;
  }

  .hud-btn-text {
    display: none;
  }

  /* Misi: ringkas di bawah multiplayer bar */
  #mission-tracker {
    position: static;
    padding: 3px 8px;
    max-width: 220px;
    border-width: 2px;
    border-radius: 12px;
  }

  .mission-header {
    gap: 4px;
  }

  .mission-title {
    font-size: 0.7rem;
    margin-bottom: 2px;
  }

  .btn-mission-toggle {
    width: 18px;
    height: 18px;
  }

  .mission-list {
    gap: 2px;
    margin-top: 2px;
  }

  .mission-item {
    font-size: 0.64rem;
    gap: 4px;
  }

  .mission-status-icon.icon-svg {
    width: 11px;
    height: 11px;
  }

  /* Kamera Toolbar: strip vertikal ikon di sisi kanan, di bawah HUD */
  .camera-toolbar {
    top: 42px;
    bottom: auto;
    right: max(10px, env(safe-area-inset-right));
    flex-direction: column;
    padding: 3px;
    gap: 3px;
    border-width: 2px;
    border-radius: 12px;
  }

  .cam-btn {
    width: 28px;
    height: 28px;
    padding: 0;
    border-radius: 50%;
    justify-content: center;
  }

  .cam-btn .icon-svg {
    width: 14px;
    height: 14px;
  }

  .cam-lbl {
    display: none;
  }

  /* Kontrol Sentuh Bawah */
  .touch-controls-layer {
    height: 118px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }

  .joystick-base {
    width: 80px;
    height: 80px;
    border-width: 3px;
  }

  .joystick-thumb {
    width: 36px;
    height: 36px;
  }

  .joystick-helper {
    display: none;
  }

  .action-buttons-group {
    gap: 8px;
  }

  .action-circle-btn {
    width: 56px;
    height: 56px;
    border-width: 2px;
    box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
  }

  .btn-emoji .icon-svg {
    width: 20px;
    height: 20px;
  }

  .btn-title {
    font-size: 0.48rem;
    line-height: 1;
  }

  .btn-shortcut {
    display: none;
  }

  /* Pesan, Prompt, dan Dialog */
  .announcement-banner {
    top: 42px;
    padding: 4px 12px;
    font-size: 0.75rem;
    max-width: 65vw;
    border-width: 2px;
  }

  .action-prompt {
    bottom: 95px;
  }

  .prompt-bubble {
    padding: 4px 12px;
    font-size: 0.75rem;
    border-width: 2.5px;
    max-width: 65vw;
  }

  .kiki-dialog {
    top: 80px;
    padding: 6px 10px;
    gap: 8px;
    max-width: 380px;
    border-width: 2.5px;
  }

  .kiki-avatar {
    width: 34px;
    height: 34px;
  }

  .kiki-avatar .icon-svg {
    width: 20px;
    height: 20px;
  }

  .kiki-name {
    font-size: 0.78rem;
  }

  .kiki-role-badge {
    font-size: 0.58rem;
  }

  .kiki-text {
    font-size: 0.7rem;
  }

  /* Intro: panel samping agar tidak menutupi permainan */
  .intro-panel {
    left: auto;
    right: max(6px, env(safe-area-inset-right));
    bottom: 6px;
    top: auto;
    transform: none;
    width: min(300px, 46vw);
    padding: 8px 12px;
    border-width: 3px;
    border-radius: 18px;
    max-height: calc(100% - 12px);
    animation: none;
  }

  .intro-icon {
    width: 36px;
    height: 36px;
    border-width: 2px;
  }

  .intro-title {
    font-size: 0.88rem;
  }

  .intro-count {
    font-size: 0.6rem;
  }

  .intro-text {
    font-size: 0.72rem;
    margin: 4px 0 6px;
    line-height: 1.3;
  }

  .intro-btn {
    padding: 4px 10px;
    font-size: 0.75rem;
  }

  /* Modals di HP Landscape: max-height fleksibel, scrollable tanpa terpotong */
  .modal-overlay {
    padding: 6px;
  }

  .modal-card {
    padding: 10px 14px;
    border-width: 3px;
    border-radius: 16px;
    max-height: 96vh;
  }

  .modal-header {
    margin-bottom: 6px;
  }

  .modal-header h2 {
    font-size: 1rem;
  }

  .btn-close {
    width: 30px;
    height: 30px;
  }

  .modal-body {
    max-height: 60vh;
    gap: 6px;
  }

  .edu-card {
    padding: 6px 10px;
  }

  .edu-card h3 {
    font-size: 0.8rem;
  }

  .edu-card p {
    font-size: 0.72rem;
  }

  .modal-footer {
    margin-top: 8px;
  }

  .btn-primary-large {
    padding: 7px 18px;
    font-size: 0.85rem;
    box-shadow: 0 3px 0 #1E8449;
  }

  .victory-card h2 {
    font-size: 1.1rem;
  }

  .victory-confetti,
  .victory-stars {
    font-size: 1.6rem;
    margin-bottom: 4px;
  }

  .victory-subtitle {
    font-size: 0.8rem;
    margin-bottom: 6px;
  }

  .victory-score-box {
    font-size: 0.9rem;
    padding: 5px;
    margin-bottom: 6px;
  }

  .input-text {
    padding: 5px 10px;
    font-size: 0.85rem;
  }

  .form-group label {
    font-size: 0.78rem;
  }

  .color-swatch {
    width: 30px;
    height: 30px;
  }

  /* Materi: kartu padat */
  .lesson-overlay {
    padding: 6px;
  }

  .lesson-card {
    padding: 8px 14px;
    border-width: 3px;
    border-radius: 16px;
    max-height: 96vh;
  }

  .lesson-heading {
    font-size: 0.8rem;
  }

  .lesson-close {
    font-size: 0.7rem;
    padding: 3px 8px;
  }

  .lesson-progress {
    margin: 4px 0;
    height: 6px;
  }

  .lesson-body {
    gap: 8px;
    max-height: 52vh;
  }

  .lesson-icon {
    width: 48px;
    height: 48px;
    border-width: 2.5px;
  }

  .lesson-title {
    font-size: 0.95rem;
    margin-bottom: 2px;
  }

  .lesson-text {
    font-size: 0.75rem;
    line-height: 1.3;
  }

  .lesson-text ul {
    margin: 2px 0 2px 14px;
  }

  .lesson-note {
    padding: 4px 8px;
    font-size: 0.7rem;
  }

  .lesson-fact {
    font-size: 0.7rem;
    padding: 3px 6px;
    margin-top: 4px;
  }

  .lesson-actions {
    margin-top: 5px;
  }

  .lesson-btn {
    padding: 4px 14px;
    font-size: 0.8rem;
    border-width: 2px;
  }

  .quiz-q {
    font-size: 0.88rem;
    margin-bottom: 4px;
  }

  .quiz-option {
    padding: 5px 10px;
    font-size: 0.75rem;
    border-width: 2px;
    border-radius: 12px;
  }

  .quiz-options {
    gap: 4px;
  }

  .quiz-feedback {
    font-size: 0.72rem;
    padding: 3px 6px;
    margin-top: 4px;
  }
}

/* Layar landscape sangat sempit (HP kecil seperti iPhone SE landscape) */
@media (max-height: 420px), (orientation: landscape) and (max-width: 720px) {
  .hud-center .stars-card {
    display: none;
  }

  #mission-tracker {
    max-width: 190px;
  }

  .action-circle-btn {
    width: 52px;
    height: 52px;
  }

  .touch-controls-layer {
    height: 110px;
  }
}
