:root {
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --bg-app: #0f172a;
    --bg-card: #1e293b;
    --text-main: #f8fafc;
    --text-dim: #94a3b8;
    --border: #334155;
    --glass: rgba(30, 41, 59, 0.7);
    --radius: 12px;
    --status-red: #ef4444;
    --status-green: #22c55e;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-app);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

header {
    height: auto;
    min-height: 64px;
    padding: env(safe-area-inset-top, 0) 16px 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    background: var(--glass);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    z-index: 1000;
}

.logo {
    font-size: 1.2rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    white-space: nowrap;
}

.logo span {
    color: var(--primary);
}

.actions {
    display: flex;
    /* gap: 4px; replaced with margin on children */
    align-items: center;
    margin-left: auto;
}

.actions>* {
    margin-right: 4px;
}

.actions>*:last-child {
    margin-right: 0;
}

.connection-status {
    display: flex;
    align-items: center;
    /* gap: 10px; */
    margin-left: 16px;
    padding: 0 4px;
}

.connection-status>* {
    margin-right: 10px;
}

.connection-status>*:last-child {
    margin-right: 0;
}

.led-red,
.led-green,
.led-orange,
.led-blue,
.led-orange-blink,
.led-red-blink {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    position: relative;
    display: inline-block;
}

.led-red {
    background: var(--status-red);
    box-shadow: 0 0 8px var(--status-red);
}

.led-green {
    background: var(--status-green);
    box-shadow: 0 0 8px var(--status-green);
}

.led-orange {
    background: #f59e0b;
    box-shadow: 0 0 8px #f59e0b;
}

.led-blue {
    background: #3b82f6;
    box-shadow: 0 0 8px #3b82f6;
}

.led-orange-blink {
    background: #f59e0b;
    box-shadow: 0 0 8px #f59e0b;
    animation: blink-orange 1s infinite alternate;
}

@keyframes blink-orange {
    from {
        opacity: 1;
        transform: scale(1);
        box-shadow: 0 0 8px #f59e0b;
    }

    to {
        opacity: 0.4;
        transform: scale(0.9);
        box-shadow: 0 0 2px #f59e0b;
    }
}

.led-red-blink {
    background: var(--status-red);
    box-shadow: 0 0 8px var(--status-red);
    animation: blink-red 1s infinite alternate;
}

@keyframes blink-red {
    from {
        opacity: 1;
        transform: scale(1);
        box-shadow: 0 0 8px var(--status-red);
    }

    to {
        opacity: 0.4;
        transform: scale(0.9);
        box-shadow: 0 0 2px var(--status-red);
    }
}

.app-info {
    display: flex;
    align-items: center;
    /* gap: 8px; */
    margin-left: 16px;
    padding-left: 16px;
    border-left: 1px solid var(--border);
}

.app-info>* {
    margin-right: 8px;
}

.app-info>*:last-child {
    margin-right: 0;
}

.app-info.hidden {
    display: none !important;
}

button {
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
    font-family: inherit;
}

.primary-btn {
    background: var(--primary);
    color: white;
}

.primary-btn:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

.secondary-btn {
    background: var(--bg-card);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.secondary-btn:hover:not(:disabled) {
    background: var(--border);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.icon-btn {
    background: transparent;
    color: var(--text-dim);
    border: none;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}

.icon-btn svg {
    margin: 0 !important;
}

main {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
    /* Needed for absolute positioning of handle */
}

.layout-handle {
    position: absolute;
    width: 24px;
    height: 24px;
    background: var(--bg-card);
    border: 1px solid var(--primary);
    border-radius: 50%;
    z-index: 50;
    cursor: move;
    /* or all-scroll */
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    color: var(--primary);
    transition: transform 0.1s, background-color 0.2s;
    /* Initial Position - will be set by JS, but good to have fallback/start */
    left: 400px;
    /* Matches default chat width */
    bottom: 121px;
    /* Approximation of input height */
    transform: translate(-50%, 50%);
    /* Center on the intersection */
}

.layout-handle:hover {
    transform: translate(-50%, 50%) scale(1.1);
    background: var(--primary);
    color: white;
}

.chat-section {
    width: 400px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    background: rgba(15, 23, 42, 0.5);
}

.chat-input-container {
    padding: 20px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: flex-end;
    /* gap: 10px; */
    background: var(--bg-app);
    min-height: 80px;
    /* Constraint */
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    /* gap: 16px; */
}

.chat-messages>* {
    margin-bottom: 16px;
}

.chat-messages>*:last-child {
    margin-bottom: 0;
}

.message {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: 12px;
    font-size: 0.95rem;
    line-height: 1.5;
    white-space: pre-wrap;
}

.suggestions-container {
    display: flex;
    flex-wrap: wrap;
    /* gap: 8px; */
    margin-top: 12px;
}

.suggestions-container>* {
    margin-right: 8px;
    margin-bottom: 8px;
}

.suggestions-container>*:last-child {
    margin-right: 0;
}

.suggestion-chip {
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid var(--primary);
    color: var(--primary);
    padding: 6px 12px;
    border-radius: 16px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

.suggestion-chip:hover {
    background: var(--primary);
    color: white;
}

.message.assistant {
    background: var(--bg-card);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}

.message.assistant.merlin {
    background: linear-gradient(135deg, #312e81 0%, #4c1d95 100%);
    border-left: 4px solid #8b5cf6;
    color: #e2e8f0;
}

.message.assistant.merlin::before {
    content: '🧙‍♂️ Merlin : ';
    font-weight: bold;
    display: block;
    margin-bottom: 4px;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #a78bfa;
}

.message.assistant.error {
    background: rgba(220, 38, 38, 0.1);
    border: 1px solid #dc2626;
    color: #ef4444;
    font-family: monospace;
    font-size: 0.85rem;
}

.message.user {
    background: var(--primary);
    color: white;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}

.message.low-budget {
    background: rgba(245, 158, 11, 0.15);
    border: 1px solid #f59e0b;
    color: #f59e0b;
    align-self: center;
    width: 90%;
    max-width: none;
    text-align: center;
    font-weight: 500;
}

.message.low-budget .recharge-btn {
    display: inline-block;
    margin-top: 10px;
    background: #f59e0b;
    color: #0f172a;
    padding: 6px 16px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    text-transform: uppercase;
}

.message.low-budget .recharge-btn:hover {
    background: #fbbf24;
    transform: scale(1.05);
}

.message.exhausted-budget {
    background: rgba(239, 68, 68, 0.15);
    border: 1px solid #ef4444;
    color: #ef4444;
    align-self: center;
    width: 90%;
    max-width: none;
    text-align: center;
    font-weight: 500;
}

.message.exhausted-budget .recharge-btn {
    display: inline-block;
    margin-top: 10px;
    background: #ef4444;
    color: white;
    padding: 6px 16px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    text-transform: uppercase;
}

.message.exhausted-budget .recharge-btn:hover {
    background: #dc2626;
    transform: scale(1.05);
}

.streaming-cursor {
    display: inline-block;
    width: 6px;
    height: 14px;
    background-color: var(--primary);
    margin-left: 2px;
    vertical-align: middle;
    animation: blink-cursor 1s step-end infinite;
}

@keyframes blink-cursor {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

.chat-input-container {
    padding: 20px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: flex-end;
    /* gap: 10px; */
    background: var(--bg-app);
}

.chat-input-container>* {
    margin-right: 10px;
}

.chat-input-container>*:last-child {
    margin-right: 0;
}

#user-input {
    flex: 1;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-main);
    border-radius: 8px;
    padding: 12px;
    resize: none;
    height: 80px;
    font-family: inherit;
    font-size: 0.95rem;
}

#user-input:focus {
    outline: none;
    border-color: var(--primary);
}

#send-btn {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: white;
    padding: 0;
}

.preview-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #000;
}

.preview-header {
    height: 48px;
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
}

.preview-tabs {
    display: flex;
    /* gap: 4px; */
}

.preview-tabs>* {
    margin-right: 4px;
}

.preview-tabs>*:last-child {
    margin-right: 0;
}

.tab {
    padding: 6px 12px;
    border-radius: 4px;
    background: transparent;
    color: var(--text-dim);
    font-size: 0.85rem;
}

.tab.active {
    background: var(--border);
    color: var(--text-main);
}

.preview-controls {
    display: flex;
    align-items: center;
    /* gap: 8px; */
    font-size: 0.8rem;
    color: var(--text-dim);
}

.preview-controls>* {
    margin-right: 8px;
}

.preview-controls>*:last-child {
    margin-right: 0;
}

#desktop-play-pause-btn.playing {
    color: var(--status-green);
}

