.game-themiddle * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    font-family: 'Baloo 2', system-ui, sans-serif;
}

html:has(.game-themiddle) {
    height: auto;
    min-height: 100%;
}

html:has(.game-themiddle), body:has(.game-themiddle) {
    color: #1a1a1a;
    touch-action: manipulation;
    -webkit-text-size-adjust: 100%;
}

body:has(.game-themiddle) {
    position: relative !important;
    width: 100% !important;
    height: auto !important;
    min-height: 100dvh !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    background: transparent !important;
    overscroll-behavior: none;
}

.game-themiddle .content {
    position: relative;
    flex: 1 1 auto;
    width: 100%;
    max-width: 1403px;
    min-height: 700px;
    display: flex !important;
    flex-direction: column;
    align-items: center;
    background: transparent;
    box-sizing: border-box;
    line-height: normal;
}

.game-themiddle .game-shell {
    width: 100%;
    max-width: 440px;
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    line-height: normal;
}

.game-themiddle .container {
    width: 100%;
    max-width: 440px;
    height: auto;
    min-height: 700px;
    display: flex;
    flex-direction: column;
    padding: 20px;
    padding-top: 24px;
    touch-action: manipulation;
    margin: 0 auto;
    box-sizing: border-box;
}

@media (max-width: 768px) {
    html:has(.game-themiddle), body:has(.game-themiddle) {
        height: auto !important;
        max-height: none !important;
        min-height: 100dvh !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
    }
    .game-themiddle .content {
        min-height: auto !important;
    }
    .game-themiddle .game-shell, .game-themiddle .container {
        min-height: 0 !important;
        height: auto !important;
    }
}

/* Back / help row */
.game-themiddle .help-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    background: none;
    border: none;
    color: #1a1a1a;
    padding: 0;
    cursor: pointer;
    text-decoration: none;
    transition: color 0.15s ease;
}
.game-themiddle .help-btn:hover {
    color: #801980;
    opacity: 1;
}
.game-themiddle .help-btn svg {
    width: 30px;
    height: 30px;
    display: block;
}

.game-themiddle .playing-area {
    max-height: none;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    overflow: visible;
    margin-bottom: 40px;
}

.game-themiddle .playing-area.has-guesses {
    justify-content: flex-start;
}

.game-themiddle .guesses-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px 8px;
    touch-action: pan-y;
}

.game-themiddle #beforeGuesses, .game-themiddle #afterGuesses {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    width: 100%;
}

/* During play: older guesses sit fully under the nearest one on each side. */
.game-themiddle .guesses-container.is-stacked #beforeGuesses,
.game-themiddle .guesses-container.is-stacked #afterGuesses {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    gap: 0;
}
.game-themiddle .guesses-container.is-stacked #beforeGuesses .guess-div,
.game-themiddle .guesses-container.is-stacked #afterGuesses .guess-div {
    grid-area: 1 / 1;
    width: 100%;
    transform: none;
}
.game-themiddle .guesses-container.is-stacked .guess-div.is-front {
    z-index: 8;
    opacity: 1;
}
.game-themiddle .guesses-container.is-stacked .guess-div:not(.is-front) {
    z-index: calc(6 - var(--stack-i, 1));
    pointer-events: none;
}

.game-themiddle .play-hint {
    align-self: center;
    max-width: 26em;
    margin: 0 0 28px;
    padding: 0 16px;
    text-align: center;
    color: #1a1a1a;
    font-family: "Source Sans 3", var(--pd-font-ui), system-ui, sans-serif;
    font-size: 20px;
    font-weight: 700;
    line-height: 1.4;
}
.game-themiddle.is-done .play-hint,
.game-themiddle.is-welcome .play-hint,
.game-themiddle.is-tutorial .play-hint {
    display: none !important;
}

.game-themiddle #tutTextSlot,
.game-themiddle .tut-text,
.game-themiddle .tut-skip {
    display: none;
}

body:has(.game-themiddle.is-tutorial) #pd-chrome-tools {
    display: none;
}

/* Tutorial is not welcome — never let the start screen cover the board. */
body.pd-game:has(.game-themiddle.is-tutorial) .pd-welcome-screen {
    display: none !important;
}

