/* =====================================================
   科學閱讀App - 行動裝置增強樣式
   最後更新：2025年12月7日
   ===================================================== */

/* =====================================================
   1. PWA 和 iOS 優化
   ===================================================== */
   
/* iOS 狀態欄安全區域 */
html {
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* 防止 iOS 文字大小自動調整 */
body {
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* 移除 iOS 上的點擊高亮 */
* {
    -webkit-tap-highlight-color: transparent;
}

/* 平滑滾動 */
html, body {
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

/* =====================================================
   2. 增強觸控互動
   ===================================================== */

/* 觸控設備上的按鈕增強 */
@media (hover: none) and (pointer: coarse) {
    /* 增大觸控目標 */
    .btn {
        min-height: 48px;
        padding: 0.75rem 1.25rem;
    }
    
    .btn-sm {
        min-height: 40px;
        padding: 0.5rem 1rem;
    }
    
    .btn-lg {
        min-height: 56px;
        padding: 1rem 1.5rem;
    }
    
    /* 增大表單控件 */
    .form-control, 
    .form-select {
        min-height: 48px;
        font-size: 16px; /* 防止 iOS 縮放 */
    }
    
    /* 增大核取方塊和單選按鈕 */
    .form-check-input {
        width: 1.5em;
        height: 1.5em;
    }
    
    .form-check-label {
        padding-left: 0.5rem;
        line-height: 1.5em;
    }
    
    /* 增大導航連結 */
    .nav-link {
        min-height: 48px;
        padding: 0.75rem 1rem;
    }
    
    /* 增大下拉選項 */
    .dropdown-item {
        min-height: 48px;
        padding: 0.75rem 1.25rem;
        display: flex;
        align-items: center;
    }
    
    /* 增大列表項 */
    .list-group-item {
        min-height: 56px;
        padding: 1rem 1.25rem;
    }
    
    /* 增大分頁按鈕 */
    .page-link {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    /* 觸控反饋 - 輕微縮放效果 */
    .btn:active,
    .card:active,
    .list-group-item:active {
        transform: scale(0.98);
    }
    
    /* 禁用 hover 效果以避免觸控後持續高亮 */
    .btn:hover,
    .card:hover,
    .nav-link:hover {
        transform: none;
    }
}

/* =====================================================
   3. 下拉刷新樣式
   ===================================================== */
.pull-to-refresh {
    position: fixed;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 50px;
    height: 50px;
    background: var(--primary-color, #667eea);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1.5rem;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    transition: top 0.3s ease;
    z-index: 9999;
}

.pull-to-refresh.visible {
    top: 20px;
}

.pull-to-refresh.refreshing {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: translateX(-50%) rotate(0deg); }
    to { transform: translateX(-50%) rotate(360deg); }
}

/* =====================================================
   4. 浮動操作按鈕 (FAB)
   ===================================================== */
.fab-container {
    position: fixed;
    bottom: 90px; /* 底部導航上方 */
    right: 20px;
    z-index: 999;
    display: flex;
    flex-direction: column-reverse;
    align-items: flex-end;
    gap: 12px;
}

.fab {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    cursor: pointer;
    transition: all 0.3s ease;
}

.fab:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
}

.fab:active {
    transform: scale(0.95);
}

.fab-main {
    width: 60px;
    height: 60px;
    font-size: 1.75rem;
}

.fab-mini {
    width: 44px;
    height: 44px;
    font-size: 1.1rem;
    opacity: 0;
    transform: scale(0);
    transition: all 0.2s ease;
}

.fab-container.expanded .fab-mini {
    opacity: 1;
    transform: scale(1);
}

.fab-label {
    position: absolute;
    right: 100%;
    margin-right: 12px;
    background: rgba(0,0,0,0.8);
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.85rem;
    white-space: nowrap;
    opacity: 0;
    transform: translateX(10px);
    transition: all 0.2s ease;
    pointer-events: none;
}

.fab-mini:hover .fab-label {
    opacity: 1;
    transform: translateX(0);
}

/* 沒有底部導航時調整 FAB 位置 */
body:not(.has-bottom-nav) .fab-container {
    bottom: 20px;
}

/* =====================================================
   5. 手機端底部導航增強
   ===================================================== */
@media (max-width: 767px) {
    /* 通用底部導航樣式 */
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: #fff;
        box-shadow: 0 -2px 20px rgba(0, 0, 0, 0.1);
        z-index: 1000;
        padding: 8px 0;
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
        justify-content: space-around;
        border-top: 1px solid rgba(0,0,0,0.05);
    }
    
    .mobile-bottom-nav .nav-link {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 8px 4px;
        color: #6c757d;
        text-decoration: none;
        font-size: 0.7rem;
        min-height: 56px;
        transition: all 0.2s ease;
        border-radius: 8px;
        margin: 0 4px;
    }
    
    .mobile-bottom-nav .nav-link.active {
        color: #667eea;
        background: rgba(102, 126, 234, 0.1);
    }
    
    .mobile-bottom-nav .nav-icon {
        font-size: 1.5rem;
        margin-bottom: 4px;
        line-height: 1;
    }
    
    .mobile-bottom-nav .nav-link img.nav-icon {
        width: 28px;
        height: 28px;
        object-fit: cover;
        margin-bottom: 4px;
    }
    
    /* 有底部導航時調整內容區域 */
    body.has-bottom-nav main {
        padding-bottom: calc(80px + env(safe-area-inset-bottom));
    }
    
    /* 調整浮動按鈕位置 */
    body.has-bottom-nav .fab-container {
        bottom: calc(90px + env(safe-area-inset-bottom));
    }
}

/* =====================================================
   6. 手機端卡片優化
   ===================================================== */
@media (max-width: 767px) {
    .card {
        border-radius: 16px;
        margin-bottom: 16px;
        border: none;
        box-shadow: 0 2px 12px rgba(0,0,0,0.08);
    }
    
    .card-header {
        border-radius: 16px 16px 0 0;
        padding: 1rem;
        font-weight: 600;
    }
    
    .card-body {
        padding: 1rem;
    }
    
    .card-footer {
        border-radius: 0 0 16px 16px;
        padding: 0.75rem 1rem;
    }
    
    /* 統計卡片特殊樣式 */
    .stat-card {
        background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
        border: none;
    }
    
    .stat-card h2 {
        font-size: 1.5rem;
        font-weight: 700;
    }
    
    .stat-card h5 {
        font-size: 0.8rem;
        color: #6c757d;
    }
}

/* =====================================================
   7. 手機端表單優化
   ===================================================== */
@media (max-width: 767px) {
    .form-control,
    .form-select {
        font-size: 16px; /* 防止 iOS 縮放 */
        padding: 0.875rem 1rem;
        border-radius: 12px;
        border: 2px solid #e9ecef;
        transition: all 0.2s ease;
    }
    
    .form-control:focus,
    .form-select:focus {
        border-color: #667eea;
        box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.15);
    }
    
    .form-label {
        font-weight: 500;
        margin-bottom: 0.5rem;
        color: #495057;
    }
    
    /* 表單錯誤狀態 */
    .form-control.is-invalid,
    .form-select.is-invalid {
        border-color: #dc3545;
    }
    
    .form-control.is-valid,
    .form-select.is-valid {
        border-color: #28a745;
    }
    
    /* 搜尋框增強 */
    .search-box {
        position: relative;
    }
    
    .search-box input {
        padding-left: 2.5rem;
        padding-right: 2.5rem;
    }
    
    .search-box .search-icon {
        position: absolute;
        left: 1rem;
        top: 50%;
        transform: translateY(-50%);
        color: #adb5bd;
    }
    
    .search-box .clear-btn {
        position: absolute;
        right: 0.5rem;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: #adb5bd;
        padding: 0.5rem;
    }
}

/* =====================================================
   8. 手機端彈窗優化
   ===================================================== */
@media (max-width: 767px) {
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        height: 100%;
        display: flex;
        align-items: flex-end;
    }
    
    .modal-content {
        border-radius: 20px 20px 0 0;
        border: none;
        max-height: 90vh;
        overflow-y: auto;
    }
    
    .modal-header {
        border-bottom: 1px solid #f0f0f0;
        padding: 1.25rem;
    }
    
    .modal-body {
        padding: 1.25rem;
    }
    
    .modal-footer {
        border-top: 1px solid #f0f0f0;
        padding: 1rem;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        flex: 1;
    }
    
    /* 全屏彈窗 */
    .modal-fullscreen-sm .modal-dialog {
        align-items: stretch;
    }
    
    .modal-fullscreen-sm .modal-content {
        border-radius: 0;
        max-height: 100%;
    }
}

