@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Space+Grotesk:wght@500;600&display=swap');
        /* Lock viewport only for the chat app shell — landing/share must scroll */
        html:has(.app-shell),
        body:has(.app-shell),
        html.app-page,
        body.app-page {
            height: 100%;
            height: 100dvh;
            overflow: hidden;
            overscroll-behavior: none;
        }
        body {
            font-family: 'Inter', system-ui, sans-serif;
        }
        .font-display { font-family: 'Space Grotesk', 'Inter', sans-serif; }
        .app-shell {
            height: 100%;
            height: 100dvh;
            /* notch / home indicator */
            padding-top: env(safe-area-inset-top);
            padding-right: env(safe-area-inset-right);
            padding-left: env(safe-area-inset-left);
            box-sizing: border-box;
            width: 100%;
            overflow: hidden;
            /* Keep below fixed sidebar/backdrop (z 110/120) */
            position: relative;
            z-index: 1;
        }
        .message-bubble {
            max-width: min(92%, 48rem);
            padding: 12px 14px;
            border-radius: 18px;
            line-height: 1.55;
            word-break: break-word;
            overflow-wrap: anywhere;
        }
        @media (min-width: 768px) {
            .message-bubble {
                max-width: min(80%, 48rem);
                padding: 14px 18px;
                border-radius: 20px;
            }
        }
        .user-bubble {
            background: #6366f1;
            color: white;
            border-bottom-right-radius: 6px;
        }
        .assistant-bubble {
            background: #1e293b;
            color: #e2e8f0;
            border: 1px solid #334155;
            border-bottom-left-radius: 6px;
        }
        .msg-row-assistant {
            display: flex;
            align-items: flex-end;
            gap: 0.45rem;
            max-width: min(92%, 48rem);
        }
        .msg-avatar {
            width: 2rem;
            height: 2rem;
            border-radius: 9999px;
            object-fit: cover;
            border: 1px solid #475569;
            flex-shrink: 0;
            background: #1e293b;
        }
        .char-cand {
            position: relative;
            border-radius: 0.75rem;
            overflow: hidden;
            border: 2px solid #334155;
            cursor: pointer;
            aspect-ratio: 3/4;
            max-height: 7.5rem;
            background: #0f172a;
        }
        .char-cand img { width: 100%; height: 100%; object-fit: cover; display: block; }
        .char-cand.selected {
            border-color: #ec4899;
            box-shadow: 0 0 0 2px rgba(236,72,153,0.45);
            outline: 2px solid #f9a8d4;
        }
        .char-list-card {
            border-radius: 0.95rem;
            background: #1e293b;
            border: 1px solid #334155;
            overflow: hidden;
        }
        .char-list-item {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            padding: 0.6rem 0.75rem;
            border-radius: 0;
            background: transparent;
            border: 0;
            cursor: pointer;
            transition: background .15s;
            -webkit-tap-highlight-color: transparent;
            width: 100%;
        }
        .char-list-item:hover,
        .char-list-item:focus-visible {
            background: #243044;
            outline: none;
        }
        .char-list-item:active {
            transform: none;
            background: #283548;
        }
        .char-list-actions {
            display: flex;
            flex-wrap: wrap;
            gap: 0.35rem;
            padding: 0 0.55rem 0.55rem;
            border-top: 1px solid #293548;
            padding-top: 0.45rem;
        }
        .char-action-btn {
            display: inline-flex;
            align-items: center;
            gap: 0.3rem;
            padding: 0.4rem 0.55rem;
            border-radius: 0.55rem;
            font-size: 0.7rem;
            font-weight: 500;
            border: 1px solid #475569;
            background: #0f172a;
            color: #e2e8f0;
            cursor: pointer;
            transition: background .12s, border-color .12s, opacity .12s;
        }
        .char-action-btn:hover:not(:disabled) {
            background: #1e293b;
        }
        .char-action-btn:disabled {
            opacity: 0.45;
            cursor: wait;
        }
        .char-action-btn span {
            line-height: 1.1;
        }
        .char-action-use {
            border-color: #be185d88;
            background: #83184344;
            color: #fbcfe8;
        }
        .char-action-public {
            border-color: #4f46e5aa;
            background: #3730a366;
            color: #c7d2fe;
        }
        .char-action-public-on {
            border-color: #059669aa;
            background: #064e3b66;
            color: #a7f3d0;
        }
        .char-action-reload {
            border-color: #475569;
            color: #cbd5e1;
        }
        .char-action-delete {
            border-color: #991b1b88;
            background: #450a0a44;
            color: #fca5a5;
            margin-left: auto;
        }
        .streaming-cursor::after {
            content: '▋';
            animation: blink 1s step-end infinite;
            margin-left: 2px;
            color: #818cf8;
        }
        @keyframes blink { 50% { opacity: 0; } }
        .chat-scroll {
            scrollbar-width: thin;
            scrollbar-color: #475569 #0f172a;
            -webkit-overflow-scrolling: touch;
        }
        .assistant-bubble pre {
            background: #0f172a;
            border: 1px solid #334155;
            border-radius: 12px;
            padding: 12px;
            overflow-x: auto;
            font-size: 12px;
            margin: 10px 0;
            font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        }
        .assistant-bubble code {
            font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
            font-size: 0.9em;
            background: #0f172a;
            padding: 1px 5px;
            border-radius: 4px;
        }
        .assistant-bubble pre code {
            background: transparent;
            padding: 0;
        }
        .panel-hidden { display: none !important; }

        /* Life Mode dossier */
        .life-highlight {
            outline: 1px solid rgba(129, 140, 248, 0.7);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
            transition: box-shadow 0.3s ease, outline 0.3s ease;
        }
        .life-field.life-locked .life-val {
            letter-spacing: 0.15em;
        }

        /* RP readability — delimiters () * ** [] stripped in JS */
        .msg-text {
            line-height: 1.55;
        }
        .rp-block {
            display: block;
            margin: 0 0 0.65rem 0;
        }
        .rp-block:last-child,
        .rp-line:last-child {
            margin-bottom: 0;
        }
        .rp-line {
            display: block;
            margin: 0 0 0.65rem 0;
            line-height: 1.55;
        }
        .rp-spacer {
            height: 0.35rem;
        }
        .rp-narration {
            color: #94a3b8;
            font-style: italic;
            font-weight: 400;
        }
        .rp-block.rp-narration {
            color: #a8b4c4;
            font-size: 0.95em;
        }
        .rp-action {
            color: #c4b5fd;
            font-style: italic;
            font-weight: 500;
        }
        .assistant-bubble .rp-action { color: #ddd6fe; }
        .user-bubble .rp-action { color: #e9d5ff; }
        .rp-block.rp-action {
            color: #c4b5fd;
        }
        .rp-dialogue {
            color: #f1f5f9;
            font-style: normal;
            font-weight: 500;
        }
        .user-bubble .rp-dialogue {
            color: #fff;
        }
        .rp-ooc {
            display: inline-block;
            margin: 0.1rem 0;
            padding: 0.15rem 0.45rem;
            border-radius: 0.35rem;
            background: rgba(51, 65, 85, 0.55);
            color: #64748b;
            font-size: 0.8em;
            font-style: normal;
            border: 1px dashed rgba(100, 116, 139, 0.45);
        }
        .rp-block.rp-ooc {
            display: block;
            width: fit-content;
            max-width: 100%;
        }
        .msg-time {
            margin-top: 0.45rem;
            font-size: 0.65rem;
            line-height: 1.2;
            color: #64748b;
            font-variant-numeric: tabular-nums;
            opacity: 0.9;
        }
        .user-bubble .msg-time { color: #94a3b8; text-align: right; }
        .msg-images img { display: block; }
        .img-gen-status { opacity: 0.85; }
        .msg-image-wrap { max-width: 100%; }
        .img-debug {
            margin-top: 0.35rem;
            padding: 0.4rem 0.5rem;
            border-radius: 0.5rem;
            background: rgba(15, 23, 42, 0.85);
            border: 1px solid rgba(71, 85, 105, 0.6);
            font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
            font-size: 0.65rem;
            line-height: 1.35;
            color: #94a3b8;
            max-width: 36rem;
        }
        .img-debug-head {
            color: #cbd5e1;
            margin-bottom: 0.25rem;
            word-break: break-word;
        }
        .img-debug-prompt {
            color: #64748b;
            white-space: pre-wrap;
            word-break: break-word;
            max-height: 8rem;
            overflow: auto;
        }

        /* Scene quality pipeline busy UI */
        .scene-work-overlay {
            position: fixed;
            inset: 0;
            z-index: 80;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(15, 23, 42, 0.55);
            backdrop-filter: grayscale(0.35) brightness(0.72);
            -webkit-backdrop-filter: grayscale(0.35) brightness(0.72);
            pointer-events: all;
        }
        .scene-work-overlay.hidden {
            display: none !important;
        }
        .scene-work-card {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 0.75rem;
            padding: 1.25rem 1.5rem;
            border-radius: 1rem;
            background: rgba(15, 23, 42, 0.92);
            border: 1px solid rgba(99, 102, 241, 0.45);
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
            min-width: 16rem;
            max-width: 90vw;
        }
        .scene-work-spinner {
            width: 2rem;
            height: 2rem;
            border-radius: 999px;
            border: 3px solid rgba(148, 163, 184, 0.35);
            border-top-color: #a5b4fc;
            animation: scene-spin 0.8s linear infinite;
        }
        @keyframes scene-spin {
            to { transform: rotate(360deg); }
        }
        .scene-work-label {
            color: #e2e8f0;
            font-size: 0.95rem;
            font-weight: 600;
            text-align: center;
        }
        .scene-work-sub {
            color: #94a3b8;
            font-size: 0.75rem;
            text-align: center;
            min-height: 1rem;
        }
        .img-gen-status {
            display: flex;
            align-items: center;
            gap: 0.4rem;
            margin-top: 0.5rem;
            padding: 0.45rem 0.65rem;
            border-radius: 0.65rem;
            background: rgba(30, 41, 59, 0.9);
            border: 1px solid rgba(99, 102, 241, 0.35);
            color: #c7d2fe;
            font-size: 0.8rem;
            font-weight: 500;
        }
        .img-gen-status.is-error {
            border-color: rgba(245, 158, 11, 0.5);
            color: #fcd34d;
        }
        .img-gen-status .dot {
            width: 0.45rem;
            height: 0.45rem;
            border-radius: 999px;
            background: #818cf8;
            animation: scene-pulse 1s ease-in-out infinite;
        }
        @keyframes scene-pulse {
            0%, 100% { opacity: 0.35; }
            50% { opacity: 1; }
        }

        /* Messenger UI (mobile chat / in-person) */
        body.messenger-mode #messages {
            background:
                radial-gradient(ellipse at top, #1a2332 0%, #0b1220 55%, #020617 100%);
            padding-left: 0.5rem;
            padding-right: 0.5rem;
        }
        body.messenger-mode #messages .message-bubble {
            max-width: min(88%, 22rem);
            padding: 10px 14px;
            border-radius: 1.15rem;
            font-size: 0.95rem;
            line-height: 1.4;
            box-shadow: 0 1px 2px rgba(0,0,0,0.25);
        }
        body.messenger-mode #messages .user-bubble {
            background: linear-gradient(145deg, #6366f1, #4f46e5);
            border-bottom-right-radius: 0.35rem;
        }
        body.messenger-mode #messages .assistant-bubble {
            background: #1e293b;
            border: none;
            border-bottom-left-radius: 0.35rem;
        }
        body.messenger-mode #messages.space-y-4 > :not([hidden]) ~ :not([hidden]),
        body.messenger-mode #messages {
            --tw-space-y-reverse: 0;
        }
        body.messenger-mode #messages {
            gap: 0.45rem;
            display: flex;
            flex-direction: column;
        }
        body.messenger-mode #chat-controls {
            background: #0f172a;
        }
        body.messenger-mode #message-input {
            font-size: 16px; /* avoid iOS zoom */
        }
        body.messenger-mode.mode-mobile .assistant-bubble {
            font-size: 0.92rem;
        }
        body.messenger-mode.mode-in_person .assistant-bubble {
            max-width: min(92%, 26rem);
            font-size: 0.95rem;
            line-height: 1.5;
        }
        .style-seg {
            display: inline-flex;
            background: #1e293b;
            border: 1px solid #334155;
            border-radius: 999px;
            padding: 2px;
            gap: 2px;
        }
        .style-seg button {
            border: none;
            background: transparent;
            color: #94a3b8;
            font-size: 0.7rem;
            font-weight: 600;
            padding: 0.4rem 0.65rem;
            border-radius: 999px;
            white-space: nowrap;
            cursor: pointer;
            transition: background .12s, color .12s;
        }
        .style-seg button.active {
            background: #4f46e5;
            color: #fff;
        }
        .rp-spoiler {
            margin: 0.75rem 0;
            border: 1px solid #7f1d1d;
            border-radius: 12px;
            background: #1c0a0a;
            overflow: hidden;
        }
        .rp-spoiler > summary {
            cursor: pointer;
            padding: 0.65rem 0.85rem;
            font-weight: 600;
            font-size: 0.85rem;
            color: #fca5a5;
            list-style: none;
            user-select: none;
        }
        .rp-spoiler > summary::-webkit-details-marker { display: none; }
        .rp-spoiler > summary::before {
            content: '▸ ';
            color: #f87171;
        }
        .rp-spoiler[open] > summary::before { content: '▾ '; }
        .rp-spoiler-body {
            padding: 0.5rem 0.85rem 0.85rem;
            border-top: 1px solid #7f1d1d55;
            color: #fecaca;
            font-size: 0.85rem;
            line-height: 1.45;
            filter: blur(0px);
        }
        .rp-spoiler:not([open]) .rp-spoiler-body { display: none; }
        #rp-result-card {
            border: 1px solid #4c1d95;
            background: linear-gradient(160deg, #1e1b4b 0%, #0f172a 100%);
            border-radius: 1rem;
            padding: 0.85rem;
            font-size: 0.85rem;
        }
        body.messenger-mode.mode-in_person .style-seg button[data-style="in_person"].active {
            background: #be185d;
        }
        body.messenger-mode.mode-mobile .style-seg button[data-style="mobile"].active {
            background: #0d9488;
        }

        /* Sidebar: drawer on mobile, static on md+
           Must sit ABOVE #sidebar-backdrop (clicks). Never put a transform on
           .app-shell while the drawer is open — transform traps position:fixed. */
        #sidebar {
            position: fixed;
            inset: 0 auto 0 0;
            width: min(18rem, 86vw);
            z-index: 120;
            transform: translateX(-105%);
            transition: transform 0.22s ease;
            max-height: 100dvh;
            pointer-events: auto;
        }
        #sidebar.sidebar-open { transform: translateX(0); }
        #sidebar-backdrop {
            position: fixed;
            inset: 0;
            background: rgba(0, 0, 0, 0.55);
            z-index: 110;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.22s ease;
        }
        #sidebar-backdrop.backdrop-open {
            opacity: 1;
            pointer-events: auto;
        }
        @media (min-width: 768px) {
            /* Stay fixed-width column on the left; spacer in .app-shell reserves room */
            #sidebar {
                position: fixed;
                inset: 0 auto 0 0;
                width: 18rem;
                transform: none !important;
                z-index: 30;
                max-height: 100dvh;
                border-right: 1px solid #1e293b;
            }
            #sidebar-backdrop { display: none !important; }
            #sidebar-desktop-spacer { width: 18rem; }
        }

        /* Prevent iOS zoom on focus (needs >= 16px) */
        #message-input,
        #model-select,
        #system-prompt,
        #doc-text,
        #doc-text-name,
        #opt-rag-top-k {
            font-size: 16px;
        }
        @media (min-width: 768px) {
            #message-input { font-size: 0.875rem; }
            #model-select { font-size: 0.75rem; }
        }

        /* Composer: roomy text field, compact side buttons */
        #chat-input-bar {
            padding: 0.5rem 0.65rem;
            padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
        }
        #chat-composer {
            gap: 0.35rem;
            padding: 0.4rem 0.45rem;
        }
        .chat-action-btn {
            width: 2rem;
            height: 2rem;
            min-width: 2rem;
            border-radius: 0.65rem;
            font-size: 0.8rem;
            flex-shrink: 0;
        }
        #send-button.chat-action-btn {
            width: 2.25rem;
            height: 2.25rem;
            min-width: 2.25rem;
        }
        #message-input {
            min-height: 3.1rem;
            max-height: 9rem;
            padding-top: 0.55rem;
            padding-bottom: 0.55rem;
            line-height: 1.4;
        }
        @media (min-width: 768px) {
            #chat-input-bar {
                padding: 0.75rem 1rem;
                padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
            }
            #chat-composer { gap: 0.5rem; padding: 0.5rem 0.65rem; }
            .chat-action-btn {
                width: 2.25rem;
                height: 2.25rem;
                min-width: 2.25rem;
                font-size: 0.85rem;
            }
            #send-button.chat-action-btn {
                width: 2.5rem;
                height: 2.5rem;
                min-width: 2.5rem;
            }
            #message-input {
                min-height: 2.75rem;
                max-height: 10rem;
            }
        }
        /* Soft keyboard open: free vertical space for messages */
        body.keyboard-open #chat-controls {
            display: none !important;
        }
        body.keyboard-open #chat-input-bar {
            padding-top: 0.35rem;
            padding-bottom: 0.35rem;
        }
        body.keyboard-open .app-shell {
            padding-top: 0;
        }
        body.keyboard-open #messages {
            padding-top: 0.5rem;
            padding-bottom: 0.5rem;
        }

        select#model-select {
            -webkit-appearance: none;
            appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%94a3b8'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 0.5rem center;
            background-size: 1rem;
            padding-right: 1.75rem;
        }

