:root {
    --md-sys-color-primary: #6750A4;
    --md-sys-color-on-primary: #FFFFFF;
    --md-sys-color-primary-container: #EADDFF;
    --md-sys-color-on-primary-container: #21005D;

    --md-sys-color-secondary: #625B71;
    --md-sys-color-secondary-container: #E8DEF8;

    --md-sys-color-background: #FFFBFE;
    --md-sys-color-on-background: #1C1B1F;
    --md-sys-color-surface: #FFFBFE;
    --md-sys-color-on-surface: #1C1B1F;

    --md-sys-color-surface-variant: #E7E0EC;
    --md-sys-color-on-surface-variant: #49454F;

    --md-sys-color-outline: #79747E;

    /* Error Colors */
    --md-sys-color-error: #B3261E;
    --md-sys-color-error-container: #F9DEDC;
    --md-sys-color-on-error-container: #410E0B;

    --md-sys-typescale-body-large-font: 'Roboto', sans-serif;
    --md-sys-typescale-body-large-size: 1rem;
    /* 16px */

    --md-sys-elevation-2: 0px 3px 1px -2px rgba(0, 0, 0, 0.2),
        0px 2px 2px 0px rgba(0, 0, 0, 0.14),
        0px 1px 5px 0px rgba(0, 0, 0, 0.12);

    /* Font Scale Variable */
    --app-font-scale: 1;
}

[data-theme="dark"] {
    --md-sys-color-primary: #D0BCFF;
    --md-sys-color-on-primary: #381E72;
    --md-sys-color-primary-container: #4F378B;
    --md-sys-color-on-primary-container: #EADDFF;

    --md-sys-color-background: #1C1B1F;
    --md-sys-color-on-background: #E6E1E5;
    --md-sys-color-surface: #1C1B1F;
    --md-sys-color-on-surface: #E6E1E5;

    --md-sys-color-surface-variant: #49454F;
    --md-sys-color-on-surface-variant: #CAC4D0;

    --md-sys-color-outline: #938F99;
    /* Lighter outline for better visibility */
    --md-sys-color-outline-variant: #49454F;
    /* Keep distinct from outline */

    --md-sys-color-error: #F2B8B5;
    --md-sys-color-error-container: #8C1D18;
    --md-sys-color-on-error-container: #F9DEDC;

    --md-sys-color-secondary-container: #4A4458;
    --md-sys-color-on-secondary-container: #E8DEF8;
}


/* Global Reset & Base */
html {
    font-size: calc(16px * var(--app-font-scale));
}

body {
    margin: 0;
    font-family: var(--md-sys-typescale-body-large-font);
    background-color: var(--md-sys-color-background);
    color: var(--md-sys-color-on-background);
    transition: background-color 0.3s, color 0.3s;
}

.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Top App Bar */
.top-app-bar {
    display: flex;
    align-items: center;
    padding: 0 16px;
    height: 64px;
    background-color: var(--md-sys-color-surface);
    color: var(--md-sys-color-on-surface);
}

.leading-icon {
    margin-right: 24px;
    font-size: 24px;
}

.headline {
    flex-grow: 1;
    font-size: 1.375rem;
    font-weight: 400;
    margin: 0;
}

.icon-button {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--md-sys-color-on-surface-variant);
    padding: 12px;
    border-radius: 50%;
}

.icon-button:hover {
    background-color: rgba(0, 0, 0, 0.08);
}

/* Main Content */
.content {
    padding: 16px;
    max-width: 800px;
    /* Center content on large screens */
    margin: 0 auto;
    width: 100%;
    box-sizing: border-box;
}

/* Tabs */
.tabs {
    display: flex;
    justify-content: center;
    margin-bottom: 24px;
    border-bottom: 1px solid var(--md-sys-color-outline);
}

.tab {
    flex: 1;
    background: none;
    border: none;
    padding: 14px 16px;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--md-sys-color-on-surface-variant);
    cursor: pointer;
    border-bottom: 2px solid transparent;
}

.tab.active {
    color: var(--md-sys-color-primary);
    border-bottom-color: var(--md-sys-color-primary);
}

/* Cards */
.card {
    background-color: var(--md-sys-color-surface);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
    /* Elevation 1 - simplified */
    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);
}

/* Input Styles */
.upload-area {
    margin-bottom: 16px;
}

.text-field {
    background-color: var(--md-sys-color-surface-variant);
    border-radius: 4px 4px 0 0;
    border-bottom: 1px solid var(--md-sys-color-on-surface-variant);
    margin-bottom: 16px;
    padding: 8px 16px 0 16px;
    display: flex;
    flex-direction: column;
}

.text-field textarea,
.text-field input {
    background: none;
    border: none;
    outline: none;
    color: var(--md-sys-color-on-surface);
    font-family: inherit;
    font-size: 1rem;
    resize: none;
    width: 100%;
    padding-bottom: 8px;
}

