*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

        :root {
            --cream:        #F8F5EF;
            --cream-dark:   #EDE8DE;
            --white:        #FFFFFF;
            --ink:          #1A1714;
            --ink-mid:      #4A4540;
            --ink-faint:    #9C958D;

            --terra:        #C4614A;
            --terra-light:  #F9EDE9;
            --terra-mid:    #E0967F;
            --terra-dark:   #A04A36;
            --terra-hover:  #b05540;

            --blue:         #2347D9;
            --blue-light:   #EEF2FF;

            --green:        #0DBF7A;
            --red:          #DC2626;

            --font-display: 'Fraunces', Georgia, serif;
            --font-body:    'DM Sans', sans-serif;

            --radius:       16px;
            --radius-sm:    10px;
            --shadow-card:  0 2px 16px rgba(26, 23, 20, 0.07);
            --shadow-hover: 0 8px 32px rgba(26, 23, 20, 0.12);
        }

        html { scroll-behavior: smooth; }

        body {
            background: var(--cream);
            color: var(--ink);
            font-family: var(--font-body);
            min-height: 100vh;
        }

        body::before {
            content: '';
            position: fixed;
            inset: 0;
            z-index: 1000;
            pointer-events: none;
            opacity: 0.025;
            background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
            background-size: 200px 200px;
        }

        /* ─── HEADER ─── */
        header {
            position: fixed;
            top: 0; left: 0; right: 0;
            z-index: 200;
            height: 64px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 2.5rem;
            background: rgba(248, 245, 239, 0.92);
            backdrop-filter: blur(20px);
            border-bottom: 1.5px solid var(--cream-dark);
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 9px;
            text-decoration: none;
        }

        .logo img { width: 32px; height: 32px; object-fit: contain; }

        .logo-word {
            font-family: var(--font-display);
            font-size: 1.4rem;
            font-weight: 900;
            color: var(--ink);
            letter-spacing: -0.03em;
        }

        .logo-word em { font-style: italic; font-weight: 300; color: var(--ink-mid); }

        .header-right {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .mode-badge {
            display: flex;
            align-items: center;
            gap: 7px;
            padding: 6px 14px;
            border-radius: 100px;
            background: var(--terra-light);
            color: var(--terra);
            font-size: 0.8rem;
            font-weight: 700;
        }

        .home-link {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 0.82rem;
            font-weight: 600;
            color: var(--ink-faint);
            text-decoration: none;
            transition: color 0.2s;
        }

        .home-link:hover { color: var(--ink); }

        /* ─── PAGE WRAPPER ─── */
        .page {
            max-width: 860px;
            margin: 0 auto;
            padding: 92px 2rem 80px;
        }

        /* ─── PAGE HEADING ─── */
        .page-heading {
            padding: 32px 0 36px;
            border-bottom: 1.5px solid var(--cream-dark);
            margin-bottom: 40px;
        }

        .page-heading h1 {
            font-family: var(--font-display);
            font-size: clamp(2.2rem, 4vw, 3.2rem);
            font-weight: 900;
            letter-spacing: -0.03em;
            line-height: 1.0;
            color: var(--ink);
            margin-bottom: 8px;
        }

        .page-heading h1 em { font-style: italic; font-weight: 300; color: var(--terra); }

        .page-heading p {
            font-size: 0.95rem;
            color: var(--ink-faint);
            line-height: 1.6;
        }

        /* ─── SECTION LABEL ─── */
        .section-label {
            font-size: 0.72rem;
            font-weight: 700;
            letter-spacing: 0.13em;
            text-transform: uppercase;
            color: var(--ink-faint);
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .section-label i { font-size: 0.68rem; }

        .optional-tag {
            font-size: 0.68rem;
            font-weight: 600;
            letter-spacing: 0.08em;
            color: var(--ink-faint);
            background: var(--cream-dark);
            padding: 2px 8px;
            border-radius: 100px;
            text-transform: uppercase;
            opacity: 0.8;
        }

        /* ─── CARD ─── */
        .card {
            background: var(--white);
            border-radius: var(--radius);
            border: 1.5px solid var(--cream-dark);
            padding: 24px;
            box-shadow: var(--shadow-card);
            margin-bottom: 16px;
        }

        /* ─── TEXT INPUTS ─── */
        .text-field {
            width: 100%;
            padding: 13px 16px;
            border: 1.5px solid var(--cream-dark);
            border-radius: var(--radius-sm);
            font-family: var(--font-body);
            font-size: 0.95rem;
            color: var(--ink);
            background: var(--cream);
            transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
        }

        .text-field:focus {
            outline: none;
            border-color: var(--terra);
            box-shadow: 0 0 0 3px rgba(196, 97, 74, 0.1);
            background: var(--white);
        }

        .text-field::placeholder { color: var(--ink-faint); }

        textarea.text-field {
            resize: vertical;
            line-height: 1.65;
        }

        textarea.text-field.tall { min-height: 200px; }
        textarea.text-field.short { min-height: 110px; }

        .word-count {
            text-align: right;
            margin-top: 8px;
            font-size: 0.82rem;
            color: var(--ink-faint);
        }

        .word-count.warning { color: var(--red); font-weight: 600; }

        /* ─── TABS ─── */
        .tabs {
            display: flex;
            gap: 6px;
            margin-bottom: 14px;
        }

        .tab {
            display: flex;
            align-items: center;
            gap: 7px;
            padding: 9px 18px;
            border: 1.5px solid var(--cream-dark);
            background: var(--cream);
            border-radius: 100px;
            cursor: pointer;
            font-family: var(--font-body);
            font-weight: 600;
            font-size: 0.83rem;
            color: var(--ink-faint);
            transition: all 0.2s;
            user-select: none;
        }

        .tab:hover:not(.active) { border-color: var(--terra-mid); color: var(--terra); }

        .tab.active {
            background: var(--terra);
            border-color: var(--terra);
            color: white;
        }

        /* File upload */
        .file-upload-area {
            border: 2px dashed var(--cream-dark);
            border-radius: var(--radius-sm);
            padding: 48px 24px;
            text-align: center;
            cursor: pointer;
            transition: all 0.2s;
            background: var(--cream);
        }

        .file-upload-area:hover, .file-upload-area.dragover {
            border-color: var(--terra);
            background: var(--terra-light);
        }

        .file-upload-area .upload-icon {
            font-size: 2.2rem;
            color: var(--terra-mid);
            margin-bottom: 14px;
        }

        .file-upload-area p { font-size: 0.9rem; color: var(--ink-mid); }
        .file-upload-area p strong { color: var(--ink); }
        .file-upload-area .upload-hint { font-size: 0.8rem; color: var(--ink-faint); margin-top: 6px; }

        input[type="file"] { display: none; }

        #fileName {
            margin-top: 10px;
            font-size: 0.85rem;
            color: var(--terra);
            font-weight: 500;
            text-align: center;
        }

        .file-upload-section { display: none; }
        .file-upload-section.active { display: block; }
        .text-input-section.hidden { display: none; }

        /* ─── SETTINGS ROW ─── */
        .settings-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 16px;
            margin-bottom: 16px;
        }

        .settings-card {
            background: var(--white);
            border-radius: var(--radius);
            border: 1.5px solid var(--cream-dark);
            padding: 20px;
            box-shadow: var(--shadow-card);
        }

        /* Orientation buttons */
        .orient-btns { display: flex; gap: 8px; }

        .orient-btn {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 9px;
            padding: 14px 8px;
            border: 1.5px solid var(--cream-dark);
            background: var(--cream);
            border-radius: var(--radius-sm);
            cursor: pointer;
            font-family: var(--font-body);
            font-size: 0.78rem;
            font-weight: 600;
            color: var(--ink-faint);
            transition: all 0.2s;
        }

        .orient-btn:hover:not(.active) { border-color: var(--terra-mid); color: var(--terra); }

        .orient-btn.active {
            background: var(--terra-light);
            border-color: var(--terra);
            color: var(--terra);
        }

        .orient-shape {
            display: block;
            border: 2px solid currentColor;
            border-radius: 3px;
            opacity: 0.85;
        }

        .orient-shape.v { width: 13px; height: 21px; }
        .orient-shape.h { width: 22px; height: 15px; }
        .orient-ratio { font-size: 0.68rem; opacity: 0.65; font-weight: 400; }

        /* Voice buttons */
        .voice-btns {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 7px;
        }

        .voice-btn {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 7px;
            padding: 12px 6px 10px;
            border: 1.5px solid var(--cream-dark);
            background: var(--cream);
            border-radius: var(--radius-sm);
            cursor: pointer;
            font-family: var(--font-body);
            font-size: 0.78rem;
            font-weight: 600;
            color: var(--ink-faint);
            transition: all 0.2s;
        }

        .voice-btn:hover:not(.active) { border-color: var(--terra-mid); color: var(--terra); }

        .voice-btn.active {
            background: var(--terra-light);
            border-color: var(--terra);
            color: var(--terra);
        }

        .voice-ring {
            width: 32px; height: 32px;
            border-radius: 50%;
            background: rgba(196, 97, 74, 0.08);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.85rem;
            transition: background 0.2s;
        }

        .voice-btn.active .voice-ring { background: rgba(196, 97, 74, 0.16); }

        .voice-btn.playing .voice-ring {
            animation: pulse-ring 1.2s ease-out infinite;
        }

        @keyframes pulse-ring {
            0%   { box-shadow: 0 0 0 0 rgba(196, 97, 74, 0.45); }
            70%  { box-shadow: 0 0 0 7px rgba(196, 97, 74, 0); }
            100% { box-shadow: 0 0 0 0 rgba(196, 97, 74, 0); }
        }

        /* ─── CHARACTER PHOTO UPLOAD ─── */
        .char-photo-upload {
            border: 2px dashed var(--cream-dark);
            border-radius: var(--radius-sm);
            padding: 24px;
            text-align: center;
            transition: all 0.2s;
            background: var(--cream);
        }

        .char-photo-upload:hover { border-color: var(--terra-mid); }

        .char-photo-label {
            font-size: 0.88rem;
            font-weight: 700;
            color: var(--ink);
            margin-bottom: 12px;
        }

        .char-photo-btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 20px;
            border-radius: var(--radius-sm);
            border: 1.5px solid var(--terra);
            background: var(--terra-light);
            color: var(--terra);
            font-family: var(--font-body);
            font-size: 0.85rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
        }

        .char-photo-btn:hover { background: #F5DDD7; }

        #charImagePreview { display: none; }
        #charImagePreview.visible {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            margin-top: 14px;
        }

        #charImageThumb {
            width: 44px; height: 44px;
            border-radius: 8px;
            object-fit: cover;
            border: 2px solid var(--terra-mid);
        }

        #charImageName {
            font-size: 0.83rem;
            color: var(--ink-mid);
            font-weight: 500;
        }

        #removeCharImage {
            background: none;
            border: none;
            cursor: pointer;
            color: var(--red);
            font-size: 1.1rem;
            padding: 4px;
            opacity: 0.7;
            transition: opacity 0.2s;
        }

        #removeCharImage:hover { opacity: 1; }

        /* ─── GENERATE BUTTON ─── */
        .generate-btn {
            width: 100%;
            padding: 18px;
            background: var(--terra);
            color: white;
            border: none;
            border-radius: var(--radius-sm);
            font-family: var(--font-display);
            font-size: 1.15rem;
            font-weight: 700;
            font-style: italic;
            cursor: pointer;
            transition: all 0.22s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            box-shadow: 0 4px 16px rgba(196, 97, 74, 0.28), 3px 3px 0 var(--terra-dark);
            letter-spacing: -0.01em;
            margin-top: 4px;
            margin-bottom: 20px;
        }

        .generate-btn:hover:not(:disabled) {
            background: var(--terra-hover);
            transform: translate(-1px, -1px);
            box-shadow: 0 4px 16px rgba(196, 97, 74, 0.28), 4px 4px 0 var(--terra-dark);
        }

        .generate-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        /* ─── LOADING ─── */
        .loading-section {
            display: none;
            text-align: center;
            padding: 80px 24px;
            background: var(--white);
            border-radius: var(--radius);
            border: 1.5px solid var(--cream-dark);
            box-shadow: var(--shadow-card);
            margin-bottom: 20px;
        }

        .loading-section.active { display: block; }

        /* Book animation */
        .loading-book {
            width: 52px; height: 40px;
            position: relative;
            margin: 0 auto 28px;
        }

        .book-page {
            position: absolute;
            width: 24px; height: 38px;
            background: var(--terra-light);
            border: 1.5px solid var(--terra-mid);
            border-radius: 2px;
            top: 0;
            transform-origin: right center;
        }

        .page-left {
            left: 2px;
            animation: page-turn 1.8s ease-in-out infinite;
            transform-origin: right center;
        }

        .page-right {
            right: 2px;
            animation: page-turn-right 1.8s ease-in-out infinite;
            transform-origin: left center;
        }

        .book-spine {
            position: absolute;
            width: 4px; height: 40px;
            background: var(--terra);
            left: 50%; transform: translateX(-50%);
            border-radius: 2px;
        }

        @keyframes page-turn {
            0%, 100% { transform: rotateY(0deg); }
            50% { transform: rotateY(-25deg); }
        }

        @keyframes page-turn-right {
            0%, 100% { transform: rotateY(0deg); }
            50% { transform: rotateY(25deg); }
        }

        .loading-title {
            font-family: var(--font-display);
            font-size: 1.4rem;
            font-weight: 700;
            font-style: italic;
            color: var(--ink);
            margin-bottom: 8px;
            letter-spacing: -0.02em;
        }

        .loading-sub { font-size: 0.88rem; color: var(--ink-faint); }

        /* ─── REF SHEET ─── */
        .ref-sheet-section {
            display: none;
            margin-bottom: 20px;
        }

        .ref-sheet-section.active { display: block; }

        .ref-heading {
            margin-bottom: 24px;
        }

        .ref-heading h2 {
            font-family: var(--font-display);
            font-size: 1.9rem;
            font-weight: 900;
            letter-spacing: -0.025em;
            color: var(--ink);
            margin-bottom: 6px;
        }

        .ref-heading h2 em { font-style: italic; font-weight: 300; color: var(--terra); }

        .ref-heading p {
            font-size: 0.88rem;
            color: var(--ink-faint);
            line-height: 1.65;
        }

        .ref-sheet-display {
            background: var(--ink);
            border-radius: var(--radius);
            overflow: hidden;
            margin-bottom: 20px;
            box-shadow: var(--shadow-hover);
            display: flex;
            justify-content: center;
        }

        .ref-sheet-display img { max-width: 100%; max-height: 70vh; display: block; object-fit: contain; }

        .ref-feedback-card {
            background: var(--white);
            border-radius: var(--radius);
            border: 1.5px solid var(--cream-dark);
            padding: 20px;
            box-shadow: var(--shadow-card);
            margin-bottom: 16px;
        }

        .ref-actions {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            flex-wrap: wrap;
        }

        .ref-revision-note {
            font-size: 0.82rem;
            color: var(--ink-faint);
            text-align: right;
            margin-top: 10px;
        }

        /* ─── SLIDE PREVIEW ─── */
        .slide-preview-section { display: none; }
        .slide-preview-section.active { display: block; }

        .preview-heading {
            margin-bottom: 24px;
        }

        .preview-heading h2 {
            font-family: var(--font-display);
            font-size: 1.9rem;
            font-weight: 900;
            letter-spacing: -0.025em;
            color: var(--ink);
            margin-bottom: 6px;
        }

        .preview-heading h2 em { font-style: italic; font-weight: 300; color: var(--terra); }

        .preview-heading p {
            font-size: 0.88rem;
            color: var(--ink-faint);
            line-height: 1.65;
        }

        .slide-gallery {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
            gap: 14px;
            margin-bottom: 24px;
            max-height: 560px;
            overflow-y: auto;
            padding: 18px;
            background: var(--cream);
            border-radius: var(--radius);
            border: 1.5px solid var(--cream-dark);
        }

        .slide-card {
            background: var(--white);
            border-radius: 10px;
            overflow: hidden;
            border: 1.5px solid var(--cream-dark);
            box-shadow: var(--shadow-card);
            display: flex;
            flex-direction: column;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .slide-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-hover); }
        .slide-card img { width: 100%; display: block; }

        .slide-num {
            padding: 6px 10px;
            font-size: 0.72rem;
            font-weight: 700;
            color: var(--ink-faint);
            background: var(--cream);
            border-top: 1px solid var(--cream-dark);
            text-align: center;
        }

        .slide-feedback-input {
            width: 100%;
            padding: 10px;
            border: none;
            border-top: 1px solid var(--cream-dark);
            font-size: 0.8rem;
            font-family: var(--font-body);
            resize: none;
            min-height: 58px;
            background: var(--white);
            color: var(--ink);
            transition: background 0.2s;
        }

        .slide-feedback-input:focus {
            outline: none;
            background: var(--terra-light);
        }

        .slide-feedback-count {
            font-size: 0.75rem;
            color: var(--ink-faint);
            text-align: right;
            padding: 4px 10px 6px;
            background: var(--white);
        }

        .slide-feedback-count.warning {
            color: var(--red);
            font-weight: 600;
        }


        .slide-feedback-input::placeholder { color: var(--ink-faint); font-style: italic; }

        .preview-footer {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            flex-wrap: wrap;
        }

        .revision-counter {
            font-size: 0.82rem;
            color: var(--ink-faint);
            font-weight: 500;
        }

        .preview-actions { display: flex; gap: 10px; }

        /* ─── BUTTONS ─── */
        .btn-secondary {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 11px 22px;
            border-radius: var(--radius-sm);
            font-family: var(--font-body);
            font-size: 0.88rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s;
            background: var(--white);
            color: var(--terra);
            border: 1.5px solid var(--terra);
        }

        .btn-secondary:hover:not(:disabled) { background: var(--terra-light); }
        .btn-secondary:disabled { opacity: 0.4; cursor: not-allowed; }

        .btn-primary {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 11px 24px;
            border-radius: var(--radius-sm);
            font-family: var(--font-body);
            font-size: 0.88rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
            background: var(--terra);
            color: white;
            border: none;
            box-shadow: 0 4px 14px rgba(196, 97, 74, 0.25);
        }

        .btn-primary:hover:not(:disabled) {
            background: var(--terra-hover);
            transform: translateY(-1px);
            box-shadow: 0 6px 20px rgba(196, 97, 74, 0.3);
        }

        .btn-primary:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

        /* ─── VIDEO READY ─── */
        .video-preview { display: none; }
        .video-preview.active { display: block; }

        .video-heading {
            font-family: var(--font-display);
            font-size: 1.9rem;
            font-weight: 900;
            font-style: italic;
            letter-spacing: -0.025em;
            color: var(--ink);
            margin-bottom: 6px;
        }

        .video-heading em { font-weight: 300; color: var(--terra); }

        .video-subheading {
            font-size: 0.88rem;
            color: var(--ink-faint);
            margin-bottom: 20px;
        }

        .video-container {
            background: var(--ink);
            border-radius: var(--radius);
            overflow: hidden;
            margin-bottom: 20px;
            box-shadow: var(--shadow-hover);
            display: flex;
            justify-content: center;
        }

        video { max-width: 100%; max-height: 70vh; display: block; }

        .download-btn {
            width: 100%;
            padding: 16px;
            background: var(--terra);
            color: white;
            border: none;
            border-radius: var(--radius-sm);
            font-family: var(--font-display);
            font-size: 1rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            box-shadow: 0 4px 16px rgba(196, 97, 74, 0.28), 3px 3px 0 var(--terra-dark);
        }

        .download-btn:hover {
            background: var(--terra-hover);
            transform: translateY(-1px);
            box-shadow: 0 4px 16px rgba(196, 97, 74, 0.28), 4px 4px 0 var(--terra-dark);
        }

        /* ─── ERROR ─── */
        .error-message {
            display: none;
            padding: 16px 20px;
            background: #FEF2F2;
            border: 1.5px solid #FECACA;
            border-radius: var(--radius-sm);
            color: var(--red);
            font-size: 0.9rem;
            font-weight: 500;
            margin-top: 16px;
        }

        .error-message.active { display: block; }

        /* ─── RATE LIMIT ERROR ─── */
        .error-rate-limit {
            display: flex;
            align-items: flex-start;
            gap: 14px;
        }

        .error-rate-limit .error-icon {
            font-size: 1.6rem;
            line-height: 1;
            flex-shrink: 0;
            margin-top: 2px;
        }

        .error-rate-limit strong {
            display: block;
            font-size: 0.95rem;
            font-weight: 700;
            margin-bottom: 4px;
            color: var(--red, #DC2626);
        }

        .error-rate-limit p {
            margin: 0;
            font-size: 0.88rem;
            font-weight: 400;
            line-height: 1.5;
            color: var(--red, #DC2626);
            opacity: 0.9;
        }


        /* ─── FOOTER ─── */
        footer {
            padding: 36px 2rem;
            text-align: center;
            border-top: 1.5px solid var(--cream-dark);
            font-size: 0.82rem;
            color: var(--ink-faint);
        }

        /* ─── RESPONSIVE ─── */
        @media (max-width: 640px) {
            .settings-row { grid-template-columns: 1fr; }
            header { padding: 0 1.2rem; }
            .page { padding: 80px 1.2rem 60px; }
            .preview-footer { flex-direction: column; align-items: stretch; }
            .preview-actions { flex-direction: column; }
            .btn-secondary, .btn-primary { justify-content: center; }
            .ref-actions { flex-direction: column; }
            .voice-btns { grid-template-columns: repeat(2, 1fr); }
        }

        #visualContextWordCount {
            margin-top: 6px;
        }