/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --color-bg: #121213;
    --color-surface: #1a1a1b;
    --color-border: #3a3a3c;
    --color-text: #ffffff;
    --color-text-dim: #818384;
    --color-correct: #538d4e;
    --color-present: #b59f3b;
    --color-absent: #3a3a3c;
    --color-key-bg: #818384;
    --color-key-text: #ffffff;
    --tile-size: clamp(30px, min(11vw, 8.2vh), 62px);
    --tile-gap: clamp(2px, 0.7vh, 5px);
    --key-height: clamp(32px, 6.8vh, 58px);
    --key-gap: clamp(3px, 0.8vh, 6px);
    --font: 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
}

html, body {
    height: 100%;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font);
    overflow-x: hidden;
    touch-action: manipulation;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 500px;
    margin: 0 auto;
    overflow: hidden;
    outline: none;
}

@supports (height: 100svh) {
    .app { height: 100svh; }
}

@supports (height: 100dvh) {
    .app { height: 100dvh; }
}

/* ===== Header ===== */
.header {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px 12px;
    height: 46px;
    flex-shrink: 0;
}

.title {
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    text-align: center;
}

/* ===== Action Bar ===== */
.action-bar {
    display: flex;
    justify-content: center;
    gap: 4px;
    padding: 4px 8px 6px;
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
}

.btn-action {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 4px;
    color: var(--color-text);
    cursor: pointer;
    padding: 4px 6px;
    flex: 1;
    min-width: 0;
    max-width: 80px;
    transition: background 0.1s;
    -webkit-tap-highlight-color: transparent;
}

.btn-action:hover { background: var(--color-border); }
.btn-action:active { transform: scale(0.95); }
a.btn-action { text-decoration: none; }

.btn-action-hard {
    background: #b59f3b;
    border-color: #b59f3b;
    color: #fff;
}
.btn-action-hard:hover { background: #c9b044; }

.btn-action-icon {
    font-size: 1rem;
    line-height: 1.2;
}

.btn-action-label {
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    white-space: nowrap;
}

/* ===== Message Toast ===== */
.message {
    position: fixed;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--color-text);
    color: var(--color-bg);
    padding: 10px 20px;
    border-radius: 6px;
    font-weight: 700;
    font-size: 0.9rem;
    z-index: 100;
    pointer-events: none;
    animation: fadeInOut 2s ease forwards;
    white-space: nowrap;
}

@keyframes fadeInOut {
    0% { opacity: 0; transform: translateX(-50%) translateY(-8px); }
    15% { opacity: 1; transform: translateX(-50%) translateY(0); }
    70% { opacity: 1; }
    100% { opacity: 0; }
}

/* ===== Board ===== */
.board-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px 0;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
}

.board {
    display: flex;
    flex-direction: column;
    gap: var(--tile-gap);
}

.row {
    display: flex;
    gap: var(--tile-gap);
}

.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--color-border);
    font-size: 2rem;
    font-weight: 700;
    text-transform: uppercase;
    user-select: none;
    transition: border-color 0.05s;
}

.tile.filled {
    border-color: #565758;
    animation: pop 0.1s ease;
}