.game-themiddle.is-tutorial #tutTextSlot {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    align-self: center;
    width: 100%;
    max-width: 26em;
    min-height: calc(2 * 1.4 * clamp(18px, 5vw, 22px));
    margin: 0 auto 24px;
    padding: 0 8px;
}

.game-themiddle.is-tutorial .tut-text {
    display: block;
    width: 100%;
    text-align: center;
    margin: 0;
    color: #1a1a1a;
    font-family: 'Baloo 2', system-ui, sans-serif;
    font-size: clamp(18px, 5vw, 22px);
    font-weight: 800;
    line-height: 1.4;
}

@keyframes themiddle-tutTextOut {
    from { opacity: 1; transform: scale(1); }
    to { opacity: 0; transform: scale(0.82); }
}

@keyframes themiddle-tutTextIn {
    from { opacity: 0; transform: scale(0.82); }
    to { opacity: 1; transform: scale(1); }
}

.game-themiddle.is-tutorial .tut-text.tut-anim-out {
    animation: themiddle-tutTextOut 0.18s ease forwards;
}

.game-themiddle.is-tutorial .tut-text.tut-anim-in {
    animation: themiddle-tutTextIn 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.game-themiddle .tut-skip {
    align-self: center;
    width: auto;
    margin: 28px auto 0;
    padding: 10px 22px;
    font-family: var(--pd-font-ui);
    font-size: 16px;
    font-weight: 700;
    line-height: 1.2;
    border-radius: 999px;
    border: 2px solid #1a1a1a;
    background: #fff;
    color: #1a1a1a;
    cursor: pointer;
}

.game-themiddle.is-tutorial .tut-skip:not([hidden]) {
    display: block;
}

.game-themiddle .tut-skip:hover {
    background: rgba(0, 0, 0, 0.04);
}

.game-themiddle .tut-skip.tut-skip-play {
    background: #1a1a1a;
    border-color: #1a1a1a;
    color: #fff;
}

/* How to play lives on document.body — extra gap under the title. */
body:has(.game-themiddle) .pd-modal:has(.pd-how-copy) .pd-modal-title,
body:has(.game-themiddle) .pd-pause-panel:has(.pd-how-copy) .pd-pause-panel-title {
    margin-bottom: 22px;
}


.game-themiddle .input-wrapper {
    position: relative;
    width: 100%;
    perspective: 1000px;
    padding: 10px 0;
    touch-action: manipulation;
    transform-origin: center center;
}
.game-themiddle .input-wrapper.is-committing .input-card {
    opacity: 0;
}
.game-themiddle .input-wrapper.is-committing.is-input-in .input-card {
    opacity: 1;
    transition: opacity 0.4s ease 0.08s;
}
.game-themiddle .input-wrapper .guess-div.is-flying {
    position: absolute;
    left: 0;
    right: 0;
    top: 10px;
    z-index: 3;
    margin: 0;
    pointer-events: none;
}
.game-themiddle .guess-div.is-sliding {
    z-index: 2;
    position: relative;
}

.game-themiddle .input-card {
    width: 100%;
    height: 58px;
    position: relative;
    transform-style: preserve-3d;
    transform-origin: center center;
    transition: transform 0.6s ease;
}

.game-themiddle .input-card.flipped {
    transform: rotateX(180deg);
}

.game-themiddle .input-front, .game-themiddle .input-back {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    border-radius: 14px;
    background: #e7e2d6;
    border: none;
    box-sizing: border-box;
}
/* Mobile-only stand-in caret (autofocus isn't allowed); hides when real caret shows */
.game-themiddle .fake-caret {
    display: none;
    pointer-events: none;
}

@media (max-width: 768px) {
    .game-themiddle .fake-caret {
        display: block;
        position: absolute;
        left: 18px;
        top: 50%;
        width: 2px;
        height: 24px;
        margin-top: -12px;
        background: #1a1a1a;
        border-radius: 1px;
        animation:themiddle-fakeCaretBlink 1s steps(1, end) infinite;
    }

    .game-themiddle .guess-input:focus ~ .fake-caret, .game-themiddle .guess-input:not(:placeholder-shown) ~ .fake-caret, .game-themiddle .input-card.flipped .fake-caret {
        display: none;
    }
}

@keyframes themiddle-fakeCaretBlink {
    0%, 49.999% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

.game-themiddle .input-back {
    transform: rotateX(180deg);
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding: 0 18px;
    font-size: 20px;
    font-weight: 700;
    text-transform: capitalize;
    color: #fff;
    background: #2e9e5b;
    border-color: #2e9e5b;
}

.game-themiddle .guess-input {
    width: 100%;
    height: 100%;
    background: transparent;
    border: none;
    outline: none;
    color: #1a1a1a;
    font-family: 'Baloo 2', system-ui, sans-serif;
    font-size: 20px;
    font-weight: 700;
    text-align: left;
    padding: 0 18px;
    text-transform: capitalize;
    letter-spacing: -0.01em;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
    caret-color: #1a1a1a;
}

.game-themiddle .guess-input::placeholder {
    color: #9c958c;
    text-align: left;
    text-transform: none;
    letter-spacing: -0.01em;
    font-family: 'Baloo 2', system-ui, sans-serif;
    font-size: 20px;
    font-weight: 700;
    transition: color 0.2s ease;
}

.game-themiddle .guess-input.error::placeholder {
    color: #ff6b6b;
}

.game-themiddle .guess-input:read-only {
    opacity: 1;
    caret-color: #1a1a1a;
    cursor: text;
}

@property --tm-fill {
    syntax: '<number>';
    inherits: true;
    initial-value: 0;
}
@property --tm-fill-h {
    syntax: '<number>';
    inherits: true;
    initial-value: 4;
}

.game-themiddle .guess-div {
    position: relative;
    overflow: hidden;
    width: 100%;
    min-height: 58px;
    padding: 12px 16px 12px 18px;
    box-sizing: border-box;
    background: #e7e2d6;
    border: none;
    border-radius: 14px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    font-family: 'Baloo 2', system-ui, sans-serif;
    color: #1a1a1a;
    line-height: 1.15;
}
.game-themiddle .guess-fill {
    position: absolute;
    inset: 0;
    z-index: 0;
    transform-origin: left center;
    transform: scaleX(var(--tm-fill, 0));
    background-color: hsl(var(--tm-fill-h, 4) 68% 64%);
    pointer-events: none;
    transition:
        transform var(--tm-fill-ms, 1.25s) cubic-bezier(0.35, 0, 0.12, 1),
        background-color var(--tm-fill-ms, 1.25s) linear;
}
.game-themiddle .guess-fill.no-anim {
    transition: none;
}
.game-themiddle .guess-div.is-bound .guess-word {
    color: #3d3832;
}
.game-themiddle .guess-word {
    position: relative;
    z-index: 1;
    display: block;
    min-width: 0;
    flex: 1 1 auto;
    font-family: 'Baloo 2', system-ui, sans-serif;
    font-size: 20px;
    font-weight: 700;
    text-transform: capitalize;
    text-align: left;
    letter-spacing: -0.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.game-themiddle .guess-away {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    flex: 0 0 auto;
    gap: 1px;
    min-width: 5.6em;
    text-align: right;
    text-transform: none;
    opacity: 0;
}
.game-themiddle .guess-away.is-in {
    opacity: 1;
    transition: opacity 0.32s ease;
}
.game-themiddle .guess-away.is-in.is-ready {
    transition: none;
}
.game-themiddle .guess-away-n {
    display: block;
    font-family: 'Baloo 2', system-ui, sans-serif;
    font-size: 17px;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
    color: #1a1a1a;
    line-height: 1;
}
.game-themiddle .guess-away-unit {
    display: block;
    font-family: var(--pd-font-ui);
    font-size: 10px;
    font-weight: 600;
    line-height: 1.15;
    letter-spacing: 0.01em;
    white-space: nowrap;
    color: #1a1a1a;
    opacity: 0.62;
}
.game-themiddle .guess-away.is-middle .guess-away-n {
    font-variant-numeric: normal;
}

@keyframes themiddle-wiggle {
    0%, 100% { transform: rotate(0deg); }
    10% { transform: rotate(-0.8deg) scale(1.005); }
    20% { transform: rotate(0.8deg) scale(1.008); }
    30% { transform: rotate(-0.8deg) scale(1.005); }
    40% { transform: rotate(0.8deg) scale(1.008); }
    50% { transform: rotate(-0.5deg) scale(1.005); }
    60% { transform: rotate(0.5deg) scale(1.005); }
    70% { transform: rotate(-0.3deg) scale(1.003); }
    80% { transform: rotate(0.3deg) scale(1.003); }
    90% { transform: rotate(0deg) scale(1); }
}

.game-themiddle .input-wrapper.wiggle {
    animation:themiddle-wiggle 1.4s ease;
}

@keyframes themiddle-shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-1px); }
    20%, 40%, 60%, 80% { transform: translateX(1px); }
}

