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

:root {
    --w-correct: #5fb96b;
    --w-correct-2: #4a9d56;
    --w-present: #e2b93b;
    --w-present-2: #c99f22;
    --w-absent: #3e4156;
    --w-absent-2: #33354a;
}

body.hc {
    --w-correct: #f5793a;
    --w-correct-2: #e0611f;
    --w-present: #85c0f9;
    --w-present-2: #5ea8f0;
}

body {
    background:
        radial-gradient(circle at 50% 0%, rgba(95, 185, 107, 0.12), transparent 50%),
        linear-gradient(180deg, #14151f 0%, #0f1018 100%) fixed;
    color: #e0e0e0;
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    overflow-x: hidden;
    touch-action: pan-y;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

/* ===== Game Container ===== */
#game-container {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0 8px 8px;
    min-height: calc(100vh - var(--gp-header-h, 80px));
    min-height: calc(100dvh - var(--gp-header-h, 80px));
}

/* ===== Header ===== */
header {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 4px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

header h1 {
    font-size: 28px;
    font-weight: 900;
    letter-spacing: 4px;
    text-transform: uppercase;
    color: #fff;
    text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
}

.icon-btn {
    background: none;
    border: none;
    color: #888;
    cursor: pointer;
    font-size: 20px;
    font-weight: 700;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: color 0.2s, background 0.2s;
}

.icon-btn:hover { color: #fff; background: rgba(255,255,255,0.08); }

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 4px;
    min-width: 76px;
}

.header-right { justify-content: flex-end; }

/* ===== Mode Toggle ===== */
.mode-toggle {
    display: flex;
    gap: 0;
    margin: 10px 0 6px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 999px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 3px;
}

.mode-btn {
    background: transparent;
    border: none;
    color: #888;
    font-size: 13px;
    font-weight: 600;
    padding: 6px 18px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.mode-btn { border-radius: 999px; }

.mode-btn.active {
    background: linear-gradient(180deg, var(--w-correct), var(--w-correct-2));
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.mode-btn:hover:not(.active) { color: #ccc; }

/* ===== Toast ===== */
#toast-container {
    position: fixed;
    top: 80px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    pointer-events: none;
}

.toast {
    background: #f4f4f8;
    color: #14151f;
    font-weight: 800;
    font-size: 14px;
    padding: 10px 20px;
    border-radius: 999px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    animation: toastIn 0.3s ease, toastOut 0.3s ease 1.2s forwards;
    pointer-events: none;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}
@keyframes toastOut {
    from { opacity: 1; }
    to { opacity: 0; }
}

/* ===== Board ===== */
#board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: 5px;
    padding: 10px 0;
    width: min(350px, 100%, max(150px, calc((100vh - var(--gp-header-h, 80px) - 347px) * 5 / 6 + 20px)));
    width: min(350px, 100%, max(150px, calc((100dvh - var(--gp-header-h, 80px) - 347px) * 5 / 6 + 20px)));
}

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

.tile {
    width: 100%;
    aspect-ratio: 1;
    max-width: 62px;
    max-height: 62px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: 900;
    text-transform: uppercase;
    background: rgba(255, 255, 255, 0.03);
    border: 2px solid rgba(255, 255, 255, 0.12);
    border-radius: 12px;
    color: #fff;
    transition: border-color 0.12s, background 0.12s;
    perspective: 600px;
}

.tile.filled {
    border-color: rgba(255, 255, 255, 0.45);
    background: rgba(255, 255, 255, 0.07);
    animation: popIn 0.14s cubic-bezier(.2, 1.6, .4, 1);
}

@keyframes popIn {
    0% { transform: scale(0.88); }
    60% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* Flip wrapper */
.tile-inner {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.55s cubic-bezier(.3, 1.35, .5, 1);
}

.tile.revealed .tile-inner {
    transform: rotateX(180deg);
}

.tile-front, .tile-back {
    position: absolute;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    border-radius: 10px;
}

.tile-back {
    transform: rotateX(180deg);
}

.tile.correct .tile-back { background: linear-gradient(160deg, var(--w-correct), var(--w-correct-2)); box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25), 0 4px 14px rgba(0, 0, 0, 0.3); }
.tile.present .tile-back { background: linear-gradient(160deg, var(--w-present), var(--w-present-2)); box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25), 0 4px 14px rgba(0, 0, 0, 0.3); }
.tile.absent .tile-back  { background: linear-gradient(160deg, var(--w-absent), var(--w-absent-2)); box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.08); }

.tile.revealed { border-color: transparent; }

/* Bounce animation on win */
.tile.bounce {
    animation: bounce 0.7s ease;
}

.tile.bounce .tile-back { filter: brightness(1.15) saturate(1.1); }

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    20% { transform: translateY(-20px); }
    40% { transform: translateY(5px); }
    60% { transform: translateY(-8px); }
    80% { transform: translateY(2px); }
}

