* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --banana: #FFD93D;
    --banana-dark: #F5C800;
    --banana-light: #FFF3B0;
    --cream: #FFFDF7;
    --dark: #1A1A2E;
    --gray-100: #F8F9FA;
    --gray-200: #E9ECEF;
    --gray-300: #DEE2E6;
    --gray-500: #6C757D;
    --gray-700: #495057;
    --gray-900: #212529;
    --accent: #FF6B6B;
    --success: #4CAF50;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow: 0 4px 6px rgba(0,0,0,0.07);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);
    --radius: 12px;
    --radius-sm: 8px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --action-bar-height: calc(60px + var(--safe-bottom));
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: 'Space Grotesk', -apple-system, BlinkMacSystemFont, sans-serif;
    background: linear-gradient(135deg, var(--banana-light) 0%, var(--cream) 50%, #FFF 100%);
    min-height: 100vh;
    min-height: 100dvh;
    color: var(--gray-900);
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
}

.hidden {
    display: none !important;
}

/* App Layout - Mobile First */
.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    position: relative;
}

/* Bottom Drawer - Mobile */
.drawer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 75vh;
    background: var(--dark);
    color: white;
    border-radius: 20px 20px 0 0;
    padding: 0 1rem 1rem;
    display: flex;
    flex-direction: column;
    z-index: 100;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
}

.drawer.open {
    transform: translateY(0);
}

.drawer-handle {
    display: flex;
    justify-content: center;
    padding: 12px 0;
    cursor: pointer;
}

.handle-bar {
    width: 40px;
    height: 4px;
    background: rgba(255,255,255,0.3);
    border-radius: 2px;
}

.drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 99;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 1rem;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    flex-shrink: 0;
}

.drawer-header h3 {
    font-size: 1.125rem;
    font-weight: 600;
}

.close-drawer {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.5rem;
    line-height: 1;
}

.drawer-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.sidebar-section {
    padding: 1rem 0;
}

.sidebar-section h4 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--gray-500);
    margin-bottom: 0.75rem;
}

/* Drop Zone */
.drop-zone {
    border: 2px dashed rgba(255,255,255,0.2);
    border-radius: var(--radius-sm);
    padding: 1.25rem;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

.drop-zone:hover,
.drop-zone.active {
    border-color: var(--banana);
    background: rgba(255,217,61,0.1);
}

.drop-icon {
    font-size: 1.75rem;
    animation: bounce 2s infinite;
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
}

/* Reference Images */
.image-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.ref-image {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius-sm);
    overflow: hidden;
}

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

.remove-img {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 24px;
    height: 24px;
    background: var(--accent);
    border: none;
    border-radius: 50%;
    color: white;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Threads List */
.threads-list {
    flex: 1;
    overflow-y: auto;
    max-height: 30vh;
}

.thread-item {
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.thread-item:hover,
.thread-item:active {
    background: rgba(255,255,255,0.05);
}

.thread-item.active {
    background: rgba(255,217,61,0.2);
    border-left: 3px solid var(--banana);
}

.thread-title {
    flex: 1;
    font-size: 0.875rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.thread-meta {
    font-size: 0.7rem;
    color: var(--gray-500);
}

.delete-thread {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    font-size: 1rem;
}

.empty-text {
    color: var(--gray-500);
    font-size: 0.875rem;
    text-align: center;
    padding: 1rem;
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding-bottom: var(--action-bar-height);
}

/* Header */
.app-header {
    background: linear-gradient(135deg, var(--banana) 0%, var(--banana-dark) 100%);
    padding: 0.875rem 1rem;
    padding-top: max(0.875rem, env(safe-area-inset-top));
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 50;
}

.menu-btn {
    display: none;
}

.settings-btn {
    height: 36px;
    padding: 0 0.75rem;
    border: none;
    background: rgba(255,255,255,0.9);
    border-radius: var(--radius-sm);
    font-size: 0.8125rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    transition: transform 0.1s, background 0.2s;
    flex-shrink: 0;
    color: var(--gray-700);
}

.settings-btn:hover {
    background: rgba(255,255,255,1);
}

.settings-btn:active {
    transform: scale(0.95);
}

.header-text {
    flex: 1;
    min-width: 0;
}

.header-text h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--gray-900);
}

.header-subtitle {
    font-size: 0.7rem;
    color: var(--gray-700);
}

/* Mobile Action Bar */
.mobile-action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--action-bar-height);
    background: var(--dark);
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
    padding-top: 8px;
    padding-bottom: var(--safe-bottom);
    z-index: 90;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.15);
}

