:root {
    --bg: #0a0a0f;
    --bg-surface: #141420;
    --bg-card: #1a1a2e;
    --text-primary: #ffffff;
    --text-secondary: #8888a0;
    --text-muted: #555570;
    --accent: #7c3aed;
    --accent-glow: rgba(124, 58, 237, 0.25);
    --accent-dim: rgba(124, 58, 237, 0.12);
    --border: #2a2a3e;
    --border-accent: #3a2a6e;
    --error: #ef4444;
    --nav-height: 64px;
    --nav-bottom-offset: 16px;
    --top-safe: env(safe-area-inset-top, 0px);
    --bottom-safe: env(safe-area-inset-bottom, 0px);
    --bottom-bar-total: calc(var(--nav-height) + var(--nav-bottom-offset) + var(--bottom-safe));
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
    height: 100vh;
}

/* ============ APP LAYOUT ============ */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 600px;
    margin: 0 auto;
}

/* ============ TOP SAFE AREA ============ */
.top-safe-area {
    flex-shrink: 0;
    height: calc(var(--top-safe) + 44px);
    position: relative;
}

.credits-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    background: linear-gradient(135deg, rgba(124,58,237,0.3), rgba(124,58,237,0.15));
    border: 1px solid var(--border-accent);
    color: white;
    padding: 6px 14px;
    border-radius: 9999px;
    font-size: 0.8rem;
    font-weight: 600;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
    position: absolute;
    bottom: 4px;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
    z-index: 10;
}

.credits-badge:active {
    transform: translateX(-50%) scale(0.95);
}

.credits-icon {
    width: 16px;
    height: 16px;
    color: var(--accent);
}

/* ============ TOP BAR ============ */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 20px 12px;
    flex-shrink: 0;
}

.top-bar h1 {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

/* ============ SCROLLABLE CONTENT ============ */
.content {
    flex: 1;
    overflow-y: auto;
    padding: 0 16px 16px;
    position: relative;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(var(--bottom-bar-total) + 68px);
}

.page {
    display: none;
    flex-direction: column;
    gap: 24px;
    animation: pageIn 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.page.active {
    display: flex;
}

@keyframes pageIn {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ============ TEXTAREA CARD ============ */
.textarea-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 4px;
    display: flex;
    flex-direction: column;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.textarea-card:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-dim), 0 4px 20px rgba(124, 58, 237, 0.1);
}

.textarea-card textarea {
    width: 100%;
    min-height: 120px;
    background: transparent;
    color: var(--text-primary);
    border: none;
    padding: 16px;
    font-size: 0.95rem;
    resize: none;
    font-family: inherit;
    line-height: 1.5;
}

.textarea-card textarea::placeholder {
    color: var(--text-muted);
    transition: opacity 0.3s;
}

.textarea-card textarea:focus {
    outline: none;
}

.textarea-card textarea:focus::placeholder {
    opacity: 0.5;
}

.textarea-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px 10px;
}

.textarea-footer-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.add-image-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(255,255,255,0.06);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
    -webkit-tap-highlight-color: transparent;
}

.add-image-btn:active {
    background: rgba(255,255,255,0.1);
    transform: scale(0.95);
}

/* ============ IMAGE THUMBNAIL ============ */
.image-thumbnail {
    position: relative;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    overflow: visible;
    flex-shrink: 0;
    animation: thumbIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes thumbIn {
    from { opacity: 0; transform: scale(0.5); }
    to { opacity: 1; transform: scale(1); }
}

.image-thumbnail img {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    object-fit: cover;
    border: 1px solid var(--border);
}

.thumbnail-delete {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--error);
    border: 2px solid var(--bg-card);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: white;
    padding: 0;
    transition: transform 0.2s;
    -webkit-tap-highlight-color: transparent;
    z-index: 2;
}

.thumbnail-delete:active {
    transform: scale(0.85);
}

.char-count {
    color: var(--text-muted);
    font-size: 0.8rem;
    transition: color 0.3s;
}

.char-count.warning {
    color: #f59e0b;
}

.char-count.danger {
    color: var(--error);
}

/* ============ SECTIONS ============ */
.section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.section h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.see-all {
    color: var(--accent);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.see-all:active {
    opacity: 0.7;
}

/* ============ STYLE GRID ============ */
.style-grid {
    display: flex;
    overflow-x: auto;
    gap: 12px;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.style-grid::-webkit-scrollbar {
    display: none;
}

.style-card {
    flex: 0 0 auto;
    width: 130px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.style-card:active {
    transform: scale(0.95);
}

.style-card-image {
    width: 130px;
    height: 130px;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--bg-card), var(--bg-surface));
    border: 2px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: center;
    padding: 8px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.style-card-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.style-card.selected .style-card-image {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), 0 0 16px var(--accent-glow);
}

.style-card-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 130px;
    transition: color 0.2s;
}

.style-card.selected .style-card-label {
    color: var(--text-primary);
}

/* ============ THEME GRID ============ */
.theme-grid {
    display: flex;
    overflow-x: auto;
    gap: 12px;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.theme-grid::-webkit-scrollbar {
    display: none;
}

.theme-card {
    flex: 0 0 auto;
    width: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.theme-card:active {
    transform: scale(0.95);
}

.theme-swatch {
    width: 100px;
    height: 70px;
    border-radius: 12px;
    border: 2px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.theme-card.selected .theme-swatch {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), 0 0 16px var(--accent-glow);
}

.theme-colors {
    display: flex;
    width: 100%;
    height: 100%;
}

.theme-colors > div {
    flex: 1;
    height: 100%;
}

.theme-swatch-icon {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-card);
}

.theme-swatch-icon svg {
    width: 28px;
    height: 28px;
    color: var(--text-secondary);
}

.theme-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    transition: color 0.2s;
}

