/* =====================================================
   科學閱讀App - 響應式設計樣式
   最後更新：2025年11月28日
   ===================================================== */

/* =====================================================
   1. 基礎樣式
   ===================================================== */
:root {
    --primary-color: #667eea;
    --primary-dark: #764ba2;
    --success-color: #28a745;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #17a2b8;
    --light-bg: #f8f9fa;
    --dark-text: #212529;
    --muted-text: #6c757d;
    --border-radius: 10px;
    --box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    --transition: all 0.3s ease;
}

* {
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    font-size: 16px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

main {
    flex: 1;
}

/* =====================================================
   2. 卡片樣式
   ===================================================== */
.card {
    transition: var(--transition);
    border-radius: var(--border-radius);
    overflow: hidden;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: var(--box-shadow);
}

.card-body {
    padding: 1.25rem;
}

/* =====================================================
   3. 導航欄響應式
   ===================================================== */
.navbar {
    padding: 0.5rem 1rem;
}

.navbar-brand {
    font-size: 1.25rem;
    font-weight: bold;
}

.navbar-nav .nav-link {
    padding: 0.5rem 0.75rem;
    white-space: nowrap;
}

/* =====================================================
   4. 按鈕樣式
   ===================================================== */
.btn {
    transition: var(--transition);
    border-radius: 8px;
    font-weight: 500;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.btn:active {
    transform: translateY(0);
}

/* =====================================================
   5. 表格響應式
   ===================================================== */
.table-responsive {
    border-radius: var(--border-radius);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.table {
    margin-bottom: 0;
}

.table th,
.table td {
    vertical-align: middle;
    white-space: nowrap;
}

/* =====================================================
   6. 表單響應式
   ===================================================== */
.form-control,
.form-select {
    border-radius: 8px;
    padding: 0.625rem 1rem;
    font-size: 1rem;
}

.form-control:focus,
.form-select:focus {
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.25);
    border-color: var(--primary-color);
}

/* =====================================================
   7. 統計卡片優化
   ===================================================== */
.stat-card {
    background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
    border: none;
    box-shadow: var(--box-shadow);
}

.stat-card .card-body {
    padding: 1.5rem;
}

.stat-card h2 {
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 0.5rem;
}

.stat-card h5 {
    font-size: 0.875rem;
    color: var(--muted-text);
    margin-bottom: 0.5rem;
}

/* =====================================================
   8. 進度條優化
   ===================================================== */
.progress {
    border-radius: 50px;
    overflow: hidden;
}

.progress-bar {
    transition: width 0.6s ease;
}

/* =====================================================
   9. 徽章和標籤
   ===================================================== */
.badge {
    font-weight: 500;
    padding: 0.5em 0.75em;
    border-radius: 6px;
}

/* =====================================================
   10. 彈窗和對話框
   ===================================================== */
.modal-content {
    border-radius: var(--border-radius);
    border: none;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
    border-bottom: 1px solid #e9ecef;
    padding: 1rem 1.5rem;
}

.modal-body {
    padding: 1.5rem;
}

.modal-footer {
    border-top: 1px solid #e9ecef;
    padding: 1rem 1.5rem;
}

/* =====================================================
   11. 列表優化
   ===================================================== */
.list-group-item {
    border-radius: 0;
    border-left: none;
    border-right: none;
    padding: 1rem 1.25rem;
}

.list-group-item:first-child {
    border-top: none;
    border-top-left-radius: var(--border-radius);
    border-top-right-radius: var(--border-radius);
}

.list-group-item:last-child {
    border-bottom: none;
    border-bottom-left-radius: var(--border-radius);
    border-bottom-right-radius: var(--border-radius);
}

/* =====================================================
   12. 工具提示和彈出框
   ===================================================== */
.tooltip-inner {
    border-radius: 6px;
    padding: 0.5rem 1rem;
}

/* =====================================================
   13. 頁面載入動畫
   ===================================================== */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.animate-fade-in {
    animation: fadeInUp 0.5s ease-out;
}

/* =====================================================
   響應式斷點
   - xs: < 576px (手機)
   - sm: 576px - 767px (手機橫向/小平板)
   - md: 768px - 991px (平板)
   - lg: 992px - 1199px (小桌面)
   - xl: >= 1200px (大桌面)
   ===================================================== */

/* =====================================================
   14. 平板端優化 (768px - 991px)
   ===================================================== */
@media (max-width: 991px) {
    /* 導航欄展開選單 */
    .navbar-collapse {
        background: #fff;
        padding: 1rem;
        border-radius: var(--border-radius);
        margin-top: 0.5rem;
        box-shadow: var(--box-shadow);
    }
    
    /* 修復 navbar-dark 下的文字顏色 */
    .navbar-dark .navbar-nav .nav-link {
        color: #333 !important;
        padding: 0.75rem 1rem;
        border-radius: 8px;
    }
    
    .navbar-dark .navbar-nav .nav-link:hover {
        background: var(--light-bg);
        color: var(--primary-color) !important;
    }
    
    .navbar-nav .dropdown-menu {
        border: none;
        box-shadow: none;
        background: var(--light-bg);
        margin: 0;
        padding: 0.5rem 0;
    }
    
    .navbar-dark .navbar-nav .dropdown-item {
        color: #333;
    }
    
    .navbar-dark .navbar-nav .dropdown-toggle::after {
        border-top-color: #333;
    }
    
    /* 統計卡片 2x2 佈局 */
    .row > [class*="col-md-3"] {
        flex: 0 0 50%;
        max-width: 50%;
        margin-bottom: 1rem;
    }
    
    /* 圖表區域全寬 */
    .row > [class*="col-md-6"] {
        flex: 0 0 100%;
        max-width: 100%;
        margin-bottom: 1rem;
    }
    
    /* 卡片內容調整 */
    .card-body {
        padding: 1rem;
    }
    
    /* 按鈕組堆疊 */
    .btn-group-responsive {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .btn-group-responsive .btn {
        width: 100%;
    }
}

/* =====================================================
   15. 手機端優化 (< 768px)
   ===================================================== */
@media (max-width: 767px) {
    /* 基礎字體調整 */
    body {
        font-size: 15px;
    }
    
    h1 { font-size: 1.75rem; }
    h2 { font-size: 1.5rem; }
    h3 { font-size: 1.25rem; }
    h4 { font-size: 1.125rem; }
    h5 { font-size: 1rem; }
    h6 { font-size: 0.875rem; }
    
    /* 容器調整 */
    .container {
        padding-left: 15px;
        padding-right: 15px;
    }
    
    main.container {
        padding-top: 1rem;
        padding-bottom: 1rem;
    }
    
    /* 導航欄優化 */
    .navbar-brand {
        font-size: 1.125rem;
    }
    
    /* 語言切換器 */
    .language-switcher {
        top: auto;
        bottom: 15px;
        right: 15px;
    }
    
    .language-switcher .btn {
        font-size: 0.75rem;
        padding: 0.25rem 0.5rem;
    }
    
    /* 統計卡片全寬 */
    .row > [class*="col-md-3"],
    .row > [class*="col-md-4"],
    .row > [class*="col-md-6"] {
        flex: 0 0 100%;
        max-width: 100%;
        margin-bottom: 1rem;
    }
    
    /* 卡片優化 */
    .card {
        margin-bottom: 1rem;
        border-radius: 12px;
    }
    
    .card-header {
        padding: 0.75rem 1rem;
    }
    
    .card-body {
        padding: 1rem;
    }
    
    .card-footer {
        padding: 0.75rem 1rem;
    }
    
    /* 統計數字縮小 */
    .stat-card h2 {
        font-size: 1.5rem;
    }
    
    /* 表格橫向滾動 */
    .table-responsive {
        margin: 0 -15px;
        padding: 0 15px;
    }
    
    .table th,
    .table td {
        padding: 0.5rem;
        font-size: 0.875rem;
    }
    
    /* 按鈕優化 */
    .btn {
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }
    
    .btn-sm {
        padding: 0.375rem 0.75rem;
        font-size: 0.8rem;
    }
    
    .btn-lg {
        padding: 0.75rem 1.5rem;
        font-size: 1rem;
    }
    
    /* 按鈕全寬 */
    .btn-mobile-full {
        width: 100%;
        margin-bottom: 0.5rem;
    }
    
    /* 表單優化 */
    .form-control,
    .form-select {
        font-size: 16px; /* 防止 iOS 縮放 */
        padding: 0.75rem 1rem;
    }
    
    .form-label {
        font-size: 0.9rem;
        margin-bottom: 0.375rem;
    }
    
    /* 彈窗優化 */
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 16px;
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 1rem;
    }
    
    /* 列表優化 */
    .list-group-item {
        padding: 0.75rem 1rem;
    }
    
    /* 徽章優化 */
    .badge {
        font-size: 0.75rem;
        padding: 0.375em 0.625em;
    }
    
    /* 進度條 */
    .progress {
        height: 8px;
    }
    
    /* 分頁優化 */
    .pagination {
        flex-wrap: wrap;
        justify-content: center;
    }
    
    .pagination .page-link {
        padding: 0.375rem 0.625rem;
        font-size: 0.875rem;
    }
    
    /* Alert 優化 */
    .alert {
        padding: 0.75rem 1rem;
        font-size: 0.9rem;
    }
    
    /* Dropdown 優化 */
    .dropdown-menu {
        font-size: 0.9rem;
    }
    
    .dropdown-item {
        padding: 0.625rem 1rem;
    }
    
    /* 頁尾 */
    footer {
        padding: 1rem !important;
    }
    
    footer p {
        font-size: 0.8rem;
    }
}

/* =====================================================
   16. 小手機優化 (< 576px)
   ===================================================== */
@media (max-width: 575px) {
    /* 更小字體 */
    body {
        font-size: 14px;
    }
    
    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.375rem; }
    h3 { font-size: 1.25rem; }
    h4 { font-size: 1.125rem; }
    h5 { font-size: 1rem; }
    
    /* 容器更小邊距 */
    .container {
        padding-left: 12px;
        padding-right: 12px;
    }
    
    /* 卡片更小圓角 */
    .card {
        border-radius: 10px;
    }
    
    /* 統計數字更小 */
    .stat-card h2 {
        font-size: 1.25rem;
    }
    
    .stat-card h5 {
        font-size: 0.75rem;
    }
    
    /* 按鈕更小 */
    .btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.85rem;
    }
    
    /* 雙欄徽章顯示 */
    .row > .col-md-2 {
        flex: 0 0 50%;
        max-width: 50%;
    }
    
    /* 隱藏次要內容 */
    .d-none-xs {
        display: none !important;
    }
    
    /* 表格文字更小 */
    .table th,
    .table td {
        font-size: 0.8rem;
        padding: 0.4rem;
    }
}