#desktop-play-pause-btn.paused {
    color: var(--status-red);
}

#desktop-reload-btn:hover {
    color: var(--text-main);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #64748b;
}

.status-dot.active {
    background: #22c55e;
    box-shadow: 0 0 8px #22c55e;
}

.preview-content {
    flex: 1;
    position: relative;
    padding: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    background: radial-gradient(circle at center, #1e293b 0%, #0f172a 100%);
}

/* Preview Wrapper (The Device Container) */
.preview-wrapper {
    position: relative;
    width: 375px;
    height: 667px;
    /* Move border/background to mask if we want handles outside border,
       but user likely wants handles outside border. */
    background: transparent;
    border-radius: 36px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    /* overflow: hidden; -- REMOVED to allow handles to stick out */
    /* IMPORTANT: Allow absolute positioning for drag */
    touch-action: none;
    max-width: 100%;
    max-height: 100%;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1),
        top 0.3s cubic-bezier(0.4, 0, 0.2, 1),
        transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
        border-radius 0.3s cubic-bezier(0.4, 0, 0.2, 1),
        opacity 0.2s;
}

.preview-wrapper.maximized {
    width: 100% !important;
    height: 100% !important;
    left: 0 !important;
    top: 0 !important;
    border-radius: 0 !important;
}

