/* ════════════════════════════════════════════════════════════════
   MOBILE.CSS v4 — Scale #wrapper (540px) as one unit
   Chiến lược: scale toàn bộ #wrapper từ top-center.
   #wheel-border-wrap (540px) + #wheel (500px centered by flex) +
   #shine, #spin, #border-svg đều scale cùng nhau.
   spinv1.js vẫn rotate #inner-wheel bình thường — không bị ảnh hưởng.
   ════════════════════════════════════════════════════════════════ */

/* ──────────────────────────────────────────────────────────────
   GLOBAL
   ────────────────────────────────────────────────────────────── */
* { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
html { height: -webkit-fill-available; }
body { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
#wheel, #inner-wheel, #spin, #wrapper { -webkit-user-select: none; user-select: none; touch-action: none; }
#inner-wheel { -webkit-backface-visibility: hidden; backface-visibility: hidden; will-change: transform; }
#guide-popup, #history-popup, #daily-popup, #pity-popup { -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
#guide-overlay, #history-overlay, #daily-overlay, #pity-overlay, #popup-overlay { -webkit-transform: translateZ(0); transform: translateZ(0); }
button, a, [onclick] { touch-action: manipulation; }


/* ══════════════════════════════════════════════════════════════
   MOBILE ≤ 768px
   ══════════════════════════════════════════════════════════════ */
@media screen and (max-width: 768px) {

  /* ── Ẩn nút gốc ── */
  .back, #guide-chip { display: none !important; }

  /* ────────────────────────────────────────────────────────────
     HAMBURGER BUTTON
     ──────────────────────────────────────────────────────────── */
  #mob-menu-btn {
    position: fixed;
    top: calc(10px + env(safe-area-inset-top));
    right: 12px;
    z-index: 999999;
    width: 42px; height: 42px;
    border-radius: 12px;
    background: rgba(16,10,3,.92);
    border: 1px solid rgba(212,175,55,.4);
    box-shadow: 0 4px 14px rgba(0,0,0,.5);
    cursor: pointer;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 5px; padding: 0;
    backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    transition: background .2s;
  }
  .mob-bar {
    width: 20px; height: 2px; border-radius: 2px;
    background: linear-gradient(90deg, #c8960c, #ffd700);
    transition: transform .28s ease, opacity .28s ease;
    transform-origin: center;
  }
  #mob-menu-btn.open .mob-bar:nth-child(1) { transform: translateY(7px) rotate(45deg); }
  #mob-menu-btn.open .mob-bar:nth-child(2) { opacity: 0; transform: scaleX(0); }
  #mob-menu-btn.open .mob-bar:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

  #mob-menu-dropdown {
    position: fixed;
    top: calc(60px + env(safe-area-inset-top));
    right: 10px;
    z-index: 999998;
    background: rgba(14,9,2,.97);
    border: 1px solid rgba(212,175,55,.32);
    border-radius: 14px; padding: 6px;
    box-shadow: 0 8px 32px rgba(0,0,0,.75);
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
    display: flex; flex-direction: column; gap: 3px;
    min-width: 190px;
    opacity: 0; pointer-events: none;
    transform: translateY(-10px) scale(.95);
    transform-origin: top right;
    transition: opacity .22s ease, transform .22s ease;
  }
  #mob-menu-dropdown.open { opacity: 1; pointer-events: auto; transform: translateY(0) scale(1); }

  .mob-menu-item {
    display: flex; align-items: center; gap: 10px;
    width: 100%; padding: 11px 14px;
    border-radius: 10px; border: none;
    background: transparent; color: rgba(255,224,130,.9);
    font-family: "Poppins", sans-serif; font-size: 14px; font-weight: 600;
    cursor: pointer; text-decoration: none; transition: background .14s; text-align: left;
  }
  .mob-menu-item:active { background: rgba(212,175,55,.12); }
  .mob-menu-item-icon { font-size: 18px; flex-shrink: 0; }
  .mob-menu-sep { height: 1px; background: rgba(212,175,55,.14); margin: 2px 8px; }

  #mob-menu-overlay {
    display: none; position: fixed; inset: 0;
    z-index: 999997; background: transparent;
  }
  #mob-menu-overlay.open { display: block; }


  /* ────────────────────────────────────────────────────────────
     WHEEL SCALE — Scale #wrapper (540×540) làm một khối duy nhất
     ──────────────────────────────────────────────────────────── */
  :root {
    --top-ui-h:  58px;
    --bot-bar-h: 60px;
    /* Không gian dọc còn lại cho wheel */
    --avail-v: calc(
      100dvh
      - var(--top-ui-h) - var(--bot-bar-h) - 20px
      - env(safe-area-inset-top) - env(safe-area-inset-bottom)
    );
    /* #wrapper gốc = 540px. Scale sao cho vừa cả ngang lẫn dọc */
    --wrap-size:  min(92vw, var(--avail-v), 500px);
    --wrap-scale: calc(var(--wrap-size) / 540px);
  }

  body {
    height: 100dvh; min-height: -webkit-fill-available;
    overflow: hidden; padding: 0;
    padding-top: calc(var(--top-ui-h) + env(safe-area-inset-top));
    padding-bottom: calc(var(--bot-bar-h) + 10px + env(safe-area-inset-bottom));
    box-sizing: border-box;
    display: flex; align-items: center; justify-content: center;
  }

  /*
   * #wrapper gốc là 540px width, không có height set (auto).
   * Ta cần một "slot" có kích thước đúng bằng sau-khi-scale để body flex
   * căn giữa được chính xác.
   *
   * Kỹ thuật: giữ #wrapper ở 540px, scale từ top-center,
   * đặt nó trong một div slot có kích thước = wrap-size.
   * Nhưng vì ta không thêm HTML được, dùng trick:
   * set #wrapper width/height = wrap-size, rồi dùng
   * transform-origin: center center + scale để nội dung bên trong
   * vẫn hiển thị đúng 540px nhưng chiếm không gian wrap-size.
   *
   * Trick CSS: scale(x) + width/height ĐÃ SET sẽ tạo ra
   * bounding box = scaled size. Đây là behavior đúng của CSS transform.
   */

  /* Reset các override của spinv1.css trong @media (max-width: 600px) */
  #wheel, #shine {
    transform-origin: unset !important;
    /* KHÔNG set transform ở đây — để spinv1.js/CSS gốc kiểm soát */
  }

  /* Scale #wrapper từ center: tất cả children scale cùng nhau */
  #wrapper {
    width: 540px !important;
    /* height tự động theo content (wheel-border-wrap 540px) */
    transform: scale(var(--wrap-scale)) !important;
    transform-origin: center center !important;
    /* Compensate: khi scale(s) một element 540px,
       nó vẫn chiếm 540px layout space, thừa (540 - 540*s)/2 mỗi bên.
       Dùng margin âm để collapse không gian thừa */
    margin-top: calc((540px * (var(--wrap-scale) - 1)) / 2) !important;
    margin-bottom: calc((540px * (var(--wrap-scale) - 1)) / 2) !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    position: relative !important;
    flex-shrink: 0;
  }

  /* Đảm bảo wheel-border-wrap không bị scale riêng (v3 cũ) */
  #wheel-border-wrap {
    width: 540px !important;
    height: 540px !important;
    transform: none !important;
  }

  /*
   * CRITICAL: spinv1.css @media (max-width:600px) set:
   *   #wheel, #shine { transform: scale(--wheel-scale); transform-origin: top left; }
   * Điều này tạo DOUBLE SCALE (wrapper đã scale, wheel lại scale thêm).
   * Reset về trạng thái bình thường — #wrapper scale sẽ lo phần còn lại.
   */
  /* spinv1.css @media(max-width:600px) set: #wheel { transform: scale(X); transform-origin: top left }
     Điều này tạo double-scale vì #wrapper đã scale. Override hoàn toàn: */
  #wheel {
    width: 500px !important;
    height: 500px !important;
    transform: none !important;   /* hủy scale của spinv1.css */
    transform-origin: 50% 50% !important;
  }
  #shine {
    width: 500px !important;
    height: 500px !important;
    transform: none !important;
    transform-origin: 50% 50% !important;
  }
  /*
   * Lưu ý: spinv1.js set transform trên #inner-wheel (rotate), KHÔNG phải #wheel.
   * #wheel { transform: none } không ảnh hưởng đến animation quay.
   */


  /* ── Bottom bar ── */
  #bottom-bar {
    position: fixed !important; top: auto !important;
    bottom: calc(8px + env(safe-area-inset-bottom)) !important;
    left: 50% !important; transform: translateX(-50%) !important;
    width: calc(100vw - 20px) !important; max-width: 520px;
    z-index: 99998; -webkit-transform: translateX(-50%); will-change: transform;
  }
  #merged-pill { width: 100%; border-radius: 22px; overflow: hidden; }

  /* History chip */
  #history-chip { flex: 1; padding: 0 8px; height: 48px; gap: 5px; justify-content: center; }
  .hc-label { font-size: 11px; } .hc-count { font-size: 10px; }
  .hc-dot { width: 5px; height: 5px; } .hc-svg { width: 13px; height: 13px; }

  /* Pity chip */
  #pity-chip { flex: 1.4; padding: 5px 8px; gap: 5px; min-width: 0; }
  .pc-icon { font-size: 14px; } .pc-label { font-size: 9.5px; } .pc-nums { font-size: 9.5px; }
  .pc-bar-bg { height: 3px; }

  /* Daily chip */
  #daily-chip { flex: 1; padding: 0 8px; height: 48px; gap: 5px; justify-content: center; }
  .dc-chip-label { font-size: 11px; } .dc-chip-sub { font-size: 9px; }
  .dc-chip-svg { width: 16px; height: 16px; } .dc-chip-dot { width: 6px; height: 6px; }
  .pill-sep { height: 22px; }

  /* Toad */
  #toad-btn-wrap { bottom: calc(70px + env(safe-area-inset-bottom)) !important; right: 10px !important; width: 60px !important; height: 60px !important; }
  #toad-toggle-btn { width: 60px !important; height: 60px !important; }
  #spin-counter { width: 22px; height: 22px; font-size: 12px; top: -5px; right: -5px; }
  #toad-toggle-btn .btn-label { font-size: 9px; }
  #toad-panel { bottom: calc(140px + env(safe-area-inset-bottom)) !important; right: 10px !important; width: min(280px, calc(100vw - 20px)) !important; }
  #toad-toast { bottom: calc(133px + env(safe-area-inset-bottom)) !important; right: 10px !important; font-size: 13px; padding: 8px 12px; max-width: calc(100vw - 24px); white-space: normal; text-align: center; }

  /* Popups */
  #guide-overlay, #history-overlay { padding: 8px; padding-top: calc(8px + env(safe-area-inset-top)); align-items: flex-start; }
  #guide-popup { width: 100%; height: calc(100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); max-height: none; border-radius: 16px; padding: 12px 12px 10px; overflow-y: auto; }
  .gp-cols { grid-template-columns: 1fr !important; } .gp-vdiv { display: none !important; }
  .gp-col { padding: 0 !important; height: auto !important; overflow: visible !important; gap: 6px; }
  .gp-col + .gp-col { border-top: 1px solid rgba(212,175,55,.15); padding-top: 8px !important; margin-top: 2px; }
  .gp-title { font-size: 19px; } .gp-sub { font-size: 12px; }
  .rw-name { font-size: 12px; } .rw-desc { font-size: 11px; }
  .gp-stxt { font-size: 12px; } .gp-col-title { font-size: 13px; }
  .gp-start-btn { font-size: 15px; padding: 10px; margin-top: 8px; flex-shrink: 0; }
  #history-popup { width: 100%; max-height: calc(100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); overflow-y: auto; border-radius: 16px; padding: 16px 12px; }
  #popup { width: calc(100vw - 28px); max-width: 340px; padding: 24px 16px 20px; border-radius: 18px; }
  .popup-content .result-number { font-size: 64px; } #close-popup { width: 100%; padding: 12px; font-size: 15px; }
  #pity-popup { width: calc(100vw - 28px); max-width: 340px; padding: 22px 14px 18px; border-radius: 18px; }
  .pp-title { font-size: 21px; } .pp-sub { font-size: 13px; } .pp-prize-icon { font-size: 50px; } .pp-prize-name { font-size: 15px; }
  #daily-popup { width: calc(100vw - 20px); max-width: 380px; padding: 18px 12px 16px; border-radius: 16px; }
  .dc-days { gap: 3px; } .dc-day { padding: 5px 2px; border-radius: 7px; }
  .dc-day-num { font-size: 20px; } .dc-day.dc-today .dc-day-num { font-size: 24px; }
  .dc-day-name { font-size: 9px; } .dc-day-sub { font-size: 9px; }
  #toad-info-popup { width: calc(100vw - 20px); max-width: 400px; padding: 18px 14px 16px; border-radius: 16px; max-height: calc(100dvh - 20px); overflow-y: auto; }
}


