/*
   Kids responsive layer.
   The story page keeps one fixed 16:9 ratio; the workspace adapts around it.
*/

html,
body {
    max-width: 100%;
    overflow-x: hidden;
}

body[data-interface-dir="rtl"] .kids-app {
    direction: rtl;
}

body[data-interface-dir="ltr"] .kids-app {
    direction: ltr;
}

body[data-story-dir="rtl"] .story-page,
body[data-story-dir="rtl"] #pagePreview,
body[data-story-dir="rtl"] .page-keyword-cloud,
body[data-story-dir="rtl"] .feedback {
    direction: rtl;
    text-align: right;
}

body[data-story-dir="ltr"] .story-page,
body[data-story-dir="ltr"] #pagePreview,
body[data-story-dir="ltr"] .page-keyword-cloud,
body[data-story-dir="ltr"] .feedback {
    direction: ltr;
    text-align: left;
}

body.writing-mode {
    --story-ratio: 1.7778;
    --page-thumb-ratio: 1.55 / 1;
    --work-gap: clamp(8px, 1.2vw, 16px);
    --side-w: clamp(220px, 22vw, 330px);
    --stage-h: calc(100dvh - 20px);
    --writer-w: min(
        980px,
        calc(100vw - var(--side-w) - (var(--work-gap) * 3)),
        calc((var(--stage-h) - 112px) * var(--story-ratio))
    );
    background: #eaf4f1;
}

body.writing-mode .kids-app {
    width: 100%;
    max-width: none;
    min-height: 100dvh;
    padding: 0 8px 16px;
}

body.writing-mode .topbar,
body.writing-mode .hero,
body.writing-mode .setup-panel,
body.writing-mode .guide-panel {
    display: none !important;
}

body.writing-mode .sticky-head {
    position: static;
    margin: 0;
}

body.writing-mode .thin-ad {
    display: none !important;
}

body.writing-mode .workspace {
    display: grid !important;
    grid-template-columns: minmax(0, var(--writer-w)) var(--side-w) !important;
    grid-template-areas: "writer steps" !important;
    align-items: center;
    justify-content: center;
    gap: var(--work-gap);
    width: min(100%, calc(100vw - 16px));
    min-height: var(--stage-h);
    margin: 0 auto;
    padding: var(--work-gap);
    overflow: visible;
    border-radius: 22px;
    background: rgba(255, 255, 255, 0.72);
    box-shadow: 0 18px 48px rgba(15, 45, 36, 0.08);
}

body.writing-mode .writer {
    grid-area: writer;
    display: grid;
    grid-template-rows: auto auto auto;
    gap: 7px;
    width: 100%;
    min-width: 0;
    align-self: center;
}

body.writing-mode .steps {
    grid-area: steps;
    display: grid;
    gap: 9px;
    width: 100%;
    min-width: 0;
    max-height: calc(100dvh - 28px);
    overflow: auto;
    align-self: center;
    padding: 10px;
    border: 1px solid rgba(11, 123, 109, 0.12);
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.92);
}

body.writing-mode .writer-toolbar {
    position: static !important;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 30px;
    margin: 0;
    pointer-events: auto !important;
}

body.writing-mode .writer-toolbar .ghost-btn,
body.writing-mode .writer-toolbar strong {
    min-height: 30px;
    padding: 5px 9px;
    font-size: clamp(0.72rem, 1vw, 0.86rem);
}

body.writing-mode .story-nudge {
    position: static !important;
    width: min(100%, 720px);
    margin: 0 auto;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: clamp(0.72rem, 1vw, 0.88rem);
    line-height: 1.35;
    background: rgba(255, 255, 255, 0.74);
    box-shadow: none;
    transform: none !important;
}

body.writing-mode .story-page {
    width: 100%;
    aspect-ratio: 16 / 9;
    min-height: 0;
    max-height: none;
    margin: 0 auto;
    padding: clamp(9px, 1.2vw, 14px);
    overflow: hidden;
    border-radius: 18px;
}

body.writing-mode .image-zone {
    width: min(86%, 760px);
    min-height: 0;
    margin: clamp(10px, 2dvh, 20px) auto 4px;
    padding: clamp(4px, 0.8vw, 8px);
}

body.writing-mode .image-zone figcaption {
    font-size: clamp(0.72rem, 1.1vw, 0.95rem);
}