.preview-wrapper.maximized .preview-mask {
    border-radius: 0 !important;
    border: none !important;
}

.preview-wrapper.maximized .preview-device-header {
    height: 40px;
}

.preview-wrapper.maximized .resize-handle {
    display: none !important;
}

/* The Mask (Actual Device Look) */
.preview-mask {
    position: relative;
    width: 100%;
    flex: 1;
    min-height: 0;
    background: #000;
    border: 8px solid #334155;
    border-radius: 36px;
    overflow: hidden;
    /* Clips the iframe */
    display: flex;
    flex-direction: column;
}

/* Drag Handle (Top Bar) */
.preview-device-header {
    height: 32px;
    background: #334155;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: grab;
    flex-shrink: 0;
    position: relative;
}

.window-controls {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    gap: 8px;
}

.window-btn {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    cursor: pointer;
    transition: filter 0.2s;
}

.window-btn.close {
    background: #ff5f56;
    border: 0.5px solid #e0443e;
}

.window-btn.minimize {
    background: #ffbd2e;
    border: 0.5px solid #dea123;
}

.window-btn.maximize {
    background: #27c93f;
    border: 0.5px solid #1aab29;
}

.window-btn:hover {
    filter: brightness(0.85);
}

.window-btn.close,
.window-btn.minimize {
    cursor: default;
    opacity: 0.6;
}

.preview-device-header:active {
    cursor: grabbing;
}

.device-notch {
    width: 60px;
    height: 16px;
    background: #1e293b;
    border-bottom-left-radius: 12px;
    border-bottom-right-radius: 12px;
}

#preview-iframe {
    width: 100%;
    height: 100%;
    border: none;
    background: #000;
    flex: 1;
}

/* Resize Handles */
/* Resize Handles */
.resize-handle {
    position: absolute;
    z-index: 20;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Hide side handles, only keep corner */
.resize-handle.right,
.resize-handle.bottom,
.resize-handle.top-left,
.resize-handle.top-right,
.resize-handle.bottom-left {
    display: none;
}

/* The Big Yellow Button */
.resize-handle.bottom-right {
    bottom: -30px;
    right: -30px;
    width: 60px;
    height: 60px;
    cursor: nwse-resize;

    background: #FFD700;
    /* Gold/Yellow */
    border: 4px solid #fff;
    /* White border for contrast */
    border-radius: 50%;

    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);

    /* Icon inside */
    display: flex;
    align-items: center;
    justify-content: center;

    /* Ensure it receives touch events well */
    touch-action: none;
}

