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

:root {
    --frame-bottom: 0px;
    --frame-left: 0px;
    --frame-right: 0px;
    --correct: #2fa65a;
    --present: #e6b422;
    --absent: #5b6472;
    --tile-bg: #ffffff;
    --tile-border: #cfd8e3;
    --key-bg: #e4ebf3;
    --text: #1f2937;
}

html, body {
    height: 100%;
}

/* Inside the site iframe a fixed footer bar (~40px, "Etusivu / Tietoa pelistä") covers the bottom.
   The site hides that bar when height <= 400px. */
html.in-frame { --frame-bottom: 44px; }
@media (max-height: 400px) {
    html.in-frame { --frame-bottom: 0px; }
}

body {
    font-family: 'Arial', sans-serif;
    background: linear-gradient(135deg, #0f4c75 0%, #1b998b 100%);
    background-attachment: fixed;
    color: #fff;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
}

#app {
    position: relative;
    width: 100%;
    max-width: 500px;
    height: calc(100vh - var(--frame-bottom));
    height: calc(100dvh - var(--frame-bottom));
    margin: 0 auto;
}

.screen {
    display: none;
    position: absolute;
    inset: 0 var(--frame-right) 0 var(--frame-left);
    flex-direction: column;
    align-items: center;
}

.screen.active {
    display: flex;
}

/* ---------- Start screen ---------- */
#start-screen {
    justify-content: center;
    padding: 20px;
    text-align: center;
}

.logo-row {
    display: flex;
    gap: 6px;
    margin-bottom: 16px;
}

.logo-row .tile {
    width: 54px;
    height: 54px;
    font-size: 30px;
    border: none;
    color: #fff;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
}

#start-screen h1 {
    font-size: 46px;
    letter-spacing: 3px;
    margin-bottom: 12px;
    text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.3);
}

#start-screen .desc {
    font-size: 17px;
    line-height: 1.5;
    max-width: 340px;
    margin-bottom: 24px;
}

.start-best {
    margin-top: 14px;
    font-size: 15px;
    opacity: 0.9;
    min-height: 1.2em;
}

/* ---------- Buttons ---------- */
.btn {
    display: block;
    width: 240px;
    max-width: 100%;
    border: none;
    padding: 14px 20px;
    margin: 7px auto;
    font-size: 19px;
    font-weight: bold;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    font-family: inherit;
}

.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); }
.btn:active { transform: translateY(0); }

.btn-primary { background: #fff; color: #0f4c75; }
.btn-secondary { background: rgba(255, 255, 255, 0.18); color: #fff; border: 2px solid rgba(255, 255, 255, 0.6); }
.btn.done { background: #d1fae5; color: #166534; }

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.6);
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    flex-shrink: 0;
}

/* ---------- Game screen ---------- */
#game-screen {
    padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 4px 6px 8px;
}

.topbar-center {
    text-align: center;
}

.mode-label {
    font-size: 20px;
    font-weight: bold;
    letter-spacing: 1px;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
}

.run-info {
    font-size: 14px;
    opacity: 0.9;
    min-height: 1.2em;
}

#board-wrap {
    flex: 1;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

#board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: 6px;
}

.row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
}

.row.shake { animation: shake 0.45s; }

.tile {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--tile, 58px);
    height: var(--tile, 58px);
    background: var(--tile-bg);
    border: 2px solid var(--tile-border);
    border-radius: 8px;
    color: var(--text);
    font-size: calc(var(--tile, 58px) * 0.55);
    font-weight: bold;
    text-transform: uppercase;
    line-height: 1;
}

.tile.filled { border-color: #7b8794; animation: pop 0.1s; }
.tile.correct, .tile.present, .tile.absent { color: #fff; border-color: transparent; }
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }
.tile.flip { animation: flip 0.5s ease forwards; }
.tile.bounce { animation: bounce 0.6s ease; }

/* ---------- Keyboard ---------- */
#keyboard {
    width: 100%;
    max-width: 500px;
    padding-top: 8px;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: 5px;
    margin-bottom: 6px;
}

