/* ===================== RESET & BASE ===================== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #121213;
    --bg-secondary: #1a1a1b;
    --text: #ffffff;
    --text-muted: #818384;
    --border: #3a3a3c;
    --tile-empty: #121213;
    --tile-filled: #121213;
    --tile-correct: #538d4e;
    --tile-present: #b59f3b;
    --tile-absent: #3a3a3c;
    --key-bg: #818384;
    --key-text: #ffffff;
    --modal-bg: #1a1a1b;

    --board-width: 350px;
    --tile-size: 62px;
    --tile-gap: 5px;
    --tile-font: 2rem;
    --key-height: 58px;
    --key-font: 1.1rem;
    --key-gap: 6px;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    display: flex;
    justify-content: center;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

#app {
    width: 100%;
    max-width: 500px;
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* ===================== HEADER ===================== */
header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

header h1 {
    font-size: 1.4rem;
    font-weight: 800;
    letter-spacing: 2px;
    text-align: center;
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 80px;
}

.header-right {
    justify-content: flex-end;
}

.icon-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.icon-btn:hover {
    background: var(--border);
}

.streak-display {
    font-size: 1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 2px;
}

/* ===================== MESSAGE BAR ===================== */
.message-bar {
    position: fixed;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    padding: 12px 24px;
    border-radius: 8px;
    font-weight: 700;
    font-size: 0.95rem;
    z-index: 1000;
    animation: fadeInDown 0.25s ease;
    white-space: nowrap;
}

.message-bar.hidden {
    display: none;
}

@keyframes fadeInDown {
    from { opacity: 0; transform: translateX(-50%) translateY(-10px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===================== BOARD ===================== */
main {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 8px 0;
}

.board {
    display: grid;
    grid-template-rows: repeat(6, var(--tile-size));
    grid-template-columns: repeat(5, var(--tile-size));
    gap: var(--tile-gap);
}

.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    border: 2px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--tile-font);
    font-weight: 800;
    text-transform: uppercase;
    background: var(--tile-empty);
    transition: transform 0.1s;
}

.tile.filled {
    border-color: #565758;
    animation: pop 0.1s ease;
}

.tile.correct {
    background: var(--tile-correct);
    border-color: var(--tile-correct);
    color: white;
}

.tile.present {
    background: var(--tile-present);
    border-color: var(--tile-present);
    color: white;
}

.tile.absent {
    background: var(--tile-absent);
    border-color: var(--tile-absent);
    color: white;
}

.tile.hint {
    background: var(--tile-correct);
    border-color: var(--tile-correct);
    color: white;
    animation: hintPulse 0.6s ease;
}

.tile.flip {
    animation: flipIn 0.3s ease forwards;
}

@keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.12); }
    100% { transform: scale(1); }
}

@keyframes flipIn {
    0% { transform: rotateX(0deg); }
    50% { transform: rotateX(90deg); }
    100% { transform: rotateX(0deg); }
}

@keyframes hintPulse {
    0% { transform: scale(1); }
    30% { transform: scale(1.2); }
    60% { transform: scale(0.95); }
    100% { transform: scale(1); }
}

/* Row bounce on win */
.tile.bounce {
    animation: bounce 0.5s ease;
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    30% { transform: translateY(-20px); }
    60% { transform: translateY(-8px); }
}

/* Row shake on invalid */
.row-shake {
    animation: shake 0.4s ease;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-8px); }
    40% { transform: translateX(8px); }
    60% { transform: translateX(-5px); }
    80% { transform: translateX(5px); }
}

/* ===================== KEYBOARD ===================== */
.keyboard {
    flex-shrink: 0;
    padding: 8px 4px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: var(--key-gap);
    margin-bottom: var(--key-gap);
}

.key {
    height: var(--key-height);
    min-width: 32px;
    border: none;
    border-radius: 6px;
    background: var(--key-bg);
    color: var(--key-text);
    font-size: var(--key-font);
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    max-width: 44px;
    text-transform: uppercase;
    transition: background 0.15s, transform 0.08s;
    -webkit-tap-highlight-color: transparent;
}

.key:active {
    transform: scale(0.93);
}