body.writing-mode .side-title {
    inset-block-start: clamp(10px, 1.4vw, 16px);
    inset-inline-start: clamp(12px, 1.6vw, 18px);
    max-width: min(42ch, calc(100% - 92px));
    padding: 4px 9px;
    font-size: clamp(0.72rem, 1.2vw, 0.95rem);
}

body.writing-mode .text-zone {
    padding: 3px clamp(8px, 1.8vw, 16px) 8px;
}

body.writing-mode .text-zone span {
    min-width: clamp(24px, 3vw, 36px);
    min-height: clamp(24px, 3vw, 36px);
    font-size: clamp(0.7rem, 1vw, 0.92rem);
}

body.writing-mode .text-zone p {
    width: min(88%, 760px);
    min-height: clamp(42px, 8dvh, 72px);
    padding: clamp(8px, 1.2vw, 12px) clamp(10px, 1.7vw, 16px);
    border-radius: clamp(16px, 2vw, 24px);
    font-size: clamp(0.76rem, 1.25vw, 1rem);
    line-height: 1.34;
    overflow-wrap: anywhere;
}

body.writing-mode .write-card {
    width: 100%;
    margin: 0;
    padding: clamp(6px, 1vw, 10px);
    overflow: hidden;
    border-radius: 18px;
}

body.writing-mode .page-keyword-cloud {
    max-height: clamp(42px, 9dvh, 76px);
    overflow: auto;
    justify-content: center;
    gap: 6px;
}

body.writing-mode .page-keyword-cloud button {
    min-height: 30px;
    padding: 5px 9px;
    font-size: clamp(0.68rem, 1vw, 0.84rem);
}

body.writing-mode .feedback {
    min-height: 18px;
    font-size: clamp(0.7rem, 1vw, 0.82rem);
}

body.writing-mode .next-writing-btn {
    min-height: clamp(32px, 5dvh, 42px);
    padding: 7px 12px;
    border-radius: 999px;
    font-size: clamp(0.76rem, 1vw, 0.92rem);
}

body.writing-mode .steps h2 {
    margin: 0;
    font-size: clamp(1rem, 1.7vw, 1.3rem);
    line-height: 1.25;
}

body.writing-mode .small-keywords,
body.writing-mode .rules {
    font-size: clamp(0.72rem, 1vw, 0.84rem);
}

body.writing-mode .page-list {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 7px;
    margin: 0;
}

body.writing-mode .page-list button {
    aspect-ratio: var(--page-thumb-ratio);
    min-height: 0;
    width: 100%;
    border-radius: 14px;
}

body.writing-mode .page-list button.has-thumb {
    aspect-ratio: var(--page-thumb-ratio);
    min-height: 0;
}

body.writing-mode .page-list button span {
    min-width: clamp(24px, 3vw, 32px);
    min-height: clamp(24px, 3vw, 32px);
    font-size: clamp(0.7rem, 1vw, 0.9rem);
}

body.writing-mode .thumb-ad {
    display: grid !important;
    width: 100%;
    margin: 10px 0 0;
    border-radius: 16px;
}

body.writing-mode .thumb-ad .side-ad-card {
    aspect-ratio: 600 / 220;
    min-height: 0;
}

body.writing-mode .thumb-ad img {
    width: 100%;
    height: 100%;
    max-height: none;
    object-fit: contain;
}

body.writing-mode .footer-ad {
    display: block;
    width: min(1180px, calc(100% - 28px));
    margin: 16px auto 0;
    border: 1px solid rgba(11, 123, 109, 0.16);
    border-radius: 18px;
    background: #fff;
    overflow: hidden;
    box-shadow: 0 16px 38px rgba(15, 45, 36, 0.1);
}

body.writing-mode .footer-ad[hidden] {
    display: none !important;
}

body.writing-mode .footer-ad img {
    display: block;
    width: 100%;
    height: clamp(120px, 14vw, 220px);
    object-fit: contain;
    background: #fff;
}

@media (min-width: 981px) and (orientation: landscape) {
    body.writing-mode .workspace {
        align-content: center;
    }
}

