/* ==========================================================================
   經典寵物連連看 - 核心樣式表 (CSS)
   ========================================================================== */

/* 引入 Google 系統字型 */
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;800&family=Share+Tech+Mono&display=swap');

:root {
    --bg-dark: #f5f7fa;      /* 經典灰白底色 */
    --bg-card: #ffffff;      /* 純白容器底色 */
    --border-color: #dcdfe6;  /* 淺灰邊框色 */
    --primary-color: #3b82f6; /* 亮藍色 */
    --primary-glow: rgba(59, 130, 246, 0.3);
    --accent-color: #10b981;  /* 經典綠色 */
    --accent-glow: rgba(16, 185, 129, 0.3);
    --yellow-glow: #f59e0b;   /* 黃色選中邊框 */
    --red-glow: #ef4444;      /* 紅色發光 */
    --text-main: #2d3748;     /* 深色主文字 */
    --text-muted: #718096;    /* 淺色輔助文字 */
    --tile-bg: #ffffff;       /* 方格純白背景 */
    --tile-border: #111111;   /* 方格經典黑邊框 */
}

/* 全域重設與滑動防禦 */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-user-select: none; /* 阻止手機端長按選擇文字 */
    user-select: none;
    -webkit-touch-callout: none;
}

html, body {
    width: 100%;
    height: 100%;
    height: 100dvh; /* 動態視窗高度，適配 Safari 手機瀏覽器 */
    background-color: var(--bg-dark);
    color: var(--text-main);
    font-family: 'Outfit', system-ui, -apple-system, sans-serif;
    overflow: hidden; /* 防止整個網頁滾動 */
    position: fixed; /* iOS 防邊緣手勢下拉 */
    overscroll-behavior: none;
}

/* 主容器 */
.app-container {
    width: 100%;
    max-width: 500px; /* 手機端黃金寬度限制 */
    height: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 10px 15px env(safe-area-inset-bottom) 15px; /* 適配 iPhone 瀏海與底部安全區 */
    background-color: var(--bg-dark);
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    position: relative;
    z-index: 1;
    overflow-y: auto; /* 允許小螢幕上可以向下滾動以查看排行榜 */
    -webkit-overflow-scrolling: touch; /* 保持 iOS 上滾動流暢 */
}

/* ==========================================================================
   1. 頂部資訊區 (Game Header)
   ========================================================================== */
.game-header {
    width: 100%;
    margin-bottom: 8px;
}

.status-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 10px 15px;
    margin-bottom: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.15);
}

.status-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.status-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 2px;
}

.status-val {
    font-size: 1.25rem;
    font-weight: 800;
    color: var(--text-main);
}

.font-mono {
    font-family: 'Share Tech Mono', monospace;
}

/* 時間進度條 */
.timer-container {
    width: 100%;
    height: 8px;
    background-color: var(--border-color);
    border-radius: 4px;
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}

.timer-bar {
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, #10b981, #34d399);
    border-radius: 4px;
    box-shadow: 0 0 8px var(--accent-glow);
    transition: width 0.1s linear, background 0.3s ease; /* 寬度平滑過渡 */
}

/* ==========================================================================
   2. 遊戲中央棋盤區 (Game Board)
   ========================================================================== */
.game-area {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 0; /* 關鍵：允許 Flex 子元素縮小而不溢出 */
    padding: 5px 0;
}

.game-board-wrapper {
    position: relative;
    z-index: 2; /* 確保遊戲版面層級高於廣告，防點擊穿透或廣告溢出遮擋 */
    width: 100%;
    height: 100%;
    max-height: 70vh; /* 防止大螢幕上把棋盤拉得太高 */
    background-color: var(--bg-card);
    border: 2px solid var(--border-color);
    border-radius: 16px;
    overflow: visible;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 繪線 Canvas 覆蓋層 */
#connection-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* 關鍵：允許點擊穿透到下方的 Grid 棋盤 */
    z-index: 999;
}

/* Grid 棋盤 */
.grid-container {
    position: relative; /* 強制所有子元素的 offsetParent 統一為此容器，消除不同瀏覽器的定位基準偏差 */
    display: grid;
    gap: 0px; /* 設為 0，讓方格緊密相連 */
    padding: 6px;
    max-width: 100%;
    max-height: 100%;
    align-content: center;
    justify-content: center;
}

