/* 全域樣式 */
:root {
    --ink: #1f2937;
    --muted: #6b7280;
    --line: #e5e7eb;
    --jev: #ea580c;
    --jev-soft: #fff7ed;
    --teal: #0f766e;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Arial', sans-serif;
    background: linear-gradient(to top, #d2691e 0%, #ffa07a 100%);
    color: #333;
    min-height: 100vh;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

/* 標題區域 */
header {
    text-align: center;
    margin-bottom: 30px;
    color: white;
}

header h1 {
    font-size: 2.5rem;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

header p {
    font-size: 1.1rem;
    opacity: 0.9;
}

/* 主要內容區域 */
main {
    display: flex;
    gap: 30px;
    align-items: flex-start;
}

/* 遊戲區域 */
.game-area {
    position: relative;
    background: white;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    overflow: hidden;
    flex-shrink: 0;
    width: 800px;
    height: 600px;
}

#gameCanvas {
    display: block;
    background: linear-gradient(to bottom, #87CEEB 0%, #98FB98 100%);
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    width: 800px;
    height: 600px;
    transform-origin: top left;
}

/* 遊戲覆蓋層 */
.game-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.start-screen,
.game-over-screen {
    background: rgba(0,0,0,0.8);
    color: white;
    padding: 40px;
    border-radius: 15px;
    text-align: center;
    pointer-events: all;
}

.start-screen h2,
.game-over-screen h2 {
    font-size: 2rem;
    margin-bottom: 15px;
}

.start-screen p,
.game-over-screen p {
    font-size: 1.1rem;
    margin-bottom: 20px;
    opacity: 0.9;
}

.hidden {
    display: none !important;
}

/* 控制面板 */
.control-panel {
    flex: 1;
    min-width: 300px;
}

.stats-section,
.ai-section,
.controls-section {
    background: white;
    border-radius: 15px;
    padding: 25px;
    margin-bottom: 20px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.stats-section h3,
.ai-section h3,
.controls-section h3 {
    color: #333;
    margin-bottom: 20px;
    font-size: 1.3rem;
    border-bottom: 2px solid #667eea;
    padding-bottom: 10px;
}

/* 統計數據網格 */
.stats-grid {
    display: grid;
    gap: 15px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 15px;
    background: #f8f9fa;
    border-radius: 8px;
    border-left: 4px solid #667eea;
}

.stat-label {
    font-weight: 500;
    color: #666;
}

.stat-value {
    font-weight: bold;
    color: #333;
    font-size: 1.1rem;
}

/* 按鈕樣式 */
.button-group {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

button {
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.3s ease;
    font-size: 0.95rem;
}

#startBtn,
#restartBtn {
    background: #667eea;
    color: white;
    font-size: 1.1rem;
    padding: 15px 30px;
}

#startBtn:hover,
#restartBtn:hover {
    background: #5a67d8;
    transform: translateY(-2px);
}

#pauseBtn {
    background: #ffa500;
    color: white;
    flex: 1;
}

#pauseBtn:hover:not(:disabled) {
    background: #ff8c00;
}

#resetBtn {
    background: #dc3545;
    color: white;
    flex: 1;
}

#resetBtn:hover {
    background: #c82333;
}

#aiModeBtn {
    background: #28a745;
    color: white;
    flex: 1;
}

#aiModeBtn:hover:not(:disabled) {
    background: #218838;
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none !important;
}

/* 滑桿控制 */
.slider-group {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 15px;
}

.slider-group label {
    font-weight: 500;
    color: #666;
    min-width: 80px;
}

#speedSlider {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: #ddd;
    outline: none;
    appearance: none;
}

#speedSlider::-webkit-slider-thumb {
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
}

#speedSlider::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    border: none;
}

#speedValue {
    min-width: 40px;
    font-weight: bold;
    color: #333;
}

/* 頁腳 */
footer {
    text-align: center;
    margin-top: 40px;
    color: white;
    opacity: 0.8;
}



/* 響應式設計 */

/* 大型桌面 (> 1200px) - 預設樣式 */

/* 中型桌面和筆電 (1024px - 1200px) */
@media (max-width: 1200px) {
    .container {
        max-width: 1200px;
        padding: 15px;
    }
    
    main {
        gap: 20px;
    }
}

/* 平板橫屏 (768px - 1024px) */
@media (max-width: 1024px) {
    main {
        flex-direction: column;
        align-items: center;
    }
    
    .game-area {
        order: 1;
        width: 600px;  /* 800 * 0.75 */
        height: 450px; /* 600 * 0.75 */
    }
    
    .control-panel {
        order: 2;
        min-width: auto;
        width: 100%;
        max-width: 600px;
    }
    
    .container {
        padding: 10px;
    }
    
    /* 使用 transform scale 進行 75% 縮放 */
    #gameCanvas {
        transform: scale(0.75);
    }
}