.text-field textarea {
    min-height: 80px;
}

/* Buttons */
.filled-button {
    background-color: var(--md-sys-color-primary);
    color: var(--md-sys-color-on-primary);
    border: none;
    border-radius: 20px;
    padding: 10px 24px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: box-shadow 0.2s;
}

.filled-button:hover {
    box-shadow: var(--md-sys-elevation-2);
}

.filled-button:disabled {
    opacity: 0.38;
    cursor: not-allowed;
    box-shadow: none;
}

.tonal-button {
    background-color: var(--md-sys-color-secondary-container);
    color: var(--md-sys-color-on-secondary-container);
    border: none;
    border-radius: 20px;
    padding: 10px 24px;
    margin-right: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

/* Dark Mode Input Fix */
input,
select,
textarea {
    background-color: transparent;
    color: inherit;
}

/* Specifically for select ensuring dropdown options are readable */
select {
    background-color: var(--md-sys-color-surface-container-low, var(--md-sys-color-surface));
    color: var(--md-sys-color-on-surface);
    border: 1px solid var(--md-sys-color-outline);
    border-radius: 4px;
    padding: 8px;
    width: 100%;
    margin-bottom: 8px;
}

option {
    background-color: var(--md-sys-color-surface-container-high, #2b2930);
    /* Darker specific fallback */
    color: var(--md-sys-color-on-surface);
}


/* Result Area */
.result-area {
    display: flex;
    flex-direction: column;
    gap: 16px;
    align-items: center;
    margin-top: 24px;
}

.generated-image {
    max-width: 100%;
    border-radius: 12px;
    box-shadow: var(--md-sys-elevation-2);
}

.generated-text {
    background-color: var(--md-sys-color-surface-variant);
    padding: 16px;
    border-radius: 12px;
    white-space: pre-wrap;
    width: 100%;
}

.placeholder-text {
    color: var(--md-sys-color-on-surface-variant);
    text-align: center;
    margin-top: 32px;
}

/* Scrollbar Styling for MD3 feel */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--md-sys-color-outline-variant);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--md-sys-color-outline);
}

/* History Area */
.history-area {
    margin-top: 16px;
}

/* Tabs (Segmented Button Style) */
.tabs {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-bottom: 24px;
    padding: 4px;
    background-color: var(--md-sys-color-surface-container-low);
    border-radius: 16px;
    /* Pill shape container */
    width: fit-content;
    margin-left: auto;
    margin-right: auto;
}

.tab {
    background: transparent;
    border: none;
    padding: 10px 24px;
    border-radius: 12px;
    /* Pill shape buttons */
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--md-sys-color-on-surface-variant);
    transition: all 0.2s;
}

.tab.active {
    background-color: var(--md-sys-color-secondary-container);
    color: var(--md-sys-color-on-secondary-container);
    box-shadow: var(--md-sys-elevation-0);
}

.tab:hover:not(.active) {
    background-color: var(--md-sys-color-surface-variant);
}

/* View Sections */
.view-section {
    display: block;
    animation: fadeIn 0.3s ease-out;
}

.view-section.hidden {
    display: none;
}

/* History - Restore Grid */
.history-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
    padding: 8px;
}

.history-card {
    background-color: var(--md-sys-color-surface-container-low);
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: transform 0.2s;
    border: 1px solid var(--md-sys-color-outline-variant);
}

.history-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--md-sys-elevation-level2);
}

.history-img-wrapper {
    width: 100%;
    aspect-ratio: 16/9;
    background-color: #000;
    overflow: hidden;
    cursor: pointer;
}

.history-img-wrapper img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: opacity 0.2s;
}

.history-img-wrapper:hover img {
    opacity: 0.9;
}

.history-content {
    padding: 12px;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.history-prompt {
    font-size: 14px;
    color: var(--md-sys-color-on-surface);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.4;
    flex: 1;
}

.history-meta {
    font-size: 12px;
    color: var(--md-sys-color-on-surface-variant);
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: auto;
}

.history-actions {
    display: flex;
    gap: 4px;
}

/* Light Mode / Root Select Fix */
select {
    background-color: var(--md-sys-color-surface-variant);
    /* Greyish in light mode */
    color: var(--md-sys-color-on-surface);
    /* ... other styles ... */
}

/* Ensure options inside are also visible */
option {
    background-color: var(--md-sys-color-surface);
    color: var(--md-sys-color-on-surface);
}

/* History Actions */
.history-actions-row {
    display: flex;
    gap: 4px;
    margin-left: auto;
}

.icon-button.small {
    padding: 4px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--md-sys-color-on-surface-variant);
}

.icon-button.small:hover {
    background-color: var(--md-sys-color-secondary-container);
    color: var(--md-sys-color-on-secondary-container);
}

/* NEW STYLES for v2 UI */
.input-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.section-headline {
    font-size: 1.25rem;
    font-weight: 500;
    color: var(--md-sys-color-primary);
    margin: 0;
}