@media (max-width: 980px) and (orientation: landscape) {
    body.writing-mode {
        --side-w: clamp(150px, 24vw, 210px);
        --stage-h: calc(100dvh - 8px);
        --writer-w: min(
            calc(100vw - var(--side-w) - 22px),
            calc((var(--stage-h) - 98px) * var(--story-ratio))
        );
    }

    body.writing-mode .kids-app {
        padding: 0 4px 8px;
    }

    body.writing-mode .workspace {
        width: calc(100vw - 8px);
        min-height: var(--stage-h);
        gap: 6px;
        padding: 5px;
        border-radius: 14px;
    }

    body.writing-mode .steps {
        gap: 6px;
        max-height: calc(100dvh - 12px);
        padding: 6px;
        border-radius: 14px;
    }

    body.writing-mode .steps h2,
    body.writing-mode .small-keywords,
    body.writing-mode .rules {
        display: none !important;
    }

    body.writing-mode .page-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    body.writing-mode .page-list button {
        aspect-ratio: var(--page-thumb-ratio);
        min-height: 0;
        border-radius: 12px;
    }

    body.writing-mode .thumb-ad {
        display: grid !important;
        margin-top: 6px;
    }

    body.writing-mode .thumb-ad .side-ad-card {
        aspect-ratio: 600 / 220;
    }

    body.writing-mode .writer {
        gap: 5px;
    }

    body.writing-mode .writer-toolbar {
        min-height: 26px;
    }

    body.writing-mode .writer-toolbar .ghost-btn,
    body.writing-mode .writer-toolbar strong {
        min-height: 26px;
        padding: 4px 8px;
        font-size: 0.72rem;
    }

    body.writing-mode .story-nudge {
        padding: 4px 9px;
        font-size: 0.7rem;
    }

    body.writing-mode .story-page {
        padding: 7px;
        border-radius: 14px;
    }

    body.writing-mode .image-zone {
        width: 88%;
        margin-top: 8px;
    }

    body.writing-mode .text-zone p {
        width: min(88%, 620px);
        min-height: 42px;
        padding: 7px 10px;
        font-size: clamp(0.68rem, 1.45vw, 0.82rem);
    }

    body.writing-mode .write-card {
        padding: 6px;
        border-radius: 14px;
    }

    body.writing-mode .page-keyword-cloud {
        max-height: 64px;
        gap: 5px;
        overflow: auto;
    }

    body.writing-mode .page-keyword-cloud button {
        min-height: 26px;
        padding: 4px 8px;
        font-size: 0.68rem;
    }

    body.writing-mode .next-writing-btn {
        min-height: 32px;
        font-size: 0.72rem;
    }

    body.writing-mode .side-action-slot .next-writing-btn {
        min-height: 34px;
        font-size: 0.72rem;
    }

    body.writing-mode .footer-ad img {
        height: 110px;
        object-fit: contain;
    }
}

@media (orientation: portrait) {
    body.writing-mode {
        --writer-w: min(760px, calc(100vw - 16px));
        --stage-h: auto;
    }

    body.writing-mode .kids-app {
        padding: 0 6px 12px;
    }

    body.writing-mode .workspace {
        grid-template-columns: minmax(0, var(--writer-w)) !important;
        grid-template-areas:
            "writer"
            "steps" !important;
        align-items: start;
        justify-content: center;
        width: min(100%, calc(100vw - 10px));
        min-height: auto;
        gap: 8px;
        padding: 6px;
        background: transparent;
        box-shadow: none;
    }

    body.writing-mode .writer,
    body.writing-mode .steps,
    body.writing-mode .story-page,
    body.writing-mode .write-card {
        width: 100%;
        max-width: var(--writer-w);
        margin-inline: auto;
    }

    body.writing-mode .steps {
        max-height: none;
        overflow: visible;
        border-radius: 20px;
    }

    body.writing-mode .page-list {
        max-width: min(440px, 100%);
        margin-inline: auto;
    }

    body.writing-mode .thumb-ad {
        width: min(600px, 100%);
        margin-inline: auto;
    }

    body.writing-mode .footer-ad {
        width: min(760px, calc(100% - 14px));
        margin-top: 12px;
    }
}