/* ≤ 430px */
@media screen and (max-width: 430px) {
  :root { --wrap-size: min(90vw, var(--avail-v), 400px); }
  #bottom-bar { width: calc(100vw - 14px) !important; }
  .pc-icon { display: none; }
  #toad-btn-wrap { width: 54px !important; height: 54px !important; bottom: calc(64px + env(safe-area-inset-bottom)) !important; right: 8px !important; }
  #toad-toggle-btn { width: 54px !important; height: 54px !important; }
}

/* ≤ 375px */
@media screen and (max-width: 375px) {
  :root { --wrap-size: min(88vw, var(--avail-v), 340px); }
  #bottom-bar { width: calc(100vw - 12px) !important; }
  .hc-label, .dc-chip-label { font-size: 9.5px; }
  #toad-btn-wrap { width: 50px !important; height: 50px !important; }
  #toad-toggle-btn { width: 50px !important; height: 50px !important; }
}

/* Landscape */
@media screen and (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  :root { --top-ui-h: 48px; --bot-bar-h: 44px; --wrap-size: min(56vh, 300px); }
  body { padding-left: calc(20px + env(safe-area-inset-left)); padding-right: calc(20px + env(safe-area-inset-right)); }
  #mob-menu-btn { top: calc(8px + env(safe-area-inset-top)); }
  #bottom-bar { bottom: calc(4px + env(safe-area-inset-bottom)) !important; }
}

/* Touch active states */
@media (hover: none) and (pointer: coarse) {
  #mob-menu-btn:active { opacity: .8; }
  #toad-toggle-btn:active { transform: scale(.93) !important; }
  #history-chip:active { background: rgba(212,175,55,.1) !important; }
  #daily-chip:active { background: rgba(212,175,55,.1) !important; }
  .dc-btn:active { transform: scale(.97) !important; }
  .pp-close-btn:active { transform: scale(.97) !important; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  #inner-wheel { transition-duration: 6s !important; }
}
