:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --border-color: #3a3a5c;
    --text-primary: #fff;
    --text-secondary: #888;
    --accent-yellow: #feca57;
    --accent-red: #ff6b6b;
    --accent-blue: #48dbfb;
    --correct: #538d4e;
    --present: #b59f3b;
    --absent: #3a3a3c;
    --key-bg: #818384;

    --header-height: 60px;
    --keyboard-height: min(200px, 28vh);
    --game-padding: 16px;
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    height: 100dvh;
}

/* Header */
header {
    height: var(--header-height);
    padding: 0 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    flex-shrink: 0;
}

h1 {
    font-size: clamp(1.5rem, 5vw, 2rem);
    background: linear-gradient(90deg, var(--accent-red), var(--accent-yellow), var(--accent-blue));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-stats {
    position: absolute;
    right: 20px;
    display: flex;
    gap: 20px;
    font-size: 0.9rem;
}

.header-stat {
    display: flex;
    align-items: center;
    gap: 6px;
}

.header-stat-value {
    font-weight: bold;
    color: var(--accent-yellow);
    font-size: 1.1rem;
}

.current-mode {
    position: absolute;
    left: 70px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.current-mode span {
    color: var(--accent-blue);
    font-weight: bold;
}

.help-btn {
    position: absolute;
    left: 20px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.2rem;
    transition: all 0.2s ease;
}

.help-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.05);
}

/* Main game area */
.game-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: var(--game-padding);
    overflow: hidden;
    min-height: 0;
}

/* Japanese word display */
.word-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 10px 0;
}

.japanese-display {
    font-size: clamp(2rem, 8vw, 3.5rem);
    padding: 15px 30px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 16px;
    letter-spacing: 0.3em;
    text-shadow: 0 0 30px rgba(255, 107, 107, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.hint {
    color: var(--text-secondary);
    font-size: clamp(0.75rem, 2vw, 0.9rem);
    margin-bottom: clamp(8px, 2vh, 16px);
}

/* Board section */
.board-section {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    width: 100%;
    padding: 10px 0;
}

.board {
    display: flex;
    flex-direction: column;
    gap: clamp(6px, 1.5vw, 10px);
}

.row {
    display: flex;
    gap: clamp(6px, 1.5vw, 10px);
    justify-content: center;
}

.tile {
    --tile-size: min(52px, calc((100vw - 80px) / var(--word-length)), calc((100dvh - var(--header-height) - var(--keyboard-height) - 200px) / 7));
    width: var(--tile-size);
    height: calc(var(--tile-size) * 1.15);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--tile-size) * 0.55);
    font-weight: bold;
    text-transform: uppercase;
    background: rgba(255, 255, 255, 0.03);
    transition: all 0.15s ease;
}

.tile.active {
    border-color: #888;
    background: rgba(255, 255, 255, 0.08);
    box-shadow: 0 0 15px rgba(254, 202, 87, 0.2);
}

.tile.filled {
    border-color: #666;
    transform: scale(1);
}

.tile.correct {
    background: var(--correct);
    border-color: var(--correct);
    animation: flip 0.5s ease;
}

.tile.present {
    background: var(--present);
    border-color: var(--present);
    animation: flip 0.5s ease;
}

.tile.absent {
    background: var(--absent);
    border-color: var(--absent);
    animation: flip 0.5s ease;
}

@keyframes flip {
    0% { transform: rotateX(0) scale(1); }
    50% { transform: rotateX(90deg) scale(1.05); }
    100% { transform: rotateX(0) scale(1); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-6px); }
    40% { transform: translateX(6px); }
    60% { transform: translateX(-6px); }
    80% { transform: translateX(6px); }
}

@keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.12); }
    100% { transform: scale(1); }
}

.shake { animation: shake 0.4s ease; }
.pop { animation: pop 0.1s ease; }

/* Keyboard */
.keyboard-section {
    width: 100%;
    max-width: 550px;
    padding: 10px 0;
    flex-shrink: 0;
}

