:root {
    --bg-dark: #0f172a;
    --bg-card: #1e293b;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --accent: #f59e0b;
    --danger: #ef4444;
    --success: #10b981;
    --numpad-bg: #334155;
    --numpad-hover: #475569;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-main);
    display: flex;
    justify-content: center;
    padding: 0;
    min-height: auto;
}

#app-container {
    width: 100%;
    max-width: 480px;
    background: radial-gradient(circle at 50% 0%, #1e293b 0%, #0f172a 100%);
    position: relative;
    box-shadow: 0 0 40px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
}

.screen {
    display: none;
    flex-direction: column;
    padding: 2rem 1.5rem;
    width: 100%;
    animation: fadeIn 0.3s ease-out;
}

.screen.active {
    display: flex;
}

.hidden {
    display: none !important;
}

/* Typography */
h1.main-title {
    font-size: 3rem;
    font-weight: 900;
    text-align: center;
    background: linear-gradient(135deg, #60a5fa, #3b82f6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
}

.subtitle {
    text-align: center;
    color: var(--text-muted);
    font-size: 1rem;
    font-weight: 600;
}

/* Audio Button */
#audio-toggle {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: rgba(255,255,255,0.1);
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--text-main);
    cursor: pointer;
    z-index: 10;
    transition: background 0.2s;
}

#audio-toggle:active {
    background: rgba(255,255,255,0.2);
}

/* Bubbles & Characters */
.character-greeting, .character-result, .character-cheer {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 2rem 0;
}

.character-cheer {
    position: absolute;
    right: 1rem;
    bottom: 0;
    margin: 0;
    align-items: flex-end;
}

.speech-bubble {
    background: var(--text-main);
    color: var(--bg-dark);
    padding: 0.75rem 1.25rem;
    border-radius: 1rem;
    font-weight: 800;
    font-size: 1.1rem;
    position: relative;
    margin-bottom: 1rem;
    text-align: center;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.speech-bubble::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 50%;
    transform: translateX(-50%);
    border-width: 8px 8px 0;
    border-style: solid;
    border-color: var(--text-main) transparent transparent transparent;
}

.character-cheer .speech-bubble::after {
    left: auto;
    right: 20px;
    transform: none;
}

.character-emoji {
    font-size: 5rem;
    filter: drop-shadow(0 4px 6px rgba(0,0,0,0.5));
    transition: transform 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.character-img {
    width: 6rem;
    height: 6rem;
    object-fit: contain;
    border-radius: 50%;
}

/* Layout Utilities */
.content-wrapper {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    flex: 1;
    padding-top: 2rem;
}

.action-area {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    align-items: center;
    margin-bottom: 2rem;
}

/* Buttons & Selectors */
.level-selector {
    display: flex;
    gap: 0.5rem;
    width: 100%;
    max-width: 300px;
    background: var(--bg-card);
    padding: 0.5rem;
    border-radius: 999px;
    margin-bottom: 0.5rem;
    border: 1px solid rgba(255,255,255,0.1);
}

.btn-level {
    flex: 1;
    background: transparent;
    color: var(--text-muted);
    border: none;
    border-radius: 999px;
    padding: 0.75rem;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}

.btn-level.active {
    background: var(--primary);
    color: white;
    box-shadow: 0 2px 10px rgba(59, 130, 246, 0.4);
}

.level-label {
    font-size: 1rem;
    color: var(--accent);
    font-weight: 800;
    margin-bottom: 0.5rem;
}

.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 999px;
    padding: 1rem 2rem;
    font-size: 1.5rem;
    font-weight: 800;
    width: 100%;
    max-width: 300px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
    transition: transform 0.1s, background 0.2s;
    font-family: inherit;
}

.btn-primary:active {
    transform: scale(0.95);
    background: var(--primary-hover);
}