/* =====================================================
   9. 手機端列表優化
   ===================================================== */
@media (max-width: 767px) {
    .list-group {
        border-radius: 16px;
        overflow: hidden;
    }
    
    .list-group-item {
        padding: 1rem;
        border-left: none;
        border-right: none;
        border-color: #f0f0f0;
    }
    
    .list-group-item:first-child {
        border-top: none;
    }
    
    .list-group-item:last-child {
        border-bottom: none;
    }
    
    /* 可滑動刪除的列表項 */
    .list-group-item.swipeable {
        position: relative;
        overflow: hidden;
    }
    
    .list-group-item.swipeable .swipe-actions {
        position: absolute;
        top: 0;
        right: -100px;
        height: 100%;
        display: flex;
        align-items: center;
        transition: right 0.3s ease;
    }
    
    .list-group-item.swipeable.swiped .swipe-actions {
        right: 0;
    }
}

/* =====================================================
   10. 骨架屏載入動畫
   ===================================================== */
.skeleton-loader {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s infinite;
    border-radius: 8px;
}

@keyframes skeleton-shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.skeleton-text {
    height: 1em;
    margin-bottom: 0.5em;
}

.skeleton-text-sm {
    height: 0.75em;
    width: 60%;
}

.skeleton-title {
    height: 1.5em;
    width: 70%;
    margin-bottom: 1em;
}