.keyboard {
    display: flex;
    flex-direction: column;
    gap: clamp(6px, 1.5vw, 10px);
}

.keyboard-row {
    display: flex;
    gap: clamp(4px, 1vw, 8px);
    justify-content: center;
}

.key {
    --key-height: clamp(45px, 7vh, 60px);
    min-width: clamp(28px, 8vw, 44px);
    height: var(--key-height);
    border: none;
    border-radius: 8px;
    background: var(--key-bg);
    color: var(--text-primary);
    font-size: clamp(0.9rem, 2.5vw, 1.1rem);
    font-weight: bold;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
}

.key:hover {
    background: #9a9a9a;
    transform: scale(1.08);
}

.key:active {
    transform: scale(0.95);
}

/* Action keys - Enter and Backspace */
.key-wide {
    min-width: clamp(50px, 12vw, 65px);
    font-size: clamp(0.65rem, 2vw, 0.75rem);
    font-weight: bold;
    letter-spacing: 0.5px;
}

.key-enter {
    background: linear-gradient(135deg, var(--correct), #6aae5f);
    box-shadow: 0 2px 8px rgba(83, 141, 78, 0.3);
}

.key-enter:hover {
    background: linear-gradient(135deg, #6aae5f, var(--correct));
    box-shadow: 0 4px 12px rgba(83, 141, 78, 0.5);
}

.key-delete {
    background: linear-gradient(135deg, #c0392b, #e74c3c);
    font-size: 1.2rem;
    box-shadow: 0 2px 8px rgba(231, 76, 60, 0.3);
}

.key-delete:hover {
    background: linear-gradient(135deg, #e74c3c, #c0392b);
    box-shadow: 0 4px 12px rgba(231, 76, 60, 0.5);
}

.key.correct { background: var(--correct); }
.key.present { background: var(--present); }
.key.absent { background: var(--absent); }

/* Message toast */
.message {
    position: fixed;
    top: calc(var(--header-height) + 20px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-primary);
    color: var(--bg-primary);
    padding: 14px 28px;
    border-radius: 10px;
    font-weight: bold;
    font-size: 1rem;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.message.show {
    opacity: 1;
}

/* Modals */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
}

.modal.show {
    display: flex;
}

.modal-content {
    background: var(--bg-primary);
    padding: clamp(24px, 5vw, 40px);
    border-radius: 20px;
    text-align: center;
    max-width: 420px;
    width: 100%;
    border: 2px solid var(--border-color);
    max-height: 90vh;
    overflow-y: auto;
}

.modal-content h2 {
    font-size: clamp(1.4rem, 5vw, 1.8rem);
    margin-bottom: 16px;
}

.modal-content .answer {
    font-size: clamp(1.8rem, 6vw, 2.5rem);
    margin: 16px 0;
    letter-spacing: 0.2em;
}

.modal-content .romaji {
    font-size: clamp(1rem, 4vw, 1.4rem);
    color: var(--text-secondary);
    margin-bottom: 12px;
    letter-spacing: 3px;
}

.modal-content .meaning {
    color: var(--accent-yellow);
    font-size: clamp(0.95rem, 3vw, 1.1rem);
    margin-bottom: 24px;
}

.play-again {
    background: linear-gradient(135deg, var(--accent-red), var(--accent-yellow));
    border: none;
    padding: 14px 45px;
    font-size: 1.1rem;
    font-weight: bold;
    border-radius: 12px;
    cursor: pointer;
    color: #000;
    transition: all 0.2s ease;
    margin-top: 10px;
}

.play-again:hover {
    transform: scale(1.05);
    box-shadow: 0 5px 25px rgba(254, 202, 87, 0.4);
}

.stats {
    display: flex;
    gap: clamp(20px, 5vw, 35px);
    justify-content: center;
    margin: 20px 0;
}

.stat { text-align: center; }

.stat-number {
    font-size: clamp(1.5rem, 5vw, 2rem);
    font-weight: bold;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* Mode selector */
.mode-selector {
    display: flex;
    flex-direction: column;
    gap: 14px;
    margin-top: 16px;
}

.mode-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 2px solid var(--border-color);
    color: var(--text-primary);
    padding: 20px 40px;
    font-size: 1.05rem;
    font-weight: bold;
    border-radius: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.mode-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: scale(1.02);
}

.mode-btn .mode-japanese {
    font-size: 1.5rem;
    margin-bottom: 2px;
}

.mode-btn .mode-desc {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-weight: normal;
}

.mode-btn.katakana:hover { border-color: var(--accent-red); box-shadow: 0 0 20px rgba(255, 107, 107, 0.3); }
.mode-btn.hiragana:hover { border-color: var(--accent-blue); box-shadow: 0 0 20px rgba(72, 219, 251, 0.3); }
.mode-btn.random:hover { border-color: var(--accent-yellow); box-shadow: 0 0 20px rgba(254, 202, 87, 0.3); }

.mode-title {
    font-size: 1.5rem;
    margin-bottom: 8px;
}

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

/* End modal mode buttons */
.end-mode-selector {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 14px;
    flex-wrap: wrap;
}

.end-mode-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 10px 16px;
    font-size: 0.85rem;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.end-mode-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}

.end-mode-btn.active {
    border-color: var(--accent-yellow);
    background: rgba(254, 202, 87, 0.2);
}

.next-word-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 20px;
    margin-bottom: 8px;
}

/* Help modal */
.help-modal .modal-content {
    text-align: left;
}

.help-modal h2 {
    text-align: center;
    margin-bottom: 20px;
}

.help-modal p {
    margin-bottom: 12px;
    line-height: 1.6;
    font-size: 0.95rem;
}

.example {
    display: flex;
    gap: 6px;
    margin: 14px 0;
}

.example .tile {
    --tile-size: 38px;
    width: var(--tile-size);
    height: calc(var(--tile-size) * 1.1);
    font-size: 1rem;
}

.close-btn {
    background: var(--border-color);
    border: none;
    padding: 12px 30px;
    color: var(--text-primary);
    border-radius: 8px;
    cursor: pointer;
    margin-top: 16px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    font-size: 1rem;
    transition: all 0.2s ease;
}

.close-btn:hover {
    background: #4a4a6c;
}

/* Footer section in Help Modal */
.made-with {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-align: center;
}

.social-links {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-top: 12px;
}

.social-links a {
    color: var(--text-secondary);
    transition: color 0.2s ease, transform 0.2s ease;
}

.social-links a:hover {
    color: var(--accent-blue);
    transform: scale(1.15);
}

.social-links svg {
    width: 24px;
    height: 24px;
}

/* Responsive adjustments */
@media (max-width: 500px) {
    :root {
        --header-height: 50px;
        --game-padding: 10px;
    }

    .header-stats {
        gap: 10px;
        font-size: 0.7rem;
        right: 10px;
    }

    .header-stat-value {
        font-size: 0.85rem;
    }

    .current-mode { display: none; }

    .help-btn {
        width: 36px;
        height: 36px;
        left: 12px;
    }

    .japanese-display {
        padding: 12px 20px;
        letter-spacing: 0.2em;
    }

    .mode-btn {
        padding: 16px 30px;
    }
}

@media (max-height: 650px) {
    :root {
        --header-height: 45px;
        --keyboard-height: min(180px, 30vh);
    }

    .word-section { padding: 5px 0; gap: 4px; }
    .board-section { padding: 5px 0; }
    .keyboard-section { padding: 5px 0; }

    .japanese-display {
        padding: 10px 20px;
        font-size: clamp(1.8rem, 7vw, 2.5rem);
    }

    .hint { font-size: 0.7rem; }
}

@media (min-width: 768px) and (min-height: 800px) {
    .tile {
        --tile-size: min(58px, calc((100vw - 100px) / var(--word-length)));
    }
}