/* =====================================================
   17. 遊戲頁面響應式
   ===================================================== */
.game-layout {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

.game-sidebar {
    flex: 0 0 250px;
}

.game-main {
    flex: 1;
    min-width: 0;
}

@media (max-width: 991px) {
    .game-layout {
        flex-direction: column;
    }
    
    .game-sidebar {
        flex: none;
        width: 100%;
        order: 2;
    }
    
    .game-main {
        order: 1;
    }
}

@media (max-width: 767px) {
    .game-sidebar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 1000;
        background: #fff;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
        padding: 1rem;
        border-radius: 20px 20px 0 0;
        max-height: 40vh;
        overflow-y: auto;
    }
    
    .game-main {
        padding-bottom: 100px;
    }
}

/* =====================================================
   18. 三欄佈局響應式（遊戲頁面）
   ===================================================== */
.three-column-layout {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    gap: 1rem;
}

@media (max-width: 991px) {
    .three-column-layout {
        grid-template-columns: 1fr 1fr;
    }
    
    .three-column-layout .left-column {
        grid-column: 1 / 2;
    }
    
    .three-column-layout .center-column {
        grid-column: 2 / 3;
    }
    
    .three-column-layout .right-column {
        grid-column: 1 / 3;
    }
}

@media (max-width: 767px) {
    .three-column-layout {
        grid-template-columns: 1fr;
    }
    
    .three-column-layout .left-column,
    .three-column-layout .center-column,
    .three-column-layout .right-column {
        grid-column: 1;
    }
}