/* Shake animation on invalid word */
.row.shake {
    animation: shake 0.5s ease;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 50%, 90% { transform: translateX(-6px); }
    30%, 70% { transform: translateX(6px); }
}

/* ===== Keyboard ===== */
#keyboard {
    width: 100%;
    max-width: 500px;
    margin-top: auto;
    padding: 0 2px 6px;
}

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

.kb-spacer-half { flex: 0.5; }

#keyboard button {
    flex: 1;
    max-width: 44px;
    height: 52px;
    border: none;
    border-radius: 10px;
    background: linear-gradient(180deg, #6b6e85, #585b72);
    box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.05s;
    text-transform: uppercase;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

#keyboard button:active {
    transform: translateY(2px);
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
}

#keyboard button.pulse { animation: key-pulse 0.4s ease-out; }

@keyframes key-pulse {
    40% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

#keyboard button.kb-wide {
    max-width: 66px;
    flex: 1.5;
    font-size: 12px;
}

#keyboard button.correct { background: linear-gradient(180deg, var(--w-correct), var(--w-correct-2)); }
#keyboard button.present { background: linear-gradient(180deg, var(--w-present), var(--w-present-2)); }
#keyboard button.absent  { background: linear-gradient(180deg, var(--w-absent), var(--w-absent-2)); color: rgba(255, 255, 255, 0.55); }

/* ===== Overlays ===== */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 500;
    animation: fadeIn 0.25s ease;
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
}