.key {
    flex: 1;
    max-width: 42px;
    height: var(--key-h, 54px);
    border: none;
    border-radius: 6px;
    background: var(--key-bg);
    color: var(--text);
    font-family: inherit;
    font-size: 17px;
    font-weight: bold;
    text-transform: uppercase;
    cursor: pointer;
    touch-action: manipulation;
    transition: background 0.2s;
}

.key.wide {
    flex: 1.6;
    max-width: 70px;
    font-size: 13px;
}

.key:active { filter: brightness(0.9); }
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent { background: var(--absent); color: #fff; }

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    top: 70px;
    left: 50%;
    transform: translateX(-50%);
    background: #111827;
    color: #fff;
    padding: 10px 18px;
    border-radius: 8px;
    font-weight: bold;
    font-size: 15px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 500;
    white-space: nowrap;
}

.toast.show { opacity: 1; }

/* ---------- Modals ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 400;
    padding: 16px calc(16px + var(--frame-right)) calc(16px + var(--frame-bottom)) calc(16px + var(--frame-left));
}

.modal-overlay.show { display: flex; }

.modal {
    background: #fff;
    color: var(--text);
    border-radius: 16px;
    padding: 24px 20px;
    width: 100%;
    max-width: 380px;
    max-height: 100%;
    overflow-y: auto;
    text-align: center;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
    animation: modalIn 0.25s ease;
}

.modal h2 {
    font-size: 26px;
    margin-bottom: 10px;
    color: #0f4c75;
}

.modal .btn-primary { background: #0f4c75; color: #fff; }
.modal .btn-secondary { background: #fff; color: #0f4c75; border-color: #0f4c75; }

.result-word { font-size: 16px; margin-bottom: 6px; }
.result-word b { font-size: 22px; letter-spacing: 2px; text-transform: uppercase; color: var(--correct); }
.result-points { font-size: 18px; font-weight: bold; color: #1b998b; min-height: 1.2em; }
.result-note { font-size: 14px; color: #6b7280; margin: 6px 0 4px; min-height: 1em; }

.stats {
    display: flex;
    justify-content: center;
    gap: 14px;
    margin: 12px 0 8px;
}

.stats:empty, .dist:empty { display: none; }

.stat .num { font-size: 26px; font-weight: bold; }
.stat .lbl { font-size: 11px; color: #6b7280; }

.dist { text-align: left; margin: 6px auto 4px; max-width: 280px; }

.dist-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
    font-size: 13px;
    font-weight: bold;
}

.dist-bar {
    background: var(--absent);
    color: #fff;
    padding: 2px 6px;
    border-radius: 3px;
    min-width: 22px;
    text-align: right;
}

.dist-bar.current { background: var(--correct); }

.modal-buttons { margin-top: 12px; }
.modal-buttons .btn { width: 100%; max-width: 260px; }

.modal.help { text-align: left; }
.modal.help h2 { text-align: center; }
.modal.help p { font-size: 15px; line-height: 1.45; margin: 8px 0; }
.help-row { display: flex; gap: 4px; margin-top: 10px; }
.tile.small { --tile: 36px; }

/* ---------- Animations ---------- */
@keyframes pop { 50% { transform: scale(1.1); } }

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

@keyframes shake {
    10%, 90% { transform: translateX(-2px); }
    20%, 80% { transform: translateX(4px); }
    30%, 50%, 70% { transform: translateX(-6px); }
    40%, 60% { transform: translateX(6px); }
}

@keyframes bounce {
    0%, 20% { transform: translateY(0); }
    40% { transform: translateY(-24px); }
    50% { transform: translateY(4px); }
    60% { transform: translateY(-12px); }
    80% { transform: translateY(2px); }
    100% { transform: translateY(0); }
}

@keyframes modalIn {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* ---------- Small / short screens ---------- */
@media (max-width: 360px) {
    .kb-row { gap: 4px; }
    .key { font-size: 15px; }
    .key.wide { font-size: 11px; }
    #start-screen h1 { font-size: 38px; }
}

@media (max-height: 600px) {
    #start-screen h1 { font-size: 34px; margin-bottom: 6px; }
    .logo-row .tile { width: 40px; height: 40px; font-size: 22px; }
    #start-screen .desc { margin-bottom: 12px; font-size: 15px; }
    .btn { padding: 10px 18px; font-size: 17px; margin: 5px auto; }
}
