/* ==========================================================================
   響應式設計總表 (Responsive Stylesheet)
   ========================================================================== */

/* 💻 平板橫向 / 小筆電：最大寬度 1024px */
@media (max-width: 1024px) {

  /* --- 全域容器調整 --- */
  .container {
    width: 98% !important;
    max-width: 98% !important;
  }

  /* --- 標題畫面 (Title Screen) 擠壓修正 --- */
  #title-screen .game-title {
    font-size: 2.5rem;
    margin-bottom: 50px;
  }

  /* 縮小水管尺寸，並往兩側推，避免擋住按鈕 */
  #title-screen .pipe-decoration {
    width: 65px;
    height: 100px;
    bottom: 50px;
    left: 2%;
  }

  #title-screen .pipe-decoration::before {
    width: 85px;
    height: 30px;
    top: -30px;
    left: -10px;
  }

  #title-screen .pipe-decoration.right {
    left: auto;
    right: 2%;
    height: 70px;
  }

  #title-screen .block {
    width: 50px;
    height: 50px;
    font-size: 24px;
  }

  /* --- 內容頁面調整 --- */
  .button-container {
    gap: 12px;
  }

  .section-title {
    font-size: 2.2rem;
  }

  
}


/* 📱 手機版 (直向)：最大寬度 768px */
@media (max-width: 768px) {

  html,
  body {
    max-width: 100%;
    overflow-x: hidden;
  }

  /* --- 📱 手機版導覽列美化（與電腦版保持一致簡約現代毛玻璃質感） --- */
  .navbar,
  body.portal-page .navbar {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    justify-content: space-between !important;
    align-items: center !important;
    height: 60px !important;
    min-height: 60px !important;
    padding: 0 16px !important;
    background: rgba(255, 255, 255, 0.88) !important;
    backdrop-filter: blur(20px) saturate(180%) !important;
    -webkit-backdrop-filter: blur(20px) saturate(180%) !important;
    border: none !important;
    border-bottom: 1px solid rgba(226, 232, 240, 0.85) !important;
    box-shadow: 0 2px 12px rgba(15, 23, 42, 0.04) !important;
    z-index: 5000 !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .navbar .logo-subtitle {
    display: none !important;
  }

  .navbar .logo,
  body.portal-page .navbar .logo {
    order: 1 !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 8px !important;
    font-size: 1.25rem !important;
    font-weight: 800 !important;
    color: #0f172a !important;
    -webkit-text-fill-color: initial !important;
    background: none !important;
    filter: none !important;
    text-decoration: none !important;
    white-space: nowrap !important;
    margin: 0 !important;
  }

  .navbar #navbar-logo-img,
  body.portal-page #navbar-logo-img {
    width: 34px !important;
    height: 34px !important;
    padding: 2px !important;
    background: #ffffff !important;
    border: 1.5px solid rgba(44, 158, 158, 0.3) !important;
    border-radius: 9px !important;
    box-shadow: 0 2px 6px rgba(44, 158, 158, 0.15) !important;
    object-fit: cover !important;
    flex-shrink: 0 !important;
  }

  #nav-identity {
    order: 2 !important;
    margin-left: auto !important;
    margin-right: 0 !important;
    font-size: 0.82rem !important;
    font-weight: 700 !important;
    padding: 5px 12px !important;
    border-radius: 999px !important;
    transition: all 0.2s ease !important;
  }

  #nav-identity:active {
    transform: scale(0.96) !important;
  }

  .menu-toggle {
    order: 3 !important;
    margin-left: 10px !important;
    font-size: 1.25rem !important;
    color: #334155 !important;
  }

  /* --- 修正下拉選單容器 --- */
  .nav-links .dropdown-content {
    background: rgba(255, 255, 255, 0.1) !important;
    box-shadow: none;
    width: 100%;
    border-radius: 12px;
    margin: 5px 0;
    padding: 5px 0;
    border: none !important;
  }

  .nav-links .dropdown-content a {
    color: #ffffff !important;
    background: transparent !important;
    padding: 12px 20px !important;
    font-size: 16px !important;
    text-align: center !important;
    border: none !important;
    opacity: 1 !important;
    visibility: visible !important;
    display: block !important;
  }

  /* 配合 main.js 的 .open 類別 */
  .nav-links .dropdown.open .dropdown-content {
    display: block !important;
  }

  .nav-links .dropdown-content a:active {
    background: rgba(232, 156, 36, 0.3) !important;
    color: #FFD700 !important;
  }

  /* --- 全域調整 --- */
  body {
    padding: 15px;
    padding-top: 82px;
    padding-bottom: 160px; max-width: 100vw; overflow-x: hidden;
    /* 給 Footer 留足夠空間 */
  }

  /* --- 解決懸浮按鈕擋住選單 --- */
  #floating-toggle {
    position: relative !important;
    top: auto !important;
    right: auto !important;
    margin: 8px auto 12px auto !important;
    width: fit-content;
    display: flex;
    justify-content: center;
    z-index: 1301 !important;
    pointer-events: auto !important;
    touch-action: pan-y !important;
    /* 讓切換鈕位在任何卡片與內容層之上，避免手機點擊被覆蓋。 */
  }

  #floating-toggle.semester-segmented-control button.segmented-item,
  .level-tabs,
  .level-tab-btn,
  .tab-btn,
  .subsection-button {
    pointer-events: auto !important;
    touch-action: pan-y !important;
    -webkit-tap-highlight-color: rgba(109, 40, 217, 0.18);
  }

  /* --- 導覽列與選單本體 --- */
  .nav-links {
    display: flex;
    flex-direction: column;
    gap: 10px;
    position: fixed;
    top: 96px;
    left: 5%;
    width: 90%;
    margin: 0 !important;
    /* 清除電腦版的向右推擠，完美置中 */
    background: rgba(44, 158, 158, 0.96);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 24px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 20px;
    box-sizing: border-box;
    z-index: 9999 !important;
    /* 所高選單層級，保證不會被蓋住 */
    box-shadow: 0 10px 40px rgba(0, 89, 89, 0.2);
    transform: translateY(-10px);
    opacity: 0;
    visibility: hidden;
    transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  }

  .nav-links.active {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
  }

  .nav-links li {
    padding: 0;
    width: 100%;
    display: block;
  }

  .nav-links a {
    color: #ffffff;
    font-family: "Chiron GoRound TC", sans-serif;
    background: transparent;
    font-size: 18px;
    font-weight: 500;
    display: block;
    padding: 12px 15px;
    width: 100%;
    transition: all 0.2s;
    border: none;
    border-radius: 12px;
    text-align: center;
    box-shadow: none;
    margin: 0;
  }

  .nav-links a:hover,
  .nav-links a:active {
    background-color: rgba(255, 255, 255, 0.15);
    transform: scale(1.02);
  }

  .nav-links a.active {
    background-color: #E89C24;
    color: #000;
    font-weight: 700;
  }

  .menu-toggle {
    display: block;
    position: relative;
    z-index: 1000;
    color: #fff;
    font-size: 26px;
    margin-left: 10px;
  }

  /* 手機版主要導覽已移至底部，避免與右上角漢堡重複 */
  .navbar nav,
  .navbar .menu-toggle {
    display: none !important;
  }

  /* --- 手機底部主要導覽 --- */
  body {
    padding-bottom: 78px;
  }

  .mobile-bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10000;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
    padding: 6px max(12px, env(safe-area-inset-left)) calc(6px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-right));
    background: rgba(255, 255, 255, 0.92) !important;
    border-top: 1px solid rgba(226, 232, 240, 0.9) !important;
    border-radius: 0 !important;
    box-shadow: 0 -4px 20px rgba(15, 23, 42, 0.05) !important;
    backdrop-filter: blur(20px) saturate(180%) !important;
    -webkit-backdrop-filter: blur(20px) saturate(180%) !important;
  }

  .mobile-bottom-nav a,
  .mobile-bottom-nav button {
    min-width: 0;
    min-height: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 5px 2px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: #64748b !important;
    font: inherit;
    font-size: 11px;
    font-weight: 700;
    line-height: 1.1;
    text-decoration: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: color .18s ease, background .18s ease, transform .15s cubic-bezier(.34,1.56,.64,1);
  }

  .mobile-bottom-nav i {
    font-size: 20px;
    transition: transform .2s cubic-bezier(.34,1.56,.64,1);
  }

  .mobile-bottom-nav a.active,
  .mobile-bottom-nav button.active {
    background: rgba(13, 148, 136, 0.1) !important;
    color: #0d9488 !important;
  }

  .mobile-bottom-nav a.active i,
  .mobile-bottom-nav button.active i {
    transform: translateY(-1px) scale(1.08);
    filter: drop-shadow(0 2px 4px rgba(13, 148, 136, 0.25));
  }

  .mobile-bottom-nav a:active,
  .mobile-bottom-nav button:active {
    background: rgba(13, 148, 136, 0.16) !important;
    color: #0f766e !important;
    transform: scale(0.96);
  }

  .mobile-bottom-nav a:active i,
  .mobile-bottom-nav button:active i {
    transform: scale(0.92);
  }

  .mobile-bottom-nav a:focus-visible,
  .mobile-bottom-nav button:focus-visible {
    outline: 2px solid rgba(13, 148, 136, 0.6);
    outline-offset: -2px;
  }

  /* --- Hero Section Mobile --- */
  .hero-section {
    padding: 40px 15px 30px 15px !important;
    border-radius: 0 0 30px 30px !important;
    margin-bottom: 25px !important;
  }

  .hero-content h1 {
    font-size: 1.8rem !important;
    margin-bottom: 10px !important;
    letter-spacing: 1px !important;
  }

  .hero-content p {
    font-size: 0.95rem !important;
    line-height: 1.4 !important;
  }

  /* --- Breadcrumb Mobile --- */
  .breadcrumb-container {
    top: 89px; /* 配合雙行導覽列 */
    padding: 8px 15px;
    font-size: 0.8rem;
    z-index: 1100;
  }

  body.with-breadcrumb {
    padding-top: 130px;
  }

  /* --- 呼吸燈標題與內容區塊 --- */
  .intro-text {
    font-size: 2.5rem;
    letter-spacing: 1px;
    border-right: none;
    padding: 0 10px;
    margin-top: 20px;
    white-space: normal;
    height: auto;
    animation: slowPulseMobile 2.5s ease-in-out infinite;
  }

  @keyframes slowPulseMobile {

    0%,
    100% {
      opacity: 1;
    }

    50% {
      opacity: 0.35;
    }
  }

  /* --- 首頁科目大按鈕 (App 網格化設計) --- */
  .button-section {
    flex-direction: column;
    align-items: center;
    gap: 15px;
    padding: 10px;
    margin-top: 10px;
  }

  .grade-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    /* 一排三個 */
    gap: 10px;
    width: 100%;
  }

  .big-button {
    width: 100%;
    max-width: none;
    padding: 15px 5px;
    font-size: 15px;
    margin: 0;
    border-radius: 16px;

    display: flex;
    flex-direction: column;
    /* 圖上字下 */
    justify-content: center;
    align-items: center;
    gap: 8px;
  }

  .big-button i {
    margin-right: 0 !important;
    font-size: 1.6em;
  }

  /* 只放大內容區裡嵌入的影片／簡報；不能用全域 iframe，
     否則 Firebase 登入用的隱形 iframe 會被撐大，擋住頁面上方的按鈕 */
  main iframe,
  .section-content iframe {
    width: 100% !important;
    height: 50vh !important;
  }

  .single-row {
    flex-direction: column;
    align-items: center;
    gap: 25px;
  }

  .social-group {
    width: 100%;
    max-width: none;
    padding: 24px;
  }

  /* --- 章節按鈕區塊：改為一排三個，大幅節省垂直空間 --- */
  .section-title {
    font-size: 1.8rem;
    margin-top: 15px;
    margin-bottom: 15px;
    white-space: normal;
  }

  .button-container,
  .button-grid-3 {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    width: 100%;
    max-width: 400px;
    margin: 10px auto 20px auto;
  }

  /* --- 門戶首頁卡片手機版排版優化 --- */
  .cards-container {
    grid-template-columns: 1fr !important;
    gap: 24px !important;
    max-width: 420px !important;
    margin: 0 auto 30px auto !important;
    padding: 0 10px !important;
  }

  .grade-card {
    border-radius: 24px !important;
  }

  .card-header {
    padding: 16px 15px !important;
    font-size: 24px !important;
  }

  .card-body {
    padding: 24px 20px !important;
    gap: 12px !important;
  }

  .subject-btn {
    padding: 16px 8px !important;
    font-size: 15px !important;
    gap: 8px !important;
    border-radius: 16px !important;
  }

  .subject-btn i {
    font-size: 22px !important;
  }

  /* --- 全局資源按鈕手機版大小縮小 --- */
  .btn {
    font-size: 15px !important;
    padding: 10px 16px !important;
    border-radius: 12px !important;
    border-left-width: 4px !important;
    margin-bottom: 8px !important;
  }

  .section-content a.btn[href*="chinese/vocab.html"],
  .section-content a.btn[href*="chinese/jumble.html"],
  .section-content a.btn[href*="chinese/text-choice.html"] {
    grid-template-columns: 52px minmax(0, 1fr) !important;
    min-height: 88px !important;
    padding: 16px 18px !important;
    border-radius: 18px !important;
    border-left-width: 7px !important;
    font-size: 1.18rem !important;
    column-gap: 14px !important;
  }

  .section-content a.btn[href*="chinese/vocab.html"]::before,
  .section-content a.btn[href*="chinese/jumble.html"]::before,
  .section-content a.btn[href*="chinese/text-choice.html"]::before {
    width: 52px !important;
    height: 52px !important;
    border-radius: 16px !important;
    font-size: 22px !important;
  }

  .section-content a.btn[href*="chinese/vocab.html"]::after,
  .section-content a.btn[href*="chinese/jumble.html"]::after,
  .section-content a.btn[href*="chinese/text-choice.html"]::after {
    font-size: 0.86rem !important;
    margin-top: 5px !important;
  }

  .section-content a.btn[href*="chinese/vocab.html"],
  .section-content a.btn[href*="chinese/jumble.html"],
  .section-content a.btn[href*="chinese/text-choice.html"] {
    box-sizing: border-box !important;
    position: relative !important;
    z-index: 1 !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(44, 158, 158, 0.18);
  }

  .section-button,
  .chapter-button {
    width: 100%;
    padding: 10px 5px;
    font-size: 16px;
    white-space: normal;
    /* 允許換行，防止超出 */
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 20px;
    min-height: 60px;
    /* 保持一致高度，但允許內容撐開 */
  }

  /* --- Footer 滿版貼底 (手機版) --- */
  .sticky-footer,
  body.portal-page .sticky-footer {
    display: none !important;
  }

  /* ========================================= */
  /* 標題畫面手機版調整 (Title Screen - <768px) */
  /* ========================================= */
  #title-screen .block-container {
    gap: 8px;
    margin-bottom: 30px;
  }

  #title-screen .block {
    width: 40px;
    height: 40px;
    font-size: 20px;
    border-radius: 8px;
  }

  #title-screen .game-title {
    font-size: 2rem;
    margin-bottom: 40px;
    letter-spacing: 1px;
    padding: 0 10px;
  }

  #title-screen .start-btn {
    padding: 12px 35px;
    font-size: 1.2rem;
  }

  #title-screen .ground-decoration {
    height: 40px;
  }

  #title-screen .pipe-decoration {
    width: 60px;
    height: 90px;
    bottom: 40px;
    left: 5%;
  }

  #title-screen .pipe-decoration::before {
    width: 76px;
    height: 25px;
    top: -25px;
    left: -8px;
  }

  #title-screen .pipe-decoration.right {
    height: 60px;
    right: 5%;
  }
}