.theme-card.selected .theme-label {
    color: var(--accent);
}

/* ============ STICKY GENERATE BUTTON ============ */
.sticky-generate {
    position: fixed;
    bottom: calc(var(--bottom-bar-total) + 8px);
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 32px);
    max-width: 568px;
    z-index: 40;
    padding: 0;
}

.generate-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 16px;
    border-radius: 14px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 1.05rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    -webkit-tap-highlight-color: transparent;
    position: relative;
    overflow: hidden;
}

.generate-btn:not(:disabled) {
    background: linear-gradient(135deg, var(--accent), #6d28d9);
    border-color: transparent;
    color: white;
    box-shadow: 0 4px 20px var(--accent-glow);
}

.generate-btn:not(:disabled):active {
    transform: scale(0.97);
    box-shadow: 0 2px 10px var(--accent-glow);
}

.generate-btn:disabled {
    cursor: default;
}

.sparkle-icon {
    flex-shrink: 0;
    transition: transform 0.3s ease;
}

.generate-btn:not(:disabled):hover .sparkle-icon,
.generate-btn:not(:disabled):active .sparkle-icon {
    transform: rotate(15deg) scale(1.1);
}

/* ============ FLOATING BOTTOM NAVIGATION ============ */
.bottom-nav {
    position: fixed;
    bottom: calc(var(--nav-bottom-offset) + var(--bottom-safe));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: var(--nav-height);
    background: rgba(20, 20, 36, 0.95);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 20px;
    flex-shrink: 0;
    padding: 0 8px;
    width: calc(100% - 32px);
    max-width: 568px;
    z-index: 50;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.04) inset;
    animation: navSlideUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.2s both;
}

@keyframes navSlideUp {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

.nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.65rem;
    font-weight: 500;
    cursor: pointer;
    padding: 8px 16px;
    border-radius: 16px;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    -webkit-tap-highlight-color: transparent;
    position: relative;
}

.nav-item:active {
    transform: scale(0.9);
}

.nav-item.active {
    color: var(--accent);
    background: rgba(124, 58, 237, 0.12);
}

.nav-item svg {
    flex-shrink: 0;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.nav-item.active svg {
    transform: scale(1.1);
}

/* ============ STYLES MODAL ============ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 200;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 24px 24px 0 0;
    width: 100%;
    max-width: 600px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}

.modal-overlay.active .modal-content {
    transform: translateY(0);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 20px 16px;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1.2rem;
    font-weight: 700;
}

.modal-close {
    background: rgba(255,255,255,0.06);
    border: 1px solid var(--border);
    border-radius: 50%;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.modal-close:active {
    transform: scale(0.9);
    background: rgba(255,255,255,0.1);
}

.modal-body {
    overflow-y: auto;
    padding: 16px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(var(--bottom-safe) + 16px);
}

.modal-style-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-style-card:active {
    transform: scale(0.95);
}

.modal-style-image {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--bg-card), var(--bg-surface));
    border: 2px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-align: center;
    padding: 6px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.modal-style-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.modal-style-card.selected .modal-style-image {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), 0 0 16px var(--accent-glow);
}

.modal-style-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
    transition: color 0.2s;
}

.modal-style-card.selected .modal-style-label {
    color: var(--text-primary);
}

/* ============ SETTINGS PAGE ============ */
.settings-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.settings-section h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.btn {
    padding: 14px 16px;
    border-radius: 12px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    text-align: center;
    transition: all 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
    -webkit-tap-highlight-color: transparent;
}

.btn:active {
    transform: scale(0.97);
    opacity: 0.85;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--accent);
    color: var(--accent);
}

.btn-outline:active {
    background: var(--accent-dim);
}

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

.btn-credit:active {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent);
}

.buy-credits-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.buy-credits-grid .btn:last-child {
    grid-column: span 2;
}

/* ============ EMPTY STATE ============ */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 60px 20px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* ============ LOADING ============ */
.loading-overlay {
    position: absolute;
    inset: 0;
    background-color: var(--bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    gap: 16px;
    color: var(--text-secondary);
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid rgba(255,255,255,0.08);
    border-left-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ============ DYNAMIC PLACEHOLDER ============ */
.placeholder-overlay {
    position: absolute;
    top: 16px;
    left: 16px;
    right: 16px;
    pointer-events: none;
    font-size: 0.95rem;
    line-height: 1.5;
    color: var(--text-muted);
    white-space: pre-wrap;
    font-family: inherit;
}

.placeholder-overlay .cursor {
    display: inline-block;
    width: 2px;
    height: 1.1em;
    background: var(--accent);
    vertical-align: text-bottom;
    animation: blink 0.8s step-end infinite;
    margin-left: 1px;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

.textarea-wrapper {
    position: relative;
}

/* ============ UTILITY ============ */
.hidden {
    display: none !important;
}

.error-message {
    color: var(--error);
    font-size: 0.8rem;
    margin-top: 4px;
    word-break: break-all;
}