@media (max-width: 560px) and (orientation: portrait) {
    body.writing-mode {
        --writer-w: calc(100vw - 8px);
    }

    body.writing-mode .kids-app {
        padding: 0 3px 10px;
    }

    body.writing-mode .workspace {
        width: 100%;
        padding: 4px;
        gap: 7px;
    }

    body.writing-mode .writer {
        gap: 6px;
    }

    body.writing-mode .writer-toolbar {
        padding-inline: 2px;
    }

    body.writing-mode .story-page {
        padding: 7px;
        border-radius: 14px;
    }

    body.writing-mode .side-title {
        max-width: calc(100% - 80px);
        font-size: 0.74rem;
    }

    body.writing-mode .image-zone {
        width: 92%;
        margin-top: 12px;
    }

    body.writing-mode .text-zone p {
        width: min(90%, 360px);
        min-height: 48px;
        padding: 8px 11px;
        font-size: clamp(0.72rem, 3.15vw, 0.86rem);
    }

    body.writing-mode .write-card {
        padding: 7px;
        border-radius: 15px;
    }

    body.writing-mode .page-keyword-cloud {
        max-height: 72px;
        gap: 5px;
    }

    body.writing-mode .page-keyword-cloud button {
        min-height: 30px;
        padding: 5px 8px;
        font-size: 0.76rem;
    }

    body.writing-mode .feedback {
        min-height: 18px;
        font-size: 0.72rem;
    }

    body.writing-mode .next-writing-btn {
        min-height: 38px;
        font-size: 0.82rem;
    }

    body.writing-mode .steps {
        padding: 8px;
    }

    body.writing-mode .page-list {
        width: 100%;
        max-width: 100%;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 8px;
    }

    body.writing-mode .page-list button {
        width: 100%;
        min-height: 44px;
        border-radius: 14px;
        font-size: 0.82rem;
    }

    body.writing-mode .page-list button.has-thumb {
        min-height: 58px;
    }

    body.writing-mode .thumb-ad img,
    body.writing-mode .footer-ad img {
        max-height: none;
        height: 110px;
        object-fit: contain;
    }
}

/* Fit pass v1: keep the action button visible on desktop/tablet landscape.
   The page remains 16:9, but the canvas no longer consumes the whole height. */
@media (min-width: 900px) and (orientation: landscape) {
    body.writing-mode {
        --side-w: clamp(360px, 30vw, 520px);
        --writer-w: min(
            840px,
            calc(100vw - var(--side-w) - 42px),
            calc((100dvh - 190px) * var(--story-ratio))
        );
    }

    body.writing-mode .workspace {
        align-items: start;
        gap: clamp(10px, 1.4vw, 18px);
        padding: clamp(8px, 1vw, 14px);
    }

    body.writing-mode .writer {
        gap: 8px;
    }

    body.writing-mode .story-page {
        border-radius: 18px;
    }

    body.writing-mode .write-card {
        padding: 8px 10px;
    }

    body.writing-mode .page-keyword-cloud {
        max-height: 82px;
        gap: 6px;
        overflow: auto;
    }

    body.writing-mode .page-keyword-cloud button {
        min-height: 32px;
        padding: 5px 10px;
    }

    body.writing-mode .next-writing-btn {
        min-height: 36px;
        padding-block: 7px;
    }

    body.writing-mode .side-action-slot .next-writing-btn {
        min-height: 44px;
        font-size: clamp(0.88rem, 1.15vw, 1rem);
    }

    body.writing-mode .steps {
        gap: 12px;
        padding: 14px;
        align-self: start;
    }

    body.writing-mode .page-list {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 10px;
    }

    body.writing-mode .page-list button {
        min-height: clamp(54px, 7dvh, 78px);
        border-radius: 16px;
    }

    body.writing-mode .page-list button.has-thumb {
        min-height: clamp(62px, 8dvh, 92px);
    }

    body.writing-mode .thumb-ad {
        width: 100%;
    }
}

@media (min-width: 900px) and (orientation: landscape) and (max-height: 760px) {
    body.writing-mode {
        --side-w: clamp(330px, 30vw, 460px);
        --writer-w: min(
            760px,
            calc(100vw - var(--side-w) - 34px),
            calc((100dvh - 164px) * var(--story-ratio))
        );
    }

    body.writing-mode .workspace {
        padding: 7px;
    }

    body.writing-mode .writer {
        gap: 6px;
    }

    body.writing-mode .write-card {
        padding: 6px 8px;
    }

    body.writing-mode .page-keyword-cloud {
        max-height: 70px;
        overflow: auto;
    }

    body.writing-mode .feedback {
        min-height: 16px;
        font-size: 0.74rem;
    }

    body.writing-mode .next-writing-btn {
        min-height: 32px;
        font-size: 0.8rem;
    }

    body.writing-mode .side-action-slot .next-writing-btn {
        min-height: 38px;
        font-size: 0.82rem;
    }

    body.writing-mode .page-list {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 7px;
    }

    body.writing-mode .page-list button {
        min-height: clamp(42px, 7dvh, 62px);
    }
}

