:root {
    --bg-color: var(--tg-theme-bg-color, #1c1c1e);
    --text-color: var(--tg-theme-text-color, #ffffff);
    --hint-color: var(--tg-theme-hint-color, #98989f);
    --button-color: var(--tg-theme-button-color, #3390ec);
    --button-text-color: var(--tg-theme-button-text-color, #ffffff);
    --secondary-bg-color: var(--tg-theme-secondary-bg-color, #2c2c2e);
    --accent-color: #64d2ff;
    --safe-area-bottom: env(safe-area-inset-bottom, 20px);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* justify-content: center;  Removed to allow scroll to work better */
    padding: 20px 20px calc(80px + var(--safe-area-bottom)) 20px; /* Add padding for fixed button */
    text-align: center;
    overflow-y: auto; /* Allow scrolling within container */
}

header {
    margin-bottom: 40px;
    flex-shrink: 0;
}

h1 {
    margin: 0;
    font-size: 28px;
    font-weight: 700;
    background: linear-gradient(90deg, var(--button-color), var(--accent-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.5px;
}

header p {
    color: var(--hint-color);
    margin: 8px 0 0;
    font-size: 15px;
}

/* Upload Area */
.upload-area {
    width: 100%;
    max-width: 320px;
    height: 200px;
    background-color: var(--secondary-bg-color);
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.1s ease, background-color 0.2s, border-color 0.2s;
    border: 2px dashed var(--button-color);
    margin-bottom: 20px;
    flex-shrink: 0;
}

.upload-area:active {
    transform: scale(0.98);
    background-color: rgba(51, 144, 236, 0.1);
}

/* Desktop / Drag & Drop Styles */
.desktop-mode .upload-area {
    height: 300px;
    max-width: 500px;
    cursor: pointer;
}

.upload-area.drag-over {
    background-color: rgba(51, 144, 236, 0.2);
    border-color: var(--accent-color);
    transform: scale(1.02);
}

.icon-container {
    color: var(--button-color);
    margin-bottom: 15px;
}

.upload-text {
    font-weight: 600;
    font-size: 17px;
    margin: 0;
}

.upload-subtext {
    color: var(--hint-color);
    font-size: 13px;
    margin: 5px 0 0;
}

/* Loader */
.loader-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    animation: fadeIn 0.3s ease;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--secondary-bg-color);
    border-top: 4px solid var(--button-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 15px;
}

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

/* Progress State */
.progress-container {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.progress-info {
    font-size: 32px;
    font-weight: 700;
    color: var(--text-color);
    margin-bottom: 15px;
}

.progress-bar-bg {
    width: 100%;
    height: 8px;
    background-color: var(--secondary-bg-color);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 15px;
}

.progress-bar-fill {
    height: 100%;
    background-color: var(--button-color);
    width: 0%;
    transition: width 0.2s ease;
}

#status-text {
    color: var(--hint-color);
    font-size: 14px;
    margin: 0;
}

/* Success State */
.success-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    animation: fadeIn 0.5s ease;
    margin-top: auto;
    margin-bottom: auto;
}

.success-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: var(--button-color);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    box-shadow: 0 10px 20px rgba(51, 144, 236, 0.3);
}

.success-container h2 {
    margin: 0 0 5px;
    font-size: 24px;
}

.success-container p {
    color: var(--hint-color);
    margin: 0;
}

.hidden {
    display: none !important;
}

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

/* Template Selection */
.template-selection {
    width: 100%;
    max-width: 400px;
    text-align: center;
    animation: fadeIn 0.4s ease;
    display: flex;
    flex-direction: column;
    /* ensure it takes available space but doesn't force overflow */
}

.template-selection h2 {
    margin: 0 0 20px;
    font-size: 20px;
    flex-shrink: 0;
}

.templates-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    margin-bottom: 30px;
    padding-bottom: 20px; /* Extra padding for scrolling feeling */
}

.template-item {
    background-color: var(--secondary-bg-color);
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
}

.template-item video {
    width: 100%;
    height: auto;
    display: block;
    aspect-ratio: 9/16;
    object-fit: cover;
    border-bottom: 1px solid rgba(0,0,0,0.1);
}

.template-info {
    padding: 10px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.template-info p {
    margin: 0;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-color);
}

.template-price {
    font-size: 11px;
    font-weight: 500;
    color: var(--button-color);
    background: rgba(51, 144, 236, 0.1);
    padding: 2px 8px;
    border-radius: 10px;
    letter-spacing: 0.2px;
}

.template-item.selected {
    border-color: var(--button-color);
    box-shadow: 0 0 0 3px rgba(51, 144, 236, 0.2);
    transform: translateY(-2px);
}

/* Fixed Bottom Button */
.process-btn {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 40px);
    max-width: 380px;
    z-index: 100;
    
    background-color: var(--button-color);
    color: var(--button-text-color);
    border: none;
    border-radius: 12px;
    padding: 14px 28px;
    font-size: 17px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s, transform 0.2s;
    box-shadow: 0 4px 12px rgba(51, 144, 236, 0.3);
    
    /* Handle iPhone home indicator area */
    margin-bottom: var(--safe-area-bottom);
}

.process-btn:active {
    opacity: 0.8;
    transform: translateX(-50%) scale(0.98);
}