.skeleton-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
}

.skeleton-card {
    height: 120px;
    border-radius: 16px;
}

.skeleton-button {
    height: 48px;
    width: 120px;
    border-radius: 8px;
}

/* =====================================================
   11. 手機端導航欄優化
   ===================================================== */
@media (max-width: 991px) {
    .navbar {
        padding: 0.5rem 1rem;
    }
    
    .navbar-brand {
        font-size: 1.1rem;
        font-weight: 600;
    }
    
    .navbar-toggler {
        border: none;
        padding: 0.5rem;
        border-radius: 8px;
    }
    
    .navbar-toggler:focus {
        box-shadow: none;
    }
    
    /* 導航列展開選單 - 修復文字顏色 */
    .navbar-collapse {
        background: #fff;
        border-radius: 0 0 12px 12px;
        margin-top: 0.5rem;
        padding: 1rem;
        box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    }
    
    /* 強制導航列文字為深色 */
    .navbar-dark .navbar-collapse .navbar-nav .nav-link {
        color: #333 !important;
        padding: 0.875rem 1rem;
        border-radius: 8px;
        margin-bottom: 2px;
    }
    
    .navbar-dark .navbar-collapse .navbar-nav .nav-link:hover,
    .navbar-dark .navbar-collapse .navbar-nav .nav-link:active {
        background: rgba(102, 126, 234, 0.1);
        color: #667eea !important;
    }
    
    .navbar-dark .navbar-collapse .navbar-nav .nav-link.active {
        color: #667eea !important;
        background: rgba(102, 126, 234, 0.1);
    }
    
    /* 下拉選單樣式 */
    .navbar-dark .navbar-collapse .dropdown-menu {
        border: none;
        box-shadow: none;
        background: #f8f9fa;
        border-radius: 8px;
        margin: 0.25rem 0;
        padding: 0.5rem;
    }
    
    .navbar-dark .navbar-collapse .dropdown-item {
        color: #333;
        padding: 0.75rem 1rem;
        border-radius: 6px;
    }
    
    .navbar-dark .navbar-collapse .dropdown-item:hover,
    .navbar-dark .navbar-collapse .dropdown-item:active {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
    }
    
    /* 下拉箭頭顏色 */
    .navbar-dark .navbar-collapse .dropdown-toggle::after {
        border-top-color: #333;
    }
    
    /* 分隔線 */
    .navbar-dark .navbar-collapse .dropdown-divider {
        border-color: #dee2e6;
    }
}

