* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: 'Nunito', sans-serif;
    background: linear-gradient(180deg, #E3F2FD 0%, #BBDEFB 100%);
    color: #263238;
    touch-action: manipulation;
}

.screen {
    position: fixed;
    inset: 0;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    transition: opacity 0.25s ease;
}

.screen.active {
    display: flex;
    opacity: 1;
}

/* Estado intermédio de fade out — ainda visível mas já não é o active */
.screen.hiding {
    display: flex;
    opacity: 0;
    pointer-events: none;
}

/* === Ecrã inicial === */

.logo {
    font-family: 'Fredoka', sans-serif;
    font-weight: 700;
    font-size: clamp(36px, 11vw, 72px);
    color: #4FC3F7;
    text-shadow: 0 4px 0 #29B6F6, 0 8px 20px rgba(79, 195, 247, 0.3);
    letter-spacing: 3px;
    line-height: 1.15;
    text-align: center;
    margin-bottom: 10px;
    animation: bob 3s ease-in-out infinite;
}

@keyframes bob {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.tagline {
    font-size: 18px;
    color: #546E7A;
    margin-bottom: 40px;
    text-align: center;
}

.btn {
    font-family: 'Fredoka', sans-serif;
    font-weight: 600;
    font-size: 20px;
    padding: 18px 48px;
    border: none;
    border-radius: 40px;
    cursor: pointer;
    letter-spacing: 2px;
    transition: transform 0.1s, box-shadow 0.1s;
    margin: 8px;
    min-width: 240px;
}

.btn-primary {
    background: #FF6B35;
    color: white;
    box-shadow: 0 6px 0 #E55722, 0 10px 20px rgba(255, 107, 53, 0.3);
}

.btn-primary:active {
    transform: translateY(4px);
    box-shadow: 0 2px 0 #E55722, 0 4px 10px rgba(255, 107, 53, 0.3);
}

.btn-secondary {
    background: white;
    color: #546E7A;
    box-shadow: 0 6px 0 #CFD8DC, 0 10px 20px rgba(0, 0, 0, 0.1);
}

.btn-secondary:active {
    transform: translateY(4px);
    box-shadow: 0 2px 0 #CFD8DC, 0 4px 10px rgba(0, 0, 0, 0.1);
}

.version {
    position: absolute;
    bottom: 20px;
    font-size: 12px;
    color: #90A4AE;
}

/* === Ecrã de níveis === */

#screen-levels {
    justify-content: flex-start;
    padding: 0;
    overflow: hidden;
}

.screen-header {
    width: 100%;
    display: flex;
    align-items: center;
    padding: 16px 20px;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 2px solid rgba(255, 255, 255, 0.8);
    flex-shrink: 0;
}

.btn-back {
    font-family: 'Fredoka', sans-serif;
    font-size: 22px;
    font-weight: 700;
    background: none;
    border: none;
    cursor: pointer;
    color: #546E7A;
    padding: 4px 12px 4px 0;
    line-height: 1;
}

.screen-title {
    font-family: 'Fredoka', sans-serif;
    font-size: 28px;
    font-weight: 700;
    color: #4FC3F7;
    letter-spacing: 2px;
}

.levels-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    padding: 20px;
    width: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.level-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: white;
    border: none;
    border-radius: 16px;
    padding: 14px 8px;
    cursor: pointer;
    box-shadow: 0 4px 0 #CFD8DC, 0 6px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.1s, box-shadow 0.1s;
}

.level-card:active {
    transform: translateY(3px);
    box-shadow: 0 1px 0 #CFD8DC, 0 2px 6px rgba(0, 0, 0, 0.1);
}

.level-card-number {
    font-family: 'Fredoka', sans-serif;
    font-size: 28px;
    font-weight: 700;
    color: #4FC3F7;
    line-height: 1;
}

.level-card-name {
    font-size: 9px;
    color: #90A4AE;
    text-align: center;
    line-height: 1.2;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.level-card-stars {
    font-size: 11px;
    letter-spacing: 1px;
}

.level-card-best {
    font-family: 'Fredoka', sans-serif;
    font-size: 13px;
    font-weight: 700;
    color: #FF6B35;
    line-height: 1;
}

/* === Barra de progresso de pontuação === */

#score-progress {
    width: 100%;
    height: 4px;
    background: rgba(0, 0, 0, 0.08);
    flex-shrink: 0;
}

#score-progress-fill {
    height: 100%;
    width: 0%;
    background: #FF6B35;
    transition: width 0.3s ease;
    border-radius: 0 2px 2px 0;
}

/* === Ecrã jogo === */

#screen-game {
    padding: 0;
    justify-content: flex-start;
}

#hud {
    width: 100%;
    padding: 12px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 10;
    border-bottom: 2px solid rgba(255, 255, 255, 0.8);
}

.hud-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.hud-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #78909C;
    font-weight: 700;
}

.hud-item span:not(.hud-label) {
    font-family: 'Fredoka', sans-serif;
    font-size: 20px;
    font-weight: 700;
    color: #263238;
}

.next-color {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #4FC3F7;
    box-shadow: inset -4px -4px 8px rgba(0, 0, 0, 0.2), 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: background 0.3s;
}

.next-color.color-changed {
    animation: pulse 0.4s ease;
}

