/* ===== Theme Variables ===== */
:root {
    --bg: #f0f2f0;
    --bg-secondary: #e0e4e0;
    --text: #1a1a1a;
    --text-muted: #666;
    --header-bg: #5d4037;
    --header-text: #fff;
    --tile-bg: #fffff0;
    --tile-border: #b8a88a;
    --tile-shadow: 0 2px 4px rgba(0,0,0,0.2);
    --tile-side: #d4c4a0;
    --tile-bottom: #b8a88a;
    --tile-selected: #4caf50;
    --tile-hint: #ff9800;
    --tile-matched: rgba(76,175,80,0.3);
    --tile-free: rgba(255,255,255,0.9);
    --tile-blocked: rgba(200,200,200,0.7);
    --btn-bg: #ffffff;
    --btn-border: #ccc;
    --btn-hover: #f5f5f5;
    --modal-bg: #fff;
    --modal-overlay: rgba(0,0,0,0.5);
    --accent: #5d4037;
    --accent-light: #795548;
    --toast-bg: #333;
    --toast-text: #fff;
    --game-bg: #2e7d32;
    --game-bg-pattern: radial-gradient(circle at 50% 50%, #388e3c 0%, #2e7d32 50%, #1b5e20 100%);
}

[data-theme="dark"] {
    --bg: #1a1a2e;
    --bg-secondary: #16213e;
    --text: #e0e0e0;
    --text-muted: #999;
    --header-bg: #3e2723;
    --tile-bg: #2a2a3a;
    --tile-border: #555;
    --tile-shadow: 0 2px 4px rgba(0,0,0,0.5);
    --tile-side: #3a3a4a;
    --tile-bottom: #2a2a35;
    --tile-selected: #66bb6a;
    --tile-hint: #ffb74d;
    --tile-free: rgba(42,42,58,0.95);
    --tile-blocked: rgba(30,30,40,0.7);
    --btn-bg: #2a2a3a;
    --btn-border: #555;
    --btn-hover: #333348;
    --modal-bg: #1e1e2e;
    --modal-overlay: rgba(0,0,0,0.7);
    --accent: #795548;
    --accent-light: #8d6e63;
    --toast-bg: #444;
    --game-bg: #1b3d1b;
    --game-bg-pattern: radial-gradient(circle at 50% 50%, #1e4620 0%, #1b3d1b 50%, #0d2e0d 100%);
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
    height: 100%;
    overflow: hidden;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* ===== Header ===== */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--header-bg);
    color: var(--header-text);
    flex-shrink: 0;
    z-index: 100;
}

.app-title {
    font-size: 1.2rem;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.header-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.icon-btn {
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}
.icon-btn:hover { background: rgba(255,255,255,0.15); }
.icon-btn svg { width: 22px; height: 22px; }

/* ===== Game Screen ===== */
.game-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.info-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    padding: 6px 16px;
    font-size: 0.85rem;
    color: var(--text-muted);
    flex-shrink: 0;
    background: var(--bg-secondary);
    font-weight: 500;
}

.game-area {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--game-bg-pattern);
    touch-action: manipulation;
}

.game-controls {
    display: flex;
    justify-content: center;
    gap: 8px;
    padding: 8px 16px;
    flex-shrink: 0;
    background: var(--bg-secondary);
}

.action-btn {
    padding: 8px 16px;
    background: var(--btn-bg);
    border: 1px solid var(--btn-border);
    border-radius: 8px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text);
    transition: all 0.15s;
}
.action-btn:hover { background: var(--btn-hover); }
.action-btn:disabled { opacity: 0.4; cursor: default; }

.action-btn--accent {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}
.action-btn--accent:hover { background: var(--accent-light); }

/* ===== Mahjong Tiles ===== */
.mj-tile {
    position: absolute;
    cursor: pointer;
    transition: transform 0.15s, opacity 0.3s, box-shadow 0.15s;
    z-index: 1;
}