.advanced-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin-top: 24px;
    /* More space from prompt */
    padding-top: 16px;
    border-top: 1px solid var(--md-sys-color-outline-variant);
    align-items: center;
}

.toolbar-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 90px;
}

.toolbar-item label {
    font-size: 0.75rem;
    color: var(--md-sys-color-on-surface-variant);
}

.compact-select {
    padding: 6px 8px;
    border-radius: 8px;
    border: 1px solid var(--md-sys-color-outline);
    background: var(--md-sys-color-surface);
    color: var(--md-sys-color-on-surface);
    font-size: 0.85rem;
}

.compact-slider {
    width: 100%;
}

.image-preview {
    width: 100%;
    height: 100%;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 12px;
    display: none;
    /* Hidden by default */
}

/* Container needs relative positioning for repeated preview */
.image-preview-container {
    border: 2px dashed var(--md-sys-color-outline-variant);
    border-radius: 12px;
    padding: 12px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--md-sys-color-secondary);
    position: relative;
    /* Context for absolute preview */
    min-height: 120px;
    /* Ensure height */
    overflow: hidden;
}

.image-preview-container:hover {
    border-color: var(--md-sys-color-primary);
    background-color: var(--md-sys-color-surface-variant);
}

.image-preview-container .placeholder {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
}

.image-preview-container.has-image .placeholder {
    display: none;
}

.url-trigger {
    color: var(--md-sys-color-primary);
    text-decoration: underline;
    cursor: pointer;
    font-size: 0.8em;
}

.url-input-container {
    margin-top: 8px;
    display: flex;
    gap: 4px;
    width: 100%;
    align-items: center;
}

.url-input-container input {
    flex: 1;
    padding: 6px 12px;
    border-radius: 16px;
    border: 1px solid var(--md-sys-color-outline);
    background: var(--md-sys-color-surface-container-high);
    color: var(--md-sys-color-on-surface);
    font-size: 0.85rem;
}

/* Dialog Header */
.dialog-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    /* Less margin */
}

.dialog-title {
    margin: 0;
}

/* Section Divider */
.section-divider {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 24px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--md-sys-color-outline-variant);
    font-weight: bold;
    color: var(--md-sys-color-primary);
}

/* Scrollbar Styling for MD3 feel */
::-webkit-scrollbar {
    width: 6px;
    /* Thinner */
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--md-sys-color-outline-variant);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--md-sys-color-outline);
}

/* Error Card */
.error-card {
    background-color: var(--md-sys-color-error-container);
    color: var(--md-sys-color-on-error-container);
    padding: 16px;
    border-radius: 12px;
    margin-top: 16px;
    display: flex;
    align-items: flex-start;
    gap: 12px;
}

.error-icon {
    font-size: 24px;
}

.error-content {
    flex: 1;
}

.error-title {
    font-weight: bold;
    margin-bottom: 4px;
}

/* Utils */
.hidden {
    display: none !important;
}

/* Dialog Styles */
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.dialog-overlay.open {
    opacity: 1;
    pointer-events: auto;
}

.dialog {
    background-color: var(--md-sys-color-surface);
    color: var(--md-sys-color-on-surface);
    border-radius: 28px;
    padding: 24px;
    width: 90%;
    max-width: 560px;
    /* MD3 recommended max-width */
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: var(--md-sys-elevation-2);
}

.dialog-title {
    font-size: 1.5rem;
    margin: 0 0 16px 0;
}

.dialog-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.section-title {
    font-size: 0.875rem;
    color: var(--md-sys-color-primary);
    font-weight: 500;
    margin-top: 8px;
}

.divider {
    border: none;
    border-top: 1px solid var(--md-sys-color-outline);
    margin: 8px 0;
    opacity: 0.4;
}

.setting-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.setting-item label {
    font-size: 0.875rem;
    color: var(--md-sys-color-on-surface);
}

.input-line {
    border: none;
    border-bottom: 1px solid var(--md-sys-color-outline);
    background: transparent;
    padding: 8px 0;
}

.switch-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
}

.switch-input {
    width: 20px;
    height: 20px;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 24px;
}

.text-button {
    background: none;
    border: none;
    color: var(--md-sys-color-primary);
    font-weight: 500;
    padding: 10px 24px;
    border-radius: 20px;
    cursor: pointer;
}

/* Progress Bar */
.progress-container {
    width: 100%;
    margin-bottom: 20px;
    text-align: center;
}

.progress-bar {
    width: 100%;
    height: 6px;
    background-color: var(--md-sys-color-surface-container-high);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 8px;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background-color: var(--md-sys-color-primary);
    transition: width 0.3s ease;
}

.progress-status {
    font-size: 0.9rem;
    color: var(--md-sys-color-secondary);
    font-family: monospace;
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.fade-in {
    animation: fadeIn 0.5s ease forwards;
}