/* 平板豎屏和大型手機 (576px - 768px) */
@media (max-width: 768px) {
    header h1 {
        font-size: 2rem;
    }
    
    .game-area {
        width: 480px;  /* 800 * 0.6 */
        height: 360px; /* 600 * 0.6 */
        margin: 0 auto; /* 置中對齊 */
    }
    
    .control-panel {
        max-width: 100%;
    }
    
    /* 使用 transform scale 進行 60% 縮放 */
    #gameCanvas {
        transform: scale(0.6);
    }
    
    .stats-section,
    .ai-section,
    .controls-section {
        padding: 15px;
        margin-bottom: 15px;
    }
    
    .start-screen,
    .game-over-screen {
        padding: 25px 15px;
        border-radius: 10px;
    }
    
    .start-screen h2,
    .game-over-screen h2 {
        font-size: 1.5rem;
    }
    
    /* 確保按鈕有足夠的觸控目標 */
    button {
        min-height: 44px;
        font-size: 1rem;
    }
}

/* 手機橫屏 (480px - 576px) */
@media (max-width: 576px) and (orientation: landscape) {
    main {
        flex-direction: row;
        gap: 15px;
    }
    
    .game-area {
        flex: 2;
        width: 320px;  /* 800 * 0.4 */
        height: 240px; /* 600 * 0.4 */
    }
    
    .control-panel {
        flex: 1;
        max-width: 35%;
        overflow-y: auto;
        max-height: 70vh;
    }
    
    /* 手機橫屏 40% 縮放 */
    #gameCanvas {
        transform: scale(0.4);
    }
    
    .stats-section,
    .ai-section,
    .controls-section {
        padding: 10px;
        margin-bottom: 10px;
    }
    
    .controls-section h3 {
        font-size: 1rem;
        margin-bottom: 10px;
    }
    
    .difficulty-section h4 {
        font-size: 0.9rem;
    }
}

/* 手機豎屏 (< 576px) */
@media (max-width: 576px) {
    .container {
        padding: 5px;
    }
    
    header {
        margin-bottom: 15px;
    }
    
    header h1 {
        font-size: 1.8rem;
    }
    
    main {
        gap: 15px;
    }
    
    .game-area {
        width: 400px;  /* 800 * 0.5 */
        height: 300px; /* 600 * 0.5 */
        margin: 0 auto; /* 置中對齊 */
    }
    
    /* 手機豎屏 50% 縮放 */
    #gameCanvas {
        transform: scale(0.5);
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .button-group {
        flex-direction: column;
        gap: 8px;
    }
    
    .button-group button {
        width: 100%;
        min-height: 48px;
    }
    
    .slider-group {
        flex-wrap: wrap;
        margin-top: 10px;
    }
    
    .slider-group label {
        min-width: auto;
        width: 100%;
        margin-bottom: 5px;
        font-size: 0.9rem;
    }
    
    .difficulty-section {
        padding: 10px;
    }
    
    .difficulty-section h4 {
        font-size: 1rem;
        margin-bottom: 10px;
    }
    
    .difficulty-params {
        padding-left: 15px;
    }
    
    /* 優化觸控體驗 */
    input[type="range"] {
        height: 8px;
        -webkit-appearance: none;
        appearance: none;
    }
    
    input[type="range"]::-webkit-slider-thumb {
        width: 24px;
        height: 24px;
    }
    
    input[type="range"]::-moz-range-thumb {
        width: 24px;
        height: 24px;
    }
}

/* 極小螢幕 (< 400px) */
@media (max-width: 400px) {
    .container {
        padding: 2px;
    }
    
    .game-area {
        width: 320px;  /* 800 * 0.4 */
        height: 240px; /* 600 * 0.4 */
        margin: 0 auto; /* 置中對齊 */
    }
    
    /* 極小螢幕 40% 縮放 */
    #gameCanvas {
        transform: scale(0.4);
    }
    
    .stats-section,
    .ai-section,
    .controls-section {
        border-radius: 8px;
        padding: 10px;
    }
    
    .start-screen,
    .game-over-screen {
        padding: 20px 10px;
    }
    
    .start-screen h2,
    .game-over-screen h2 {
        font-size: 1.3rem;
    }
}

/* 動畫效果 */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.stats-section,
.ai-section,
.controls-section {
    animation: fadeIn 0.6s ease-out;
}

/* AI 專用樣式 (未來使用) */
.ai-section {
    border-left: 4px solid #28a745;
}

.ai-section .stat-item {
    border-left-color: #28a745;
}

/* 動態難度樣式 */
.difficulty-section {
    margin-top: 20px;
    padding: 15px;
    background: rgba(255, 193, 7, 0.1);
    border-radius: 10px;
    border-left: 4px solid #ffc107;
}

.difficulty-section h4 {
    color: #333;
    margin-bottom: 15px;
    font-size: 1.1rem;
}

.difficulty-section .control-group {
    margin-bottom: 10px;
}

.difficulty-section label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    color: #666;
}