.resize-handle.bottom-right::after {
    content: '';
    width: 24px;
    height: 24px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 9l-4 3 4 3'/%3E%3Cpath d='M9 5l3-4 3 4'/%3E%3Cpath d='M19 9l4 3-4 3'/%3E%3Cpath d='M9 19l3 4 3-4'/%3E%3Cpath d='M2 12h20'/%3E%3Cpath d='M12 2v20'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    opacity: 0.6;
}

.resize-handle.bottom-right:hover,
.resize-handle.bottom-right:active {
    transform: scale(1.1);
    background: #FFEA00;
    /* Brighter yellow on interaction */
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
}

/* Prevent Iframe from stealing mouse events during drag/resize */
.preview-wrapper.is-interacting iframe {
    pointer-events: none;
    opacity: 0.8;
    /* Visual feedback */
}

.preview-wrapper.is-interacting {
    box-shadow: 0 0 0 3px #FFD700, 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    /* Yellow highlight */
    transition: box-shadow 0.2s;
    user-select: none;
    -webkit-user-select: none;
}

#code-view {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: #0f172a;
    color: #94a3b8;
    padding: 0;
    /* Removed padding to allow tabs to be at the top */
    overflow: hidden;
    /* Changed to hidden to allow content container to scroll */
    font-family: monospace;
    font-size: 0.85rem;
    z-index: 10;
    display: flex;
    flex-direction: column;
}

.code-sub-tabs {
    display: flex;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    padding: 0 10px;
    /* gap: 5px; */
    flex-shrink: 0;
}

.code-sub-tabs>* {
    margin-right: 5px;
}

.code-sub-tabs>*:last-child {
    margin-right: 0;
}

.sub-tab {
    padding: 10px 16px;
    background: transparent;
    border: none;
    color: var(--text-dim);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
    border-radius: 0;
}

.sub-tab:hover {
    color: var(--text-main);
    background: rgba(255, 255, 255, 0.05);
}

.sub-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

#code-content-container {
    flex: 1;
    overflow: auto;
    background: #0f172a;
    position: relative;
    padding: 20px;
}

#code-view pre {
    margin: 0;
    font-family: inherit;
    white-space: pre;
}

#assets-view {
    padding: 10px 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    grid-gap: 20px;
}

.asset-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* gap: 12px; */
    text-align: center;
    transition: transform 0.2s;
}

.asset-card>* {
    margin-bottom: 12px;
}

.asset-card>*:last-child {
    margin-bottom: 0;
}

.asset-card:hover {
    transform: translateY(-2px);
    border-color: var(--primary);
}

.asset-preview {
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    border-radius: 8px;
    overflow: hidden;
}

.asset-preview img,
.asset-preview svg,
.asset-preview video,
.asset-preview audio {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Asset item in Grid */
.asset-item {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: all 0.2s ease;
    text-align: center;
    cursor: pointer;
    position: relative;
    /* gap: 12px; */
}

.asset-item>* {
    margin-bottom: 12px;
}

.asset-item>*:last-child {
    margin-bottom: 0;
}

.asset-item:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.5);
    border-color: var(--primary);
    background: var(--bg-app);
}