@media (hover: none), (pointer: coarse) {
  .social-group:hover,
  .section-content .btn:hover,
  .section-content .btn:active,
  .section-content a.btn[href*="chinese/vocab.html"]:hover,
  .section-content a.btn[href*="chinese/jumble.html"]:hover,
  .section-content a.btn[href*="chinese/text-choice.html"]:hover {
    transform: none !important;
  }

  .section-content a.btn[href*="chinese/vocab.html"]::before,
  .section-content a.btn[href*="chinese/vocab.html"]::after,
  .section-content a.btn[href*="chinese/jumble.html"]::before,
  .section-content a.btn[href*="chinese/jumble.html"]::after,
  .section-content a.btn[href*="chinese/text-choice.html"]::before,
  .section-content a.btn[href*="chinese/text-choice.html"]::after {
    pointer-events: none;
  }
}

@media (max-width: 768px) {
  .reference-badge {
    margin: -4px auto 14px auto !important;
    font-size: 0.82rem !important;
    padding: 5px 12px !important;
  }
}

@media (max-width: 768px) {
  .breadcrumb-container {
    display: none !important;
  }

  body.with-breadcrumb {
    padding-top: 105px !important;
  }

  .subsection-selector {
    margin-top: 0;
  }
}

@media (max-width: 768px) {
  /* ----------------------------------------- */
  /* 手機版 Bottom Sheet 大改造 */
  /* ----------------------------------------- */
  
  /* 避免底層背景滾動 */
  body.modal-open {
    overflow: hidden !important;
  }

  .modal {
    display: flex !important; /* 強制覆寫 display: none/block 改以 opacity 控制 */
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    align-items: flex-end;
    justify-content: center;
    background-color: rgba(15, 23, 42, 0); /* 初始透明 */
  }

  .modal.open {
    opacity: 1;
    visibility: visible;
    background-color: rgba(15, 23, 42, 0.65); /* 遮罩漸顯 */
  }

  /* 阻尼條樣式 */
  .modal-drag-handle {
    display: block;
    width: 40px;
    height: 5px;
    background: rgba(148, 163, 184, 0.4);
    border-radius: 3px;
    margin: -8px auto 20px auto;
  }

  /* 手機版右上角紅色關閉叉叉按鈕 */
  .close-modal {
    display: flex !important;
    position: absolute !important;
    top: 16px !important;
    right: 16px !important;
    width: 36px !important;
    height: 36px !important;
    font-size: 18px !important;
    z-index: 20 !important;
  }

  .close-modal:active {
    background: #ef4444 !important;
    color: #ffffff !important;
    transform: scale(0.92) !important;
  }

  /* 隱藏底部關閉按鈕 */
  .modal-bottom-close-btn {
    display: none !important;
  }

  /* 抽屜主體 */
  .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    border-radius: 28px 28px 0 0 !important;
    padding: 24px 20px calc(20px + env(safe-area-inset-bottom)) 20px !important;
    box-shadow: 0 -10px 40px rgba(15, 23, 42, 0.12) !important;
    border: none !important;
    max-height: 85vh !important;
    transform: translateY(100%);
    transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
    background: rgba(255, 255, 255, 0.94) !important;
    backdrop-filter: blur(20px) !important;
    -webkit-backdrop-filter: blur(20px) !important;
    animation: none !important; /* 停用桌機版縮放動畫 */
  }

  .modal.open .modal-content {
    transform: translateY(0);
  }

  .modal-title {
    font-size: 20px;
    margin-bottom: 20px;
    color: #2C9E9E;
    font-weight: 800;
    text-align: center;
  }

  .modal-grid {
    flex-direction: column;
    gap: 16px;
  }

  .modal-group {
    flex: none;
    width: 100%;
    min-width: 0;
    padding: 16px 18px;
    background: rgba(241, 245, 249, 0.6);
    border: 1px solid rgba(226, 232, 240, 0.7);
    border-radius: 18px;
    box-sizing: border-box;
  }

  .modal-group:hover {
    transform: none; /* 移除 hover 位移 */
  }

  .modal-group h3 {
    text-align: left;
    font-size: 15px;
    color: #475569;
    border-bottom: 1px solid rgba(148, 163, 184, 0.15);
    padding-bottom: 8px;
    margin-bottom: 12px;
    justify-content: flex-start;
  }

  /* 按鈕改為雙欄網格 */
  .modal-btn-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 10px;
  }

  /* 管理員專區與單欄網格 */
  .modal-btn-grid.single-col {
    grid-template-columns: 1fr;
  }

  .modal-group .btn {
    margin: 0 !important;
    padding: 12px 8px;
    font-size: 14px;
    font-weight: 700;
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 12px;
    border: 1px solid rgba(44, 158, 158, 0.2);
    background: #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.01);
  }

  /* 點擊縮小回饋 */
  .modal-group .btn:active {
    background: rgba(44, 158, 158, 0.08) !important;
    border-color: #2C9E9E !important;
    color: #2C9E9E !important;
    transform: scale(0.96) !important;
    box-shadow: none !important;
  }

  /* 鎖定按鈕在手機上的 active 狀態覆寫，避免縮放與變色 */
  .modal-group .btn.locked:active {
    transform: none !important;
    background: rgba(241, 245, 249, 0.5) !important;
    color: #94a3b8 !important;
    border-color: rgba(226, 232, 240, 0.5) !important;
  }

  /* 管理員專區 */
  .admin-group {
    background: rgba(243, 244, 246, 0.6);
  }

  .modal-group .btn.admin-btn {
    background: linear-gradient(135deg, #4f46e5, #7c3aed) !important;
    color: #ffffff !important;
    border: none !important;
    box-shadow: 0 4px 10px rgba(79, 70, 229, 0.15) !important;
  }

  .modal-group .btn.admin-btn:active {
    background: linear-gradient(135deg, #4338ca, #6d28d9) !important;
    opacity: 0.9;
    transform: scale(0.96) !important;
  }
}

/* 確保小節頁籤 (Subsection Selector) 在手機上擁有高層級與獨立觸控處理，解決手機點擊無反應問題 */
@media (max-width: 768px) {
  /* 移除 .active-section 在動畫完成後殘留的 transform，
     避免建立 stacking context 導致子元素 z-index 失效。 */
  .section-content.active-section {
    animation: none !important;
    transform: none !important;
    align-items: flex-start !important;
    min-height: 0 !important;
  }

  .subsection-selector {
    position: relative !important;
    z-index: 1301 !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap !important;
    border-radius: 999px !important;
    padding: 4px !important;
    scrollbar-width: none !important;
  }
  .subsection-selector::-webkit-scrollbar {
    display: none !important;
  }

  .subsection-button,
  .subsection-selector button {
    flex: 1 0 auto !important;
    position: relative !important;
    z-index: 1302 !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    -webkit-tap-highlight-color: rgba(109, 40, 217, 0.18);
    cursor: pointer !important;
    -webkit-user-select: none;
    user-select: none;
    font-size: 0.86rem !important;
    padding: 8px 14px !important;
    white-space: nowrap !important;
  }
}

/* =========================================
   完成彈窗 & 學習獎賞回饋卡片 RWD 響應式優化 (電腦端橫式佈局)
   ========================================= */
#completion-modal,
.sped-completion-card,
#result-modal,
.quiz-result-card,
.modal-box[id*="completion"] {
  max-width: 520px;
  width: min(calc(100% - 32px), 520px);
  max-height: 90vh;
  overflow-y: auto;
  box-sizing: border-box;
}

#completion-modal .sped-reward-feedback,
.sped-completion-card .sped-reward-feedback {
  width: 100%;
  margin-bottom: 16px;
}

@media (min-width: 680px) {
  #completion-modal,
  .sped-completion-card,
  #result-modal,
  .quiz-result-card,
  .modal-box[id*="completion"] {
    max-width: 780px !important;
    width: min(calc(100% - 36px), 780px) !important;
    padding: 28px 32px !important;
    border-radius: 28px !important;
  }

  .completion-grid {
    display: flex !important;
    flex-direction: row !important;
    gap: 24px !important;
    align-items: stretch !important;
    justify-content: center !important;
    width: 100% !important;
  }

  .completion-reward-mount {
    flex: 1 1 340px !important;
    max-width: 380px !important;
    display: flex !important;
    flex-direction: column !important;
  }

  .completion-reward-mount .sped-reward-feedback {
    height: 100% !important;
    margin-bottom: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: space-between !important;
  }

  .completion-content-pane {
    flex: 1 1 300px !important;
    max-width: 360px !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: center !important;
  }

  .completion-reward-mount:empty + .completion-content-pane {
    max-width: 460px !important;
    margin: 0 auto !important;
  }

  .sped-reward-feedback {
    padding: 16px 20px !important;
    border-radius: 20px !important;
  }

  .sped-reward-companion-box {
    padding: 12px 18px !important;
  }

  .sped-reward-companion-avatar {
    width: 64px !important;
    height: 64px !important;
  }

  .sped-reward-companion-name {
    font-size: 14px !important;
  }

  .sped-reward-companion-speech {
    font-size: 13px !important;
  }

  .sped-reward-feedback-values {
    gap: 12px !important;
  }

  .sped-reward-feedback-value {
    padding: 11px 16px !important;
    font-size: 13px !important;
  }

  .sped-reward-feedback-value strong {
    font-size: 20px !important;
  }
}

@media (max-width: 679px) {
  #completion-modal,
  .sped-completion-card,
  #result-modal,
  .quiz-result-card,
  .modal-box[id*="completion"] {
    max-width: 420px !important;
    width: calc(100% - 24px) !important;
    padding: 24px 18px !important;
    border-radius: 24px !important;
  }

  .completion-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 14px !important;
  }

  .sped-reward-feedback {
    padding: 12px 14px !important;
    border-radius: 16px !important;
  }

  .sped-reward-companion-avatar {
    width: 52px !important;
    height: 52px !important;
  }
}