.difficulty-section small {
    color: #999;
    font-size: 0.8rem;
    margin-left: 24px;
    display: block;
}

.difficulty-params {
    margin-top: 15px;
    padding-left: 20px;
    border-left: 2px solid #ffc107;
}

.difficulty-params .slider-group {
    margin-bottom: 10px;
}




/* 狀態 */
.hint {
    font-size: 0.82rem;
    color: var(--muted);
    line-height: 1.5;
    margin: -8px 0 14px;
}

.status-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.status-grid .stat-item,
.cost-section .stat-item {
    padding: 8px 10px;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
}

.status-grid .stat-label,
.cost-section .stat-label {
    font-size: 0.78rem;
}

.status-grid .stat-value,
.cost-section .stat-value {
    font-size: 0.98rem;
    font-variant-numeric: tabular-nums;
}

.jev-state {
    margin-top: 12px;
    font-size: 0.88rem;
    color: var(--ink);
    background: var(--jev-soft);
    border-radius: 8px;
    padding: 8px 10px;
    line-height: 1.5;
}

/* API 花費 */
.cost-section {
    border-left-color: var(--jev);
}

.cost-section h3,
.api-section h3 {
    border-bottom-color: var(--jev);
}

.cost-section .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.cost-section .stat-item {
    border-left-color: var(--jev);
}

.cost-section .stat-item.total {
    background: var(--jev-soft);
}

.cost-section .stat-item.total .stat-value {
    color: var(--jev);
    font-size: 1.15rem;
}

.policy-caption {
    font-size: 0.8rem;
    color: var(--muted);
    line-height: 1.5;
    margin: 10px 0 0;
}

.policy-caption code {
    font-family: ui-monospace, Menlo, monospace;
    font-size: 0.76rem;
}

/* 版面：桌機左欄遊戲 + API 紀錄、右欄控制與花費；窄螢幕依序堆疊 */
main {
    display: grid;
    grid-template-columns: 800px minmax(300px, 1fr);
    grid-template-areas: "game side" "log side";
    gap: 20px 30px;
    align-items: start;
}

.game-area { grid-area: game; }
.control-panel { grid-area: side; }
.api-section { grid-area: log; }

@media (max-width: 1024px) {
    main {
        grid-template-columns: 1fr;
        grid-template-areas: "game" "side" "log";
        justify-items: center;
    }

    .control-panel,
    .api-section {
        width: 100%;
        max-width: 600px;
    }
}

.cost-section .stat-item.wide {
    grid-column: 1 / -1;
}

.api-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.74rem;
    font-variant-numeric: tabular-nums;
    margin-bottom: 4px;
}

.api-table th,
.api-table td {
    text-align: right;
    padding: 3px 6px;
    border-bottom: 1px solid var(--line);
}

.api-table th:nth-child(2),
.api-table td:nth-child(2) {
    text-align: left;
}

.src-cache {
    color: var(--teal);
    font-weight: 600;
}

.src-jev {
    color: var(--jev);
    font-weight: 600;
}

.api-entry[data-status="cache"] .api-dot {
    background: var(--teal);
}

/* API 紀錄 */
.api-section {
    border-left-color: var(--teal);
    margin-bottom: 0;
    min-width: 0;
}

.api-list {
    margin-top: 12px;
    max-height: 640px;
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
}

.api-empty {
    padding: 14px;
    font-size: 0.85rem;
    color: var(--muted);
}

.api-entry {
    border-bottom: 1px solid var(--line);
    font-size: 0.8rem;
}

.api-entry:last-child {
    border-bottom: none;
}

.api-entry summary {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 8px;
    padding: 8px 10px;
    cursor: pointer;
    list-style: none;
    font-variant-numeric: tabular-nums;
    color: var(--ink);
}

.api-entry summary::-webkit-details-marker {
    display: none;
}

.api-entry[open] summary {
    background: #f8f9fa;
}

.api-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #d1d5db;
    flex-shrink: 0;
}

.api-entry[data-status="ok"] .api-dot {
    background: #16a34a;
}

.api-entry[data-status="error"] .api-dot {
    background: #dc2626;
}

.api-entry[data-status="error"] .api-line {
    color: #b91c1c;
}

.api-outcome {
    color: var(--muted);
    margin-left: 16px;
    width: 100%;
}

.api-body {
    padding: 4px 10px 12px;
}

.api-body h5 {
    font-size: 0.78rem;
    color: var(--ink);
    margin: 10px 0 4px;
}

.api-meta,
.api-note {
    font-size: 0.76rem;
    color: var(--muted);
    line-height: 1.5;
    font-variant-numeric: tabular-nums;
}

.api-body pre {
    font-family: ui-monospace, Menlo, monospace;
    font-size: 0.72rem;
    line-height: 1.45;
    background: #f8f9fa;
    border-radius: 6px;
    padding: 8px;
    max-height: 280px;
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

footer a {
    color: white;
}