.asset-item .asset-preview {
    width: 64px;
    height: 64px;
    border-radius: 14px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.asset-info {
    width: 100%;
}

.asset-name {
    font-size: 0.9rem;
    color: var(--text-main);
    font-weight: 600;
    word-break: break-all;
}

.asset-size {
    font-size: 0.75rem;
    color: var(--text-dim);
    margin-top: -4px;
}

.asset-type {
    font-size: 0.65rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.delete-asset-btn {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 24px;
    height: 24px;
    background: rgba(0, 0, 0, 0.6);
    color: #ef4444;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    cursor: pointer;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.2s;
}

.asset-item:hover .delete-asset-btn {
    opacity: 1;
}



/* Modal Styling */
.modal {
    position: fixed;
    /* inset: 0; */
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

#confirmation-modal,
#prompt-modal {
    z-index: 2100;
}

.modal-content {
    background: var(--bg-card);
    padding: 32px;
    border-radius: 16px;
    max-width: 400px;
    width: 90%;
    border: 1px solid var(--border);
    max-height: 85vh;
    overflow-y: auto;
}

.modal-content h3 {
    margin-bottom: 12px;
}

.modal-content p {
    color: var(--text-dim);
    margin-bottom: 24px;
}


.modal-content input[type="text"],
.modal-content input[type="password"],
.modal-content input[type="email"] {
    width: 100%;
    background: var(--bg-app) !important;
    border: 1px solid var(--border) !important;
    color: var(--text-main) !important;
    padding: 12px !important;
    border-radius: 8px !important;
    font-size: 0.95rem;
    font-family: inherit;
    transition: border-color 0.2s;
    outline: none;
    margin-bottom: 16px;
    /* consistent spacing */
}

.modal-content input[type="text"]:focus,
.modal-content input[type="password"]:focus,
.modal-content input[type="email"]:focus {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

/* Fix browser autofill white background */
.modal-content input:-webkit-autofill,
.modal-content input:-webkit-autofill:hover,
.modal-content input:-webkit-autofill:focus,
.modal-content input:-webkit-autofill:active,
.modal-content input[type="email"]:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 30px var(--bg-app) inset !important;
    -webkit-text-fill-color: var(--text-main) !important;
    transition: background-color 5000s ease-in-out 0s;
}

.share-link-copy {
    display: flex;
    /* gap: 8px; */
    margin-bottom: 24px;
}

.share-link-copy>* {
    margin-right: 8px;
}

.share-link-copy>*:last-child {
    margin-right: 0;
}

#share-url {
    flex: 1;
    background: var(--bg-app);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.9rem;
}

#copy-btn {
    background: var(--primary);
    color: white;
}

.close-modal {
    width: 100%;
}

.hidden {
    display: none !important;
}

/* Store Grid */
.app-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    /* gap: 20px; */
    grid-gap: 20px;
    /* Fallback for older browsers */
    flex: 1;
    /* Take remaining space */
    min-height: 0;
    /* Allow shrinking for scrollbar */
    overflow-y: auto;
    padding: 10px;
}

.app-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: all 0.2s ease;
    text-align: center;
    text-decoration: none;
    /* Remove underline from anchor */
    cursor: pointer;
}

.app-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.5);
    border-color: var(--primary);
    background: var(--bg-app);
    /* Slightly lighter on hover? or just keep surface */
}

.app-card img {
    width: 64px;
    height: 64px;
    border-radius: 14px;
    margin-bottom: 12px;
    object-fit: cover;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.app-card h4 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--text-main);
    font-weight: 600;
}

.upload-card {
    border: 2px dashed var(--border);
    background: transparent;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* gap: 12px; */
    color: var(--text-dim);
    transition: all 0.2s ease;
}

.upload-card>* {
    margin-bottom: 12px;
}

.upload-card>*:last-child {
    margin-bottom: 0;
}

.upload-card:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(99, 102, 241, 0.05);
}

.upload-card svg {
    width: 48px;
    height: 48px;
    opacity: 0.5;
}

.upload-card:hover svg {
    opacity: 1;
}

/* Hide the old delete button by default as we move to long-press */
.delete-project-btn {
    display: none !important;
}

.app-card-content {
    display: flex;
    flex-direction: column;
    /* gap: 2px; */
}

.app-card-content>* {
    margin-bottom: 2px;
}

.app-card-content>*:last-child {
    margin-bottom: 0;
}

.app-author {
    font-size: 0.85rem;
    color: var(--primary);
    font-weight: 500;
}

.app-email {
    font-size: 0.75rem;
    color: var(--text-dim);
    opacity: 0.8;
}

.app-date {
    font-size: 0.75rem;
    color: var(--text-dim);
    margin-top: 4px;
}

/* Generation Progress */
#generation-progress {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    animation: fadeIn 0.3s ease;
}

#generation-progress.hidden {
    display: none;
}

.progress-container {
    width: 100%;
    max-width: 400px;
    height: 6px;
    background: #334155;
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 8px;
    position: relative;
}