.action-bar-btn,
.action-bar-link {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 6px 16px;
    text-decoration: none;
    transition: opacity 0.2s;
}

.action-bar-btn:active,
.action-bar-link:active {
    opacity: 0.7;
}

.action-icon {
    font-size: 1.25rem;
}

.action-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    font-weight: 500;
}

/* Settings Panel */
.settings-panel {
    background: white;
    margin: 0.75rem;
    padding: 1.25rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.settings-panel h3 {
    font-size: 1rem;
    margin-bottom: 0.75rem;
}

.settings-panel label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
}

.template-input {
    width: 100%;
    padding: 0.625rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-sm);
    font-family: 'JetBrains Mono', monospace;
    font-size: 16px; /* Prevents auto-zoom on iOS */
    resize: none;
}

.template-help {
    font-size: 0.6875rem;
    color: var(--gray-500);
    margin: 0.5rem 0 0.75rem;
}

.template-help code {
    background: var(--gray-100);
    padding: 0.125rem 0.375rem;
    border-radius: 4px;
}

/* Content Area */
.content-area {
    flex: 1;
    padding: 0;
}

/* New Thread Form */
.new-thread-form {
    padding: 1rem;
}

.new-thread-form h2 {
    font-size: 1.125rem;
    margin-bottom: 0.75rem;
}

.main-prompt {
    width: 100%;
    padding: 0.875rem;
    border: 2px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 16px; /* Prevents auto-zoom on iOS */
    font-family: inherit;
    resize: none;
    transition: border-color 0.2s;
}

.main-prompt:focus {
    outline: none;
    border-color: var(--banana);
}

.style-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin: 0.875rem 0;
}

.chip {
    padding: 0.5rem 0.875rem;
    border: 1px solid var(--gray-300);
    border-radius: 20px;
    background: white;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s;
}

.chip.active {
    background: var(--banana);
    border-color: var(--banana);
    font-weight: 600;
}

.btn-generate {
    width: 100%;
    padding: 0.875rem;
    background: linear-gradient(135deg, var(--banana) 0%, var(--banana-dark) 100%);
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.2s;
}

.btn-generate:active:not(:disabled) {
    transform: scale(0.98);
}