/* =====================================================
   19. 故事和閱讀材料響應式
   ===================================================== */
.story-opening {
    padding: 1.5rem;
}

@media (max-width: 767px) {
    .story-opening {
        padding: 1rem;
        font-size: 0.95rem;
        border-radius: 8px;
    }
    
    .story-opening h4 {
        font-size: 1.125rem;
    }
}

.reading-material {
    max-height: 60vh;
    overflow-y: auto;
}

@media (max-width: 767px) {
    .reading-material {
        max-height: 40vh;
    }
}

/* =====================================================
   20. 彈窗響應式
   ===================================================== */
.story-progress-popup,
.random-event-popup {
    width: 90%;
    max-width: 500px;
}

@media (max-width: 767px) {
    .story-progress-popup,
    .random-event-popup {
        width: 95%;
        padding: 1rem;
    }
    
    .story-progress-popup h4,
    .random-event-popup h4 {
        font-size: 1.125rem;
    }
}

/* =====================================================
   21. 排行榜響應式
   ===================================================== */
.leaderboard-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
}

@media (max-width: 767px) {
    .leaderboard-item {
        gap: 0.5rem;
        padding: 0.5rem 0.75rem;
        font-size: 0.9rem;
    }
    
    .leaderboard-item .rank {
        min-width: 30px;
    }
    
    .leaderboard-item .avatar {
        width: 32px;
        height: 32px;
    }
}

/* =====================================================
   22. 圖表響應式
   ===================================================== */
.chart-container {
    position: relative;
    height: 300px;
}

@media (max-width: 767px) {
    .chart-container {
        height: 200px;
    }
}

/* =====================================================
   23. 底部固定導航（手機）
   ===================================================== */
.mobile-bottom-nav {
    display: none;
}

