:root {
    --ink: #10251f;
    --muted: #61736e;
    --line: #d9ebe6;
    --paper: #fbfff9;
    --soft: #f1fbf7;
    --accent: #c2177a;
    --accent-2: #0b7b6d;
    --sun: #ffcf4a;
    --mint: #6ee7bd;
    --sky: #8ad7ff;
    --peach: #ffb58a;
    --lilac: #bda2ff;
    --shadow: 0 18px 48px rgba(15, 45, 36, 0.12);
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

.startup-error {
    margin: 12px auto;
    padding: 12px 16px;
    border: 1px solid #f1a8c9;
    border-radius: 16px;
    background: #fff5fa;
    color: #7a1248;
    font-weight: 800;
    box-shadow: 0 10px 28px rgba(122, 18, 72, 0.12);
}

.startup-error span {
    display: block;
    margin-top: 4px;
    color: #5d6f69;
    font-weight: 600;
}

body {
    margin: 0;
    color: var(--ink);
    background:
        radial-gradient(circle at 8% 12%, rgba(255, 207, 74, 0.22), transparent 24%),
        radial-gradient(circle at 94% 18%, rgba(138, 215, 255, 0.28), transparent 26%),
        linear-gradient(180deg, #f6fbf8 0%, #edf5f2 42%, #f8fbf4 100%);
    font-family: "Nunito Sans", "Cairo", "Tajawal", "Noto Sans SC", sans-serif;
}

html[dir="rtl"] body {
    font-family: "Cairo", "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
}

html[lang="zh"] body {
    font-family: "Noto Sans SC", "Nunito Sans", sans-serif;
}

html[dir="rtl"] input,
html[dir="rtl"] select,
html[dir="rtl"] textarea,
html[dir="rtl"] button {
    font-family: "Cairo", "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
}

button,
input,
select,
textarea {
    font: inherit;
}

.kids-app {
    width: min(1180px, calc(100% - 28px));
    margin: 0 auto;
    padding: 0 0 42px;
}

.topbar,
.setup-panel,
.workspace,
.export-panel,
.guide-panel {
    border: 1px solid var(--line);
    border-radius: 26px;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: var(--shadow);
}

.sticky-head {
    position: sticky;
    top: 0;
    z-index: 30;
}

.topbar {
    display: grid;
    grid-template-columns: 140px 1fr auto;
    gap: 18px;
    align-items: center;
    padding: 10px 18px;
    position: relative;
    z-index: 1;
    backdrop-filter: blur(14px);
    border-radius: 0 0 26px 26px;
}

.brand img {
    width: 114px;
    max-width: 100%;
    display: block;
}

.thin-ad {
    width: min(1400px, 100%);
    margin: 0 auto;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.82);
    box-shadow: 0 10px 26px rgba(15, 45, 36, 0.08);
    overflow: hidden;
    border-radius: 18px 18px 0 0;
}

.thin-ad picture,
.thin-ad img {
    display: block;
    width: 100%;
}

.thin-ad img {
    height: clamp(64px, 7vw, 108px);
    object-fit: contain;
    object-position: center;
}

body.writing-mode {
    background: #eaf4f1;
    overflow-x: hidden;
    overflow-y: auto;
}

body.writing-mode .kids-app {
    width: 100%;
    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;
    top: auto;
    z-index: auto;
}

body.writing-mode .thin-ad {
    width: min(600px, calc(100% - 18px));
    margin: 12px auto 0;
    border-inline: 1px solid rgba(11, 123, 109, 0.12);
    border-radius: 16px;
    box-shadow: 0 10px 24px rgba(21, 71, 61, 0.08);
}

body.writing-mode .thin-ad img {
    height: clamp(52px, 7vw, 86px);
}

.language-switch,
.top-actions,
.button-row,
.writer-toolbar,
.segmented {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.session-chip {
    display: inline-flex;
    align-items: center;
    min-height: 40px;
    max-width: 220px;
    padding: 8px 12px;
    border: 1px solid rgba(11, 123, 109, 0.16);
    border-radius: 999px;
    background: #f4fffb;
    color: var(--accent-2);
    font-size: 0.86rem;
    font-weight: 800;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.session-logout {
    min-height: 40px;
    padding: 8px 13px;
}

.language-switch {
    justify-content: center;
}

.language-field {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 46px;
    padding: 6px 8px 6px 14px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
}

.language-field span {
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 800;
    line-height: 1;
}

.language-field select {
    width: auto;
    min-width: 138px;
    border: 0;
    border-radius: 999px;
    padding: 6px 28px 6px 4px;
    background: transparent;
    color: var(--ink);
    font-weight: 800;
    outline: none;
}

html[dir="rtl"] .language-field select {
    padding: 6px 4px 6px 28px;
}

.language-switch button,
.ghost-btn,
.secondary-btn,
.primary-btn,
.upload-btn,
.top-actions a {
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
    color: var(--ink);
    padding: 10px 16px;
    font-weight: 700;
    text-decoration: none;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.flag-btn {
    width: 42px;
    min-width: 42px;
    height: 28px;
    padding: 0;
    display: inline-grid;
    place-items: center;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
    line-height: 1;
}

.language-switch .flag-btn,
.language-switch .flag-btn:hover,
.language-switch .flag-btn:active,
.language-switch .flag-btn.active {
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
    transform: none;
}

.flag-btn img {
    display: block;
    width: 42px;
    height: 28px;
    border-radius: 5px;
    box-shadow: 0 0 0 1px rgba(16, 37, 31, 0.08);
    object-fit: cover;
}

.language-switch button:hover,
.ghost-btn:hover,
.secondary-btn:hover,
.top-actions a:hover,
.upload-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 10px 22px rgba(15, 45, 36, 0.12);
}

.language-switch button:active,
.ghost-btn:active,
.secondary-btn:active,
.primary-btn:active,
.upload-btn:active {
    transform: translateY(1px) scale(0.98);
}

button:disabled,
button[aria-disabled="true"] {
    cursor: not-allowed;
    opacity: 0.52;
    box-shadow: none;
    transform: none;
}

button:disabled:hover {
    box-shadow: none;
    transform: none;
}

.language-switch button.active,
.primary-btn {
    border-color: transparent;
    background: linear-gradient(135deg, var(--accent), #6d4cd8);
    color: #fff;
}

.secondary-btn,
.upload-btn {
    color: var(--accent-2);
}

.image-action-btn {
    min-width: 0;
    padding: 10px 14px;
    gap: 8px;
}

.image-action-btn span[aria-hidden="true"] {
    display: inline-grid;
    place-items: center;
    width: 24px;
    height: 24px;
    border-radius: 8px;
    background: rgba(12, 127, 108, 0.1);
    color: var(--accent-2);
    font-size: 1rem;
}

.primary-btn.full {
    width: 100%;
    justify-content: center;
}

.primary-btn.is-processing {
    opacity: 0.9;
    cursor: wait;
    position: relative;
}

.primary-btn.is-processing::before {
    content: "";
    width: 18px;
    height: 18px;
    border-radius: 999px;
    border: 3px solid rgba(255, 255, 255, 0.38);
    border-top-color: #fff;
    animation: kidsSpin 0.85s linear infinite;
}

.primary-btn.small {
    padding: 8px 14px;
}

.hero {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 118px;
    gap: 24px;
    align-items: center;
    padding: 42px 8px 24px;
}

.pill {
    display: inline-flex;
    padding: 8px 14px;
    border-radius: 999px;
    color: var(--accent-2);
    background: #e8f8f2;
    font-weight: 800;
}

.hero h1 {
    margin: 14px 0 12px;
    max-width: 780px;
    font-size: clamp(2rem, 4.5vw, 4.2rem);
    line-height: 1.02;
    letter-spacing: 0;
}

.hero p {
    max-width: 720px;
    color: var(--muted);
    font-size: 1.08rem;
    line-height: 1.8;
}

.price-note {
    border-radius: 18px;
    padding: 10px;
    background: #fff;
    border: 1px solid var(--line);
    text-align: center;
}

.hero-logo {
    display: block;
    width: min(82px, 100%);
    height: auto;
    margin: 0 auto 5px;
}

.price-note strong {
    display: block;
    color: var(--accent);
    font-size: 1.7rem;
    line-height: 1;
}

.price-note span {
    display: block;
    font-size: 0.78rem;
    line-height: 1.35;
}

.setup-panel,
.export-panel,
.guide-panel {
    padding: 24px;
}

.setup-panel {
    position: relative;
    overflow: hidden;
    background:
        linear-gradient(135deg, rgba(255, 255, 255, 0.96), rgba(255, 250, 246, 0.94)),
        radial-gradient(circle at 98% 0%, rgba(255, 207, 74, 0.26), transparent 24%);
}

.setup-panel::before {
    content: "";
    position: absolute;
    inset: auto 22px 18px auto;
    width: 82px;
    height: 82px;
    border-radius: 28px;
    background:
        linear-gradient(135deg, rgba(194, 23, 122, 0.14), rgba(109, 76, 216, 0.08));
    transform: rotate(-8deg);
    pointer-events: none;
}

.panel-title,
.keyword-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.panel-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    flex-wrap: wrap;
}

.ghost-btn.danger {
    color: var(--accent);
    border-color: rgba(194, 23, 122, 0.25);
    background: #fff7fb;
}

.ghost-btn.danger:hover {
    background: #ffeaf5;
}

h2,
h3,
p {
    margin-top: 0;
}

.setup-panel p,
.keyword-head p,
.rules,
.readyCopy {
    color: var(--muted);
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
}

.field {
    display: grid;
    gap: 8px;
}

.setup-panel .field {
    gap: 6px;
}

.setup-panel .field > span {
    font-size: clamp(0.92rem, 1.2vw, 1.05rem);
    line-height: 1.2;
}

.title-field {
    align-content: start;
}

.title-line,
.keyword-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.mini-magic-btn {
    border: 1px solid rgba(194, 23, 122, 0.18);
    border-radius: 999px;
    background: #fff7fb;
    color: var(--accent);
    padding: 6px 10px;
    font-size: 0.78rem;
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
    white-space: nowrap;
    box-shadow: 0 8px 18px rgba(194, 23, 122, 0.08);
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.mini-magic-btn:hover {
    transform: translateY(-1px);
    background: #ffeef7;
    box-shadow: 0 12px 24px rgba(194, 23, 122, 0.13);
}

.mini-magic-btn:active {
    transform: translateY(1px) scale(0.98);
}

.field > span,
.keyword-head h3 {
    font-weight: 800;
}

.field small {
    color: var(--muted);
    font-size: 0.78rem;
    line-height: 1.45;
}

.mic-permission-field {
    grid-template-columns: auto minmax(84px, 118px);
    align-items: end;
    gap: 5px 10px;
}

.mic-permission-field > span {
    white-space: nowrap;
    line-height: 1.15;
}

.mic-permission-field select {
    min-height: 44px;
    padding-block: 10px;
}

.mic-permission-field small {
    grid-column: 1 / -1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.72rem;
    line-height: 1.15;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: #fff;
    padding: 13px 14px;
    outline: none;
}

.setup-panel input,
.setup-panel select {
    min-height: 50px;
    padding: 10px 13px;
    border-radius: 15px;
}

textarea {
    min-height: 96px;
    resize: vertical;
    line-height: 1.7;
}

input:focus,
select:focus,
textarea:focus {
    border-color: rgba(194, 23, 122, 0.5);
    box-shadow: 0 0 0 4px rgba(194, 23, 122, 0.08);
}

.segmented {
    padding: 5px;
    border-radius: 18px;
    background: var(--soft);
}

.setup-panel .segmented {
    min-height: 50px;
    align-items: center;
}

.segmented button {
    border: 0;
    border-radius: 14px;
    background: transparent;
    padding: 10px 22px;
    font-weight: 800;
    cursor: pointer;
}

.setup-panel .segmented button {
    min-height: 40px;
    padding: 8px 18px;
}

.segmented button.active {
    color: #fff;
    background: var(--accent-2);
}

.keyword-area {
    margin-top: 22px;
    padding: 18px;
    border: 1px solid rgba(194, 23, 122, 0.12);
    border-radius: 24px;
    background:
        linear-gradient(135deg, rgba(255, 246, 252, 0.92), rgba(239, 251, 246, 0.94));
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.72);
}

.keyword-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 12px;
}

.keyword-box {
    position: relative;
    isolation: isolate;
}

.keyword-box input {
    min-height: 58px;
    padding-inline-start: 46px;
    padding-inline-end: 38px;
    border-width: 2px;
    border-color: rgba(16, 37, 31, 0.06);
    border-radius: 20px;
    background: #fff;
    color: var(--ink);
    font-weight: 800;
    box-shadow: 0 10px 20px rgba(15, 45, 36, 0.06);
    transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease;
}

.keyword-box span {
    position: absolute;
    inset-inline-start: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 25px;
    height: 25px;
    display: grid;
    place-items: center;
    border-radius: 999px;
    color: #fff;
    background: var(--accent);
    font-weight: 900;
    font-size: 0.8rem;
    box-shadow: 0 6px 14px rgba(194, 23, 122, 0.25);
    z-index: 1;
}

.keyword-remove {
    position: absolute;
    inset-inline-end: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: 28px;
    height: 28px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 999px;
    background: rgba(194, 23, 122, 0.1);
    color: var(--accent);
    font-size: 1.1rem;
    font-weight: 900;
    line-height: 1;
    cursor: pointer;
    z-index: 2;
    transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.keyword-remove:hover {
    transform: translateY(-50%) scale(1.05);
    background: var(--accent);
    color: #fff;
}

.keyword-remove:active {
    transform: translateY(-50%) scale(0.95);
}

.keyword-box input:hover {
    transform: translateY(-1px);
    box-shadow: 0 14px 24px rgba(15, 45, 36, 0.09);
}

.keyword-box:nth-child(5n + 1) input {
    background: linear-gradient(180deg, #fff 0%, #fff6d9 100%);
}

.keyword-box:nth-child(5n + 2) input {
    background: linear-gradient(180deg, #fff 0%, #e9fff5 100%);
}

.keyword-box:nth-child(5n + 3) input {
    background: linear-gradient(180deg, #fff 0%, #eff7ff 100%);
}

.keyword-box:nth-child(5n + 4) input {
    background: linear-gradient(180deg, #fff 0%, #fff0e8 100%);
}

.keyword-box:nth-child(5n + 5) input {
    background: linear-gradient(180deg, #fff 0%, #f4efff 100%);
}

.keyword-box:nth-child(5n + 2) span {
    background: var(--accent-2);
}

.keyword-box:nth-child(5n + 3) span {
    background: #3478f6;
}

.keyword-box:nth-child(5n + 4) span {
    background: #ef7d31;
}

.keyword-box:nth-child(5n + 5) span {
    background: #7b4de2;
}

.story-objects-field {
    display: block;
    margin-top: 16px;
}

.story-objects-field > span {
    display: block;
    margin-bottom: 8px;
    font-weight: 800;
}

.story-objects-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 10px;
}

.story-objects-grid input {
    min-height: 48px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.9);
    font-size: 0.95rem;
    font-weight: 700;
}

.story-objects-field small {
    display: block;
    margin-top: 6px;
    color: var(--muted);
    font-size: 0.86rem;
    line-height: 1.55;
}

.setup-start-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-top: 22px;
}

.setup-start-btn {
    min-width: min(100%, 320px);
    justify-content: center;
}

.setup-resume-btn {
    min-width: min(100%, 240px);
    justify-content: center;
}

.autosave-note {
    width: 100%;
    color: var(--muted);
    font-size: 0.92rem;
    text-align: center;
}

.workspace {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    gap: 18px;
    padding: 16px;
    margin-top: 22px;
}

body.writing-mode .workspace {
    display: grid;
    grid-template-columns: 300px minmax(0, 1fr);
    width: min(1180px, calc(100% - 28px));
    margin: 8px auto 0;
    padding: 12px;
}

body.writing-mode .steps {
    display: block;
}

body.writing-mode .writer {
    width: 100%;
    margin: 0;
    position: relative;
    padding-inline: 0;
}

.steps,
.writer {
    min-width: 0;
}

.small-keywords {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
    margin: 12px 0;
}

.small-keywords span {
    border-radius: 999px;
    padding: 7px 12px;
    background: #f7eef6;
    color: var(--accent);
    font-weight: 800;
    font-size: 0.88rem;
    border: 1px solid rgba(194, 23, 122, 0.12);
}

.small-keywords span.used {
    background: #e7f8f1;
    color: var(--accent-2);
}

.small-keywords span.missing {
    background: #fff4fb;
    color: var(--accent);
}

.page-list {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin: 16px 0;
}

.page-list button {
    position: relative;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 18px;
    min-height: 58px;
    background: #fff;
    background-position: center;
    background-size: contain;
    background-repeat: no-repeat;
    color: var(--ink);
    font-weight: 900;
    cursor: pointer;
    box-shadow: 0 8px 16px rgba(15, 45, 36, 0.05);
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.page-list button span {
    position: relative;
    z-index: 2;
    min-width: 32px;
    min-height: 32px;
    display: inline-grid;
    place-items: center;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.9);
    color: var(--ink);
    box-shadow: 0 8px 18px rgba(15, 45, 36, 0.1);
}

.page-list button.has-thumb::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 1;
    background: linear-gradient(180deg, rgba(255,255,255,0.1), rgba(16,37,31,0.24));
}

.page-list button.has-thumb {
    border-color: rgba(194, 23, 122, 0.24);
    min-height: 72px;
    background-size: contain !important;
    background-position: center !important;
    background-repeat: no-repeat !important;
}

.page-list button:hover {
    transform: translateY(-1px);
    box-shadow: 0 12px 22px rgba(15, 45, 36, 0.1);
}

.page-list button:active {
    transform: translateY(1px) scale(0.98);
}

.page-list button.active {
    border-color: var(--accent);
    color: var(--accent);
    background: #fff4fb;
}

.page-list button.active span {
    color: var(--accent);
}

.page-list button.done {
    border-color: #bde6d7;
    background: #edf9f4;
}

.page-list button.done.has-thumb {
    background-color: #edf9f4;
}

.side-ad-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    width: min(300px, 100%);
    margin: 16px auto 12px;
}

.side-ad-card {
    width: 100%;
    aspect-ratio: 600 / 220;
    min-height: 88px;
    border: 1px solid rgba(11, 123, 109, 0.16);
    border-radius: 12px;
    background: #fff;
    overflow: hidden;
}

.side-ad-card img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.side-action-slot {
    width: 100%;
    padding-bottom: 14px;
    scroll-margin-bottom: 34px;
}

.side-action-slot:empty {
    display: none;
}

.side-action-slot .writing-action-row {
    margin: 8px 0 14px;
}

.writer-toolbar {
    justify-content: space-between;
    margin-bottom: 12px;
}

body.writing-mode .writer-toolbar {
    position: static;
    width: auto;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

body.writing-mode .writer-toolbar .ghost-btn {
    min-height: 42px;
    padding: 8px 10px;
    border-radius: 16px;
    font-size: 0.86rem;
}

body[data-allow-mic="no"] .mic-record-btn {
    display: none !important;
}

.mic-record-btn {
    min-width: 42px;
    aspect-ratio: 1;
    display: inline-grid;
    place-items: center;
    color: var(--accent-2);
    border-color: rgba(11, 123, 109, 0.22);
    background: #f2fffa;
}

.mic-record-btn.is-recording {
    color: #fff;
    border-color: rgba(194, 23, 122, 0.35);
    background: linear-gradient(135deg, #c2177a, #704be8);
    box-shadow: 0 12px 28px rgba(194, 23, 122, 0.22);
    animation: micPulse 1s ease-in-out infinite;
}

.text-zone .in-text-mic {
    position: absolute;
    inset-block-start: 50%;
    inset-inline-end: 20px;
    z-index: 4;
    width: 44px;
    min-width: 44px;
    height: 44px;
    padding: 0;
    transform: translateY(-50%);
    border-radius: 999px;
}

.text-zone .in-text-mic.is-recording {
    animation: inTextMicPulse 1s ease-in-out infinite;
}

@keyframes inTextMicPulse {
    0%, 100% {
        transform: translateY(-50%) scale(1);
    }

    50% {
        transform: translateY(-50%) scale(1.05);
    }
}

@keyframes micPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

body.writing-mode .writer-toolbar strong {
    min-height: 38px;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.88);
    font-size: 0.86rem;
    text-align: center;
}

.voice-prompt {
    width: min(100%, 980px);
    min-height: 28px;
    margin: 0 auto 8px;
    padding: 6px 12px;
    border: 1px solid rgba(11, 123, 109, 0.08);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.38);
    color: var(--muted);
    font-size: clamp(0.76rem, 0.95vw, 0.9rem);
    font-weight: 700;
    line-height: 1.35;
    text-align: center;
    backdrop-filter: blur(5px);
}

.story-nudge {
    position: absolute;
    z-index: 12;
    inset-block-start: 44px;
    inset-inline-start: 50%;
    display: block;
    width: min(100%, 720px);
    min-height: 0;
    margin: 0;
    transform: translateX(-50%) translateY(-6px);
    pointer-events: none;
    opacity: 0;
    box-shadow: none;
    transition: opacity 0.22s ease, transform 0.22s ease;
}

html[dir="rtl"] .story-nudge {
    transform: translateX(-50%) translateY(-6px);
}

.story-nudge.show {
    opacity: 1;
    cursor: pointer;
    pointer-events: auto;
    transform: translateX(-50%) translateY(0);
}

html[dir="rtl"] .story-nudge.show {
    transform: translateX(-50%) translateY(0);
}

.stop-voice-btn {
    flex: 0 0 auto;
    min-height: 48px;
    padding-inline: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: transform 0.15s ease, color 0.15s ease, border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.stop-voice-btn::before {
    content: "";
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: currentColor;
    box-shadow: 0 0 0 5px color-mix(in srgb, currentColor 12%, transparent);
}

.stop-voice-btn.is-on {
    color: var(--accent-2);
    border-color: rgba(11, 123, 109, 0.25);
    background: rgba(232, 249, 242, 0.92);
}

.stop-voice-btn.is-off {
    color: #b42318;
    border-color: rgba(180, 35, 24, 0.28);
    background: rgba(255, 239, 236, 0.94);
}

.stop-voice-btn:active {
    transform: translateY(1px) scale(0.98);
}

.story-page {
    position: relative;
    display: grid;
    grid-template-rows: minmax(240px, 1fr) auto;
    width: 100%;
    aspect-ratio: 16 / 9;
    min-height: 0;
    max-height: min(62vw, 620px);
    overflow: hidden;
    border-radius: 24px;
    border: 1px solid var(--line);
    background: #fffaf2;
    padding: 22px;
}

body.writing-mode .story-page {
    width: min(100%, 780px);
    min-width: 0;
    max-height: none;
    margin: 0 auto;
    padding: 14px;
    border-radius: 18px;
}

html[dir="rtl"] .story-page {
    direction: rtl;
}

.side-title {
    position: absolute;
    z-index: 3;
    inset-block-start: 16px;
    inset-inline-start: 18px;
    max-width: min(46ch, calc(100% - 110px));
    padding: 5px 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.42);
    color: var(--ink);
    font-size: clamp(0.82rem, 1.25vw, 1.08rem);
    font-weight: 700;
    letter-spacing: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.image-zone,
.text-zone {
    margin: 0;
    min-width: 0;
}

.image-zone {
    display: grid;
    place-items: center;
    width: min(78%, 760px);
    min-height: 280px;
    margin: 34px auto 12px;
    padding: 18px;
    background: transparent;
    overflow: hidden;
}

body.writing-mode .image-zone {
    min-height: 150px;
    margin: 18px auto 6px;
    padding: 8px;
}

.story-page.has-image .image-zone {
    width: min(88%, 880px);
    min-height: 0;
    margin: 10px auto 0;
    padding: 0;
}

.story-page.has-image .text-zone {
    z-index: 4;
}

.image-zone img {
    width: 100%;
    height: 100%;
    max-height: 360px;
    object-fit: contain;
    object-position: center;
    -webkit-mask-image: radial-gradient(ellipse at center, #000 62%, rgba(0,0,0,0.72) 78%, transparent 100%);
    mask-image: radial-gradient(ellipse at center, #000 62%, rgba(0,0,0,0.72) 78%, transparent 100%);
}

.image-zone figcaption {
    color: var(--muted);
    text-align: center;
    line-height: 1.8;
}

.image-zone.is-loading figcaption::before {
    content: "";
    display: block;
    width: 28px;
    height: 28px;
    margin: 0 auto 10px;
    border-radius: 999px;
    border: 4px solid rgba(11, 123, 109, 0.18);
    border-top-color: var(--accent);
    animation: kidsSpin 1s linear infinite;
}

@keyframes kidsSpin {
    to {
        transform: rotate(360deg);
    }
}

.text-zone {
    position: relative;
    display: grid;
    place-items: center;
    padding: 12px 28px 24px;
    text-align: center;
}

.text-zone span {
    position: absolute;
    inset-block-start: 12px;
    inset-inline-end: 14px;
    min-width: 38px;
    min-height: 38px;
    display: grid;
    place-items: center;
    border-radius: 999px;
    color: var(--accent);
    background: #fff;
    font-weight: 900;
}

.text-zone p {
    margin: 0;
    width: min(82%, 760px);
    min-height: 96px;
    max-height: none;
    overflow: visible;
    resize: both;
    padding: 16px 72px 16px 20px;
    border: 2px dashed rgba(194, 23, 122, 0.2);
    border-radius: 22px;
    background: rgba(255, 255, 255, 0.58);
    box-shadow: 0 10px 24px rgba(15, 45, 36, 0.06);
    backdrop-filter: blur(4px);
    color: var(--ink);
    font-size: clamp(0.95rem, 1.65vw, 1.45rem);
    line-height: 1.35;
    font-weight: 700;
    outline: none;
}

.story-page.has-image .text-zone p {
    background: rgba(255, 255, 255, 0.5);
    border-color: rgba(194, 23, 122, 0.18);
    box-shadow: 0 8px 20px rgba(15, 45, 36, 0.05);
}

html[dir="rtl"] .text-zone p {
    padding-inline-start: 72px;
    padding-inline-end: 20px;
}

body.writing-mode .text-zone p {
    width: min(88%, 860px);
    min-height: 66px;
    padding: 10px 64px 10px 14px;
    font-size: clamp(0.9rem, 1.25vw, 1.22rem);
    line-height: 1.38;
    overflow-wrap: anywhere;
}

html[dir="rtl"] body.writing-mode .text-zone p {
    padding-inline-start: 64px;
    padding-inline-end: 14px;
}

.regenerate-image-btn {
    min-height: 48px;
    border-radius: 999px;
    font-weight: 800;
}

.regenerate-image-btn[hidden] {
    display: none !important;
}

.text-zone p:focus {
    border-color: rgba(194, 23, 122, 0.55);
    box-shadow: 0 0 0 5px rgba(194, 23, 122, 0.08), 0 12px 28px rgba(15, 45, 36, 0.08);
}

.text-zone p:empty::before {
    content: attr(data-placeholder);
    color: rgba(16, 37, 31, 0.42);
}

.write-card {
    margin-top: 14px;
    padding: 16px;
    border-radius: 22px;
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(250, 255, 252, 0.96));
    border: 1px solid var(--line);
}

body.writing-mode .write-card {
    width: min(100%, 780px);
    margin: 8px auto 0;
    padding: 10px;
    border-radius: 18px;
}

.page-keyword-cloud {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
    max-height: 128px;
    overflow: auto;
    padding: 0 0 12px;
    margin-bottom: 12px;
    border-bottom: 1px solid var(--line);
}

.page-keyword-cloud button {
    border: 2px solid rgba(16, 37, 31, 0.06);
    border-radius: 999px;
    padding: 8px 13px;
    background: #fff;
    color: var(--ink);
    font-size: 0.94rem;
    font-weight: 800;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
    box-shadow: 0 8px 18px rgba(15, 45, 36, 0.06);
}

.page-keyword-cloud button:hover {
    transform: translateY(-1px);
    box-shadow: 0 8px 18px rgba(15, 45, 36, 0.1);
}

.page-keyword-cloud button:active {
    transform: translateY(1px) scale(0.98);
}

.page-keyword-cloud button.in-page {
    border-color: rgba(11, 123, 109, 0.24);
    background: linear-gradient(135deg, #dcf6ec, #f8fff4);
    color: var(--accent-2);
}

.page-keyword-cloud button.used {
    background: #eef8f4;
    color: #54746c;
}

.page-keyword-cloud button.missing {
    border-color: rgba(194, 23, 122, 0.2);
    background: linear-gradient(135deg, #fff6fb, #fffdf0);
    color: var(--accent);
}

.feedback {
    min-height: 26px;
    margin: 8px 0 12px;
    color: var(--muted);
    font-weight: 700;
}

.feedback.ok {
    color: var(--accent-2);
}

.feedback.warn {
    color: #b65600;
}

.feedback.gentle {
    color: var(--accent);
}

.upload-btn input {
    display: none;
}

.suggestion {
    margin-top: 12px;
    padding: 14px;
    border-radius: 18px;
    border: 1px solid #f4c6df;
    background: #fff7fb;
}

.export-panel,
.guide-panel {
    margin-top: 22px;
}

.export-pin {
    margin-top: 18px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: #fff;
}

.export-pin p {
    margin: 8px 0 14px;
    color: var(--muted);
    font-size: 0.95rem;
}

.library-submit {
    margin-top: 16px;
    padding: 16px;
    border: 1px solid #bfdbfe;
    border-radius: 18px;
    background: linear-gradient(135deg, #f8fbff, #eef6ff);
}

.library-submit p {
    margin: 0 0 12px;
    color: var(--muted);
    font-size: 0.95rem;
}

#customStoryLanguage {
    margin-top: 8px;
}

.guide-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
}

.guide-grid article {
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: #fff;
}

.guide-grid strong {
    display: block;
    margin-bottom: 8px;
    color: var(--accent-2);
    font-size: 1.05rem;
}

.guide-grid p {
    margin: 0;
    color: var(--muted);
    line-height: 1.75;
}

.auth-mini .topbar {
    margin-bottom: 26px;
}

.auth-card {
    max-width: 980px;
    margin: 28px auto 0;
}

.auth-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.auth-box {
    padding: 20px;
    border: 1px solid var(--line);
    border-radius: 22px;
    background: #fff;
}

.auth-box h2 {
    margin-bottom: 14px;
}

.phone-row {
    display: grid;
    grid-template-columns: 96px minmax(150px, 220px);
    gap: 8px;
    width: fit-content;
    max-width: 100%;
}

.phone-row select,
.phone-row input {
    min-width: 0;
}

.dial-code-input {
    text-align: center;
    font-weight: 800;
}

.auth-page-shell {
    padding-bottom: 48px;
}

.auth-topbar {
    margin-bottom: 24px;
}

.auth-hero-panel {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(170px, 240px);
    gap: 24px;
    align-items: stretch;
    margin: 30px 0 18px;
}

.auth-hero-copy {
    min-width: 0;
    padding: clamp(18px, 3vw, 34px);
    border: 1px solid var(--line);
    border-radius: 24px;
    background:
        linear-gradient(135deg, rgba(255, 255, 255, 0.94), rgba(244, 253, 249, 0.94)),
        radial-gradient(circle at 92% 8%, rgba(255, 207, 74, 0.22), transparent 24%);
    box-shadow: var(--shadow);
}

.auth-hero-copy h1 {
    max-width: 780px;
    margin: 14px 0 12px;
    font-size: clamp(2rem, 4vw, 3.9rem);
    line-height: 1.04;
    letter-spacing: 0;
}

.auth-hero-copy p {
    max-width: 760px;
    margin-bottom: 0;
    color: var(--muted);
    font-size: 1.05rem;
    line-height: 1.8;
}

.auth-trust-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 22px;
}

.auth-trust-row span {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 7px 12px;
    border: 1px solid rgba(11, 123, 109, 0.15);
    border-radius: 999px;
    background: #fff;
    color: var(--accent-2);
    font-size: 0.86rem;
    font-weight: 800;
}

.auth-status-card {
    display: grid;
    min-width: 0;
    align-content: center;
    justify-items: center;
    gap: 10px;
    min-height: 100%;
    padding: 20px;
    border: 1px solid rgba(194, 23, 122, 0.14);
    border-radius: 24px;
    background:
        linear-gradient(160deg, #fff, #fff8fc 62%, #eefbf6);
    box-shadow: 0 18px 42px rgba(15, 45, 36, 0.1);
    text-align: center;
}

.auth-status-card img {
    width: min(112px, 70%);
    height: auto;
}

.auth-status-card strong {
    color: var(--accent);
    font-size: 1.05rem;
    line-height: 1.25;
}

.auth-status-card span {
    color: var(--muted);
    font-size: 0.88rem;
    line-height: 1.55;
}

.auth-panel {
    max-width: 980px;
    margin: 0 auto;
    padding: clamp(16px, 2vw, 22px);
}

.auth-mode-tabs {
    width: fit-content;
    margin: 0 auto 18px;
}

.auth-mode-tabs button {
    min-width: 148px;
}

.auth-panel-grid {
    display: grid;
    grid-template-columns: minmax(280px, 520px);
    gap: 16px;
    justify-content: center;
    align-items: stretch;
}

.auth-box {
    display: grid;
    align-content: start;
    gap: 12px;
    min-width: 0;
    padding: clamp(16px, 1.8vw, 22px);
    border-radius: 20px;
    box-shadow: 0 14px 28px rgba(15, 45, 36, 0.07);
}

.auth-box-featured {
    border-color: rgba(11, 123, 109, 0.18);
    background:
        linear-gradient(180deg, #fff, #f6fffb);
}

.auth-box.is-muted {
    opacity: 0.82;
}

.auth-box[hidden] {
    display: none !important;
}

.auth-box-head {
    margin-bottom: 2px;
}

.auth-box-head span {
    display: block;
    margin-bottom: 6px;
    color: var(--accent-2);
    font-size: 0.82rem;
    font-weight: 900;
    text-transform: uppercase;
}

html[lang="ar"] .auth-box-head span,
html[lang="zh"] .auth-box-head span {
    text-transform: none;
}

.auth-box-head h2 {
    margin: 0 0 8px;
    font-size: clamp(1.45rem, 2.1vw, 1.9rem);
    line-height: 1.12;
}

.auth-box-head p,
.auth-help {
    margin: 0;
    color: var(--muted);
    line-height: 1.65;
}

.auth-two-col {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

html[dir="rtl"] .auth-panel {
    direction: rtl;
}

html[dir="rtl"] .auth-panel-grid {
    direction: rtl;
}

html[dir="rtl"] .auth-box,
html[dir="rtl"] .auth-box input {
    text-align: right;
}

html[dir="rtl"] .auth-box-head p,
html[dir="rtl"] .auth-help {
    line-height: 1.8;
}

.auth-hero-copy,
.auth-status-card,
.auth-box,
.auth-trust-row span,
.auth-status-card span {
    overflow-wrap: anywhere;
}

.auth-panel .field > span {
    font-size: 0.94rem;
}

.auth-panel input,
.auth-panel select {
    min-height: 44px;
    padding: 9px 12px;
    border-radius: 12px;
    font-size: 0.95rem;
}

.auth-panel .field {
    gap: 5px;
}

.auth-panel .field > span:first-child {
    line-height: 1.25;
}

.auth-panel .primary-btn.full {
    min-height: 46px;
    margin-top: 4px;
}

.auth-switch-line {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 4px 0 0;
    color: var(--muted);
    font-weight: 700;
    line-height: 1.5;
    text-align: center;
}

.auth-switch-line button {
    border: 0;
    background: transparent;
    color: var(--accent);
    padding: 0;
    font-weight: 900;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 4px;
}

.auth-switch-line button:hover {
    color: var(--accent-2);
}

.password-field {
    position: relative;
    display: block;
    width: 100%;
}

.password-field input {
    padding-inline-end: 46px;
}

html[dir="rtl"] .password-field input {
    padding-inline-start: 46px;
    padding-inline-end: 12px;
}

.password-toggle {
    position: absolute;
    inset-block: 5px;
    inset-inline-end: 5px;
    width: 34px;
    border: 0;
    border-radius: 10px;
    background: #effaf6;
    color: var(--accent-2);
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
}

html[dir="rtl"] .password-toggle {
    inset-inline-start: 5px;
    inset-inline-end: auto;
}

.password-toggle:hover,
.password-toggle.is-visible {
    background: #dff5ee;
    color: var(--accent);
}

body[data-interface-dir="rtl"] .auth-page-shell {
    direction: rtl;
}

body[data-interface-dir="ltr"] .auth-page-shell {
    direction: ltr;
}

.clean-list {
    margin: 18px 0;
    padding-inline-start: 22px;
    color: var(--muted);
    line-height: 1.9;
}

.idea-dialog,
.story-dialog {
    width: min(1000px, 94vw);
    border: 0;
    border-radius: 26px;
    padding: 18px;
    box-shadow: var(--shadow);
}

.idea-dialog {
    width: min(980px, 94vw);
    max-height: min(720px, calc(100dvh - 28px));
    overflow: hidden;
    background:
        linear-gradient(135deg, rgba(255, 255, 255, 0.98), rgba(255, 248, 252, 0.98)),
        radial-gradient(circle at 100% 0%, rgba(255, 207, 74, 0.26), transparent 28%);
}

.story-dialog::backdrop {
    background: rgba(16, 37, 31, 0.5);
}

.idea-dialog::backdrop {
    background: rgba(16, 37, 31, 0.5);
}

.idea-head {
    padding-inline-end: 44px;
}

.idea-head h2 {
    margin-bottom: 4px;
    font-size: clamp(1.3rem, 2vw, 1.8rem);
}

.idea-head p {
    color: var(--muted);
    line-height: 1.45;
    margin: 0;
    font-size: 0.95rem;
}

.idea-layout {
    display: grid;
    grid-template-columns: minmax(220px, 0.9fr) minmax(0, 1.4fr);
    gap: 12px;
    margin-top: 10px;
    min-height: 0;
}

.idea-list {
    display: grid;
    gap: 6px;
    max-height: min(430px, calc(100dvh - 210px));
    overflow: auto;
    padding-inline-end: 4px;
}

.idea-list button {
    border: 1px solid rgba(194, 23, 122, 0.14);
    border-radius: 16px;
    background: #fff;
    color: var(--ink);
    padding: 9px 12px;
    text-align: start;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 8px 18px rgba(15, 45, 36, 0.05);
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.idea-list button:hover {
    transform: translateY(-1px);
    box-shadow: 0 12px 24px rgba(15, 45, 36, 0.1);
}

.idea-list button.active {
    border-color: rgba(194, 23, 122, 0.35);
    background: linear-gradient(135deg, #fff4fb, #fffdf2);
    color: var(--accent);
}

.idea-editor {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    gap: 10px;
    padding: 12px;
    min-height: 0;
    border: 1px solid var(--line);
    border-radius: 22px;
    background: rgba(255, 255, 255, 0.9);
}

.idea-keyword-head,
.idea-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
}

.idea-keyword-editor {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    max-height: min(260px, calc(100dvh - 342px));
    min-height: 0;
    overflow: auto;
    padding-inline-end: 4px;
}

.idea-keyword-row {
    position: relative;
    display: block;
}

.idea-keyword-editor input {
    min-height: 38px;
    padding-inline-end: 38px;
    border-radius: 14px;
    background: linear-gradient(180deg, #fff, #f8fffb);
    font-weight: 800;
}

.idea-footer {
    position: sticky;
    bottom: 0;
    z-index: 2;
    padding-top: 8px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.72), #fff);
}

.close-dialog {
    position: absolute;
    inset-block-start: 12px;
    inset-inline-end: 12px;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 999px;
    background: #fff;
    cursor: pointer;
    font-size: 1.4rem;
}

.story-dialog footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-top: 12px;
}

.toast {
    position: fixed;
    left: 50%;
    right: auto;
    bottom: max(18px, env(safe-area-inset-bottom));
    transform: translateX(-50%) translateY(18px);
    width: max-content;
    max-width: min(640px, calc(100vw - 28px));
    padding: 14px 18px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 18px;
    background: rgba(16, 37, 31, 0.96);
    color: #fff;
    font-weight: 800;
    line-height: 1.55;
    text-align: center;
    white-space: normal;
    overflow-wrap: anywhere;
    opacity: 0;
    pointer-events: none;
    transition: 0.2s ease;
    z-index: 5000;
    box-shadow: 0 18px 42px rgba(0, 0, 0, 0.18);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast[dir="rtl"] {
    text-align: right;
}

@media (max-width: 860px) {
    .topbar,
    .hero,
    .workspace,
    .form-grid {
        grid-template-columns: 1fr;
    }

    .auth-hero-panel,
    .auth-panel-grid,
    .auth-two-col {
        grid-template-columns: 1fr;
    }

    .auth-panel-grid {
        justify-content: stretch;
    }

    .auth-status-card {
        min-height: auto;
    }

    .topbar {
        gap: 10px;
        padding: 12px 14px;
    }

    body.writing-mode .workspace {
        grid-template-columns: 1fr;
        width: 100%;
        padding: 12px;
    }

    body.writing-mode .writer {
        order: 1;
    }

    body.writing-mode .steps {
        order: 2;
    }

    .brand img {
        width: 104px;
    }

    .keyword-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .story-page {
        grid-template-rows: minmax(0, 1fr) auto;
        padding: 16px;
    }

    .image-zone,
    .text-zone p {
        width: 100%;
    }

    .guide-grid {
        grid-template-columns: 1fr;
    }

    .idea-layout {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .idea-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        max-height: 150px;
    }

    .idea-keyword-editor {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        max-height: min(190px, calc(100dvh - 410px));
    }
}

@media (max-width: 620px) {
    .auth-page-shell {
        width: min(1180px, calc(100% - 14px));
    }

    .auth-hero-panel {
        margin-top: 16px;
        gap: 12px;
    }

    .auth-topbar {
        grid-template-columns: 1fr;
        justify-items: center;
        gap: 8px;
        padding-inline: 10px;
    }

    .auth-topbar .language-switch,
    .auth-topbar .top-actions {
        justify-content: center;
        width: 100%;
    }

    .auth-topbar .top-actions a {
        padding: 8px 12px;
    }

    .auth-hero-copy,
    .auth-panel,
    .auth-box {
        border-radius: 18px;
    }

    .auth-hero-copy h1 {
        font-size: clamp(1.85rem, 10vw, 2.65rem);
    }

    .auth-mode-tabs {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        width: 100%;
    }

    .auth-mode-tabs button {
        width: 100%;
        min-width: 0;
        padding-inline: 8px;
        white-space: normal;
    }

    .phone-row {
        grid-template-columns: 1fr;
    }

    .idea-dialog {
        width: min(96vw, 460px);
        max-height: calc(100dvh - 18px);
        padding: 12px;
        border-radius: 20px;
    }

    .idea-head h2 {
        font-size: 1.2rem;
    }

    .idea-head p {
        font-size: 0.82rem;
        line-height: 1.35;
    }

    .idea-layout {
        margin-top: 8px;
    }

    .idea-list {
        grid-template-columns: 1fr;
        max-height: 112px;
    }

    .idea-list button {
        padding: 8px 10px;
        border-radius: 13px;
        font-size: 0.86rem;
    }

    .idea-editor {
        gap: 8px;
        padding: 10px;
        border-radius: 16px;
    }

    .idea-keyword-head {
        gap: 8px;
    }

    .idea-keyword-editor {
        grid-template-columns: 1fr 1fr;
        gap: 6px;
        max-height: 150px;
    }

    .idea-keyword-editor input {
        min-height: 34px;
        padding: 6px 34px 6px 10px;
        font-size: 0.84rem;
        border-radius: 12px;
    }

    .idea-footer {
        flex-wrap: nowrap;
    }

    .idea-footer .primary-btn,
    .idea-footer .secondary-btn {
        min-height: 38px;
        padding: 8px 12px;
        font-size: 0.86rem;
    }
}

@media (orientation: portrait) and (max-width: 1180px) {
    body.writing-mode .kids-app {
        width: 100%;
        padding-inline: 8px;
    }

    body.writing-mode .workspace {
        grid-template-columns: 1fr;
        width: min(100%, 980px);
        margin: 6px auto 0;
        padding: 10px;
        gap: 10px;
    }

    body.writing-mode .writer {
        order: 1;
        overflow: visible;
    }

    body.writing-mode .steps {
        order: 2;
        width: min(100%, 780px);
        margin-inline: auto;
        padding-top: 0;
    }

    body.writing-mode .story-page,
    body.writing-mode .write-card {
        width: min(100%, calc(177.78dvh - 409px), 900px);
        max-width: calc(100vw - 28px);
    }

    body.writing-mode .story-page {
        padding: 12px;
    }

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

    body.writing-mode .page-keyword-cloud {
        justify-content: center;
        max-height: 82px;
        padding-bottom: 8px;
        margin-bottom: 8px;
    }

    body.writing-mode .page-keyword-cloud button {
        padding: 7px 11px;
        font-size: 0.88rem;
    }

    body.writing-mode .feedback {
        margin: 5px 0 8px;
        text-align: center;
        font-size: 0.84rem;
    }

    body.writing-mode .story-nudge {
        inset-block-start: 40px;
        width: min(calc(100% - 32px), 620px);
        padding: 6px 10px;
        font-size: clamp(0.78rem, 1.9vw, 0.94rem);
    }

    body.writing-mode .side-title {
        font-size: clamp(0.78rem, 1.8vw, 0.96rem);
        max-width: calc(100% - 98px);
    }

    body.writing-mode .image-zone {
        min-height: clamp(96px, 18dvh, 170px);
        margin-top: 12px;
        margin-bottom: 4px;
    }

    body.writing-mode .text-zone {
        padding: 4px 12px 10px;
    }

    body.writing-mode .text-zone p {
        min-height: clamp(54px, 8dvh, 82px);
        font-size: clamp(0.82rem, 1.7vw, 1.05rem);
        line-height: 1.32;
        padding: 9px 14px;
    }

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

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

@media (orientation: portrait) and (max-width: 700px) {
    .kids-app {
        width: 100%;
    }

    .auth-page-shell {
        width: 100%;
        max-width: 100vw;
        overflow: hidden;
    }

    .auth-page-shell .auth-topbar {
        grid-template-columns: auto auto minmax(0, 1fr);
        width: 100%;
        overflow: hidden;
    }

    .auth-page-shell .top-actions {
        justify-content: flex-end;
        overflow: hidden;
    }

    .auth-page-shell .top-actions a {
        min-width: 0;
        padding-inline: 9px;
        white-space: nowrap;
    }

    .auth-hero-panel,
    .auth-panel {
        width: calc(100vw - 20px);
        max-width: calc(100vw - 20px);
        margin-inline: auto;
    }

    .auth-hero-copy,
    .auth-status-card,
    .auth-box {
        width: 100%;
        max-width: 100%;
        overflow: hidden;
    }

    .auth-trust-row {
        display: grid;
        grid-template-columns: 1fr;
    }

    .auth-trust-row span {
        width: 100%;
        justify-content: center;
        text-align: center;
    }

    .auth-page-shell .auth-topbar {
        grid-template-columns: 1fr;
        justify-items: center;
        gap: 8px;
        padding: 10px 12px 12px;
        overflow: visible;
    }

    .auth-page-shell .brand img {
        width: 102px;
    }

    .auth-page-shell .language-switch,
    .auth-page-shell .top-actions {
        width: 100%;
        justify-content: center;
        flex-wrap: wrap;
        overflow: visible;
    }

    .auth-page-shell .top-actions a {
        flex: 1 1 auto;
        min-width: min(108px, 31%);
        max-width: 148px;
        padding-inline: 8px;
        white-space: normal;
        text-align: center;
    }

    .auth-hero-copy h1 {
        font-size: clamp(2rem, 9vw, 2.55rem);
        line-height: 1.1;
        overflow-wrap: normal;
        word-break: normal;
    }

    .auth-hero-copy p {
        font-size: 0.98rem;
        line-height: 1.65;
    }

    .thin-ad {
        width: min(600px, 100%);
        border-radius: 0;
        border-inline: 0;
    }

    .thin-ad img {
        height: 94px;
    }

    .topbar {
        width: 100%;
        grid-template-columns: auto auto 1fr;
        align-items: center;
        gap: 7px;
        padding: 8px 10px 10px;
        border-radius: 0 0 22px 22px;
        border-inline: 0;
        border-top: 0;
        box-shadow: 0 12px 28px rgba(15, 45, 36, 0.1);
    }

    .brand img {
        width: 82px;
    }

    .language-switch {
        justify-content: center;
        gap: 5px;
        flex-wrap: nowrap;
    }

    .language-field {
        min-height: 38px;
        padding: 5px 8px;
    }

    .language-field span {
        display: none;
    }

    .language-field select {
        min-width: 116px;
        padding-block: 5px;
        font-size: 0.92rem;
    }

    .top-actions {
        justify-content: flex-end;
        gap: 5px;
        flex-wrap: nowrap;
        min-width: 0;
    }

    .language-switch button,
    .top-actions a {
        padding: 7px 10px;
        min-height: 36px;
        font-size: 0.86rem;
        line-height: 1;
    }

    .language-switch button {
        width: 36px;
        min-width: 36px;
        height: 24px;
        padding: 0;
        border: 0;
        border-radius: 0;
        background: transparent;
        box-shadow: none;
    }

    .flag-btn img {
        width: 36px;
        height: 24px;
    }

.top-actions a {
        min-width: 48px;
        text-align: center;
    }

    .side-ad-grid {
        width: min(300px, calc(100% - 24px));
        margin: 14px auto 12px;
    }

    .side-ad-card {
        min-height: 84px;
    }

    .primary-btn.full {
        margin-top: 6px;
    }

    .writer {
        overflow-x: auto;
    }

    .story-page {
        width: min(100vw - 24px, 680px);
        min-width: 520px;
        margin-inline: auto;
        aspect-ratio: 16 / 9;
        max-height: none;
    }

    .image-zone {
        min-height: 96px;
        margin-top: 22px;
        padding: 8px;
    }

    .text-zone {
        padding: 6px 18px 14px;
    }

    .text-zone p {
        min-height: 72px;
        max-height: 108px;
        font-size: clamp(1rem, 4vw, 1.45rem);
        padding: 12px 16px;
    }

    .idea-dialog {
        width: calc(100vw - 18px);
        max-height: calc(100dvh - 24px);
        padding: 14px;
    }

    .idea-list {
        grid-template-columns: 1fr;
    }
}

body.writing-mode .text-zone p {
    width: min(88%, 860px);
    min-height: 86px;
    max-height: none;
    overflow: visible;
    resize: none;
    font-size: clamp(0.9rem, 1.25vw, 1.22rem);
    line-height: 1.38;
}

@media (orientation: portrait) and (max-width: 700px) {
    body.writing-mode .thin-ad img {
        height: 54px;
    }

    body.writing-mode .writer {
        overflow: visible;
    }

    body.writing-mode .workspace {
        gap: 10px;
        padding: 8px;
    }

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

    body.writing-mode .page-list {
        margin: 10px 0;
    }

    body.writing-mode .story-page {
        width: min(100%, calc(100vw - 24px));
        min-width: 0;
        padding: 10px;
    }

    body.writing-mode .write-card {
        width: min(100%, calc(100vw - 24px));
        max-width: calc(100vw - 24px);
    }

    body.writing-mode .story-nudge {
        inset-block-start: 38px;
        width: calc(100% - 28px);
        font-size: 0.74rem;
        padding: 5px 8px;
    }

    body.writing-mode .side-title {
        font-size: 0.78rem;
        max-width: calc(100% - 84px);
        padding: 4px 8px;
    }

    body.writing-mode .image-zone {
        min-height: 104px;
        margin-top: 12px;
    }

    body.writing-mode .text-zone p {
        min-height: 62px;
        padding: 10px 14px;
        font-size: 0.88rem;
    }

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

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

    body.writing-mode .page-keyword-cloud button {
        padding: 7px 10px;
        font-size: 0.86rem;
    }
}

/* Writing workspace stabilizer.
   Keep the story canvas inside the viewport in every orientation and stop
   older breakpoint rules from pushing the work area sideways. */
body.writing-mode {
    --kids-side-w: clamp(220px, 22vw, 300px);
    --kids-canvas-w: min(820px, calc(177.78dvh - 245px), calc(100vw - var(--kids-side-w) - 44px));
    max-width: 100vw;
    overflow-x: hidden;
}

body.writing-mode .workspace {
    display: grid;
    grid-template-columns: minmax(0, var(--kids-canvas-w)) var(--kids-side-w);
    grid-template-areas: "writer steps";
    justify-content: center;
    align-items: start;
    gap: 10px;
    width: min(100%, calc(100vw - 16px));
    max-width: 1160px;
    margin: 6px auto 0;
    padding: 8px;
    overflow: hidden;
    overflow-x: clip;
    direction: ltr;
}

body.writing-mode .writer {
    grid-area: writer;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow: visible;
    direction: ltr;
}

body.writing-mode .steps {
    grid-area: steps;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    max-height: calc(100dvh - 98px);
    overflow: auto;
}

html[dir="rtl"] body.writing-mode .writer,
html[dir="rtl"] body.writing-mode .steps {
    direction: rtl;
}

html:not([dir="rtl"]) body.writing-mode .writer,
html:not([dir="rtl"]) body.writing-mode .steps {
    direction: ltr;
}

body.writing-mode .story-page,
body.writing-mode .write-card {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    contain: inline-size;
}

body.writing-mode .story-page {
    grid-template-rows: minmax(0, 1fr) auto;
    aspect-ratio: 16 / 9;
    max-height: none;
    padding: clamp(8px, 1.15vw, 14px);
}

body.writing-mode .writer-toolbar {
    margin-bottom: 6px;
}

body.writing-mode .image-zone {
    min-height: clamp(92px, 18dvh, 160px);
    margin: clamp(10px, 2dvh, 18px) auto 4px;
    padding: clamp(6px, 1vw, 10px);
}

body.writing-mode .text-zone {
    padding: 4px clamp(8px, 2vw, 18px) 8px;
}

body.writing-mode .text-zone p {
    width: min(88%, 720px);
    min-height: clamp(46px, 8dvh, 74px);
    padding: clamp(8px, 1.3vw, 12px) clamp(10px, 1.8vw, 16px);
    font-size: clamp(0.78rem, 1.45vw, 1.02rem);
    line-height: 1.32;
}

body.writing-mode .write-card {
    margin-top: 8px;
    padding: clamp(6px, 1vw, 10px);
    overflow: hidden;
}

body.writing-mode .page-keyword-cloud {
    justify-content: center;
    max-height: clamp(42px, 11dvh, 82px);
    gap: 5px;
    margin-bottom: 6px;
    padding-bottom: 6px;
}

body.writing-mode .page-keyword-cloud button {
    padding: 6px 10px;
    font-size: clamp(0.72rem, 1.1vw, 0.88rem);
}

body.writing-mode .story-nudge {
    position: static;
    display: none;
    width: 100%;
    margin: 0 auto 6px;
    padding: 6px 12px;
    transform: none;
    opacity: 1;
    z-index: auto;
    direction: inherit;
    unicode-bidi: plaintext;
    background: rgba(255, 255, 255, 0.78);
    border-color: rgba(11, 123, 109, 0.12);
}

body.writing-mode .story-nudge.show,
html[dir="rtl"] body.writing-mode .story-nudge.show {
    display: block;
    transform: none;
}

body.writing-mode .feedback {
    min-height: 20px;
    margin: 3px 0 6px;
    text-align: center;
    font-size: clamp(0.72rem, 1vw, 0.84rem);
}

body.writing-mode .next-writing-btn {
    min-height: 34px;
    padding: 7px 12px;
    font-size: clamp(0.76rem, 1.1vw, 0.9rem);
}

@media (orientation: portrait) {
    html,
    body.writing-mode {
        max-width: 100vw;
        overflow-x: hidden;
    }

    body.writing-mode {
        --kids-canvas-w: min(760px, calc(100vw - 18px));
    }

    body.writing-mode .workspace {
        grid-template-columns: minmax(0, var(--kids-canvas-w));
        grid-template-areas:
            "writer"
            "steps";
        width: 100%;
        max-width: 100%;
        padding: 8px 0 14px;
        gap: 8px;
        overflow: visible;
    }

    body.writing-mode .writer {
        order: 1;
    }

    body.writing-mode .steps {
        order: 2;
        max-height: none;
        overflow: visible;
    }

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

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

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

@media (orientation: landscape) and (max-height: 560px) {
    body.writing-mode {
        --kids-side-w: clamp(190px, 24vw, 240px);
        --kids-canvas-w: min(700px, calc(177.78dvh - 178px), calc(100vw - var(--kids-side-w) - 30px));
    }

    body.writing-mode .kids-app {
        padding-inline: 4px;
        padding-bottom: 6px;
    }

    body.writing-mode .thin-ad {
        width: min(420px, calc(100% - 18px));
        margin-top: 8px;
    }

    body.writing-mode .thin-ad img {
        height: 42px;
    }

    body.writing-mode .workspace {
        width: min(100%, calc(100vw - 8px));
        gap: 6px;
        margin-top: 3px;
        padding: 5px;
    }

    body.writing-mode .writer-toolbar {
        position: absolute;
        z-index: 14;
        inset: 8px 8px auto 8px;
        margin: 0;
        pointer-events: none;
    }

    body.writing-mode .writer-toolbar .ghost-btn,
    body.writing-mode .writer-toolbar strong {
        min-height: 30px;
        padding: 5px 8px;
        font-size: 0.7rem;
        pointer-events: auto;
    }

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

    body.writing-mode .side-title {
        inset-block-start: 10px;
        font-size: 0.68rem;
        padding: 3px 7px;
    }

    body.writing-mode .image-zone {
        min-height: 72px;
        margin-top: 8px;
        padding: 4px;
    }

    body.writing-mode .text-zone {
        padding: 2px 8px 6px;
    }

    body.writing-mode .text-zone span {
        min-width: 26px;
        min-height: 26px;
        font-size: 0.7rem;
    }

    body.writing-mode .text-zone p {
        min-height: 42px;
        padding: 6px 10px;
        font-size: 0.72rem;
        border-radius: 14px;
    }

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

    body.writing-mode .page-keyword-cloud {
        max-height: 38px;
        overflow: hidden;
        gap: 4px;
        margin-bottom: 4px;
        padding-bottom: 4px;
    }

    body.writing-mode .page-keyword-cloud button {
        padding: 4px 7px;
        font-size: 0.66rem;
    }

    body.writing-mode .feedback {
        display: none;
    }

    body.writing-mode .next-writing-btn {
        min-height: 28px;
        padding: 5px 10px;
        font-size: 0.68rem;
    }

    body.writing-mode .steps {
        max-height: calc(100dvh - 46px);
        overflow: auto;
        padding: 8px;
    }

    body.writing-mode .small-keywords,
    body.writing-mode .rules,
    body.writing-mode .side-ad-grid {
        display: none;
    }

    body.writing-mode .page-list {
        gap: 5px;
        margin: 6px 0;
    }
}

/* UX lock v1: keep the child writing flow focused and viewport-led. */
body.writing-mode {
    --kids-side-w: clamp(176px, 21vw, 270px);
    --kids-canvas-w: min(820px, calc(100vw - var(--kids-side-w) - 32px), calc(177.78dvh - 250px));
    background: #eaf4f1;
}

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

body.writing-mode .workspace {
    display: grid;
    grid-template-columns: minmax(0, var(--kids-canvas-w)) minmax(168px, var(--kids-side-w));
    grid-template-areas: "writer steps";
    align-items: start;
    justify-content: center;
    gap: 8px;
    width: min(100%, calc(100vw - 12px));
    max-width: none;
    margin: 6px auto 0;
    padding: 8px;
    overflow: visible;
    border-radius: 22px;
    background: rgba(255, 255, 255, 0.74);
    box-shadow: 0 18px 48px rgba(15, 45, 36, 0.08);
}

body.writing-mode .writer {
    display: grid;
    grid-template-rows: auto auto auto;
    gap: 6px;
    min-width: 0;
}

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-page {
    width: 100%;
    aspect-ratio: 16 / 9;
    min-height: 0;
    max-height: none;
    overflow: hidden;
}

body.writing-mode .image-zone {
    width: min(82%, 720px);
    min-height: 0;
    margin: clamp(12px, 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 .text-zone {
    padding: 3px clamp(8px, 1.8vw, 16px) 8px;
}

body.writing-mode .text-zone span {
    min-width: clamp(26px, 3vw, 38px);
    min-height: clamp(26px, 3vw, 38px);
    font-size: clamp(0.72rem, 1vw, 0.95rem);
}

body.writing-mode .text-zone p {
    width: min(86%, 690px);
    min-height: clamp(42px, 8dvh, 72px);
    padding: clamp(8px, 1.25vw, 12px) clamp(10px, 1.8vw, 16px);
    border-radius: clamp(16px, 2vw, 24px);
    font-size: clamp(0.76rem, 1.3vw, 1rem);
    line-height: 1.32;
}

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

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

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

body.writing-mode .story-nudge {
    position: static !important;
    width: min(100%, 720px);
    margin: 0 auto 5px;
    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;
}

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

body.writing-mode .steps {
    display: grid;
    gap: 8px;
    max-height: calc(100dvh - 18px);
    overflow: auto;
    padding: 10px;
    border: 1px solid rgba(11, 123, 109, 0.12);
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.9);
}

body.writing-mode .small-keywords,
body.writing-mode .rules,
body.writing-mode .side-ad-grid {
    display: none !important;
}

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

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

body.writing-mode .page-list button {
    min-height: clamp(40px, 8dvh, 62px);
    border-radius: 14px;
}

body.writing-mode .page-list button.has-thumb {
    min-height: clamp(54px, 10dvh, 74px);
}

body.writing-mode .thin-ad {
    width: min(560px, calc(100% - 18px));
    margin-top: 10px;
}

body.writing-mode .thin-ad img {
    height: clamp(42px, 6vw, 76px);
}

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

    body.writing-mode .workspace {
        grid-template-columns: minmax(0, var(--kids-canvas-w));
        grid-template-areas:
            "writer"
            "steps";
        width: min(100%, calc(100vw - 8px));
        padding: 6px;
        gap: 8px;
    }

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

    body.writing-mode .steps {
        max-height: none;
        overflow: visible;
    }

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

@media (orientation: portrait) and (max-width: 520px) {
    body.writing-mode .kids-app {
        padding-inline: 4px;
    }

    body.writing-mode .workspace {
        border-radius: 16px;
        padding: 5px;
    }

    body.writing-mode .story-page {
        padding: 8px;
        border-radius: 16px;
    }

    body.writing-mode .side-title {
        inset-block-start: 9px;
        max-width: calc(100% - 92px);
        padding: 3px 7px;
        font-size: 0.72rem;
    }

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

    body.writing-mode .text-zone p {
        width: min(88%, 330px);
        min-height: 52px;
        font-size: 0.8rem;
    }

    body.writing-mode .page-keyword-cloud {
        max-height: 88px;
    }

    body.writing-mode .steps h2,
    body.writing-mode .thin-ad {
        display: none;
    }
}

@media (orientation: landscape) and (max-height: 560px) {
    body.writing-mode {
        --kids-side-w: clamp(164px, 22vw, 230px);
        --kids-canvas-w: min(680px, calc(100vw - var(--kids-side-w) - 26px), calc(177.78dvh - 250px));
    }

    body.writing-mode .workspace {
        gap: 6px;
        padding: 5px;
        margin-top: 3px;
        border-radius: 16px;
    }

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

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

    body.writing-mode .side-title {
        inset-block-start: 8px;
        font-size: 0.66rem;
        padding: 3px 7px;
    }

    body.writing-mode .image-zone {
        margin-top: 10px;
    }

    body.writing-mode .text-zone p {
        min-height: 40px;
        font-size: 0.7rem;
        padding: 5px 9px;
    }

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

    body.writing-mode .page-keyword-cloud {
        max-height: 34px;
        overflow: hidden;
        gap: 4px;
    }

    body.writing-mode .feedback {
        display: none;
    }

    body.writing-mode .next-writing-btn {
        min-height: 28px;
        padding: 5px 10px;
        font-size: 0.68rem;
    }

    body.writing-mode .steps {
        max-height: calc(100dvh - 12px);
        padding: 7px;
    }

    body.writing-mode .steps h2 {
        display: none;
    }

    body.writing-mode .page-list button {
        min-height: 34px;
    }

    body.writing-mode .thin-ad {
        width: min(360px, calc(100% - 18px));
        margin-top: 8px;
    }

    body.writing-mode .thin-ad img {
        height: 36px;
    }
}

/* UX lock v2: final viewport fitting layer for the child writing screen. */
html:has(body.writing-mode),
body.writing-mode {
    overflow-x: clip;
}

body.writing-mode .workspace,
body.writing-mode .writer,
body.writing-mode .steps,
body.writing-mode .story-page,
body.writing-mode .write-card {
    box-sizing: border-box;
}

body.writing-mode .story-page {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    background: linear-gradient(180deg, #fffdf8 0%, #fff9ef 100%);
    border: 1px solid rgba(11, 123, 109, 0.13);
    box-shadow:
        inset 0 0 0 1px rgba(255, 255, 255, 0.72),
        0 12px 28px rgba(15, 45, 36, 0.08);
}

body.writing-mode .image-zone {
    align-self: stretch;
    height: 100%;
}

body.writing-mode .image-zone img {
    max-height: none;
}

body.writing-mode .story-page.has-image .image-zone img {
    display: block;
    width: 100%;
    height: 100%;
    min-height: 0;
    object-fit: contain;
    object-position: center;
}

body.writing-mode .image-zone.is-loading {
    border-radius: 24px;
    background: rgba(255, 255, 255, 0.34);
}

body.writing-mode .next-writing-btn.is-processing {
    opacity: 0.92;
    cursor: wait;
}

body.writing-mode .next-writing-btn {
    background: linear-gradient(135deg, #c2177a, #6f55e8);
    color: #fff;
    box-shadow: 0 12px 28px rgba(194, 23, 122, 0.22);
    transition: transform 0.16s ease, box-shadow 0.16s ease, filter 0.16s ease;
}

body.writing-mode .next-writing-btn:hover {
    transform: translateY(-1px);
    filter: saturate(1.05);
}

body.writing-mode .next-writing-btn:active {
    transform: translateY(1px) scale(0.99);
    box-shadow: 0 8px 18px rgba(194, 23, 122, 0.18);
}

body.writing-mode .next-writing-btn.is-processing::before {
    content: "";
    width: 18px;
    height: 18px;
    border-radius: 999px;
    border: 3px solid rgba(255, 255, 255, 0.45);
    border-top-color: #fff;
    animation: kidsSpin 1s linear infinite;
}

body.writing-mode .page-keyword-cloud button {
    min-height: 32px;
    border-width: 2px;
    background: linear-gradient(180deg, #fff, #fff8fc);
    border-color: rgba(194, 23, 122, 0.22);
    color: var(--accent);
    font-weight: 900;
    box-shadow: 0 7px 14px rgba(15, 45, 36, 0.05);
}

body.writing-mode .page-keyword-cloud button.in-page {
    background: linear-gradient(180deg, #e5fff4, #f7fffb);
    border-color: rgba(11, 123, 109, 0.36);
    color: var(--accent-2);
    box-shadow: 0 0 0 3px rgba(11, 123, 109, 0.08);
}

body.writing-mode .page-keyword-cloud button.used {
    background: #f2fbf7;
    color: #668278;
}

body.writing-mode .text-zone p:focus {
    outline: 3px solid rgba(194, 23, 122, 0.18);
    border-color: rgba(194, 23, 122, 0.42);
    background: rgba(255, 255, 255, 0.92);
}

body.writing-mode .page-list button.done span::after {
    content: "✓";
    display: inline-grid;
    place-items: center;
    width: 16px;
    height: 16px;
    margin-inline-start: 6px;
    border-radius: 999px;
    background: rgba(11, 123, 109, 0.12);
    color: var(--accent-2);
    font-size: 0.72rem;
}

body.writing-mode .page-keyword-cloud button.missing {
    box-shadow: 0 0 0 3px rgba(194, 23, 122, 0.06);
}

body.writing-mode .feedback.warn {
    color: #b65600;
}

body.writing-mode .feedback.ok {
    color: var(--accent-2);
}

@media (min-width: 900px) and (orientation: landscape) {
    body.writing-mode {
        --kids-side-w: clamp(190px, 20vw, 260px);
        --kids-canvas-w: min(820px, calc(100vw - var(--kids-side-w) - 34px), calc(177.78dvh - 156px));
    }

    body.writing-mode .workspace {
        min-height: auto;
    }
}

@media (max-width: 899px) and (orientation: landscape) {
    body.writing-mode {
        --kids-side-w: clamp(148px, 23vw, 210px);
        --kids-canvas-w: min(calc(100vw - var(--kids-side-w) - 20px), calc(177.78dvh - 199px));
    }

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

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

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

    body.writing-mode .page-list button {
        min-height: 32px;
        border-radius: 12px;
    }
}

@media (orientation: portrait) {
    body.writing-mode .workspace {
        box-shadow: none;
        background: transparent;
    }

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

    body.writing-mode .steps {
        border-radius: 20px;
    }
}

@media (max-width: 620px) and (orientation: portrait) {
    body.writing-mode {
        --kids-canvas-w: calc(100vw - 10px);
    }

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

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

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

    body.writing-mode .story-page {
        min-height: auto;
        aspect-ratio: 16 / 9;
        padding: 7px;
        border-radius: 14px;
    }

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

    body.writing-mode .text-zone {
        padding-bottom: 6px;
    }

    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);
        line-height: 1.3;
    }

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

    body.writing-mode .page-keyword-cloud {
        max-height: 72px;
        overflow: auto;
        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;
        border-radius: 999px;
        font-size: 0.82rem;
    }

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

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

@media print {
    .topbar,
    .thin-ad,
    .hero,
    .setup-panel,
    .steps,
    .write-card,
    .writer-toolbar,
    .mic-record-btn,
    .regenerate-image-btn,
    .export-panel {
        display: none !important;
    }

    .kids-app,
    .workspace {
        width: 100%;
        margin: 0;
        padding: 0;
        box-shadow: none;
        border: 0;
        display: block;
    }

    .story-page {
        page-break-after: always;
        border-radius: 0;
        min-height: 100vh;
    }
}

body.writing-mode .text-zone .in-text-mic {
    inset-inline-end: 16px;
}

body.writing-mode .text-zone p {
    padding-inline-end: 64px;
}

html[dir="rtl"] body.writing-mode .text-zone p {
    padding-inline-start: 64px;
    padding-inline-end: 14px;
}

@media (max-width: 720px) {
    body.writing-mode .text-zone .in-text-mic {
        width: 36px;
        min-width: 36px;
        height: 36px;
        inset-inline-end: 10px;
    }

    body.writing-mode .text-zone p {
        padding-inline-end: 48px;
    }

    html[dir="rtl"] body.writing-mode .text-zone p {
        padding-inline-start: 48px;
        padding-inline-end: 11px;
    }
}

/* Final writing-surface alignment overrides. Keep the microphone inside the text cell
   so the sentence box stays visually centered on the page. */
body.writing-mode .text-zone {
    justify-items: center;
}

body.writing-mode .text-zone p {
    width: min(86%, 820px);
    padding-inline-end: 70px;
    padding-inline-start: 20px;
    font-weight: 650;
}

html[dir="rtl"] body.writing-mode .text-zone p {
    padding-inline-start: 70px;
    padding-inline-end: 20px;
}

body.writing-mode .text-zone .in-text-mic {
    width: 44px;
    min-width: 44px;
    height: 44px;
    font-size: 1.05rem;
    inset-inline-end: calc(7% + 14px);
}

html[dir="rtl"] body.writing-mode .text-zone .in-text-mic {
    inset-inline-start: calc(7% + 14px);
    inset-inline-end: auto;
}

body.writing-mode .stop-voice-btn {
    min-height: 52px;
    font-size: clamp(0.82rem, 0.95vw, 0.96rem);
    font-weight: 700;
    white-space: nowrap;
}

@media (max-width: 720px) {
    body.writing-mode .text-zone p {
        width: min(88%, 720px);
        padding-inline-end: 58px;
        padding-inline-start: 14px;
        font-size: clamp(0.86rem, 3.2vw, 1rem);
    }

    html[dir="rtl"] body.writing-mode .text-zone p {
        padding-inline-start: 58px;
        padding-inline-end: 14px;
    }

    body.writing-mode .text-zone .in-text-mic {
        width: 38px;
        min-width: 38px;
        height: 38px;
        inset-inline-end: calc(6% + 10px);
    }

    html[dir="rtl"] body.writing-mode .text-zone .in-text-mic {
        inset-inline-start: calc(6% + 10px);
        inset-inline-end: auto;
    }

    body.writing-mode .stop-voice-btn {
        min-height: 46px;
        font-size: 0.86rem;
    }
}

@media print {
    .mic-record-btn,
    .regenerate-image-btn {
        display: none !important;
    }
}

body.writing-mode .text-zone {
    justify-items: center;
}

body.writing-mode .text-zone .in-text-mic {
    width: 56px;
    min-width: 56px;
    height: 56px;
    font-size: 1.25rem;
    inset-inline-end: 18px;
}

body.writing-mode .text-zone p {
    padding-inline-end: 86px;
}

html[dir="rtl"] body.writing-mode .text-zone p {
    padding-inline-start: 86px;
    padding-inline-end: 16px;
}

body.writing-mode .writing-action-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    align-items: stretch;
}

body.writing-mode .next-writing-btn {
    grid-column: 1 / -1;
}

body.writing-mode .writing-status-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 6px 0 10px;
    padding-inline: 4px;
}

body.writing-mode .writing-status-row .feedback {
    flex: 1;
    min-height: 24px;
    margin: 0;
}

body.writing-mode .undo-text-btn {
    width: auto;
    flex: 0 0 auto;
    min-width: 104px;
    min-height: 34px;
    padding: 6px 14px;
    border: 0;
    border-radius: 8px;
    background: #1f1f1f;
    color: #fff;
    font-size: 0.82rem;
    font-weight: 700;
    box-shadow: 0 10px 20px rgba(16, 37, 31, 0.14);
}

body.writing-mode .undo-text-btn:disabled {
    background: #3b3b3b;
    color: rgba(255, 255, 255, 0.72);
    opacity: 0.52;
}

body.writing-mode .regenerate-image-btn,
body.writing-mode .stop-voice-btn {
    width: 100%;
    min-height: 56px;
    border-radius: 999px;
    font-size: 1rem;
    font-weight: 800;
}

body.writing-mode .writing-action-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

body.writing-mode .writing-action-row .next-writing-btn {
    flex: 1 0 100%;
}

body.writing-mode .writing-action-row .regenerate-image-btn,
body.writing-mode .writing-action-row .stop-voice-btn {
    width: auto;
    min-width: 132px;
    max-width: 190px;
    min-height: 42px;
    padding: 8px 16px;
    border-radius: 999px;
    font-size: 0.88rem;
    line-height: 1.2;
    white-space: nowrap;
    box-shadow: 0 10px 22px rgba(15, 45, 36, 0.08);
}

body.writing-mode .writing-action-row .stop-voice-btn {
    min-width: 106px;
}

body.writing-mode .writing-action-row .stop-voice-btn::before {
    width: 8px;
    height: 8px;
    box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 12%, transparent);
}

body.writing-mode .page-list button.is-dragging {
    opacity: 0.48;
    transform: scale(0.96);
}

body.writing-mode .page-list button.is-drop-target {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(194, 23, 122, 0.14);
}

.inspiration-overlay {
    position: fixed;
    inset: 0;
    z-index: 1700;
    display: grid;
    place-items: center;
    padding: 24px;
    background: rgba(7, 25, 21, 0.18);
    backdrop-filter: blur(5px);
}

.inspiration-overlay[hidden] {
    display: none !important;
}

.inspiration-card {
    width: min(560px, calc(100vw - 32px));
    border: 1px solid var(--line);
    border-radius: 28px;
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 24px 70px rgba(15, 40, 36, 0.18);
    padding: 22px 24px;
    text-align: center;
    color: var(--ink);
    animation: inspirationFloat 0.28s ease both;
}

.inspiration-kicker {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 30px;
    padding: 5px 14px;
    border-radius: 999px;
    background: rgba(194, 23, 122, 0.09);
    color: var(--accent);
    font-size: 0.84rem;
    font-weight: 800;
    margin-bottom: 12px;
}

.inspiration-card p {
    margin: 0;
    white-space: pre-line;
    font-size: clamp(1rem, 2.4vw, 1.18rem);
    line-height: 1.75;
    font-weight: 650;
}

.primary-btn.is-processing {
    cursor: wait;
    opacity: 0.76;
}

@keyframes inspirationFloat {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@media (max-width: 720px) {
    body.writing-mode .text-zone .in-text-mic {
        width: 46px;
        min-width: 46px;
        height: 46px;
        inset-inline-end: 12px;
        font-size: 1.08rem;
    }

    body.writing-mode .text-zone p {
        padding-inline-end: 64px;
    }

    html[dir="rtl"] body.writing-mode .text-zone p {
        padding-inline-start: 64px;
        padding-inline-end: 12px;
    }

    body.writing-mode .writing-action-row {
        gap: 9px;
    }

    body.writing-mode .regenerate-image-btn,
    body.writing-mode .stop-voice-btn {
        min-height: 48px;
        font-size: 0.9rem;
    }

    body.writing-mode .writing-status-row {
        gap: 8px;
        margin-top: 5px;
    }

    body.writing-mode .undo-text-btn {
        min-width: 94px;
        min-height: 32px;
        padding: 6px 14px;
        font-size: 0.78rem;
    }
}
@media (max-width: 700px) {
    .auth-page-shell .auth-topbar {
        display: grid;
        grid-template-columns: 1fr !important;
        justify-items: center;
        max-width: 100vw;
    }

    .auth-page-shell .top-actions {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
        width: 100%;
        max-width: calc(100vw - 28px);
    }

    .auth-page-shell .top-actions a {
        display: inline-flex;
        justify-content: center;
        min-width: 0;
        max-width: none;
        padding: 8px 6px;
        font-size: 0.82rem;
        white-space: normal;
    }

    .auth-page-shell .auth-hero-copy h1 {
        max-width: 100%;
        font-size: clamp(1.75rem, 8vw, 2.18rem);
        line-height: 1.12;
    }

    .auth-page-shell .auth-hero-copy {
        padding-inline: 18px;
    }
}