.btn-generate:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Thread View */
.thread-view {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.thread-header {
    padding: 0.75rem 1rem;
    background: white;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.back-btn {
    background: none;
    border: none;
    color: var(--gray-700);
    font-size: 0.8125rem;
    cursor: pointer;
    padding: 0.5rem;
}

.thread-header h2 {
    font-size: 0.9375rem;
    font-weight: 600;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Posts Feed */
.posts-feed {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0.5rem;
    background: var(--gray-100);
}

.empty-thread {
    text-align: center;
    padding: 1.5rem 1rem;
}

.empty-thread p {
    margin-bottom: 0.75rem;
    font-size: 1rem;
}

/* Post Card */
.post-wrapper {
    margin-bottom: 0.625rem;
}

.post-wrapper.nested {
    margin-left: 0.75rem;
    padding-left: 0.625rem;
    border-left: 2px solid var(--gray-300);
}

.post-card {
    display: flex;
    background: white;
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.post-card.reply-target {
    box-shadow: 0 0 0 2px var(--banana), var(--shadow);
}

.vote-column {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.625rem 0.375rem;
    background: var(--gray-100);
    min-width: 36px;
}

.vote-btn {
    background: none;
    border: none;
    font-size: 0.875rem;
    color: var(--gray-500);
    cursor: pointer;
    padding: 0.375rem;
    transition: color 0.2s;
}

.vote-btn.up:active {
    color: #FF4500;
}

.vote-btn.down:active {
    color: #7193FF;
}

.vote-score {
    font-size: 0.6875rem;
    font-weight: 600;
    color: var(--gray-700);
}

.post-body {
    flex: 1;
    padding: 0.625rem;
    min-width: 0;
}

.post-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.65rem;
    color: var(--gray-500);
    margin-bottom: 0.375rem;
}

.post-author {
    font-weight: 600;
    color: var(--gray-700);
}

.error-tag {
    font-size: 0.8125rem;
}

.post-text {
    font-size: 0.875rem;
    line-height: 1.5;
    margin-bottom: 0.625rem;
    word-wrap: break-word;
}

/* Touch Zoom Image Container - OPTIMIZED */
.touch-zoom-container,
.post-image-container {
    position: relative;
    width: calc(100% + 1.25rem);
    margin-left: -0.625rem;
    margin-right: -0.625rem;
    overflow: hidden;
    background: var(--gray-100);
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.touch-zoom-image {
    width: 100%;
    height: auto;
    display: block;
    user-select: none;
    -webkit-user-drag: none;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.zoom-controls {
    position: absolute;
    bottom: 0.5rem;
    right: 0.5rem;
    display: flex;
    gap: 0.25rem;
    background: rgba(0,0,0,0.7);
    padding: 0.25rem;
    border-radius: 20px;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.zoom-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: rgba(255,255,255,0.1);
    color: white;
    border-radius: 50%;
    font-size: 1.125rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.1s;
}

.zoom-btn:active:not(:disabled) {
    background: rgba(255,255,255,0.25);
}

.zoom-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.zoom-level {
    color: white;
    font-size: 0.65rem;
    padding: 0 0.5rem;
    display: flex;
    align-items: center;
    font-family: 'JetBrains Mono', monospace;
}

.zoom-hint {
    position: absolute;
    bottom: 0.5rem;
    left: 0.5rem;
    font-size: 0.6rem;
    color: white;
    background: rgba(0,0,0,0.5);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    pointer-events: none;
    opacity: 0.7;
}

/* Fullscreen Overlay */
.fullscreen-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.95);
    z-index: 200;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.fullscreen-image-container {
    position: relative;
    max-width: 100%;
    max-height: calc(100% - 4rem);
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
}

.fullscreen-image-container .touch-zoom-image {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    object-fit: contain;
}

/* Position controls near the image, not at screen edge */
.fullscreen-overlay .zoom-controls {
    position: relative;
    margin-top: 1rem;
    bottom: auto;
    right: auto;
}

.fullscreen-overlay .zoom-hint {
    position: absolute;
    bottom: 0.5rem;
    left: 50%;
    transform: translateX(-50%);
}

/* Close button positioned at top-right of image container */
.fullscreen-close-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    width: 44px;
    height: 44px;
    border: none;
    background: rgba(0,0,0,0.7);
    color: white;
    border-radius: 50%;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.fullscreen-close-btn:active {
    background: rgba(255,255,255,0.2);
}

/* Error Message */
.error-message {
    background: #FFF3CD;
    border: 1px solid #FFEAA7;
    border-radius: var(--radius-sm);
    padding: 0.625rem;
    margin-bottom: 0.625rem;
    font-size: 0.8125rem;
    color: #856404;
}

/* Post Actions */
.post-actions {
    display: flex;
    gap: 0.625rem;
    flex-wrap: wrap;
}

.action-btn {
    background: none;
    border: none;
    color: var(--gray-500);
    font-size: 0.6875rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0.375rem 0.5rem;
    border-radius: 4px;
    transition: background 0.1s;
}

.action-btn:active {
    background: var(--gray-100);
}

/* Reply Box */
.reply-box {
    background: var(--banana-light);
    border: 2px solid var(--banana);
    border-radius: var(--radius);
    padding: 0.875rem;
    margin-top: 0.625rem;
}

.reply-context {
    font-size: 0.6875rem;
    color: var(--gray-600);
    margin-bottom: 0.625rem;
}

.reply-input {
    width: 100%;
    padding: 0.625rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-sm);
    font-size: 16px; /* Prevents auto-zoom on iOS */
    font-family: inherit;
    resize: none;
    margin-bottom: 0.625rem;
}

.reply-input:focus {
    outline: none;
    border-color: var(--banana-dark);
}

.reply-actions {
    display: flex;
    gap: 0.5rem;
}

/* Replies Thread */
.replies-thread {
    margin-top: 0.5rem;
}

/* Thread Footer */
.thread-footer {
    padding: 0.75rem;
    background: white;
    border-top: 1px solid var(--gray-200);
}

.quick-prompts {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    overflow-x: auto;
    padding-bottom: 0.375rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.quick-prompts::-webkit-scrollbar {
    display: none;
}

.quick-chip {
    padding: 0.5rem 0.75rem;
    background: var(--gray-100);
    border: none;
    border-radius: 16px;
    font-size: 0.6875rem;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.1s;
}

.quick-chip:active:not(:disabled) {
    background: var(--gray-200);
}

.quick-chip:disabled {
    opacity: 0.5;
}

.new-post-box {
    display: flex;
    gap: 0.5rem;
}

.new-post-input {
    flex: 1;
    padding: 0.625rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-sm);
    font-size: 16px; /* Prevents auto-zoom on iOS */
    font-family: inherit;
    resize: none;
    min-width: 0;
}

.new-post-input:focus {
    outline: none;
    border-color: var(--banana);
}

/* Buttons */
.btn-primary {
    padding: 0.625rem 1rem;
    background: var(--banana);
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.1s;
    flex-shrink: 0;
}

.btn-primary:active:not(:disabled) {
    background: var(--banana-dark);
}

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn-secondary {
    padding: 0.625rem 1rem;
    background: var(--gray-200);
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.1s;
}

.btn-secondary:active {
    background: var(--gray-300);
}

.btn-danger {
    width: 100%;
    padding: 0.625rem;
    background: transparent;
    border: 1px solid rgba(255,107,107,0.5);
    border-radius: var(--radius-sm);
    color: var(--accent);
    font-weight: 500;
    cursor: pointer;
    transition: background 0.1s;
}

.btn-danger:active {
    background: rgba(255,107,107,0.1);
}

/* =========================
   TABLET & DESKTOP STYLES
   ========================= */
@media (min-width: 768px) {
    .app-container {
        flex-direction: row;
    }

    /* Convert drawer to sidebar on desktop */
    .drawer {
        position: relative;
        transform: none;
        max-height: none;
        border-radius: 0;
        width: 280px;
        flex-shrink: 0;
        order: 2;
    }

    .drawer-handle,
    .drawer-overlay,
    .close-drawer {
        display: none;
    }

    .drawer-header {
        padding-top: 1rem;
    }

    .threads-list {
        max-height: none;
    }

    .main-content {
        flex: 1;
        order: 1;
        padding-bottom: 0;
    }

    /* Hide mobile action bar */
    .mobile-action-bar {
        display: none;
    }

    .app-header {
        padding: 1.25rem 1.5rem;
    }

    .header-text h1 {
        font-size: 1.5rem;
    }

    .header-subtitle {
        font-size: 0.8125rem;
    }

    .settings-panel {
        margin: 1rem 1.5rem;
        padding: 1.5rem;
    }

    .new-thread-form {
        padding: 1.5rem;
        max-width: 720px;
    }

    .posts-feed {
        padding: 1rem;
    }

    .post-wrapper.nested {
        margin-left: 1.5rem;
    }

    .post-body {
        padding: 1rem;
    }

    .touch-zoom-container,
    .post-image-container {
        width: 100%;
        margin-left: 0;
        margin-right: 0;
        border-radius: var(--radius-sm);
    }

    .zoom-hint {
        display: none;
    }

    .thread-footer {
        padding: 1.25rem 1.5rem;
    }

    .vote-btn.up:hover {
        color: #FF4500;
    }

    .vote-btn.down:hover {
        color: #7193FF;
    }

    .ref-image:hover .remove-img {
        opacity: 1;
    }

    .remove-img {
        opacity: 0;
        transition: opacity 0.2s;
    }
}

@media (min-width: 1024px) {
    .drawer {
        width: 320px;
    }

    .new-thread-form {
        max-width: 800px;
    }

    .posts-feed {
        padding: 1.5rem;
    }
}

/* Safe area insets for notch devices */
@supports (padding: max(0px)) {
    .app-header {
        padding-top: max(0.875rem, env(safe-area-inset-top));
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }

    .thread-footer {
        padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
    }
}