.btn-share {
    background: #000000;
    color: white;
    border: 1px solid #333;
    border-radius: 999px;
    padding: 0.75rem 1.5rem;
    font-size: 1.1rem;
    font-weight: 600;
    width: 100%;
    max-width: 300px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
    transition: background 0.2s;
    font-family: inherit;
}

.btn-share:active {
    background: #222;
}

.high-score-display {
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 600;
}

/* Play Screen */
.timer-bar-container {
    width: 100%;
    height: 8px;
    background: var(--bg-card);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 2rem;
}

.timer-bar {
    height: 100%;
    width: 100%;
    background: var(--success);
    transition: width 0.1s linear, background-color 0.3s;
}

.timer-bar.warning {
    background: var(--accent);
}

.timer-bar.danger {
    background: var(--danger);
}

.play-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
    padding-bottom: 2rem;
}

/* Question container: flex row for "a + b = " + input display */
.question-container {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(2.5rem, 8vw, 4rem);
    font-weight: 900;
    letter-spacing: 1px;
    text-shadow: 0 4px 10px rgba(0,0,0,0.5);
    height: 120px;
    width: 100%;
}

.question-text {
    display: inline;
    font-size: inherit;
    font-weight: inherit;
    letter-spacing: inherit;
    text-shadow: inherit;
}

.input-display {
    display: inline;
    font-size: inherit;
    font-weight: inherit;
    color: var(--primary);
    min-width: 2.5rem;
    text-align: left;
    transition: color 0.15s;
}

.input-display.error {
    color: var(--danger);
    animation: shake 0.3s ease;
}

/* Numpad */
.numpad-control {
    width: 100%;
    padding-bottom: 1rem;
}

.numpad-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    max-width: 380px;
    margin: 0 auto;
}

.num-btn {
    background: var(--numpad-bg);
    color: var(--text-main);
    border: none;
    border-radius: 12px;
    font-size: 2rem;
    font-weight: 800;
    aspect-ratio: 1.5;
    cursor: pointer;
    transition: background 0.1s, transform 0.1s;
    font-family: inherit;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 4px 0 rgba(0,0,0,0.2);
}

.num-btn:active:not(.empty) {
    background: var(--numpad-hover);
    transform: translateY(4px);
    box-shadow: 0 0 0 rgba(0,0,0,0);
}

.num-btn.empty {
    background: transparent;
    box-shadow: none;
    cursor: default;
}

/* Result Screen */
.result-title {
    font-size: 2.5rem;
    font-weight: 900;
    color: var(--accent);
}

.score-card {
    background: var(--bg-card);
    padding: 2rem;
    border-radius: 1.5rem;
    text-align: center;
    width: 100%;
    border: 1px solid rgba(255,255,255,0.1);
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}

.score-label {
    font-size: 1.2rem;
    color: var(--text-muted);
    font-weight: 600;
}

.score-value {
    font-size: 5rem;
    font-weight: 900;
    background: linear-gradient(135deg, #fde047, #f59e0b);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    line-height: 1;
    margin: 0.5rem 0;
}

.rank-label {
    font-size: 1.2rem;
    font-weight: 800;
    color: var(--text-main);
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes popIn {
    0% { transform: scale(0.8); opacity: 0; }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
}

.anim-bounce {
    animation: bounce 0.3s ease;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-10px); }
    50% { transform: translateX(10px); }
    75% { transform: translateX(-10px); }
}

.anim-shake {
    animation: shake 0.3s ease;
}

@media (max-width: 360px) {
    .num-btn { aspect-ratio: 1.2; font-size: 1.5rem; }
    .question-text { font-size: 3.5rem; }
    .speech-bubble { font-size: 0.9rem; padding: 0.5rem 1rem; }
}

/* Ad Section */
.ad-section {
    width: 100%;
    max-width: 480px;
    margin: 0 auto;
    flex-shrink: 0;
}

.ad-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-align: center;
    padding: 0.25rem 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

