/* ==========================================================================
   Kitap Kumbaram (Kumbo) — Mobil & Tablet Platform Uyumluluk ve Kararlılık Katmanı
   - Tüm görünüm kapılarında (320x568, 375x667, 390x844, 667x375 yatay, 768x1024)
     yatay taşmayı (overflow-x), HUD/alt çubuk çakışmalarını ve kırpılmaları önler.
   - Etkileşimli dokunma hedeflerini >= 44x44px ve form girdilerini >= 16px tutar.
   - Mevcut Warm Liquid Glass tasarım dilini korur.
   ========================================================================== */

@layer warm-glass {
  /* 1. Temel Görünüm Kapısı Güvenliği & Mobil Arka Plan Titreme/Kayma Koruması */
  html,
  body {
    max-width: 100%;
    overflow-x: clip;
    overscroll-behavior-x: none;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  /* Arka plan katmanlarını dinamik adres çubuğu yükseklik değişiminden (%100/%100dvh)
     bağımsız sabit büyük görünüm yüksekliğine (100lvh / --kk-bg-height) ve ayrı GPU
     kompozit katmanına kilitle; böylece mobilde kaydırma sırasında arka plan titremez
     veya ölçek değiştirip kaymaz. */
  body .library-backdrop,
  body .magical-decor-layer,
  body .space-route-backdrop,
  body.reference-ui #progressView::before,
  body.reference-ui #collectionView::before {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: auto !important;
    width: 100vw !important;
    width: 100lvw !important;
    height: 100vh !important;
    height: var(--kk-bg-height, 100lvh) !important;
    min-height: 100vh !important;
    min-height: 100lvh !important;
    background-attachment: scroll !important;
    background-position: center top !important;
    background-size: cover !important;
    background-repeat: no-repeat !important;
    transform: translate3d(0, 0, 0) !important;
    -webkit-transform: translate3d(0, 0, 0) !important;
    backface-visibility: hidden !important;
    -webkit-backface-visibility: hidden !important;
    will-change: transform !important;
    pointer-events: none !important;
    transition: none !important;
    animation: none !important;
  }

  /* Ana sayfa .library-stage eski apple-theme sticky/width geçişi nedeniyle
     scroll sırasında zıplamasın; akış içinde sabit dursun. */
  body.reference-ui #homeView .library-stage,
  body.reference-ui.is-scrolled #homeView .library-stage {
    position: relative !important;
    top: auto !important;
    transition: none !important;
  }

  @media (max-width: 1024px), (pointer: coarse) {
    /* iOS Safari odak yakınlaştırmasını (auto-zoom) %100 engelle */
    body :is(
      input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]),
      select,
      textarea
    ) {
      font-size: max(16px, 1rem) !important;
    }
  }

  /* 2. İkincil Alt Çubuk Engeli & Resmi #floatingBottomNav Mobil Düzeni */
  #kk-mobile-bottom-dock {
    display: none !important;
  }

  /* Tam ekran diyalog, değerlendirme veya sanal klavye açıkken alt gezinti çubuğunu gizle */
  html.is-virtual-keyboard-open :is(#floatingBottomNav, #dockRoutesBubble),
  body:has(dialog[open]) :is(#floatingBottomNav, #dockRoutesBubble),
  body:has(#bg-math-adventure-overlay:not([hidden]):not([style*="display: none"])) :is(#floatingBottomNav, #dockRoutesBubble),
  body:has(#bg-modal-quiz:not([hidden]):not([style*="display: none"])) :is(#floatingBottomNav, #dockRoutesBubble),
  body:has(.kk-nextgen-modal-backdrop) :is(#floatingBottomNav, #dockRoutesBubble),
  body:has(#hacFilterPopupModal:not([hidden])) :is(#floatingBottomNav, #dockRoutesBubble),
  body[data-view="authView"] #dockRoutesBubble,
  body[data-view="teacherView"] #dockRoutesBubble,
  body[data-view="teacherSettingsView"] #dockRoutesBubble {
    display: none !important;
  }

  @media (max-width: 640px) {
    body.reference-ui #floatingBottomNav.floating-nav-dock,
    body #floatingBottomNav.floating-nav-dock {
      width: calc(100vw - 16px) !important;
      max-width: 520px !important;
      left: 50% !important;
      transform: translateX(-50%) !important;
      bottom: max(8px, env(safe-area-inset-bottom, 8px)) !important;
    }

    body.reference-ui #floatingBottomNav .floating-nav-capsule,
    body #floatingBottomNav .floating-nav-capsule {
      min-height: 58px !important;
      padding: 4px 5px !important;
      gap: 2px !important;
      width: 100% !important;
      box-sizing: border-box !important;
    }

    body.reference-ui #floatingBottomNav .float-tab,
    body #floatingBottomNav .float-tab {
      flex: 1 1 0 !important;
      min-width: 44px !important;
      min-height: 48px !important;
      padding: 4px 1px !important;
      gap: 2px !important;
    }

    body.reference-ui #floatingBottomNav .tab-label,
    body #floatingBottomNav .tab-label {
      display: block !important;
      font-size: clamp(8.8px, 2.5vw, 10.8px) !important;
      line-height: 1.15 !important;
      max-width: 100% !important;
      overflow: hidden !important;
      text-overflow: ellipsis !important;
      white-space: nowrap !important;
    }
  }

  @media (max-height: 500px) and (orientation: landscape) {
    body.reference-ui #floatingBottomNav.floating-nav-dock,
    body #floatingBottomNav.floating-nav-dock {
      width: min(560px, calc(100vw - 24px)) !important;
      bottom: max(4px, env(safe-area-inset-bottom, 4px)) !important;
    }

    body.reference-ui #floatingBottomNav .floating-nav-capsule,
    body #floatingBottomNav .floating-nav-capsule {
      min-height: 48px !important;
      padding: 2px 8px !important;
    }

    body.reference-ui #floatingBottomNav .float-tab,
    body #floatingBottomNav .float-tab {
      min-height: 44px !important;
      flex-direction: row !important;
      gap: 5px !important;
      padding: 2px 6px !important;
    }
  }

  /* 3. Mobil & Küçük Tablet Üst Çubuk (Topbar) Sıfır Taşma & >= 44px Dokunma Hedefleri */
  @media (max-width: 820px) {
    body:not(.reference-ui) #brandHome {
      min-height: 44px !important;
      min-width: 44px !important;
      display: inline-flex !important;
      align-items: center !important;
      flex-shrink: 0 !important;
      visibility: visible !important;
      opacity: 1 !important;
      font-size: clamp(1.08rem, 4.2vw, 1.32rem) !important;
    }

    body .header-right-group .kk-atmosphere-switcher-btn {
      display: none !important;
    }
  }

  @media (max-width: 640px) {
    body.reference-ui #brandHome {
      display: none !important;
    }

    body.reference-ui .topbar.apple-header,
    body .topbar.apple-header {
      padding-top: max(6px, env(safe-area-inset-top, 6px)) !important;
      padding-inline: clamp(8px, 2.8vw, 14px) !important;
      max-width: 100vw !important;
      box-sizing: border-box !important;
    }

    body.reference-ui .topbar .header-inner,
    body .topbar .header-inner {
      width: 100% !important;
      max-width: 100% !important;
      padding-inline: 0 !important;
      gap: 6px !important;
      justify-content: flex-end !important;
      box-sizing: border-box !important;
    }

    body .header-right-group {
      min-width: 0 !important;
      max-width: 100% !important;
      flex-shrink: 1 !important;
      gap: 6px !important;
    }

    body.reference-ui .header-utility-btns {
      gap: 6px !important;
    }

    body .header-right-group :is(#accountMenuButton, #btnNotificationBell, #btnTopMenuToggle, #teacherSettingsButton, .glass-circle-btn) {
      width: 44px !important;
      min-width: 44px !important;
      height: 44px !important;
      min-height: 44px !important;
      padding: 0 !important;
      flex-shrink: 0 !important;
    }

    body .header-right-group #coinWallet {
      min-height: 44px !important;
      min-width: 44px !important;
      height: 44px !important;
      padding-inline: 8px !important;
      display: inline-flex !important;
      align-items: center !important;
      flex-shrink: 1 !important;
    }

    body #accountMenu {
      right: 0 !important;
      left: auto !important;
      width: min(300px, calc(100vw - 16px)) !important;
      max-width: calc(100vw - 16px) !important;
      max-height: calc(100dvh - 76px) !important;
      overflow-y: auto !important;
      box-sizing: border-box !important;
    }
  }

  @media (max-width: 400px) {
    body.reference-ui .header-right-group {
      gap: 4px !important;
    }
    body.reference-ui .header-utility-btns {
      gap: 4px !important;
    }
    body.reference-ui #coinWallet .coin-hub-pill {
      display: none !important;
    }
  }

  body #accountMenu .top-actions :is(button, a.GlassButton, .panel-tab):not(.hidden):not([hidden]) {
    min-height: 40px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  body #accountMenu :is(.hidden, [hidden], #accountLoginButton) {
    display: none !important;
  }

  /* 4. Ana Sayfa Arama Kutusu, Kart Izgaraları & Alt Çubuk Boşluğu */
  @media (max-width: 600px) {
    body.reference-ui #homeView.view.content,
    body #homeView.view.content {
      padding-inline: 10px !important;
      padding-bottom: calc(116px + env(safe-area-inset-bottom, 0px)) !important;
      box-sizing: border-box !important;
    }

    body.reference-ui #homeView .home-stage-grid,
    body.reference-ui #homeView .library-stage,
    body.reference-ui #homeView .library-stage.discovery-stage-unified,
    body #homeView .home-stage-grid,
    body #homeView .discovery-stage-unified {
      width: 100% !important;
      max-width: 100% !important;
      padding-inline: 0 !important;
      margin-inline: 0 !important;
    }

    body.reference-ui #homeView .search-box-apple {
      width: 100% !important;
      max-width: 100% !important;
      min-height: 54px !important;
      padding: 4px 6px 4px 12px !important;
      gap: 6px !important;
      box-sizing: border-box !important;
    }

    body.reference-ui #homeView #searchInput {
      min-width: 0 !important;
      width: 100% !important;
      flex: 1 1 auto !important;
      font-size: 16px !important;
    }

    body.reference-ui #homeView :is(#voiceSearchBtn, #searchSubmit) {
      width: 44px !important;
      height: 44px !important;
      min-width: 44px !important;
      min-height: 44px !important;
      flex: 0 0 44px !important;
      padding: 0 !important;
    }

    body #homeAdventureCategoriesSection .hac-category-btn {
      height: auto !important;
      min-height: 146px !important;
      padding: 12px 10px 12px !important;
    }

    body #homeAdventureCategoriesSection .hac-btn-sub {
      white-space: nowrap !important;
      overflow: hidden !important;
      text-overflow: ellipsis !important;
      font-size: 0.72rem !important;
      line-height: 1.25 !important;
    }
  }

  @media (max-width: 540px) {
    body :is(#homeAdventureCategoriesSection, #dockRoutesBubble) .hac-route-grid {
      grid-template-columns: minmax(0, 1fr) !important;
      gap: 12px !important;
    }

    body :is(#homeAdventureCategoriesSection, #dockRoutesBubble) .hac-route-card {
      min-height: 0 !important;
      padding: 14px !important;
    }

    body #homeAdventureCategoriesSection .hac-toolbar-row {
      flex-wrap: wrap !important;
      gap: 8px !important;
    }

    body #homeAdventureCategoriesSection .hac-search-box {
      flex: 1 1 180px !important;
      min-width: 0 !important;
    }
  }

  @media (min-width: 541px) and (max-width: 900px) {
    body :is(#homeAdventureCategoriesSection, #dockRoutesBubble) .hac-route-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
      gap: 14px !important;
    }
  }

  /* 5. Form, Profil, İlerlemem, Koleksiyonum ve Öğretmen Görünümleri Alt Boşluğu & >= 44px Hedefler */
  @media (max-width: 860px) {
    body :is(
      #authView,
      #profileSelectView,
      #childFormView,
      #profileView,
      #progressView,
      #collectionView,
      #teacherView,
      #quizView,
      #summaryView,
      #resultView
    ) {
      padding-bottom: calc(124px + env(safe-area-inset-bottom, 0px)) !important;
      box-sizing: border-box !important;
    }
  }

  body :is(
    .profile-tab-btn,
    #authView button,
    #profileSelectView button,
    #childFormView button,
    #profileView button,
    #progressView button,
    #collectionView button,
    #collectionView a.GlassButton,
    .hac-popup-reset-btn,
    .hac-popup-apply-btn,
    .hac-filter-pill,
    .hac-filter-chip
  ) {
    min-height: 44px !important;
  }

  body .hac-search-clear-btn {
    width: 36px !important;
    height: 36px !important;
    right: 6px !important;
  }

  /* 6. NextGen Modallar (Beceri Karnesi, Günlük Görevler, Canlı Okuma) Kontrast Koruması */
  html body :is(
    .kk-live-reader-card,
    .kk-quests-museum-card,
    .kk-report-certificate-card,
    .kk-postcard-modal
  ) {
    background: linear-gradient(155deg, #0f172a 0%, #1e293b 100%) !important;
    border: 1.5px solid rgba(125, 211, 252, 0.44) !important;
    color: #f8fafc !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }

  /* 7. 3B Bilgi Gezegenleri, Petek Evren & Kavram Kartları Mobil Ergonomisi */
  body .bilgi-gezegenleri-root :is(
    .bg-bottom-controls-strip button,
    .bg-hud-header button,
    .bg-hud-header summary,
    #bg-col-dock-toggle,
    #bg-focus-close,
    #bg-close-collection-inventory,
    #bg-close-card-preview
  ),
  body :is(.concept-card-close, .kk-learning-close, .kk-learning-modal button) {
    min-width: 44px !important;
    min-height: 44px !important;
  }

  @media (max-width: 860px) {
    body .bg-honeycomb-active #bg-collection-hud-container {
      bottom: calc(76px + env(safe-area-inset-bottom, 0px)) !important;
      z-index: 46 !important;
    }

    body #bg-collection-hud-container.is-collapsed .bg-col-dock-items-row {
      display: none !important;
    }

    body #bg-collection-hud-container.is-collapsed .bg-collection-planets-dock {
      width: auto !important;
      padding: 4px 6px !important;
    }
  }

  @media (max-height: 500px) and (orientation: landscape) {
    body .bilgi-gezegenleri-root #bg-planet-weather-bar {
      display: none !important;
    }

    body .bilgi-gezegenleri-root .bg-bottom-controls-strip {
      bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
      padding-bottom: 0 !important;
    }

    body .bg-honeycomb-active #bg-collection-hud-container {
      bottom: calc(58px + env(safe-area-inset-bottom, 0px)) !important;
    }

    body .bg-honeycomb-active #bg-universe-hud-bar {
      top: calc(58px + env(safe-area-inset-top, 0px)) !important;
      left: auto !important;
      right: 8px !important;
      width: min(320px, 52vw) !important;
    }

    body .bg-honeycomb-active #bg-galaxy-quick-bar {
      width: min(42vw, 290px) !important;
      right: auto !important;
    }
  }

  /* 8. Kitap Keşif Merkezi (discovery.html) Mobil Çekmece & Sıfır Taşma Düzeni */
  body.warm-glass-discovery .sidebar-mobile-backdrop,
  body.warm-glass-discovery .mobile-discovery-menu-btn,
  body.warm-glass-discovery .mobile-sidebar-close-btn {
    display: none;
  }

  body.warm-glass-discovery :is(
    .nav-item,
    .icon-btn-round,
    .user-pill,
    .source-cat-pill,
    .btn-search,
    .custom-select,
    .sort-select,
    .search-input-text,
    .btn-action-primary,
    .btn-action-secondary,
    .btn-state-action,
    .btn-card-more,
    .insp-tab-btn,
    .btn-insp-action,
    .btn-add-new-source,
    .btn-insp-questions,
    .btn-insp-publish
  ) {
    min-height: 44px !important;
    min-width: 44px !important;
  }

  body.warm-glass-discovery .source-cat-pill {
    display: inline-flex !important;
    align-items: center !important;
    padding: 8px 14px !important;
  }

  @media (max-width: 1000px) {
    body.warm-glass-discovery .mobile-discovery-menu-btn {
      display: inline-flex !important;
      align-items: center !important;
      justify-content: center !important;
      gap: 8px !important;
      min-height: 44px !important;
      min-width: 44px !important;
      padding: 0 14px !important;
      border-radius: 12px !important;
      background: var(--wg-control) !important;
      color: var(--wg-text) !important;
      border: 1px solid var(--wg-border) !important;
      box-shadow: var(--wg-highlight) !important;
      font-weight: 700 !important;
      flex-shrink: 0 !important;
    }

    body.warm-glass-discovery .mobile-sidebar-close-btn {
      display: inline-flex !important;
      align-items: center !important;
      justify-content: center !important;
      width: 44px !important;
      height: 44px !important;
      min-width: 44px !important;
      min-height: 44px !important;
      margin-left: auto !important;
      border-radius: 12px !important;
      background: var(--wg-control) !important;
      color: var(--wg-text) !important;
      border: 1px solid var(--wg-border) !important;
      flex-shrink: 0 !important;
    }

    body.warm-glass-discovery .sidebar {
      position: fixed !important;
      inset: 0 auto 0 0 !important;
      width: min(290px, 85vw) !important;
      max-width: 85vw !important;
      height: 100vh !important;
      height: 100dvh !important;
      max-height: 100dvh !important;
      z-index: 1200 !important;
      padding: max(16px, env(safe-area-inset-top, 16px)) 14px max(24px, env(safe-area-inset-bottom, 24px)) !important;
      transform: translateX(-105%) !important;
      transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1) !important;
      overflow-y: auto !important;
      overflow-x: hidden !important;
    }

    body.warm-glass-discovery.is-mobile-sidebar-open .sidebar {
      transform: translateX(0) !important;
    }

    body.warm-glass-discovery .sidebar-nav {
      flex-direction: column !important;
      overflow-x: hidden !important;
      overflow-y: visible !important;
    }

    body.warm-glass-discovery .sidebar-nav > * {
      flex: 0 0 auto !important;
      width: 100% !important;
    }

    body.warm-glass-discovery .nav-section-title {
      display: block !important;
    }

    body.warm-glass-discovery.is-mobile-sidebar-open .sidebar-mobile-backdrop {
      display: block !important;
      position: fixed !important;
      inset: 0 !important;
      background: rgba(4, 8, 20, 0.68) !important;
      backdrop-filter: blur(4px) !important;
      z-index: 1190 !important;
    }

    body.warm-glass-discovery .main-wrapper {
      width: 100% !important;
      max-width: 100vw !important;
      min-width: 0 !important;
    }

    body.warm-glass-discovery .header-left-row {
      display: flex !important;
      align-items: flex-start !important;
      gap: 10px !important;
      width: 100% !important;
      min-width: 0 !important;
      flex-wrap: wrap !important;
    }

    body.warm-glass-discovery .header-title-box {
      flex: 1 1 180px !important;
      min-width: 0 !important;
    }

    body.warm-glass-discovery .header-title-box h2 {
      font-size: clamp(1.22rem, 5.2vw, 1.65rem) !important;
      flex-wrap: wrap !important;
      word-break: break-word !important;
    }

    body.warm-glass-discovery .dropdowns-row {
      grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
      gap: 10px !important;
    }

    body.warm-glass-discovery .book-source-status {
      flex-wrap: wrap !important;
      flex-shrink: 1 !important;
      min-width: 0 !important;
      width: 100% !important;
      gap: 6px 10px !important;
    }

    body.warm-glass-discovery .book-card-actions {
      width: 100% !important;
      justify-content: space-between !important;
      flex-wrap: wrap !important;
    }

    body.warm-glass-discovery .inspector-header-info h2 {
      padding-right: 68px !important;
      word-break: break-word !important;
    }

    body.warm-glass-discovery :is(.inspector-tabs, .insp-source-item) {
      flex-wrap: wrap !important;
    }
  }

  @media (max-width: 480px) {
    body.warm-glass-discovery .top-header {
      padding-inline: 12px !important;
    }

    body.warm-glass-discovery :is(.stats-grid, .controls-row, .workspace-grid) {
      padding-inline: 12px !important;
    }

    body.warm-glass-discovery .sources-bar {
      margin-inline: 12px !important;
      padding: 12px !important;
    }

    body.warm-glass-discovery .dropdowns-row {
      grid-template-columns: minmax(0, 1fr) !important;
    }

    body.warm-glass-discovery :is(.search-filters-card, .inspector-panel, .book-card-row) {
      padding: 14px 12px !important;
    }
  }
}
/* Browser zoom must not resize the app; single-finger scrolling stays native.
   Canvas controls retain their own touch-action:none rules. */