.tile {
    background: var(--tile-bg);
    border: 1px solid var(--tile-border);
    margin: -0.5px; /* 用負外邊距使 1px 邊框重疊，形成漂亮的 1px 單實線 */
    border-radius: 0px; /* 傳統相連方格是直角，移除圓角 */
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: min(1.6rem, 5.5vw); /* 自適應大小 */
    line-height: 1;
    overflow: hidden; /* 防止內容溢出 */
    min-width: 0; /* 強迫 Grid 可收縮到小於內容 */
    min-height: 0;
    position: relative;
    transition: transform 0.1s ease, border-color 0.2s ease, box-shadow 0.2s ease;
    aspect-ratio: 1 / 1; /* 方塊強制為正方形 */
    box-shadow: none; /* 移除方塊各自的陰影以形成平整棋盤 */
    /* 點擊回饋優化 */
    -webkit-tap-highlight-color: transparent;
}

.tile:active:not(.matched) {
    transform: scale(0.95);
}

/* 選中狀態 */
.tile.selected {
    border-color: var(--yellow-glow);
    box-shadow: 0 0 8px var(--yellow-glow);
    transform: scale(1.05);
    z-index: 10; /* 選中時浮在最上層，防相鄰邊框遮擋 */
    background: #fffbeb; /* 經典淡黃選中色 */
}

/* 消除/配對狀態 */
.tile.matched {
    visibility: hidden;
    opacity: 0;
    pointer-events: none; /* 不可再點擊 */
}

/* 最外圈虛擬空白格 */
.tile.virtual-empty {
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
    aspect-ratio: 1 / 1;
}

/* 像素小圖樣式 */
.tile-img {
    width: 100%;
    height: 100%;
    object-fit: fill; /* 填滿方格 */
    display: block;
    pointer-events: none;
}

/* 提示狀態 */
.tile.hinted {
    border-color: var(--accent-color);
    box-shadow: 0 0 12px var(--accent-color);
    animation: flashHint 0.5s infinite alternate;
}

@keyframes flashHint {
    from { transform: scale(1); }
    to { transform: scale(1.04); }
}

/* ==========================================================================
   3. 覆蓋提示層 (Overlay Screens)
   ========================================================================== */
.overlay-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(13, 15, 23, 0.9);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: opacity 0.3s ease;
}

.overlay-screen.hidden {
    display: none;
}

@media (max-width: 500px) {
    .app-container {
        padding: 10px 0px env(safe-area-inset-bottom) 0px; /* 手機端左右 padding 直接歸 0，實現完全滿版 */
    }
    .game-area {
        flex: none;         /* 手機端不要限制高度，讓它隨棋盤高度自然撐開，防範與廣告重疊 */
        height: auto;
    }
    .game-board-wrapper {
        height: auto;       /* 手機端放開高度限制，由棋盤實際高度撐開 */
        max-height: none;   /* 放開高度最大限制，使 12 列棋盤能自然向下延伸而不縮小方塊 */
        border-left: none;  /* 手機端移除左右邊框，直接拉滿寬度 */
        border-right: none;
        border-radius: 0;   /* 手機端移除圓角，看起來更滿 */
        overflow: visible;  /* 允許溢出顯示，防止底部發光連線被外殼隱藏裁切 */
    }
    .grid-container {
        padding: 4px; /* 保留 4px padding 預留安全緩衝，防範最外圈消除連線被邊界裁切 */
    }
    .overlay-content {
        padding: 24px 8px; /* 手機版進一步縮小左右 padding，確保可用寬度大於 320px */
    }
}