/* ===========================================================================
   2026-09 Mobile layout polish
   Keep the portal, resource sheet, and fixed navigation usable on 320–768px.
   ========================================================================== */
@media (max-width: 768px) {
  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  /* The bottom navigation must never cover the last course card. */
  body.portal-page {
    padding-bottom: calc(82px + env(safe-area-inset-bottom)) !important;
  }

  body.portal-page .subject-selection-section {
    padding-inline: 12px !important;
    scroll-margin-top: 78px;
  }

  body.portal-page .cards-container-three {
    width: min(100%, 480px);
    gap: 12px !important;
  }

  body.portal-page .card-header {
    padding: 12px 14px 9px !important;
  }

  body.portal-page .card-header small {
    font-size: .84rem !important;
  }

  body.portal-page .card-body {
    padding: 10px 9px 13px !important;
    gap: 8px !important;
  }

  body.portal-page .subject-btn {
    min-width: 0;
    padding: 8px 5px !important;
    font-size: clamp(.84rem, 4vw, 1rem) !important;
  }

  body.portal-page .subject-btn span {
    overflow-wrap: anywhere;
  }

  /* Long account names shrink gracefully instead of squeezing the brand. */
  #nav-identity {
    min-width: 0 !important;
    max-width: min(46vw, 190px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Keep the resource sheet above the fixed mobile navigation. */
  .modal {
    z-index: 11000;
  }

  .modal-content {
    display: flex;
    flex-direction: column;
    max-height: 86vh !important;
    max-height: 86dvh !important;
    padding: 20px 16px calc(16px + env(safe-area-inset-bottom)) !important;
    overflow: hidden !important;
  }

  .modal-drag-handle {
    margin: -4px auto 14px auto;
  }

  .modal-title {
    margin-bottom: 14px;
  }

  .modal-grid {
    flex: 1 1 auto;
    flex-wrap: nowrap;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    gap: 12px;
    padding: 0 2px 6px;
  }

  .modal-group {
    padding: 14px;
  }

  .modal-group h3 {
    margin-bottom: 9px;
    padding-bottom: 7px;
  }

  .modal-btn-grid {
    gap: 8px;
    margin-top: 8px;
  }

  .modal-group .btn {
    min-height: 46px;
    padding: 10px 7px;
    line-height: 1.3;
  }

  /* A third resource becomes a deliberate full-width action, not an empty cell. */
  .modal-group:not(.admin-group) .modal-btn-grid > .btn:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  .modal-bottom-close-btn {
    display: none !important;
  }
}

@media (max-width: 359px) {
  .navbar .logo,
  body.portal-page .navbar .logo {
    font-size: 1.16rem !important;
  }

  #nav-identity {
    max-width: 42vw;
    padding-inline: 9px !important;
  }

  .modal-content {
    padding-inline: 12px !important;
  }
}