@keyframes pulse {
    0%   { transform: scale(1); }
    35%  { transform: scale(1.4); box-shadow: inset -4px -4px 8px rgba(0,0,0,0.2), 0 4px 16px rgba(0,0,0,0.3); }
    70%  { transform: scale(0.92); }
    100% { transform: scale(1); }
}

#canvas {
    flex: 1;
    width: 100%;
    display: block;
    touch-action: none;
}

/* === Botões em linha e variante pequena === */

.btn-row {
    display: flex;
    gap: 8px;
    justify-content: center;
    flex-wrap: wrap;
}

.btn-small {
    font-size: 14px;
    padding: 12px 20px;
    min-width: 0;
}

/* === Ecrã nível completo === */

.complete-level-name {
    font-size: 14px;
    color: #90A4AE;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 8px;
    font-weight: 700;
}

.complete-title {
    font-family: 'Fredoka', sans-serif;
    font-size: 42px;
    color: #4FC3F7;
    margin-bottom: 16px;
}

.complete-stars {
    font-size: 40px;
    letter-spacing: 6px;
    margin-bottom: 16px;
}

/* === Submissão de score === */

.score-submit-wrap {
    background: rgba(255, 255, 255, 0.6);
    border-radius: 16px;
    padding: 14px 20px;
    margin: 0 0 16px;
    width: 100%;
    max-width: 320px;
    text-align: center;
}

.submit-hint {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #90A4AE;
    font-weight: 700;
    margin-bottom: 10px;
}

.submit-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

.player-name-input {
    flex: 1;
    font-family: 'Nunito', sans-serif;
    font-size: 16px;
    padding: 10px 14px;
    border: 2px solid #CFD8DC;
    border-radius: 12px;
    outline: none;
    background: white;
    color: #263238;
    transition: border-color 0.2s;
    -webkit-user-select: text;
    user-select: text;
}

.player-name-input:focus {
    border-color: #4FC3F7;
}

.player-name-input.input-error {
    border-color: #E57373;
    animation: shake 0.3s ease;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25%       { transform: translateX(-6px); }
    75%       { transform: translateX(6px); }
}

.btn-submit-arrow {
    font-family: 'Fredoka', sans-serif;
    font-size: 20px;
    font-weight: 700;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 12px;
    background: #FF6B35;
    color: white;
    cursor: pointer;
    flex-shrink: 0;
    box-shadow: 0 4px 0 #E55722;
    transition: transform 0.1s, box-shadow 0.1s;
}

.btn-submit-arrow:active {
    transform: translateY(3px);
    box-shadow: 0 1px 0 #E55722;
}

.btn-submit-arrow:disabled {
    opacity: 0.6;
    cursor: default;
}

.submit-rank-msg {
    font-family: 'Fredoka', sans-serif;
    font-size: 20px;
    color: #263238;
    margin-bottom: 8px;
}

.btn-link {
    background: none;
    border: none;
    font-family: 'Nunito', sans-serif;
    font-size: 14px;
    font-weight: 700;
    color: #4FC3F7;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
}

/* === Ecrã leaderboard === */

#screen-leaderboard {
    justify-content: flex-start;
    padding: 0;
    overflow: hidden;
}

.lb-level-tabs {
    display: flex;
    gap: 6px;
    padding: 12px 16px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-shrink: 0;
    scrollbar-width: none;
}

.lb-level-tabs::-webkit-scrollbar { display: none; }

.lb-tab {
    font-family: 'Fredoka', sans-serif;
    font-size: 16px;
    font-weight: 700;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: white;
    color: #90A4AE;
    cursor: pointer;
    flex-shrink: 0;
    box-shadow: 0 3px 0 #CFD8DC;
    transition: transform 0.1s;
}

.lb-tab.active {
    background: #4FC3F7;
    color: white;
    box-shadow: 0 3px 0 #29B6F6;
}

.lb-tab:active {
    transform: translateY(2px);
}

.lb-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 8px 16px 24px;
    width: 100%;
}

.lb-loading,
.lb-empty {
    text-align: center;
    color: #90A4AE;
    font-size: 15px;
    padding: 40px 20px;
    line-height: 1.6;
}

.lb-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15px;
    table-layout: fixed;
}

.lb-table thead th {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #90A4AE;
    font-weight: 700;
    padding: 4px 6px 10px;
    text-align: left;
}

.lb-table tbody tr {
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.lb-table tbody tr:last-child { border-bottom: none; }

.lb-table td {
    padding: 10px 6px;
    vertical-align: middle;
}

.lb-top3 td { background: rgba(79, 195, 247, 0.06); }

.lb-rank {
    font-size: 18px;
    width: 40px;
}

.lb-name {
    font-weight: 700;
    color: #263238;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lb-score {
    font-family: 'Fredoka', sans-serif;
    font-size: 20px;
    font-weight: 700;
    color: #FF6B35;
    text-align: right;
    width: 64px;
}

.lb-dur {
    font-size: 12px;
    color: #90A4AE;
    text-align: right;
    white-space: nowrap;
    width: 48px;
}

/* === Ecrã game over === */

#over-title {
    font-family: 'Fredoka', sans-serif;
    font-size: 48px;
    color: #E57373;
    margin-bottom: 20px;
}

.final-score {
    font-size: 24px;
    margin-bottom: 40px;
    color: #546E7A;
}

.final-score span {
    font-family: 'Fredoka', sans-serif;
    font-weight: 700;
    color: #FF6B35;
    font-size: 36px;
}