.key.wide {
    max-width: 66px;
    font-size: 0.8rem;
    padding: 0 8px;
}

.key.hint-key {
    max-width: 52px;
    font-size: 1.1rem;
    background: #665e2f;
    position: relative;
}

.key.hint-key.disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.key.hint-key.active {
    background: var(--tile-present);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(181,159,59,0.5); }
    50% { box-shadow: 0 0 12px 4px rgba(181,159,59,0.3); }
}

.key.correct {
    background: var(--tile-correct);
}

.key.present {
    background: var(--tile-present);
}

.key.absent {
    background: #3a3a3c;
    opacity: 0.6;
}

/* ===================== MODALS ===================== */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    animation: fadeIn 0.2s ease;
    padding: 16px;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--modal-bg);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 32px 28px;
    max-width: 420px;
    width: 100%;
    max-height: 85vh;
    overflow-y: auto;
    position: relative;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.6rem;
    cursor: pointer;
}

.modal-content h2 {
    margin-bottom: 16px;
    font-size: 1.4rem;
}

.modal-content h3 {
    margin: 12px 0 6px;
    font-size: 1rem;
}

.modal-content p {
    margin-bottom: 8px;
    line-height: 1.5;
    color: var(--text-muted);
}

.modal-content ul {
    padding-left: 20px;
    margin-bottom: 12px;
}

.modal-content li {
    margin-bottom: 8px;
    line-height: 1.5;
    color: var(--text-muted);
}

.modal-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 16px 0;
}

.rule-section h3 {
    font-size: 1.3rem;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* ===================== STATS ===================== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 20px;
}

.stat-item {
    text-align: center;
}

.stat-number {
    display: block;
    font-size: 2rem;
    font-weight: 800;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.guess-distribution {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.dist-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.dist-row .dist-label {
    font-weight: 700;
    width: 14px;
    text-align: right;
}

.dist-row .dist-bar {
    background: var(--tile-absent);
    height: 24px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 8px;
    font-size: 0.8rem;
    font-weight: 700;
    min-width: 24px;
    transition: width 0.4s ease;
}

.dist-row.highlight .dist-bar {
    background: var(--tile-correct);
}

/* ===================== RESULT MODAL ===================== */
.result-content {
    text-align: center;
}

.result-emoji {
    font-size: 3rem;
    margin-bottom: 8px;
}

.result-word {
    margin: 16px 0;
    font-size: 1.1rem;
    color: var(--text) !important;
}

.btn-primary {
    background: var(--tile-correct);
    color: white;
    border: none;
    padding: 14px 32px;
    font-size: 1.1rem;
    font-weight: 700;
    border-radius: 30px;
    cursor: pointer;
    transition: transform 0.1s, filter 0.15s;
    margin-top: 8px;
}

.btn-primary:hover {
    filter: brightness(1.15);
}

.btn-primary:active {
    transform: scale(0.96);
}

/* ===================== RESPONSIVE ===================== */

/* Tablet */
@media (min-width: 600px) and (max-width: 1024px) {
    :root {
        --tile-size: 64px;
        --tile-font: 2rem;
        --key-height: 60px;
        --key-font: 1.1rem;
    }
}

/* Small phones */
@media (max-height: 680px) {
    :root {
        --tile-size: 52px;
        --tile-font: 1.6rem;
        --key-height: 48px;
        --key-font: 0.9rem;
        --key-gap: 4px;
        --tile-gap: 4px;
    }
    header h1 { font-size: 1.1rem; }
    header { padding: 4px 12px; }
    .keyboard { padding: 4px 2px; }
}

@media (max-height: 580px) {
    :root {
        --tile-size: 44px;
        --tile-font: 1.3rem;
        --key-height: 42px;
        --key-font: 0.8rem;
    }
}

/* Very small width */
@media (max-width: 360px) {
    :root {
        --tile-size: 50px;
        --tile-font: 1.5rem;
        --key-gap: 3px;
    }
    .key { min-width: 26px; }
    .key.wide { max-width: 52px; font-size: 0.7rem; }
}

/* Large desktop */
@media (min-width: 1025px) {
    :root {
        --tile-size: 66px;
        --tile-font: 2.1rem;
        --key-height: 60px;
    }
}