.tile.correct { background: var(--color-correct); border-color: var(--color-correct); color: #fff; }
.tile.present { background: var(--color-present); border-color: var(--color-present); color: #fff; }
.tile.absent  { background: var(--color-absent);  border-color: var(--color-absent);  color: #fff; }

.row.submitted .tile {
    animation: flip 0.5s ease forwards;
}
.row.submitted .tile:nth-child(1) { animation-delay: 0s; }
.row.submitted .tile:nth-child(2) { animation-delay: 0.1s; }
.row.submitted .tile:nth-child(3) { animation-delay: 0.2s; }
.row.submitted .tile:nth-child(4) { animation-delay: 0.3s; }
.row.submitted .tile:nth-child(5) { animation-delay: 0.4s; }

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

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

/* ===== Keyboard ===== */
.keyboard {
    padding: 6px 6px calc(12px + env(safe-area-inset-bottom, 0px));
    flex-shrink: 0;
    background: var(--color-bg);
    position: sticky;
    bottom: 0;
    z-index: 10;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: var(--key-gap);
    margin-bottom: var(--key-gap);
    width: 100%;
}

.key {
    flex: 1;
    height: var(--key-height);
    max-width: 43px;
    min-width: 0;
    padding: 0 2px;
    border: none;
    border-radius: 4px;
    background: var(--color-key-bg);
    color: var(--color-key-text);
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
    transition: background 0.1s;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

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

.key-wide {
    flex: 1.5;
    max-width: 65px;
    font-size: 0.75rem;
}

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

/* ===== Stats Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
}

.modal {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 24px 28px;
    max-width: 380px;
    width: 90%;
    position: relative;
    text-align: center;
    max-height: 90vh;
    overflow-y: auto;
}

.modal h2 {
    font-size: 1rem;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    margin-bottom: 20px;
    font-weight: 700;
}

.modal h3 {
    font-size: 0.85rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    margin: 20px 0 10px;
    font-weight: 700;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 14px;
    background: none;
    border: none;
    color: var(--color-text-dim);
    font-size: 1.1rem;
    cursor: pointer;
}

.stats-row {
    display: flex;
    justify-content: space-around;
    gap: 8px;
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.stat-number {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1;
}

.stat-label {
    font-size: 0.6rem;
    color: var(--color-text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    text-align: center;
    max-width: 60px;
}

/* Distribution chart */
.dist-chart { text-align: left; }

.dist-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.dist-label {
    width: 16px;
    font-size: 0.85rem;
    font-weight: 700;
    text-align: right;
    flex-shrink: 0;
}

.dist-bar {
    background: var(--color-absent);
    color: #fff;
    font-size: 0.75rem;
    font-weight: 700;
    padding: 2px 6px;
    text-align: right;
    min-width: 24px;
    border-radius: 2px;
    transition: width 0.5s ease;
}

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

/* Share section */
.share-section {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
}

.share-label {
    font-size: 0.8rem;
    color: var(--color-text-dim);
}

.share-url {
    width: 100%;
    padding: 8px;
    background: var(--color-border);
    border: none;
    border-radius: 4px;
    color: var(--color-text);
    font-size: 0.75rem;
    text-align: center;
}

.btn-share {
    padding: 10px 24px;
    background: var(--color-correct);
    border: none;
    border-radius: 4px;
    color: #fff;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.btn-share:hover { filter: brightness(1.1); }

.btn-new-game {
    margin-top: 16px;
    padding: 12px 28px;
    background: var(--color-correct);
    border: none;
    border-radius: 4px;
    color: #fff;
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    width: 100%;
}
.btn-new-game:hover { filter: brightness(1.1); }

/* ===== Create Challenge Link ===== */
.create-link-desc {
    font-size: 0.8rem;
    color: var(--color-text-dim);
    margin-bottom: 16px;
}

.create-link-input {
    width: 100%;
    padding: 12px;
    background: var(--color-border);
    border: 2px solid var(--color-border);
    border-radius: 4px;
    color: var(--color-text);
    font-size: 1.5rem;
    font-weight: 700;
    text-align: center;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    margin-bottom: 8px;
    font-family: var(--font);
}

.create-link-input:focus {
    outline: none;
    border-color: var(--color-text-dim);
}

.create-link-error {
    color: #e74c3c;
    font-size: 0.8rem;
    margin-bottom: 8px;
}

.create-link-url-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
    margin-top: 8px;
    width: 100%;
}

/* ===== How To Play Modal ===== */
.how-to-section {
    text-align: left;
    margin-bottom: 14px;
}

.how-to-section h3 {
    margin: 0 0 6px;
}

.how-to-section p {
    font-size: 0.82rem;
    color: var(--color-text-dim);
    line-height: 1.4;
    margin-bottom: 4px;
}

/* ===== Responsive ===== */
@media (max-height: 700px) {
    :root {
        --tile-size: 52px;
        --key-height: 50px;
        --key-gap: 5px;
    }
    .header { height: 40px; }
    .title { font-size: 1.3rem; }
    .action-bar { padding: 3px 6px 5px; gap: 3px; }
    .btn-action-icon { font-size: 0.9rem; }
    .tile { font-size: 1.7rem; }
}

@media (max-height: 600px) {
    :root {
        --tile-size: 44px;
        --tile-gap: 4px;
        --key-height: 44px;
        --key-gap: 4px;
    }
    .header { height: 36px; }
    .title { font-size: 1.15rem; }
    .action-bar { padding: 2px 6px 4px; }
    .btn-action-icon { font-size: 0.8rem; }
    .btn-action-label { font-size: 0.6rem; }
    .tile { font-size: 1.4rem; }
    .key { font-size: 0.8rem; }
    .key-wide { font-size: 0.65rem; }
}

@media (max-height: 540px) {
    :root {
        --tile-size: 38px;
        --tile-gap: 3px;
        --key-height: 38px;
        --key-gap: 3px;
    }
    .tile { font-size: 1.2rem; }
    .key { font-size: 0.75rem; }
    .key-wide { font-size: 0.6rem; }
}

@media (max-width: 380px) {
    :root { --key-gap: 4px; }
    .keyboard { padding: 4px 4px 10px; }
    .action-bar { gap: 3px; padding: 3px 4px 5px; }
    .btn-action { padding: 3px 4px; }
}

/* ===== Install Banner ===== */
.install-banner {
    width: 100%;
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    display: flex;
    align-items: center;
    gap: 10px;
    box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.4);
}

.install-banner-icon {
    font-size: 1.4rem;
    flex-shrink: 0;
}

.install-banner-content {
    flex: 1;
    min-width: 0;
}

.install-banner-title {
    font-size: 0.85rem;
    font-weight: 700;
    margin-bottom: 2px;
}

.install-banner-text {
    font-size: 0.75rem;
    color: var(--color-text-dim);
    line-height: 1.4;
}

.install-banner-close {
    background: none;
    border: none;
    color: var(--color-text-dim);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 4px;
    flex-shrink: 0;
    line-height: 1;
    -webkit-tap-highlight-color: transparent;
}

.install-banner-close:hover {
    color: var(--color-text);
}