.progress-bar {
    width: 30%;
    height: 100%;
    background: var(--primary);
    border-radius: 3px;
    position: absolute;
    animation: scan 2s infinite linear;
}

#upload-progress-modal,
#upload-success-modal {
    z-index: 3000;
    /* On top of everything */
}

#upload-progress-bar {
    animation: none;
    left: 0;
    transition: width 0.2s ease;
}

@keyframes scan {
    0% {
        left: -30%;
    }

    100% {
        left: 100%;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#update-status.success {
    color: var(--status-green) !important;
    font-weight: bold;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    header {
        padding: 0 12px;
    }

    .logo span {
        /* Keep visible on mobile as requested */
        display: inline;
    }

    .actions button span {
        display: none;
        /* Hide text labels on mobile */
    }

    .actions button svg {
        margin-right: 0 !important;
    }

    main {
        position: relative;
        overflow: hidden;
        margin-top: calc(64px + env(safe-area-inset-top, 0px));
    }

    .chat-section,
    .preview-section {
        position: absolute;
        /* inset: 0; */
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        transition: transform 0.3s ease-in-out;
        background: var(--bg-app);
        /* Ensure opacity */
    }

    /* Stacked behaviors */
    .chat-section.mobile-active {
        transform: translateX(0);
        z-index: 10;
        height: calc(100vh - 64px - env(safe-area-inset-top, 0px));
        /* Keep header visible for chat */
    }

    .chat-section.mobile-inactive {
        transform: translateX(-100%);
        z-index: 5;
    }

    /* Force Full Screen Preview Wrapper */
    .preview-wrapper {
        width: 100% !important;
        height: 100% !important;
        box-shadow: none !important;
        border-radius: 0 !important;
        top: 0 !important;
        left: 0 !important;
        transform: none !important;
    }

    .preview-section.mobile-active {
        transform: translateX(0);
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 200;
        background: var(--bg-app);
    }

    .preview-header,
    .preview-device-header {
        display: none !important;
    }

    .preview-mask {
        border: none !important;
        border-radius: 0 !important;
    }

    .resize-handle {
        display: none !important;
    }

    #mobile-view-switcher {
        display: flex !important;
        z-index: 201 !important;
    }

    header {
        height: auto;
        min-height: 64px;
        padding: env(safe-area-inset-top, 0) 8px 0 8px;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        z-index: 110;
        overflow-x: auto;
        justify-content: flex-start;
        /* gap: 8px; */
    }

    /* Obsolete .provider-menu mobile positioning removed (handled by JS now) */

    header>* {
        margin-right: 8px;
    }

    header>*:last-child {
        margin-right: 0;
    }

    .logo {
        font-size: 1rem;
    }

    .connection-status {
        margin-left: 4px;
    }

    .app-info {
        margin-left: 4px;
        padding-left: 8px;
        white-space: nowrap;
    }

    .app-info span {
        font-size: 0.8rem;
        max-width: 80px;
        overflow: hidden;
        text-overflow: ellipsis;
    }


    .actions>* {
        margin-right: 2px;
    }

    .actions>*:last-child {
        margin-right: 0;
    }

    #user-display span {
        font-size: 0.7rem !important;
    }

    .preview-content {
        padding: 0;
        height: 100%;
    }

    /* Raise chat input to be above switcher */
    .chat-input-container,
    #generation-progress {
        padding-bottom: 90px;
    }
}

/* Base style for switcher (hidden on desktop) */
#mobile-view-switcher {
    display: none;
    position: fixed;
    bottom: calc(4px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 4px;
    z-index: 50;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    flex-direction: row;
    align-items: center;
    /* gap: 0; */
    /* Remove gap to make calculations easier, use margin if needed */
    touch-action: none !important;
    /* Prevent browser scroll/zoom */
    user-select: none;
    /* Prevent text selection */
}

.active-indicator {
    position: absolute;
    top: 4px;
    bottom: 4px;
    left: 4px;
    /* Default start, will be changed by JS */
    width: 0;
    /* Will be set by JS */
    background: var(--border);
    border-radius: 20px;
    z-index: 0;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
}