@media (min-width: 700px) and (orientation: portrait) {
    body.writing-mode .page-list {
        max-width: min(640px, 100%);
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 10px;
    }

    body.writing-mode .page-list button {
        min-height: clamp(58px, 7dvh, 86px);
        border-radius: 18px;
    }

    body.writing-mode .page-list button.has-thumb {
        min-height: clamp(70px, 9dvh, 104px);
    }
}

body.writing-mode .side-export-preview {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 2px;
}

body.writing-mode .side-export-cell,
body.writing-mode .side-pin-cell {
    min-width: 0;
    min-height: 38px;
    border: 1px solid rgba(11, 123, 109, 0.16);
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.72);
    color: rgba(16, 37, 31, 0.52);
    font-size: clamp(0.68rem, 0.9vw, 0.82rem);
    font-weight: 800;
}

body.writing-mode .side-export-cell {
    display: grid;
    place-items: center;
    padding: 7px 8px;
}

body.writing-mode .side-pin-cell {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 72px;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
}

body.writing-mode .side-pin-cell span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

body.writing-mode .side-pin-cell input {
    min-height: 28px;
    padding: 4px 8px;
    border-radius: 10px;
    font-size: 0.78rem;
    text-align: center;
}

body.writing-mode .side-export-cell:disabled,
body.writing-mode .side-pin-cell input:disabled {
    cursor: not-allowed;
}

@media (max-width: 560px) and (orientation: portrait) {
    body.writing-mode .side-export-preview {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 7px;
    }

    body.writing-mode .side-pin-cell {
        grid-template-columns: minmax(0, 1fr) 62px;
    }
}

/* Release-candidate layout lock.
   Keep the story canvas at one fixed 16:9 ratio; adapt only the surrounding layout. */
body.writing-mode {
    --story-ratio: 1.7778;
    --panel-gap: clamp(8px, 1.2vw, 16px);
    min-width: 0;
}

body.writing-mode .story-control-row,
body.writing-mode .writer-toolbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

body.writing-mode .story-control-row {
    width: 100%;
}

body.writing-mode .story-control-row > .ghost-btn,
body.writing-mode .writer-toolbar-actions > .ghost-btn {
    min-height: 34px;
    padding: 6px 12px;
    font-size: clamp(0.72rem, 1vw, 0.88rem);
}

body.writing-mode .exit-story-btn,
body.writing-mode .exit-story-toolbar-btn {
    color: #a11358;
    background: #fff4fa;
    border-color: rgba(194, 23, 122, 0.22);
}

body.writing-mode .exit-story-btn:hover,
body.writing-mode .exit-story-toolbar-btn:hover {
    background: #ffe8f4;
    transform: translateY(-1px);
}

body.writing-mode .story-page {
    aspect-ratio: 16 / 9 !important;
    height: auto !important;
    max-width: 100% !important;
    overflow: hidden;
}

body.writing-mode .page-list button {
    aspect-ratio: 16 / 9 !important;
    min-height: 0 !important;
    background-size: cover;
    background-position: center;
}

body.writing-mode .thumb-ad img,
body.writing-mode .footer-ad img {
    width: 100%;
    object-fit: contain;
}

body.writing-mode[data-layout$="landscape"] {
    --side-w: clamp(230px, 25vw, 430px);
    --writer-w: min(
        calc(100vw - var(--side-w) - (var(--panel-gap) * 3)),
        calc((100dvh - 154px) * var(--story-ratio)),
        1120px
    );
}

body.writing-mode[data-layout$="landscape"] .kids-app {
    padding: 0 6px 10px !important;
}

body.writing-mode[data-layout$="landscape"] .workspace {
    display: grid !important;
    grid-template-columns: minmax(0, var(--writer-w)) minmax(210px, var(--side-w)) !important;
    grid-template-areas: "writer steps" !important;
    align-items: start !important;
    justify-content: center !important;
    gap: var(--panel-gap) !important;
    width: min(100%, calc(100vw - 12px)) !important;
    min-height: auto !important;
    margin: 0 auto !important;
    padding: clamp(6px, 1vw, 12px) !important;
    overflow: visible !important;
}