.overlay.hidden { display: none; }

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.overlay-content {
    background: linear-gradient(160deg, #23253a, #1a1b2c);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 18px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    padding: 28px 24px;
    max-width: 420px;
    width: 92%;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
    animation: slideUp 0.3s ease;
}

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

.overlay-content h2 {
    text-align: center;
    font-size: 22px;
    margin-bottom: 16px;
    color: #fff;
}

.overlay-content h3 {
    text-align: center;
    font-size: 16px;
    margin: 16px 0 10px;
    color: #ccc;
}

.overlay-content p {
    font-size: 14px;
    line-height: 1.6;
    color: #bbb;
    margin-bottom: 10px;
}

.overlay-content ul {
    padding-left: 20px;
    margin-bottom: 14px;
}

.overlay-content li {
    font-size: 14px;
    color: #bbb;
    line-height: 1.6;
    margin-bottom: 4px;
}

.overlay-content hr {
    border: none;
    border-top: 1px solid #3a3a3c;
    margin: 16px 0;
}

.close-btn {
    position: absolute;
    top: 12px;
    right: 14px;
    background: none;
    border: none;
    color: #888;
    font-size: 26px;
    cursor: pointer;
    line-height: 1;
    transition: color 0.2s;
}
.close-btn:hover { color: #fff; }

/* ===== How To Play Examples ===== */
.example-row {
    display: flex;
    gap: 4px;
    margin: 10px 0 4px;
}

.example-tile {
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 800;
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 9px;
    color: #fff;
    background: rgba(255, 255, 255, 0.04);
}

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

.example-desc {
    font-size: 13px !important;
    margin-bottom: 12px !important;
}

/* ===== Stats ===== */
.stats-row {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 10px;
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 55px;
}

.stat-number {
    font-size: 28px;
    font-weight: 800;
    color: #fff;
}

.stat-label {
    font-size: 11px;
    color: #888;
    text-align: center;
}

.guess-distribution {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 16px;
}

.dist-row {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
}

.dist-label {
    width: 12px;
    text-align: right;
    font-weight: 700;
    color: #aaa;
    flex-shrink: 0;
}

.dist-bar {
    min-width: 20px;
    padding: 2px 8px;
    background: #3a3a3c;
    color: #fff;
    font-weight: 700;
    font-size: 12px;
    text-align: right;
    border-radius: 3px;
    transition: width 0.4s ease;
}

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

/* ===== Buttons ===== */
.btn-primary {
    display: block;
    width: 100%;
    padding: 12px;
    background: linear-gradient(180deg, var(--w-correct), var(--w-correct-2));
    color: #fff;
    border: none;
    border-radius: 999px;
    box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.btn-primary:hover { filter: brightness(1.08); }

.setting-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.setting-name { font-weight: 800; font-size: 15px; }
.setting-desc { font-size: 12px; color: #9a9cb5; margin-top: 2px; }

.switch {
    flex-shrink: 0;
    width: 46px;
    height: 26px;
    border-radius: 999px;
    border: 0;
    background: #4a4c63;
    position: relative;
    cursor: pointer;
    transition: background 0.2s;
}

.switch span {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    transition: transform 0.2s cubic-bezier(.3, 1.4, .5, 1);
}

.switch[aria-checked="true"] { background: var(--w-correct); }
.switch[aria-checked="true"] span { transform: translateX(20px); }

#fx-layer { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 400; }

.fx-confetti {
    position: absolute;
    top: -14px;
    width: 9px;
    height: 13px;
    border-radius: 2px;
    animation: confetti 2.2s cubic-bezier(.3, .6, .5, 1) both;
}

@keyframes confetti {
    from { transform: translate(0, 0) rotate(0deg); opacity: 1; }
    to { transform: translate(var(--dx), 105vh) rotate(var(--rot)); opacity: 0.2; }
}

/* Share toast */
.share-toast {
    text-align: center;
    margin-top: 8px;
    padding: 8px;
    background: var(--w-correct);
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    color: #fff;
    animation: fadeIn 0.2s ease;
}

.share-toast.hidden { display: none; }

/* ===== Responsive ===== */
@media (max-width: 400px) {
    header h1 { font-size: 22px; letter-spacing: 2px; }
    .tile { font-size: 22px; max-width: 54px; max-height: 54px; }
    #board { max-height: 360px; max-width: 300px; gap: 4px; }
    .row { gap: 4px; }
    #keyboard button { height: 48px; font-size: 12px; }
    #keyboard button.kb-wide { font-size: 11px; }
    .overlay-content { padding: 20px 16px; }
}

@media (max-height: 650px) {
    #board {
        padding: 4px 0;
        width: min(350px, 100%, max(150px, calc((100vh - var(--gp-header-h, 80px) - 305px) * 5 / 6 + 20px)));
        width: min(350px, 100%, max(150px, calc((100dvh - var(--gp-header-h, 80px) - 305px) * 5 / 6 + 20px)));
    }
    .tile { font-size: 22px; }
    header { padding: 6px 4px; }
    .mode-toggle { margin: 4px 0; }
    #keyboard button { height: 44px; }
}

@media (orientation: landscape) and (max-height: 500px) {
    #game-container {
        max-width: 900px;
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        grid-template-rows: auto 1fr;
        column-gap: 20px;
        align-items: center;
    }
    header { grid-column: 1; grid-row: 1; padding: 2px 0; }
    header h1 { font-size: 18px; letter-spacing: 2px; }
    .mode-toggle { grid-column: 2; grid-row: 1; margin: 2px auto; }
    #board {
        grid-column: 1;
        grid-row: 2;
        padding: 0;
        width: max(120px, calc((100vh - var(--gp-header-h, 48px) - 60px) * 5 / 6));
        width: max(120px, calc((100dvh - var(--gp-header-h, 48px) - 60px) * 5 / 6));
    }
    #keyboard { grid-column: 2; grid-row: 2; margin: 0; align-self: center; }
    #keyboard button { height: 44px; }
}

.overlay {
    top: var(--gp-header-h, 80px);
    overflow-y: auto;
    align-items: safe center;
}

@media (pointer: coarse) {
    .mode-btn { min-height: 36px; }
}

.overlay-content {
    max-height: calc(100% - 24px);
}

@media (pointer: coarse) and (min-width: 700px) and (min-height: 700px) {
  #board {
    width: min(440px, 100%, max(150px, calc((100vh - var(--gp-header-h, 80px) - 347px) * 5 / 6 + 20px)));
    width: min(440px, 100%, max(150px, calc((100dvh - var(--gp-header-h, 80px) - 347px) * 5 / 6 + 20px)));
  }
}