.drag-handle {
    width: 24px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    margin-right: 4px;
    margin-bottom: 0;
    touch-action: none !important;
    /* Critical for drag */
    position: relative;
    z-index: 1;
}

.drag-handle:active {
    cursor: grabbing;
}

.handle-bar {
    width: 4px;
    height: 20px;
    /* Vertical Handle */
    background: var(--border);
    border-radius: 2px;
}

.view-btn {
    padding: 8px 16px;
    border-radius: 20px;
    background: transparent;
    color: var(--text-dim);
    font-size: 0.9rem;
    border: none;
    cursor: pointer;
    font-weight: 600;
    white-space: nowrap;
    touch-action: manipulation;
    position: relative;
    z-index: 1;
    transition: color 0.2s;
}

.view-btn.active {
    color: var(--text-main);
    background: transparent;
    /* Remove existing background */
}

/* Loading Bubble & Progress */
.loading-bubble {
    color: var(--text-dim);
    font-style: italic;
    font-size: 0.9rem;
    display: flex;
    flex-direction: column;
    /* gap: 8px; */
}

.loading-bubble>* {
    margin-bottom: 8px;
}

.loading-bubble>*:last-child {
    margin-bottom: 0;
}

.loading-bar-container {
    width: 100%;
    height: 4px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
    overflow: hidden;
    margin-top: 4px;
}

.loading-bar {
    height: 100%;
    background: var(--primary);
    width: 0%;
    transition: width 0.3s ease;
    animation: loading 2s infinite ease-in-out;
}

@keyframes loading {
    0% {
        width: 0%;
        translateX: -100%;
    }

    50% {
        width: 70%;
    }

    100% {
        width: 100%;
    }
}

/* Disabled Input State */
.chat-input-container.disabled {
    opacity: 0.5;
    pointer-events: none;
    filter: grayscale(1);
    background: var(--bg-app);
    /* visual clue */
}

/* Edit Message Button */
.edit-msg-btn {
    background: transparent;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 1rem;
    padding: 4px;

    /* Permanent Visibility & Positioning */
    opacity: 1;
    /* Always visible */
    position: absolute;
    bottom: 4px;
    right: 4px;
    /* Bottom right */

    transition: color 0.2s, transform 0.2s;
}

/* Ensure container has relative positioning */
.message.user {
    position: relative;
    padding-bottom: 24px;
    /* Add space for the button */
}

/* No hover opacity change needed anymore */

.edit-msg-btn:hover {
    color: var(--primary);
    transform: scale(1.1);
}



.message.assistant.global-threshold-error {
    background: #fee2e2;
    border-left: 4px solid #ef4444;
    color: #b91c1c;
    font-weight: 500;
}

/* Users Modal Styles */
.users-view {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    padding-right: 5px;
}

.user-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    grid-gap: 16px;
    padding: 10px;
    overflow-y: auto;
}

.user-vignette {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    display: flex;
    align-items: center;
    /* gap: 16px; */
    cursor: pointer;
    transition: all 0.2s ease;
}

.user-vignette>* {
    margin-right: 16px;
}

.user-vignette>*:last-child {
    margin-right: 0;
}

.user-vignette:hover {
    transform: translateY(-2px);
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.05);
}

.user-avatar {
    width: 48px;
    height: 48px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: bold;
    flex-shrink: 0;
}