/* =====================================================
   12. 手機端表格優化
   ===================================================== */
@media (max-width: 767px) {
    .table-responsive {
        border-radius: 12px;
        overflow: hidden;
        border: 1px solid #e9ecef;
    }
    
    .table {
        font-size: 0.875rem;
    }
    
    .table th,
    .table td {
        padding: 0.75rem 0.5rem;
        white-space: nowrap;
    }
    
    /* 卡片式表格（可選替代方案） */
    .table-cards {
        display: block;
    }
    
    .table-cards thead {
        display: none;
    }
    
    .table-cards tbody {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }
    
    .table-cards tr {
        display: block;
        background: #fff;
        border-radius: 12px;
        padding: 1rem;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .table-cards td {
        display: flex;
        justify-content: space-between;
        padding: 0.5rem 0;
        border: none;
    }
    
    .table-cards td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6c757d;
    }
}

/* =====================================================
   13. 手機端通知和提示
   ===================================================== */
@media (max-width: 767px) {
    /* Toast 通知 */
    .toast-container {
        position: fixed;
        top: auto;
        bottom: calc(100px + env(safe-area-inset-bottom));
        left: 50%;
        transform: translateX(-50%);
        z-index: 1100;
        width: calc(100% - 32px);
        max-width: 360px;
    }
    
    .toast {
        border-radius: 12px;
        border: none;
        box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    }
    
    /* 提示訊息 */
    .alert {
        border-radius: 12px;
        border: none;
        padding: 1rem;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .alert-dismissible .btn-close {
        padding: 1.25rem 1rem;
    }
}

/* =====================================================
   14. 手機端圖片和媒體
   ===================================================== */
@media (max-width: 767px) {
    img {
        max-width: 100%;
        height: auto;
    }
    
    .img-fluid {
        border-radius: 12px;
    }
    
    /* 頭像 */
    .avatar {
        border-radius: 50%;
        object-fit: cover;
    }
    
    .avatar-sm {
        width: 32px;
        height: 32px;
    }
    
    .avatar-md {
        width: 48px;
        height: 48px;
    }
    
    .avatar-lg {
        width: 64px;
        height: 64px;
    }
    
    /* 圖片預覽 */
    .image-preview {
        position: relative;
        border-radius: 12px;
        overflow: hidden;
    }
    
    .image-preview img {
        width: 100%;
        aspect-ratio: 16/9;
        object-fit: cover;
    }
}

/* =====================================================
   15. 手機端動畫效果
   ===================================================== */

/* 頁面進入動畫 */
.page-enter {
    animation: pageEnter 0.3s ease-out;
}

@keyframes pageEnter {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* 卡片進入動畫 */
.card-enter {
    animation: cardEnter 0.4s ease-out;
}

@keyframes cardEnter {
    from {
        opacity: 0;
        transform: translateY(30px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* 列表項動畫 */
.list-enter {
    animation: listEnter 0.3s ease-out;
    animation-fill-mode: both;
}

@keyframes listEnter {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* 延遲動畫類 */
.delay-1 { animation-delay: 0.1s; }
.delay-2 { animation-delay: 0.2s; }
.delay-3 { animation-delay: 0.3s; }
.delay-4 { animation-delay: 0.4s; }
.delay-5 { animation-delay: 0.5s; }

/* =====================================================
   16. 工具類
   ===================================================== */

/* 隱藏/顯示類 */
@media (max-width: 767px) {
    .hide-on-mobile { display: none !important; }
    .show-on-mobile { display: block !important; }
    .show-on-mobile-flex { display: flex !important; }
    .show-on-mobile-inline { display: inline !important; }
}

@media (min-width: 768px) {
    .show-on-mobile { display: none !important; }
    .hide-on-desktop { display: none !important; }
}

/* 間距工具 */
@media (max-width: 767px) {
    .p-mobile-0 { padding: 0 !important; }
    .p-mobile-1 { padding: 0.25rem !important; }
    .p-mobile-2 { padding: 0.5rem !important; }
    .p-mobile-3 { padding: 1rem !important; }
    .p-mobile-4 { padding: 1.5rem !important; }
    
    .m-mobile-0 { margin: 0 !important; }
    .m-mobile-1 { margin: 0.25rem !important; }
    .m-mobile-2 { margin: 0.5rem !important; }
    .m-mobile-3 { margin: 1rem !important; }
    .m-mobile-4 { margin: 1.5rem !important; }
    
    .mb-mobile-0 { margin-bottom: 0 !important; }
    .mb-mobile-1 { margin-bottom: 0.25rem !important; }
    .mb-mobile-2 { margin-bottom: 0.5rem !important; }
    .mb-mobile-3 { margin-bottom: 1rem !important; }
    .mb-mobile-4 { margin-bottom: 1.5rem !important; }
}

/* 文字工具 */
@media (max-width: 767px) {
    .text-mobile-center { text-align: center !important; }
    .text-mobile-start { text-align: left !important; }
    .text-mobile-end { text-align: right !important; }
    
    .fs-mobile-sm { font-size: 0.875rem !important; }
    .fs-mobile-md { font-size: 1rem !important; }
    .fs-mobile-lg { font-size: 1.125rem !important; }
}

/* 佈局工具 */
@media (max-width: 767px) {
    .d-mobile-block { display: block !important; }
    .d-mobile-flex { display: flex !important; }
    .d-mobile-none { display: none !important; }
    .d-mobile-grid { display: grid !important; }
    
    .flex-mobile-column { flex-direction: column !important; }
    .flex-mobile-row { flex-direction: row !important; }
    .flex-mobile-wrap { flex-wrap: wrap !important; }
    
    .gap-mobile-1 { gap: 0.25rem !important; }
    .gap-mobile-2 { gap: 0.5rem !important; }
    .gap-mobile-3 { gap: 1rem !important; }
    
    .w-mobile-100 { width: 100% !important; }
    .w-mobile-auto { width: auto !important; }
}

/* =====================================================
   17. 深色模式支援
   ===================================================== */
@media (prefers-color-scheme: dark) {
    :root {
        --mobile-bg: #1a1a2e;
        --mobile-card-bg: #16213e;
        --mobile-text: #eee;
        --mobile-text-muted: #aaa;
        --mobile-border: #2a2a4a;
    }
    
    /* 如需啟用深色模式，取消以下註解 */
    /*
    body.dark-mode {
        background-color: var(--mobile-bg);
        color: var(--mobile-text);
    }
    
    body.dark-mode .card {
        background-color: var(--mobile-card-bg);
        border-color: var(--mobile-border);
    }
    
    body.dark-mode .mobile-bottom-nav {
        background-color: var(--mobile-card-bg);
        border-color: var(--mobile-border);
    }
    */
}

/* =====================================================
   18. 無障礙支援
   ===================================================== */

/* 減少動態效果（尊重用戶偏好） */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* 高對比度模式 */
@media (prefers-contrast: high) {
    .btn {
        border-width: 2px;
    }
    
    .form-control,
    .form-select {
        border-width: 2px;
    }
    
    .card {
        border-width: 2px;
    }
}

/* 焦點可見性 */
:focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

/* 跳過導航連結 */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: #667eea;
    color: white;
    padding: 8px 16px;
    z-index: 10000;
    transition: top 0.3s;
}

.skip-link:focus {
    top: 0;
}