[data-theme="light"] html,
[data-theme="light"] body { background: var(--gp-page-grad) fixed; color: var(--gp-ink); }
[data-theme="light"] header h1 { color: var(--gp-ink); }
[data-theme="light"] .icon-btn { color: var(--gp-ink-2); }
[data-theme="light"] .icon-btn:hover { color: var(--gp-ink); }
[data-theme="light"] .mode-toggle { background: var(--gp-surface); box-shadow: inset 0 0 0 1px var(--gp-line); }
[data-theme="light"] .mode-btn { color: var(--gp-ink-2); }
[data-theme="light"] .mode-btn:hover:not(.active) { color: var(--gp-ink); }
[data-theme="light"] .overlay { background: rgba(31, 27, 69, 0.35); }
[data-theme="light"] .overlay-content { background: var(--gp-surface); color: var(--gp-ink); box-shadow: var(--gp-shadow); }
[data-theme="light"] .overlay-content h2 { color: var(--gp-ink); }
[data-theme="light"] .overlay-content h3 { color: var(--gp-ink-2); }
[data-theme="light"] .overlay-content p, [data-theme="light"] .overlay-content li, [data-theme="light"] .stat-label, [data-theme="light"] .dist-label { color: var(--gp-ink-2); }
[data-theme="light"] .close-btn { color: var(--gp-ink-3); }
[data-theme="light"] .close-btn:hover, [data-theme="light"] .stat-number { color: var(--gp-ink); }
[data-theme="light"] .dist-bar { background: #8b87ad; }
[data-theme="light"] .tile { background: #ffffff; border-color: #d9dbe8; color: #1f2133; }
[data-theme="light"] .tile.filled { background: #ffffff; border-color: #8a8da8; }
[data-theme="light"] .tile.revealed { color: #fff; border-color: transparent; }
[data-theme="light"] .tile.absent .tile-back { background: linear-gradient(160deg, #8b8fa8, #787c96); }
[data-theme="light"] #keyboard button { background: linear-gradient(180deg, #e3e5f0, #d3d6e6); color: #1f2133; box-shadow: 0 3px 0 rgba(60, 64, 110, 0.25); }
[data-theme="light"] #keyboard button.correct { background: linear-gradient(180deg, var(--w-correct), var(--w-correct-2)); color: #fff; }
[data-theme="light"] #keyboard button.present { background: linear-gradient(180deg, var(--w-present), var(--w-present-2)); color: #fff; }
[data-theme="light"] #keyboard button.absent { background: linear-gradient(180deg, #8b8fa8, #787c96); color: rgba(255, 255, 255, 0.85); }
[data-theme="light"] header { border-bottom-color: var(--gp-line); }
[data-theme="light"] header h1 { text-shadow: none; }
[data-theme="light"] .example-tile { color: #1f2133; background: #fff; border-color: #d9dbe8; }
[data-theme="light"] .example-tile.correct, [data-theme="light"] .example-tile.present, [data-theme="light"] .example-tile.absent { color: #fff; }
[data-theme="light"] .example-tile.absent { background: #8b8fa8; border-color: #8b8fa8; }
[data-theme="light"] .setting-row { border-bottom-color: var(--gp-line); }
[data-theme="light"] .setting-desc { color: var(--gp-ink-3); }
[data-theme="light"] .switch { background: #c9cbe0; }
[data-theme="light"] .switch[aria-checked="true"] { background: var(--w-correct); }

.gp-btn-music.off { opacity: .4; }