.mj-tile-inner {
    width: 100%;
    height: 100%;
    position: relative;
    border-radius: 6px;
    background: var(--tile-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    border: 1px solid var(--tile-border);
    box-shadow:
        2px 2px 0 0 var(--tile-side),
        4px 4px 0 0 var(--tile-bottom),
        4px 4px 8px rgba(0,0,0,0.2);
}

.mj-tile-face {
    font-size: 1.6rem;
    line-height: 1;
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    flex: 1;
    min-height: 0;
}

/* SVG tile faces (dots & bamboo) */
.face-svg {
    width: 88%;
    height: 88%;
}

/* Character suit (万子) */
.face-wan {
    display: flex;
    flex-direction: column;
    align-items: center;
    line-height: 1;
    color: #c62828;
    font-weight: 800;
}
.wan-n { font-size: 1.3em; }
.wan-c { font-size: 0.75em; margin-top: -0.05em; }

/* Wind tiles (風牌) */
.face-wind {
    font-size: 1.5em;
    font-weight: 900;
    color: #1a237e;
}

/* Dragon tiles (三元牌) */
.face-dragon { font-size: 1.5em; font-weight: 900; }
.dr-red { color: #c62828; }
.dr-grn { color: #2e7d32; }
.dr-wht { color: #1565c0; }

/* Dark theme adjustments for text faces */
[data-theme="dark"] .face-wan { color: #ef9a9a; }
[data-theme="dark"] .wan-c { color: #ef5350; }
[data-theme="dark"] .face-wind { color: #90caf9; }
[data-theme="dark"] .dr-red { color: #ef5350; }
[data-theme="dark"] .dr-grn { color: #66bb6a; }
[data-theme="dark"] .dr-wht { color: #42a5f5; }

.mj-tile-label {
    font-size: 0.55rem;
    color: var(--text-muted);
    margin-top: 2px;
    pointer-events: none;
    font-weight: 500;
    letter-spacing: 0.3px;
}

.mj-tile:hover .mj-tile-inner {
    box-shadow:
        2px 2px 0 0 var(--tile-side),
        4px 4px 0 0 var(--tile-bottom),
        4px 4px 12px rgba(0,0,0,0.3);
}

.mj-tile.selected .mj-tile-inner {
    border-color: var(--tile-selected);
    box-shadow:
        2px 2px 0 0 var(--tile-selected),
        4px 4px 0 0 var(--tile-selected),
        0 0 16px rgba(76,175,80,0.5);
}

.mj-tile.hint .mj-tile-inner {
    border-color: var(--tile-hint);
    box-shadow:
        2px 2px 0 0 var(--tile-hint),
        4px 4px 0 0 var(--tile-hint),
        0 0 16px rgba(255,152,0,0.5);
    animation: hintPulse 1s ease-in-out infinite;
}

@keyframes hintPulse {
    0%, 100% { box-shadow: 2px 2px 0 0 var(--tile-hint), 4px 4px 0 0 var(--tile-hint), 0 0 16px rgba(255,152,0,0.5); }
    50% { box-shadow: 2px 2px 0 0 var(--tile-hint), 4px 4px 0 0 var(--tile-hint), 0 0 24px rgba(255,152,0,0.8); }
}

.mj-tile.blocked {
    cursor: default;
    opacity: 0.7;
}

.mj-tile.blocked .mj-tile-inner {
    filter: brightness(0.85);
}

.mj-tile.removing {
    transform: scale(0);
    opacity: 0;
    transition: transform 0.3s ease-in, opacity 0.3s ease-in;
}

.mj-tile.matched .mj-tile-inner {
    border-color: var(--tile-selected);
    background: var(--tile-matched);
}

/* ===== Modals ===== */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--modal-overlay);
    z-index: 5000;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
}
.modal-overlay.active {
    display: flex;
    opacity: 1;
}

.modal {
    background: var(--modal-bg);
    border-radius: 16px;
    padding: 32px;
    max-width: 400px;
    width: 90%;
    text-align: center;
    transform: scale(0.9);
    transition: transform 0.2s;
}
.modal-overlay.active .modal {
    transform: scale(1);
}

.modal-title {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: 12px;
}

.modal-subtitle {
    color: var(--text-muted);
    margin-bottom: 20px;
    font-size: 0.9rem;
}

.modal-option {
    padding: 14px;
    background: var(--btn-bg);
    border: 2px solid var(--btn-border);
    border-radius: 10px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text);
    transition: all 0.15s;
}
.modal-option:hover {
    border-color: var(--accent);
}

/* Win modal */
.win-icon { font-size: 3rem; margin-bottom: 8px; }

.win-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin: 20px 0;
    text-align: center;
}
.win-stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.win-stat-value {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--accent);
}

/* Confirm dialog */
.confirm-actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 20px;
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--toast-bg);
    color: var(--toast-text);
    padding: 10px 24px;
    border-radius: 8px;
    font-size: 0.85rem;
    opacity: 0;
    transition: all 0.3s;
    z-index: 6000;
    pointer-events: none;
}
.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ===== Confetti ===== */
.confetti {
    position: fixed;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    z-index: 7000;
    pointer-events: none;
    animation: confettiFall linear forwards;
}
@keyframes confettiFall {
    0% { transform: translateY(0) rotate(0deg); opacity: 1; }
    100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .mj-tile-face { font-size: 1.1rem; }
    .mj-tile-label { font-size: 0.4rem; margin-top: 0; }
    .mj-tile-inner {
        border-radius: 4px;
        box-shadow:
            1px 1px 0 0 var(--tile-side),
            2px 2px 0 0 var(--tile-bottom),
            2px 2px 6px rgba(0,0,0,0.2);
    }
    .mj-tile.selected .mj-tile-inner {
        box-shadow:
            1px 1px 0 0 var(--tile-selected),
            2px 2px 0 0 var(--tile-selected),
            0 0 12px rgba(76,175,80,0.5);
    }
    .mj-tile.hint .mj-tile-inner {
        box-shadow:
            1px 1px 0 0 var(--tile-hint),
            2px 2px 0 0 var(--tile-hint),
            0 0 12px rgba(255,152,0,0.5);
    }
    .game-controls { gap: 4px; padding: 6px 8px; }
    .action-btn { padding: 6px 10px; font-size: 0.75rem; }
    .info-bar { gap: 12px; font-size: 0.78rem; }
    .app-title { font-size: 1rem; }
}

@media (max-width: 400px) {
    .mj-tile-face { font-size: 0.9rem; }
    .mj-tile-label { display: none; }
    .action-btn { padding: 5px 8px; font-size: 0.7rem; }
}