.user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.user-name {
    font-weight: 600;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-email {
    font-size: 0.8rem;
    color: var(--text-dim);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-budget {
    font-size: 0.85rem;
    color: #4caf50;
    font-weight: 600;
    margin-top: 4px;
}

.user-detail-view {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.user-apps-list {
    display: flex;
    flex-direction: column;
    /* gap: 8px; */
    overflow-y: auto;
    padding: 10px;
}

.user-apps-list>* {
    margin-bottom: 8px;
}

.user-apps-list>*:last-child {
    margin-bottom: 0;
}

.user-app-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.user-app-row:hover {
    background: var(--border);
    border-color: var(--primary);
}

/* Indeterminate Progress Bar */
.progress-bar-indeterminate {
    position: absolute;
    height: 100%;
    background: var(--primary);
    border-radius: 4px;
    animation: progress-indeterminate 1.5s infinite ease-in-out;
}

@keyframes progress-indeterminate {
    0% {
        left: -40%;
        width: 40%;
    }

    50% {
        width: 60%;
    }

    100% {
        left: 100%;
        width: 40%;
    }
}

/* AI Cost Modal Styles */
.transaction-table th {
    font-size: 0.8rem;
    color: var(--text-dim);
    border-bottom: 2px solid var(--border);
}

.transaction-table td {
    padding: 12px;
    font-size: 0.85rem;
    border-bottom: 1px solid var(--border);
}

.transaction-table tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

.stat-card {
    background: var(--bg-card);
    padding: 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    /* gap: 8px; */
}

.stat-card>* {
    margin-bottom: 8px;
}

.stat-card>*:last-child {
    margin-bottom: 0;
}

.stat-card h5 {
    margin: 0;
    font-size: 0.75rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-card .value {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-main);
}

.stat-card .sub-value {
    font-size: 0.8rem;
    color: var(--status-green);
}

/* AI Provider Selector - Header Integrated */
.provider-selector-container {
    position: relative;
    display: flex;
    align-items: center;
}

.provider-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    /* Square-ish to match header buttons */
    padding: 2px 8px;
    color: var(--text-dim);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    /* gap: 6px; */
    transition: all 0.2s ease;
    height: 28px;
}

.provider-btn>* {
    margin-right: 6px;
}

.provider-btn>*:last-child {
    margin-right: 0;
}

.provider-btn:hover {
    border-color: var(--accent);
    color: var(--text-primary);
    background: var(--bg-item);
}

.provider-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    width: 200px;
    overflow: hidden;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.1s ease;
    z-index: 9999;
    backdrop-filter: blur(8px);
}

.provider-option {
    padding: 10px 14px;
    display: flex;
    align-items: center;
    /* gap: 12px; */
    cursor: pointer;
    transition: background 0.2s;
}

.provider-option>* {
    margin-right: 12px;
}

.provider-option>*:last-child {
    margin-right: 0;
}

.provider-option:hover {
    background: rgba(255, 255, 255, 0.05);
}

.provider-option.selected {
    background: rgba(99, 102, 241, 0.1);
}

.provider-icon {
    font-size: 1.2rem;
}

.provider-info {
    display: flex;
    flex-direction: column;
}

.provider-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-main);
}

.provider-desc {
    font-size: 0.65rem;
    color: var(--text-dim);
}

@media (max-width: 768px) {
    #current-provider-name {
        display: none;
    }

    .chat-input-container textarea {
        padding-right: 60px;
    }
}

/* OpenRouter Model Modal Styles */
.filter-tab {
    padding: 6px 14px;
    font-size: 0.75rem;
    font-weight: 500;
    border-radius: 16px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    transition: all 0.2s;
}

.filter-tab:hover {
    color: var(--text-main);
}

.filter-tab.active {
    background: var(--primary);
    color: white;
}

.model-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: all 0.2s;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    min-width: 0;
}

.model-card:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}

.model-card.selected {
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.05);
}

.model-card.selected::after {
    content: "✓";
    position: absolute;
    top: 10px;
    right: 10px;
    background: var(--primary);
    color: white;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: bold;
}

.model-name {
    font-weight: 700;
    font-size: 1rem;
    color: var(--text-main);
    margin-bottom: 2px;
    word-break: break-word;
}

.model-id {
    font-family: monospace;
    font-size: 0.75rem;
    color: var(--text-dim);
    opacity: 0.6;
    word-break: break-all;
}

.model-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 4px;
    font-size: 0.8rem;
}

.model-price {
    color: var(--status-green);
    font-weight: 600;
}

.model-tag {
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: bold;
    text-transform: uppercase;
}

.tag-coding {
    background: rgba(99, 102, 241, 0.2);
    color: #818cf8;
}

.tag-free {
    background: rgba(16, 185, 129, 0.2);
    color: #34d399;
}

.loading-spinner {
    width: 30px;
    height: 30px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top: 3px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    display: inline-block;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}




.message-model-name {
    font-size: 0.65rem;
    color: var(--text-dim);
    display: block;
    margin-top: 2px;
    opacity: 0.7;
    text-align: right;
}