@media (max-width: 768px) and (max-height: 680px) {
  .modal-content {
    max-height: 90dvh !important;
  }

  .modal-group {
    padding: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .modal,
  .modal-content,
  .modal-group .btn {
    transition: none !important;
  }
}


/* =========================================================
   2026-10-04 首頁外觀調整
   1) 桌機第一屏垂直置中 + 最近練習列
   2) 標題/夥伴卡避免孤字斷行
   3) 無紀錄時的成績卡空狀態
   ========================================================= */

/* 1) 桌機：第一屏在 header 與底部 footer 之間垂直置中 */
@media (min-width: 769px) {
  body.portal-page .hero-section.hero-dashboard {
    min-height: calc(100vh - 68px - 52px);
    min-height: calc(100dvh - 68px - 52px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-bottom: 12px !important;
  }
}

body.portal-page .recent-practice {
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 4px 24px 24px;
  box-sizing: border-box;
}
body.portal-page .recent-practice[hidden] { display: none; }
body.portal-page .recent-practice-title {
  margin: 0 0 10px 4px;
  font-size: 1rem;
  font-weight: 900;
  color: var(--adventure-ink, #12395a);
  letter-spacing: .04em;
}
body.portal-page .recent-practice-title i { color: #0d9488; margin-right: 6px; }
body.portal-page .recent-practice-list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}
body.portal-page .recent-practice-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 14px 16px;
  border-radius: 18px;
  background: rgba(255, 255, 255, .92);
  border: 1.5px solid rgba(185, 232, 226, .7);
  box-shadow: 0 5px 16px rgba(20, 115, 115, .08);
  color: var(--adventure-ink, #12395a);
  text-decoration: none;
  transition: transform .2s ease, box-shadow .2s ease;
}
body.portal-page .recent-practice-item:hover,
body.portal-page .recent-practice-item:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(20, 115, 115, .16);
}
body.portal-page .recent-practice-icon {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  background: #0d9488;
  font-size: 1.05rem;
}
body.portal-page .recent-practice-item.subj-chinese .recent-practice-icon { background: #ef4444; }
body.portal-page .recent-practice-item.subj-math .recent-practice-icon { background: #0891b2; }
body.portal-page .recent-practice-item.subj-english .recent-practice-icon { background: #7c3aed; }
body.portal-page .recent-practice-item.subj-social .recent-practice-icon { background: #d97706; }
body.portal-page .recent-practice-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}
body.portal-page .recent-practice-meta strong {
  font-size: .95rem;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
body.portal-page .recent-practice-meta small { font-size: .78rem; color: #5b7a90; font-weight: 700; }
body.portal-page .recent-practice-go {
  flex-shrink: 0;
  padding: 5px 10px;
  border-radius: 99px;
  background: rgba(228, 252, 250, .9);
  border: 1px solid rgba(140, 220, 214, .6);
  color: #068f92;
  font-size: .78rem;
  font-weight: 800;
  white-space: nowrap;
}
body.portal-page .recent-practice-go i { margin-left: 2px; font-size: .7rem; }

@media (max-width: 1024px) {
  body.portal-page .recent-practice { max-width: 980px; padding: 0 16px 20px; }
}
@media (max-width: 768px) {
  body.portal-page .recent-practice { padding: 4px 14px 16px; }
  body.portal-page .recent-practice-list { grid-template-columns: 1fr; gap: 10px; }
  body.portal-page .recent-practice-item { padding: 12px 14px; }
}

/* 2) 避免孤字斷行 */
body.portal-page .panel-title-text { white-space: nowrap; }
body.portal-page .panel-title-badge { gap: 8px; }
body.portal-page .panel-title-badge a { white-space: nowrap; }
body.portal-page .companion-stars { white-space: nowrap; }
body.portal-page .profile-name > #adventure-title { white-space: nowrap; }
body.portal-page .companion-message { word-break: keep-all; }
@media (max-width: 768px) {
  /* 手機：規則按鈕縮成圖示，讓「我的步步夥伴」有完整一行 */
  body.portal-page .rules-badge-btn .rules-badge-text { display: none; }
  body.portal-page .rules-badge-btn { padding: 4px 9px; flex-shrink: 0; }
  /* 星等獨立一行，不留「·」在行首 */
  body.portal-page .companion-stars-sep { display: none; }
  body.portal-page .companion-stars { display: block; margin-top: 2px; }
}

/* 3) 成績卡空狀態 */
body.portal-page .stat-empty-hint {
  margin: 0;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(228, 252, 250, .7);
  border: 1px dashed rgba(140, 220, 214, .8);
  color: #2f6f7a;
  font-size: .85rem;
  font-weight: 700;
  line-height: 1.5;
  word-break: keep-all;
}
body.portal-page .stat-empty-hint[hidden] { display: none; }
body.portal-page .subject-stat-val.is-empty {
  font-size: .85rem !important;
  font-weight: 800;
  opacity: .6;
  letter-spacing: .02em;
}

/* =========================================================
   2026-10-04 訪客模式扭蛋警告
   ========================================================= */
.guest-gacha-warning {
  margin: 0 0 14px;
  padding: 12px 16px;
  border-radius: 16px;
  background: #fff7e6;
  border: 1.5px solid #f5c26b;
  color: #6b3f00;
  font-size: .9rem;
  font-weight: 600;
  line-height: 1.6;
  text-align: left;
}
.guest-gacha-warning[hidden],
.guest-gacha-confirm[hidden] { display: none; }
.guest-gacha-warning p { margin: 0 0 6px; }
.guest-gacha-warning .guest-gacha-warning-title { font-weight: 900; font-size: .98rem; color: #8a4b00; }
.guest-gacha-warning ul { margin: 0 0 6px; padding-left: 1.3em; }
.guest-gacha-warning li { margin: 2px 0; }
.guest-gacha-warning strong { color: #b45309; }
.guest-gacha-login-btn,
.guest-gacha-proceed-btn {
  border-radius: 99px;
  padding: 8px 14px;
  font-size: .85rem;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
}
.guest-gacha-login-btn {
  border: 0;
  background: #0d9488;
  color: #fff;
}
.guest-gacha-login-btn:hover { background: #0f766e; }
.guest-gacha-proceed-btn {
  border: 1.5px solid #f5c26b;
  background: #fff;
  color: #8a4b00;
}
.guest-gacha-proceed-btn:hover { background: #fff3d6; }
.guest-gacha-confirm {
  margin-top: 10px;
  padding: 12px 14px;
  border-radius: 14px;
  background: #fff7e6;
  border: 1.5px dashed #f5a524;
  color: #6b3f00;
  font-size: .88rem;
  font-weight: 700;
  line-height: 1.55;
  text-align: left;
}
#guest-gacha-confirm p { margin: 0 0 10px; color: #6b3f00; font-size: .88rem; font-weight: 700; line-height: 1.55; }
#guest-gacha-warning p:not(.guest-gacha-warning-title) { color: #6b3f00; }
.guest-gacha-confirm-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* 訪客警告：桌機完整顯示；手機預設收合，只留標題、一句摘要與登入按鈕 */
.guest-gacha-warning-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.guest-gacha-warning-toggle,
.guest-gacha-warning-short { display: none; }
@media (max-width: 768px) {
  .guest-gacha-warning { padding: 10px 12px; font-size: .85rem; }
  .guest-gacha-warning-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    padding: 4px 10px;
    border-radius: 99px;
    border: 1.5px solid #f5c26b;
    background: #fff;
    color: #8a4b00;
    font-size: .78rem;
    font-weight: 800;
    font-family: inherit;
    cursor: pointer;
  }
  .guest-gacha-warning-toggle i { font-size: .7rem; }
  .guest-gacha-warning.is-collapsed .guest-gacha-warning-short { display: block; }
  .guest-gacha-warning.is-collapsed .guest-gacha-warning-body { display: none; }
  .guest-gacha-warning .guest-gacha-login-btn { margin-top: 2px; }
}