html, body {
  touch-action: pan-x pan-y;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
@media (max-width: 960px) {
  /* The reference theme otherwise overrides the compact header's hidden pill
     and pushes its contents beyond the viewport. Coin remains in the wallet. */
  body.reference-ui #accountMenuButton #kkProfileLiveStatsPill {
    display: none !important;
  }
  body .kumbo-voice-toggle {
    right: 0;
  }
}

/* 320px dar mobil: alt görev çubuğu etiket kesilmesini ("Gezegenl...") ve kart alt örtüşmesini önle */
@media (max-width: 360px) {
  body.reference-ui #homeView {
    padding-bottom: 108px !important;
  }
  body.reference-ui #floatingBottomNav .float-tab {
    padding-inline: 2px !important;
  }
  body.reference-ui #floatingBottomNav .float-tab .tab-label {
    font-size: 9.2px !important;
    letter-spacing: -0.025em !important;
    white-space: nowrap !important;
  }
}

/* Mobil yatay konum (844x390 vb.): arama kutusu ve ana kapılar alt dock altında kalmasın */
@media (max-height: 460px) {
  body.reference-ui #homeView {
    justify-content: flex-start !important;
    padding-top: 62px !important;
    padding-bottom: 92px !important;
    min-height: auto !important;
  }
}

/* Gezegen görünümünde kavram kartı açıkken alt görev çubuğu kartın alt kenarını örtmesin */
body:has(#bg-modal-concept:not(.hidden)) #floatingBottomNav,
body:has(#planet-adv-info-card-modal) #floatingBottomNav {
  opacity: 0 !important;
  pointer-events: none !important;
  transform: translate(-50%, 24px) !important;
}