body.writing-mode[data-layout$="landscape"] .writer {
    grid-area: writer !important;
    width: 100% !important;
    max-width: var(--writer-w) !important;
    gap: 7px !important;
    align-self: start !important;
}

body.writing-mode[data-layout$="landscape"] .steps {
    grid-area: steps !important;
    width: 100% !important;
    max-width: var(--side-w) !important;
    align-self: start !important;
    max-height: calc(100dvh - 18px) !important;
    overflow: auto !important;
    padding: clamp(8px, 1.1vw, 14px) !important;
    padding-bottom: clamp(24px, 4dvh, 44px) !important;
    scroll-padding-bottom: 52px !important;
    gap: clamp(8px, 1vw, 12px) !important;
}

body.writing-mode[data-layout$="landscape"] .story-page,
body.writing-mode[data-layout$="landscape"] .write-card {
    width: 100% !important;
}

body.writing-mode[data-layout$="landscape"] .page-list {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: clamp(8px, 1vw, 12px) !important;
    width: 100% !important;
}

body.writing-mode[data-layout$="landscape"] .page-list button {
    width: 100% !important;
    max-height: clamp(54px, 9.5dvh, 98px) !important;
    border-radius: clamp(12px, 1.3vw, 18px) !important;
}

body.writing-mode[data-layout="desktop-landscape"] {
    --side-w: clamp(230px, 21vw, 340px);
}

body.writing-mode[data-layout$="landscape"] .thumb-ad {
    display: block !important;
}

body.writing-mode[data-layout$="landscape"] .side-ad-card {
    overflow: hidden;
}

body.writing-mode[data-layout$="landscape"] .thumb-ad img {
    max-height: clamp(78px, 15dvh, 138px) !important;
}

body.writing-mode[data-layout="mobile-landscape"] {
    --side-w: clamp(160px, 32vw, 240px);
    --writer-w: min(
        calc(100vw - var(--side-w) - 26px),
        calc((100dvh - 106px) * var(--story-ratio))
    );
}

body.writing-mode[data-layout="mobile-landscape"] .workspace {
    grid-template-columns: minmax(0, var(--writer-w)) minmax(150px, var(--side-w)) !important;
    padding: 4px !important;
    gap: 6px !important;
}

body.writing-mode[data-layout="mobile-landscape"] .writer-toolbar {
    min-height: 24px !important;
}

body.writing-mode[data-layout="mobile-landscape"] .story-page {
    max-height: calc(100dvh - 120px) !important;
}

body.writing-mode[data-layout="mobile-landscape"] .write-card {
    padding: 5px 6px !important;
}

body.writing-mode[data-layout="mobile-landscape"] .page-keyword-cloud {
    max-height: 42px !important;
    overflow: auto !important;
}

body.writing-mode[data-layout="mobile-landscape"] .page-keyword-cloud button {
    min-height: 24px !important;
    padding: 3px 7px !important;
    font-size: 0.66rem !important;
}

body.writing-mode[data-layout="mobile-landscape"] .feedback {
    display: none !important;
}

body.writing-mode[data-layout="mobile-landscape"] .next-writing-btn {
    min-height: 30px !important;
    padding-block: 5px !important;
    font-size: 0.72rem !important;
}

body.writing-mode[data-layout="mobile-landscape"] .side-action-slot {
    padding-bottom: 24px !important;
}

body.writing-mode[data-layout="mobile-landscape"] .steps h2,
body.writing-mode[data-layout="mobile-landscape"] .small-keywords,
body.writing-mode[data-layout="mobile-landscape"] .rules,
body.writing-mode[data-layout="mobile-landscape"] .side-export-preview {
    display: none !important;
}

body.writing-mode[data-layout="mobile-landscape"] .page-list {
    gap: 5px !important;
}

body.writing-mode[data-layout="mobile-landscape"] .page-list button {
    max-height: 48px !important;
}

body.writing-mode[data-layout="mobile-landscape"] .thumb-ad img {
    max-height: 58px !important;
}