.game-themiddle .input-wrapper.shake {
    animation:themiddle-shake 0.6s ease;
}

/* Modal */
.game-themiddle .modal-overlay {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    padding: 20px;
    box-sizing: border-box;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    transition: opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                visibility 0s linear 0.25s;
}

.game-themiddle .modal-overlay.active {
    opacity: 1;
    visibility: visible;
    background: transparent;
    transition: opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                visibility 0s linear 0s;
}

.game-themiddle .modal,
.game-themiddle .modal * {
    font-family: var(--pd-font-ui);
}

.game-themiddle .modal {
    position: relative;
    background: var(--pd-colour-background, #fff);
    border: 2px solid #2B2B2B;
    border-radius: 12px;
    box-shadow: none;
    padding: 28px 16px 22px;
    max-width: 400px;
    width: 100%;
    max-height: min(58dvh, calc(100dvh - 96px));
    overflow: hidden;
    overscroll-behavior: none;
    text-align: center;
    box-sizing: border-box;
    transform: scale(0.95) translateY(20px);
    opacity: 0;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
body:has(.game-themiddle):has(.modal-overlay.active) {
    overflow: hidden !important;
    overscroll-behavior: none;
}

.game-themiddle .modal-overlay.active .modal {
    transform: scale(1) translateY(0);
    opacity: 1;
}

.game-themiddle .modal h2 {
    font-family: var(--pd-font-ui);
    font-size: 24px;
    font-weight: 800;
    line-height: 28px;
    margin-top: 0;
    margin-bottom: 12px;
    color: #1a1a1a;
    padding: 0 28px;
    text-align: center;
}

.game-themiddle .modal-close {
    position: absolute;
    top: 25px;
    right: 12px;
    width: 34px;
    height: 34px;
    margin: 0;
    padding: 0;
    display: grid;
    place-items: center;
    float: none;
    background: none;
    border: none;
    color: #2B2B2B;
    font-size: 32px;
    font-weight: 600;
    cursor: pointer;
    line-height: 1;
    text-align: center;
    border-radius: 8px;
    z-index: 10;
}
.game-themiddle .modal-close:hover {
    background: rgba(0, 0, 0, .06);
}
.game-themiddle .modal-bottom-close {
    display: block;
    width: 100%;
    margin-top: 16px;
    padding: 12px 24px;
    border: 2px solid #2B2B2B;
    border-radius: 999px;
    background: #2B2B2B;
    color: #fff;
    font-family: var(--pd-font-ui);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
}
.game-themiddle .modal-bottom-close:hover {
    background: #3D3D3D;
    border-color: #3D3D3D;
}
.game-themiddle {
    --ink: #1a1a1a;
    --muted: #b8b8b4;
    --card: #ffffff;
    --green: #2e9e5b;
    --green-dark: #278a4f;
    --blue: #4169f1;
    --blue-dark: #2f50d8;
}

.game-themiddle.is-tutorial #completeScreen {
    display: none !important;
}
.game-themiddle.is-done .container {
    min-height: 0 !important;
    height: auto !important;
    padding-bottom: 8px;
    flex: none;
}
.game-themiddle.is-done .playing-area {
    max-height: none;
    overflow: visible;
    margin-bottom: 12px;
}

/* Done: grow with results and scroll the document — never the play-area. */
html:has(.game-themiddle.is-done),
body:has(.game-themiddle.is-done) {
    height: auto !important;
    max-height: none !important;
    min-height: 100dvh !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    touch-action: manipulation !important;
    overscroll-behavior-y: auto !important;
}
.game-themiddle.is-done .content,
.game-themiddle.is-done .game-shell,
.game-themiddle.is-done .pd-play-area,
.game-themiddle.is-done .playing-area,
.game-themiddle.is-done .container {
    flex: none !important;
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
    overflow: visible !important;
}

/* Hide pause only — keep help (?) and archive (calendar) on completion */
body:has(.game-themiddle.is-done) #pd-chrome-tools #pauseBtnTop { visibility: hidden; }
.game-themiddle.is-paused #playingArea { visibility: hidden; }

/* Under-board completion — staged reveal like Sunflowers */
.game-themiddle #completeScreen.pd-complete-panel {
    margin-top: 36px;
    opacity: 1;
    transform: none;
    transition: none;
}
.game-themiddle #completeScreen.pd-complete-panel:not(.is-shown) {
    visibility: hidden;
    pointer-events: none;
}
.game-themiddle #completeScreen.pd-complete-panel.is-shown {
    visibility: visible;
    pointer-events: auto;
}
.game-themiddle #completeScreen.pd-complete-panel.is-instant .tm-c-stage {
    transition: none;
}
.game-themiddle .tm-c-stage {
    opacity: 0;
    transform: translateY(22px);
    transition: opacity 0.45s cubic-bezier(.22,1,.36,1), transform 0.45s cubic-bezier(.22,1,.36,1);
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.game-themiddle .tm-c-stage.is-in {
    opacity: 1;
    transform: none;
}
.game-themiddle .tm-c-score-block {
    width: 100%;
}

/* Site-wide dark theme */
html[data-theme="dark"] body:has(.game-themiddle),
html[data-theme="dark"] .game-themiddle {
  color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .content {
  background: transparent;
}
html[data-theme="dark"] .game-themiddle .help-btn {
  color: var(--pd-colour-heading, #f5f4ed);
}
html[data-theme="dark"] .game-themiddle .input-front {
  background: #2a2a26;
}
html[data-theme="dark"] .game-themiddle .guess-input {
  color: var(--pd-colour-text, #f0efe8);
  caret-color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .guess-input::placeholder {
  color: #7a7a74;
}
html[data-theme="dark"] .game-themiddle .fake-caret {
  background: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .guess-div {
  background: #2a2a26;
  color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .guess-fill {
  background-color: hsl(var(--tm-fill-h, 4) 52% 36%);
}
html[data-theme="dark"] .game-themiddle .guess-div.is-bound .guess-word,
html[data-theme="dark"] .game-themiddle .guess-away,
html[data-theme="dark"] .game-themiddle .guess-away-n,
html[data-theme="dark"] .game-themiddle .guess-away-unit {
  color: #d8d2c8;
}
html[data-theme="dark"] .game-themiddle .play-hint,
html[data-theme="dark"] .game-themiddle .tut-text {
  color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .tut-skip {
  border-color: var(--pd-colour-text, #f0efe8);
  background: var(--pd-colour-surface, #1c1c19);
  color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .tut-skip:hover {
  background: rgba(255, 255, 255, 0.06);
}
html[data-theme="dark"] .game-themiddle .tut-skip.tut-skip-play {
  background: var(--pd-colour-text, #f0efe8);
  border-color: var(--pd-colour-text, #f0efe8);
  color: var(--pd-colour-background, #141412);
}
html[data-theme="dark"] .game-themiddle .modal,
html[data-theme="dark"] .game-themiddle .confirm-modal {
  background: var(--pd-colour-surface, #1c1c19);
  color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .modal-x,
html[data-theme="dark"] .game-themiddle .modal-close {
  color: var(--pd-colour-text, #f0efe8);
}
html[data-theme="dark"] .game-themiddle .btn-secondary,
html[data-theme="dark"] .game-themiddle button.secondary {
  background: #2a2a26;
  color: var(--pd-colour-text, #f0efe8);
  border-color: #4a4a44;
}