/* Storybook picturebook */
#storybook-rail-thumbs .sb-thumb {
  position: relative;
  flex: 0 0 auto;
  width: 4.5rem;
  height: 4.5rem;
  border-radius: 0.75rem;
  overflow: hidden;
  border: 1px solid rgb(51 65 85);
  background: rgb(2 6 23);
}
#storybook-rail-thumbs .sb-thumb.locked {
  filter: grayscale(0.8);
  opacity: 0.55;
}
#storybook-rail-thumbs .sb-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
#storybook-rail-thumbs .sb-thumb .sb-lock {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.45);
  color: rgb(251 191 36);
  font-size: 1rem;
}
#storybook-rail-thumbs .sb-thumb .sb-new {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 9px;
  background: rgb(16 185 129);
  color: white;
  padding: 0 4px;
  border-radius: 4px;
}
.sb-mood.active {
  border-color: rgb(245 158 11) !important;
  background: rgba(120, 53, 15, 0.45) !important;
  color: rgb(253 230 138) !important;
}

/* Storybook unlock feedback */
#storybook-rail.sb-rail-pulse {
  animation: sbRailPulse 1s ease;
}
@keyframes sbRailPulse {
  0%, 100% { background-color: rgba(15, 23, 42, 0.9); }
  30% { background-color: rgba(120, 53, 15, 0.55); }
  60% { background-color: rgba(69, 26, 3, 0.45); }
}
#storybook-unlock-flash.sb-flash-pulse {
  animation: sbFlashIn 0.35s ease;
}
@keyframes sbFlashIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
#storybook-rail-thumbs .sb-thumb.sb-thumb-flash {
  outline: 2px solid rgb(251 191 36);
  box-shadow: 0 0 0 3px rgba(251, 191, 36, 0.35);
  animation: sbThumbPop 0.6s ease;
}
@keyframes sbThumbPop {
  0% { transform: scale(0.92); }
  50% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

/* Chat busy — AI thinking / streaming */
#chat-composer.chat-busy {
  opacity: 0.72;
  pointer-events: none;
  border-color: rgb(79 70 229 / 0.45) !important;
  box-shadow: 0 0 0 1px rgb(99 102 241 / 0.25);
}
#chat-input-bar.chat-busy {
  position: relative;
}
#chat-input-bar.chat-busy::after {
  content: 'KI denkt nach…';
  display: block;
  text-align: center;
  font-size: 10px;
  color: rgb(165 180 252);
  padding: 4px 0 2px;
  letter-spacing: 0.02em;
  animation: chatBusyPulse 1.2s ease-in-out infinite;
}
@keyframes chatBusyPulse {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}
#conversation-list.chat-busy-list {
  opacity: 0.45;
  pointer-events: none;
}
#message-input[readonly] {
  cursor: wait;
  color: rgb(148 163 184);
}