.overlay-content {
    text-align: center;
    padding: 30px;
    border-radius: 20px;
    background-color: var(--bg-card);
    border: 2px solid var(--border-color);
    max-width: 85%;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    animation: zoomIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes zoomIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.overlay-content h2 {
    font-size: 2rem;
    font-weight: 800;
    margin-bottom: 10px;
    background: linear-gradient(135deg, #f3f4f6, #9ca3af);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.overlay-content p {
    font-size: 0.95rem;
    color: var(--text-muted);
    margin-bottom: 24px;
}

.primary-btn {
    padding: 12px 30px;
    font-size: 1rem;
    font-weight: 700;
    color: #ffffff;
    background: linear-gradient(135deg, #4f46e5, #6366f1);
    border: none;
    border-radius: 12px;
    cursor: pointer;
    box-shadow: 0 4px 12px var(--primary-glow);
    transition: transform 0.1s ease, box-shadow 0.2s ease;
}

.primary-btn:active {
    transform: scale(0.96);
    box-shadow: 0 2px 6px var(--primary-glow);
}

.secondary-btn {
    background: linear-gradient(135deg, #6b7280, #4b5563);
    box-shadow: 0 4px 12px rgba(107, 114, 128, 0.3);
}

.secondary-btn:active {
    box-shadow: 0 2px 6px rgba(107, 114, 128, 0.3);
}

/* ==========================================================================
   4. AdSense 廣告容器樣式與自適應 (防誤觸與沒廣告自動隱藏)
   ========================================================================== */
.ad-container-petconnect {
    display: flex;
    margin: 12px auto 8px auto;
    width: 100%;
    max-width: 320px;
    min-height: 50px;
    height: 50px;
    background: transparent;
    border-radius: 8px;
    justify-content: center;
    align-items: center;
    position: relative;
    box-sizing: border-box;
    overflow: hidden;
    flex-shrink: 0;
}

.ad-container-petconnect:has(ins[data-ad-status="unfilled"]),
.ad-container-petconnect:has(ins[style*="display: none"]) {
    display: none !important;
    margin: 0 !important;
    height: 0 !important;
    min-height: 0 !important;
}

@media (min-width: 500px) {
    .ad-container-petconnect {
        max-width: 468px;
        min-height: 60px;
        height: 60px;
    }
}

/* ==========================================================================
   5. SEO 防禦性隱藏樣式
   ========================================================================== */
.seo-description {
    position: absolute;
    top: -9999px;
    left: -9999px;
    width: 1px;
    height: 1px;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
}

/* ==========================================================================
   6. 排行榜樣式 (Leaderboard)
   ========================================================================== */
.leaderboard-section {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 10px 12px;
    margin-top: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.leaderboard-header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 6px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 4px;
}

.leaderboard-header h3 {
    font-size: 0.85rem;
    font-weight: 800;
    color: var(--text-main);
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    text-align: center;
}

.leaderboard-table th {
    color: var(--text-muted);
    font-weight: 600;
    padding: 4px 0;
    border-bottom: 1px solid var(--border-color);
}

.leaderboard-table td {
    padding: 6px 0;
    color: var(--text-main);
    border-bottom: 1px dashed #e2e8f0;
}

.leaderboard-table tr:last-child td {
    border-bottom: none;
}

/* 金銀銅前三名樣式 */
.leaderboard-rank-1 {
    font-weight: 800;
    color: #d97706 !important; /* 金牌深黃色 */
}
.leaderboard-rank-2 {
    font-weight: 800;
    color: #4b5563 !important; /* 銀牌灰色 */
}
.leaderboard-rank-3 {
    font-weight: 800;
    color: #b45309 !important; /* 銅牌咖啡色 */
}

/* 剛剛提交的當前玩家高亮效果 */
.leaderboard-row-highlight {
    background-color: #fffbeb;
    font-weight: 600;
}

/* 排行榜暱稱輸入彈窗樣式 */
.modal-card {
    max-width: 320px;
    padding: 24px 20px;
    border-radius: 16px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
}

.modal-card h2 {
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--text-main) !important;
    background: none !important;
    -webkit-text-fill-color: initial !important;
    margin-bottom: 8px;
}

.modal-p {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 16px;
}

#new-high-score {
    font-weight: 800;
    color: #ef4444;
    font-size: 1.05rem;
}

#player-name-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    font-size: 0.95rem;
    text-align: center;
    background-color: #f9fafb;
    color: var(--text-main);
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#player-name-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--primary-glow);
    background-color: #ffffff;
}

.modal-buttons {
    margin-top: 20px;
    display: flex;
    justify-content: center;
}


