/* ========== 全域樣式 ========== */
:root {
    --primary: #d32f2f;
    --primary-dark: #9a0007;
    --secondary: #616161;
    --success: #388e3c;
    --bg: #f5f5f5;
    --card-bg: #ffffff;
    --text: #212121;
    --text-secondary: #757575;
    --border: #e0e0e0;
    --shadow: 0 2px 4px rgba(0,0,0,0.1);
    --shadow-lg: 0 4px 8px rgba(0,0,0,0.15);
    --radius: 8px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", "微軟正黑體", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1rem;
}

/* ========== 標題 ========== */
.header {
    text-align: center;
    padding: 2rem 0;
}

.header h1 {
    font-size: 2rem;
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 0.95rem;
}

/* ========== 卡片 ========== */
.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1rem;
    box-shadow: var(--shadow);
}

.card h2 {
    font-size: 1.5rem;
    margin-bottom: 1rem;
    color: var(--text);
    border-bottom: 2px solid var(--primary);
    padding-bottom: 0.5rem;
}

.card h3 {
    font-size: 1.2rem;
    margin: 1rem 0 0.5rem;
    color: var(--text-secondary);
}

.card h4 {
    font-size: 1rem;
    margin: 0.75rem 0 0.5rem;
}

/* ========== 可折疊區塊 ========== */
details summary {
    cursor: pointer;
    user-select: none;
    list-style: none;
}

details summary::-webkit-details-marker {
    display: none;
}

details summary h2::before {
    content: '▶ ';
    transition: transform 0.2s;
    display: inline-block;
}

details[open] summary h2::before {
    transform: rotate(90deg);
}

/* ========== 表單元素 ========== */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
}

.form-control,
input[type="text"],
input[type="number"],
select,
textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 1rem;
    font-family: inherit;
}

.form-control:focus,
input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(211, 47, 47, 0.1);
}

input[type="number"] {
    width: 80px;
}

textarea {
    resize: vertical;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
}

/* ========== 按鈕 ========== */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

.btn:focus {
    outline: 2px solid currentColor;
    outline-offset: 2px;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-dark);
    box-shadow: var(--shadow-lg);
}

.btn-secondary {
    background: var(--secondary);
    color: white;
}

.btn-secondary:hover {
    background: #424242;
}

.btn-success {
    background: var(--success);
    color: white;
}

.btn-success:hover {
    background: #2e7d32;
}

.button-group {
    display: flex;
    gap: 0.75rem;
    margin-top: 1rem;
    flex-wrap: wrap;
}

/* ========== 玩家設定 ========== */
.players-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1rem;
}

.player-input label {
    display: block;
    margin-bottom: 0.25rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.game-controls {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
}

/* ========== Checkbox Grid ========== */
.checkbox-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.5rem;
    margin: 0.5rem 0;
}

.checkbox-label {
    display: flex;
    align-items: center;
    padding: 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
}

.checkbox-label:hover {
    background: var(--bg);
}

.checkbox-label input[type="checkbox"] {
    margin-right: 0.5rem;
    width: 18px;
    height: 18px;
    cursor: pointer;
}

/* ========== Input Row ========== */
.input-row {
    display: flex;
    gap: 1rem;
    margin: 0.75rem 0;
    flex-wrap: wrap;
}

.input-row label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ========== 牌選擇器 ========== */
.tile-selector {
    margin: 1rem 0;
}

.suit-group {
    margin-bottom: 1rem;
}

.tile-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.tile-btn {
    width: 50px;
    height: 70px;
    padding: 0.25rem;
    border: 2px solid var(--border);
    background: white;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    position: relative;
}

.tile-btn:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
}

.tile-btn.selected {
    border-color: var(--primary);
    background: #ffebee;
}

.tile-btn .tile-label {
    font-size: 0.7rem;
    color: var(--text-secondary);
}

.tile-btn .count-badge {
    position: absolute;
    top: -8px;
    right: -8px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: bold;
}

.selected-tiles {
    margin: 1rem 0;
    padding: 1rem;
    background: var(--bg);
    border-radius: var(--radius);
    min-height: 60px;
}

.selected-tiles-display {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.tile-chip {
    padding: 0.5rem 0.75rem;
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
}

.tile-chip button {
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
    background: var(--secondary);
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
}

/* ========== 結果顯示 ========== */
.result-display {
    padding: 1rem;
    background: var(--bg);
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

.result-summary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    padding: 1.5rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    text-align: center;
}

.result-summary h3 {
    font-size: 2rem;
    margin: 0 0 0.5rem;
    color: white;
}

.result-breakdown {
    margin-top: 1rem;
}

.breakdown-item {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.breakdown-item:last-child {
    border-bottom: none;
}

.breakdown-item .name {
    color: var(--text);
}

.breakdown-item .tai {
    color: var(--primary);
    font-weight: bold;
}

.result-tip {
    margin-top: 1rem;
    padding: 1rem;
    background: #e3f2fd;
    border-left: 4px solid #1976d2;
    border-radius: 4px;
    color: #0d47a1;
}

/* ========== 分數總結 ========== */
.scores-summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.score-card {
    padding: 1rem;
    background: var(--bg);
    border-radius: var(--radius);
    text-align: center;
}

.score-card .player-name {
    font-weight: bold;
    margin-bottom: 0.5rem;
}

.score-card .score {
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--primary);
}

.score-card.positive .score {
    color: var(--success);
}

.score-card.negative .score {
    color: var(--primary);
}

/* ========== 局數列表 ========== */
.rounds-list {
    max-height: 400px;
    overflow-y: auto;
}

.round-item {
    padding: 1rem;
    margin-bottom: 0.5rem;
    background: var(--bg);
    border-left: 4px solid var(--primary);
    border-radius: 4px;
}

.round-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.round-winner {
    font-weight: bold;
}

.round-tai {
    color: var(--primary);
    font-weight: bold;
}

.round-details {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

/* ========== 規則顯示 ========== */
.rules-display {
    padding: 1rem;
    background: var(--bg);
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

.rule-category {
    margin-bottom: 1rem;
}

.rule-category h4 {
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.rule-item {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.rule-item:last-child {
    border-bottom: none;
}

/* ========== 測試結果 ========== */
.test-results {
    background: #263238;
    color: #aed581;
    padding: 1rem;
    border-radius: 4px;
    overflow-x: auto;
    font-size: 0.9rem;
    margin-top: 1rem;
    max-height: 400px;
    overflow-y: auto;
}

/* ========== 頁尾 ========== */
.footer {
    margin-top: 2rem;
    padding: 1.5rem 0;
    text-align: center;
}

.footer-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.copyright {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* ========== Toast ========== */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #323232;
    color: white;
    padding: 1rem 1.5rem;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    opacity: 0;
    transition: all 0.3s;
    z-index: 1000;
    max-width: 90%;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.toast.success {
    background: var(--success);
}

.toast.error {
    background: var(--primary);
}

/* ========== 提示文字 ========== */
.hint {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

/* ========== 響應式 ========== */
@media (max-width: 768px) {
    .container {
        padding: 0.5rem;
    }

    .header h1 {
        font-size: 1.5rem;
    }

    .card {
        padding: 1rem;
    }

    .players-grid {
        grid-template-columns: 1fr;
    }

    .checkbox-grid {
        grid-template-columns: 1fr;
    }

    .button-group {
        flex-direction: column;
    }

    .button-group .btn {
        width: 100%;
    }

    .tile-btn {
        width: 45px;
        height: 60px;
        font-size: 1.2rem;
    }
}

/* ========== 無障礙 ========== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