/* Adsense Container */
.adsense-container {
    width: 100%;
    min-height: 100px;
    padding: 0.5rem 0;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Footer */
.app-footer {
    text-align: center;
    padding: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    width: 100%;
    flex-shrink: 0;
}

.app-footer a {
    color: var(--text-muted);
    text-decoration: underline;
    transition: color 0.2s;
}

.app-footer a:hover {
    color: var(--text-main);
}

/* ---- Level locked state ---- */
.btn-level.locked {
    opacity: 0.35;
    cursor: default;
}

/* ---- Clear button ---- */
.num-btn.btn-clear {
    background: #78350f;
    color: #fde68a;
    font-size: 1.4rem;
}
.num-btn.btn-clear:active {
    background: #92400e;
}

/* ---- Play screen score display ---- */
.play-header {
    text-align: center;
    padding: 0 1.5rem 0.25rem;
}
.play-score {
    font-size: 2.5rem;
    font-weight: 900;
    color: var(--accent);
    text-shadow: 0 2px 8px rgba(245, 158, 11, 0.4);
}

/* ---- Unlock notification on result screen ---- */
.result-unlock {
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: white;
    padding: 0.5rem 1.5rem;
    border-radius: 999px;
    font-weight: 800;
    font-size: 1rem;
    text-align: center;
    animation: unlockCelebrate 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    box-shadow: 0 0 30px rgba(245, 158, 11, 0.6);
}

/* ---- Unlock progress display ---- */
.unlock-progress-container {
    margin-top: 0.75rem;
    padding: 0.5rem 1rem;
    background: rgba(59, 130, 246, 0.08);
    border-radius: 8px;
    font-size: 0.85rem;
}

.unlock-progress-label {
    display: block;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 0.4rem;
    font-size: 0.8rem;
}

.unlock-progress-bar {
    width: 100%;
    height: 8px;
    background: rgba(255,255,255,0.1);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 0.4rem;
}

#unlock-progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #3b82f6, #60a5fa);
    transition: width 0.3s ease;
    border-radius: 4px;
}

#unlock-progress-fill.unlock-ready {
    background: linear-gradient(90deg, #fbbf24, #f59e0b);
    box-shadow: 0 0 12px rgba(251, 191, 36, 0.6);
    animation: pulse-glow 0.8s infinite;
}

.unlock-progress-text {
    display: inline-block;
    color: var(--primary);
    font-weight: 700;
    font-size: 0.9rem;
}

#unlock-progress-fill.unlock-ready ~ .unlock-progress-text {
    color: var(--accent);
    animation: pulse-text 0.8s infinite;
}

@keyframes pulse-glow {
    0%, 100% { box-shadow: 0 0 12px rgba(251, 191, 36, 0.6); }
    50% { box-shadow: 0 0 20px rgba(251, 191, 36, 1); }
}

@keyframes pulse-text {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 1.1; transform: scale(1.05); }
}

@keyframes unlockCelebrate {
    0% { transform: scale(0.5) rotate(-10deg); opacity: 0; }
    50% { transform: scale(1.1); }
    100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

/* ---- 10点達成時のパーティクルエフェクト ---- */
.celebrate-flash {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle at 50% 30%, rgba(251, 191, 36, 0.4), transparent);
    pointer-events: none;
    z-index: 999;
    animation: flashBurst 0.7s ease-out;
}

@keyframes flashBurst {
    0% { opacity: 0; }
    15% { opacity: 1; }
    100% { opacity: 0; }
}

.celebrate-particles {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 998;
}

.particle {
    position: absolute;
    font-size: 1.5rem;
    animation: particleFall 2.5s ease-in forwards;
    filter: drop-shadow(0 0 4px rgba(251, 191, 36, 0.8));
}

@keyframes particleFall {
    0% {
        opacity: 1;
        transform: translateY(-80px) translateX(0) rotate(0deg) scale(1);
    }
    100% {
        opacity: 0;
        transform: translateY(120vh) translateX(var(--tx, 0)) rotate(720deg) scale(0.1);
    }
}