body.writing-mode[data-layout$="portrait"] .workspace {
    display: grid !important;
    grid-template-columns: minmax(0, min(760px, calc(100vw - 12px))) !important;
    grid-template-areas:
        "writer"
        "steps" !important;
    align-items: start !important;
    justify-content: center !important;
    width: min(100%, calc(100vw - 8px)) !important;
    min-height: auto !important;
    padding: 6px !important;
    gap: 8px !important;
    overflow: visible !important;
}

body.writing-mode[data-layout$="portrait"] .writer,
body.writing-mode[data-layout$="portrait"] .steps,
body.writing-mode[data-layout$="portrait"] .story-page,
body.writing-mode[data-layout$="portrait"] .write-card {
    width: 100% !important;
    max-width: min(760px, calc(100vw - 12px)) !important;
}

body.writing-mode[data-layout$="portrait"] .steps {
    max-height: none !important;
    overflow: visible !important;
    padding: 10px !important;
}

body.writing-mode[data-layout$="portrait"] .page-list {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: clamp(7px, 2vw, 12px) !important;
    width: 100% !important;
}

body.writing-mode[data-layout$="portrait"] .page-list button {
    border-radius: clamp(12px, 3vw, 18px) !important;
}

body.writing-mode[data-layout$="portrait"] .thumb-ad {
    display: block !important;
    width: 100% !important;
    margin-top: 4px !important;
}

body.writing-mode[data-layout$="portrait"] .thumb-ad img {
    max-height: clamp(78px, 22vw, 150px) !important;
}

body.writing-mode[data-layout="tablet-portrait"] .page-list button {
    min-height: 74px !important;
}

body.writing-mode[data-layout="mobile-portrait"] .kids-app {
    padding-inline: 2px !important;
}

body.writing-mode[data-layout="mobile-portrait"] .writer-toolbar-actions {
    gap: 5px;
}

body.writing-mode[data-layout="mobile-portrait"] .writer-toolbar-actions > .ghost-btn {
    min-height: 30px;
    padding: 5px 8px;
    font-size: 0.68rem;
}

body.writing-mode[data-layout="mobile-portrait"] .story-page {
    border-radius: 15px !important;
}

body.writing-mode[data-layout="mobile-portrait"] .page-list button {
    min-height: 44px !important;
}

/* Final writing UX pass: keep keywords and action buttons visible without an inner scrollbar. */
body.writing-mode .write-card {
    overflow: visible !important;
}

body.writing-mode .page-keyword-cloud,
body.writing-mode[data-layout="mobile-landscape"] .page-keyword-cloud,
body.writing-mode[data-layout$="portrait"] .page-keyword-cloud {
    max-height: none !important;
    overflow: visible !important;
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: center !important;
    gap: clamp(5px, 1vw, 8px) !important;
    padding: clamp(5px, 1vw, 8px) !important;
}

body.writing-mode .page-keyword-cloud button,
body.writing-mode[data-layout="mobile-landscape"] .page-keyword-cloud button,
body.writing-mode[data-layout$="portrait"] .page-keyword-cloud button {
    min-height: clamp(27px, 4.2vh, 36px) !important;
    max-width: min(160px, 32vw) !important;
    padding: 4px clamp(8px, 1.2vw, 12px) !important;
    border-radius: 999px !important;
    font-size: clamp(0.72rem, 1.45vw, 0.92rem) !important;
    line-height: 1.15 !important;
    white-space: nowrap !important;
}

body.writing-mode .writing-status-row {
    align-items: center !important;
    gap: 8px !important;
}

body.writing-mode .feedback {
    min-height: 0 !important;
    line-height: 1.35 !important;
}

body.writing-mode .writing-action-row {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto auto !important;
    gap: 8px !important;
    align-items: center !important;
}

body.writing-mode .writing-action-row .primary-btn,
body.writing-mode .writing-action-row .secondary-btn,
body.writing-mode .writing-action-row .ghost-btn {
    min-height: clamp(34px, 5vh, 48px) !important;
    padding: 7px 12px !important;
    line-height: 1.18 !important;
    white-space: normal !important;
}

@media (max-width: 760px), (max-height: 540px) {
    body.writing-mode .page-keyword-cloud button {
        max-width: 30vw !important;
        font-size: clamp(0.66rem, 2.4vw, 0.82rem) !important;
    }

    body.writing-mode .writing-action-row {
        grid-template-columns: 1fr 1fr !important;
    }

    body.writing-mode .writing-action-row .next-writing-btn {
        grid-column: 1 / -1 !important;
    }
}