@media (max-width: 767px) {
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: #fff;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
        z-index: 1000;
        padding: 0.5rem 0;
        justify-content: space-around;
    }
    
    .mobile-bottom-nav .nav-item {
        flex: 1;
        text-align: center;
    }
    
    .mobile-bottom-nav .nav-link {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 0.5rem;
        color: var(--muted-text);
        font-size: 0.7rem;
    }
    
    .mobile-bottom-nav .nav-link.active {
        color: var(--primary-color);
    }
    
    .mobile-bottom-nav .nav-icon {
        font-size: 1.25rem;
        margin-bottom: 0.25rem;
    }
    
    /* 有底部導航時調整內容區域 */
    body.has-bottom-nav main {
        padding-bottom: 70px;
    }
}

/* =====================================================
   24. 觸控優化
   ===================================================== */
@media (hover: none) and (pointer: coarse) {
    /* 觸控設備 - 增大點擊區域 */
    .btn {
        min-height: 44px;
        min-width: 44px;
    }
    
    .nav-link {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    
    .dropdown-item {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    
    .form-check-input {
        width: 1.25em;
        height: 1.25em;
    }
    
    /* 禁用 hover 效果 */
    .card:hover {
        transform: none;
    }
    
    .btn:hover {
        transform: none;
    }
}

/* =====================================================
   25. 安全區域（iPhone X+）
   ===================================================== */
@supports (padding: env(safe-area-inset-bottom)) {
    .mobile-bottom-nav {
        padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
    }
    
    body.has-bottom-nav main {
        padding-bottom: calc(70px + env(safe-area-inset-bottom));
    }
}

/* =====================================================
   26. 暗色模式支援（可選）
   ===================================================== */
@media (prefers-color-scheme: dark) {
    :root {
        --light-bg: #1a1a2e;
        --dark-text: #eee;
        --muted-text: #aaa;
    }
    
    /* 如需啟用暗色模式，取消以下註解 */
    /*
    body {
        background-color: #0f0f1a;
        color: var(--dark-text);
    }
    
    .card {
        background-color: var(--light-bg);
        border-color: #2a2a4a;
    }
    
    .navbar-collapse {
        background: var(--light-bg);
    }
    */
}

/* =====================================================
   27. 列印樣式
   ===================================================== */
@media print {
    .navbar,
    .mobile-bottom-nav,
    .language-switcher,
    footer,
    .btn,
    .no-print {
        display: none !important;
    }
    
    body {
        font-size: 12pt;
    }
    
    .card {
        break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ddd;
    }
    
    .container {
        max-width: 100%;
        padding: 0;
    }
}

/* =====================================================
   28. 輔助類別
   ===================================================== */
/* 響應式顯示/隱藏 */
.show-mobile {
    display: none !important;
}

.hide-mobile {
    display: block !important;
}

@media (max-width: 767px) {
    .show-mobile {
        display: block !important;
    }
    
    .hide-mobile {
        display: none !important;
    }
}

/* 響應式間距 */
.mb-mobile-1 {
    margin-bottom: 0.25rem;
}

@media (max-width: 767px) {
    .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; }
    
    .p-mobile-1 { padding: 0.25rem !important; }
    .p-mobile-2 { padding: 0.5rem !important; }
    .p-mobile-3 { padding: 1rem !important; }
}

/* 文字截斷 */
.text-truncate-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.text-truncate-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* =====================================================
   29. 載入動畫
   ===================================================== */
.skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: 4px;
}

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

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

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

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

/* =====================================================
   30. 圖片懶加載樣式
   ===================================================== */

/* 懶加載圖片佔位 */
img.lazy {
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
    background-color: #f0f0f0;
    background-image: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
}

img.lazy-loaded {
    opacity: 1;
    animation: none;
    background: none;
}

/* 圖片載入失敗 */
img.img-error {
    opacity: 0.5;
    filter: grayscale(100%);
}

/* 懶加載背景 */
[data-bg].lazy {
    background-color: #f0f0f0;
}

[data-bg].lazy-loaded {
    background-color: transparent;
}

/* 漸進式圖片載入效果 */
.progressive-image {
    position: relative;
    overflow: hidden;
}

.progressive-image img {
    display: block;
    width: 100%;
    height: auto;
}

.progressive-image .placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    filter: blur(20px);
    transform: scale(1.1);
    transition: opacity 0.3s ease;
}

.progressive-image.loaded .placeholder {
    opacity: 0;
}

/* =====================================================
   31. 滾動優化
   ===================================================== */
.smooth-scroll {
    scroll-behavior: smooth;
}

.overflow-scroll-touch {
    -webkit-overflow-scrolling: touch;
}

/* 自定義滾動條 */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #a1a1a1;
}

@media (max-width: 767px) {
    /* 手機上隱藏滾動條 */
    ::-webkit-scrollbar {
        width: 4px;
        height: 4px;
    }
}