.particle:nth-child(odd) {
    animation-name: particleFallLeft;
}

@keyframes particleFallLeft {
    0% {
        opacity: 1;
        transform: translateY(-80px) translateX(0) rotate(0deg) scale(1);
    }
    100% {
        opacity: 0;
        transform: translateY(120vh) translateX(-200px) rotate(-720deg) scale(0.1);
    }
}

/* ---- Ranking Button ---- */
.btn-ranking {
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: white;
    border: none;
    border-radius: 999px;
    padding: 0.75rem 1.5rem;
    font-size: 1rem;
    font-weight: 700;
    width: 100%;
    max-width: 300px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
    transition: transform 0.1s, background 0.2s;
    font-family: inherit;
}

.btn-ranking:active {
    transform: scale(0.95);
    background: linear-gradient(135deg, #d97706, #b45309);
}

/* ---- Score Save Form Modal ---- */
.score-save-form {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.score-save-form.hidden {
    display: none;
}

.form-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    cursor: pointer;
}

.form-modal {
    position: relative;
    z-index: 2001;
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 1.5rem;
    padding: 2rem;
    max-width: 360px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.form-modal h3 {
    font-size: 1.5rem;
    color: var(--accent);
    text-align: center;
    margin-bottom: 0.5rem;
}

.form-modal p {
    text-align: center;
    color: var(--text-muted);
    margin-bottom: 1.5rem;
}

.form-input {
    width: 100%;
    padding: 0.75rem;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 0.5rem;
    color: var(--text-main);
    font-size: 1rem;
    margin-bottom: 1.5rem;
    font-family: inherit;
}

.form-input::placeholder {
    color: var(--text-muted);
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
    background: rgba(59, 130, 246, 0.1);
}

.form-actions {
    display: flex;
    gap: 1rem;
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-muted);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 999px;
    padding: 0.75rem 1.5rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    flex: 1;
}

.btn-secondary:active {
    background: rgba(255, 255, 255, 0.2);
}

.form-actions .btn-primary {
    flex: 1;
    max-width: none;
    margin: 0;
}

/* ---- Ranking Modal ---- */
.ranking-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1999;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ranking-modal.hidden {
    display: none;
}

.modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    cursor: pointer;
}

.modal-content {
    position: relative;
    z-index: 2000;
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 1.5rem;
    padding: 2rem;
    max-width: 400px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
    animation: slideUp 0.3s ease-out;
}

.btn-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    font-size: 1.5rem;
    color: var(--text-main);
    cursor: pointer;
    transition: background 0.2s;
}

.btn-close:active {
    background: rgba(255, 255, 255, 0.2);
}

#ranking-title {
    font-size: 1.5rem;
    color: var(--accent);
    text-align: center;
    margin-bottom: 1.5rem;
}

.ranking-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.ranking-item {
    display: flex;
    align-items: center;
    padding: 0.75rem;
    background: rgba(59, 130, 246, 0.1);
    border-radius: 0.75rem;
    border-left: 3px solid var(--primary);
}

.ranking-item.rank-1 {
    border-left-color: #fbbf24;
    background: rgba(251, 191, 36, 0.15);
}

.ranking-item.rank-2 {
    border-left-color: #a8a8a8;
    background: rgba(168, 168, 168, 0.1);
}

.ranking-item.rank-3 {
    border-left-color: #d97706;
    background: rgba(217, 119, 6, 0.1);
}

.rank-number {
    font-size: 1.5rem;
    font-weight: 900;
    width: 3rem;
    text-align: center;
}

.rank-number.rank-1 {
    color: #fbbf24;
}

.rank-number.rank-2 {
    color: #a8a8a8;
}

.rank-number.rank-3 {
    color: #d97706;
}

.rank-info {
    flex: 1;
}

.rank-name {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-main);
    margin-bottom: 0.25rem;
}

.rank-score {
    font-size: 1.2rem;
    font-weight: 900;
    color: var(--accent);
}
