:root {
    --m3-surface: #1C1B1F;
    --m3-surface-container: #2B2930;
    --m3-primary: #D0BCFF;
    --m3-on-primary: #381E72;
    --m3-secondary-container: #4A4458;
    --m3-on-secondary-container: #E8DEF8;
    --m3-outline: #938F99;
    --m3-on-surface: #E6E1E5;
    --m3-on-surface-variant: #CAC4D0;
    --m3-tertiary-container: #2D323F;
}


@media (max-width: 840px) {

    /* Core Reset: Essential for mobile symmetry */
    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }


    /* Ensure the main layout doesn't hug the edges */
    .m3-layout {
        width: 100%;
        padding: 16px;
        /* Equal padding on both sides */
        margin: 0 auto;
        display: flex;
        flex-direction: column;
    }

    body {
        background-color: var(--m3-surface);
        color: var(--m3-on-surface);
        font-family: 'Roboto', 'Segoe UI', sans-serif;
        margin: 0;
        padding: 20px;
        overflow-x: hidden;
        /* Prevents the horizontal "jiggle" */
        width: 100%;
    }

    .m3-header h1 {
        text-align: center;
        font-weight: 300;
        letter-spacing: -0.5px;
        margin-bottom: 32px;
    }

    /* Layout Grid */
    .m3-layout {
        display: grid;
        grid-template-columns: 1fr 450px;
        gap: 24px;
        max-width: 1200px;
        margin: 0 auto;
    }

    @media (max-width: 900px) {
        .m3-layout {
            grid-template-columns: 1fr;
        }
    }

    /* Card Design */
    .m3-card {
        background: var(--m3-surface-container);
        border-radius: 24px;
        padding: 20px;
        margin-bottom: 16px;
    }

    .m3-label {
        font-size: 0.85rem;
        color: var(--m3-primary);
        margin: 0 0 16px 0;
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    /* Text Fields */
    .m3-text-field {
        background: #313033;
        padding: 8px 16px;
        border-radius: 12px 12px 0 0;
        border-bottom: 2px solid var(--m3-outline);
        display: flex;
        flex-direction: column;
        flex: 1;
    }

    .m3-text-field label {
        font-size: 11px;
        color: var(--m3-primary);
    }

    .m3-text-field input {
        background: transparent;
        border: none;
        color: white;
        font-size: 1.1rem;
        outline: none;
        padding: 4px 0;
    }

    .m3-input-row {
        display: flex;
        gap: 12px;
        margin-bottom: 16px;
    }

    /* Chips (Toggle Buttons) */
    .m3-chip-group {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .m3-chip {
        background: transparent;
        border: 1px solid var(--m3-outline);
        color: var(--m3-on-surface-variant);
        border-radius: 8px;
        padding: 6px 12px;
        font-size: 13px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .m3-chip:hover {
        background: rgba(208, 188, 255, 0.08);
        border-color: var(--m3-primary);
    }

    /* Color Matrix Grid */
    .color-matrix {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
        gap: 12px;
    }

    .color-unit {
        display: flex;
        flex-direction: column;
        align-items: center;
        background: var(--m3-tertiary-container);
        padding: 8px;
        border-radius: 12px;
    }

    .color-unit span {
        font-size: 10px;
        margin-bottom: 4px;
        opacity: 0.8;
    }

    .color-unit input[type="color"] {
        width: 40px;
        height: 30px;
        border: none;
        background: none;
        cursor: pointer;
    }

    .primary-item {
        border: 1px solid var(--m3-primary);
    }

    .stripe-item {
        border: 1px solid #ff4444;
    }

    /* Radio Presets */
    .m3-fieldset {
        border: 1px solid var(--m3-outline);
        border-radius: 16px;
        padding: 16px;
    }

    .m3-radio-group {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 10px;
    }

    .m3-radio {
        font-size: 13px;
        display: flex;
        align-items: center;
        gap: 6px;
        cursor: pointer;
    }

    .separator {
        grid-column: 1 / -1;
        margin-top: 10px;
        border-top: 1px solid var(--m3-outline);
        padding-top: 10px;
    }

    /* Preview & Export */
    .m3-preview-card {
        background: black;
        border-radius: 0px;
        padding: 0px;
        display: flex;
        justify-content: center;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    }

    #render-target {
        max-width: 100%;
        height: auto;
        border-radius: 0px;
    }

    .m3-export-card {
        margin-top: 20px;
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .m3-button-row {
        display: flex;
        gap: 12px;
    }

    .m3-btn-filled {
        background: var(--m3-primary);
        color: var(--m3-on-primary);
        border: none;
        padding: 12px 24px;
        border-radius: 100px;
        flex: 1;
        font-weight: 600;
        cursor: pointer;
    }

    .m3-btn-tonal {
        background: var(--m3-secondary-container);
        color: var(--m3-on-secondary-container);
        border: none;
        padding: 12px 24px;
        border-radius: 100px;
        cursor: pointer;
    }

    /* Container for all switches */
    .m3-switch-group {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 16px;
        margin-top: 10px;
    }

    .m3-switch-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: rgba(255, 255, 255, 0.04);
        padding: 8px 12px;
        border-radius: 12px;
    }

    .m3-switch-item span {
        font-size: 14px;
        color: var(--m3-on-surface-variant);
    }

    .m3-switch {
        position: relative;
        display: inline-block;
        width: 52px;
        height: 32px;
    }

    .m3-switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }

    /* The Track */
    .m3-slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: var(--m3-surface-container);
        border: 2px solid var(--m3-outline);
        transition: .3s;
        border-radius: 32px;
    }

    /* The Thumb */
    .m3-slider:before {
        position: absolute;
        content: "";
        height: 24px;
        width: 24px;
        left: 2px;
        bottom: 2px;
        background-color: var(--m3-outline);
        transition: .3s;
        border-radius: 50%;
    }

    /* Active State */
    input:checked+.m3-slider {
        background-color: var(--m3-primary);
        border-color: var(--m3-primary);
    }

    input:checked+.m3-slider:before {
        transform: translateX(20px);
        background-color: var(--m3-on-primary);
        height: 24px;
        width: 24px;
        bottom: 2px;
        /* Centers thumb in M3 style */
    }


    /* 1. Global Reset to prevent layout 'leakage' */
    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }

    html,
    body {
        width: 100%;
        overflow-x: hidden;
        /* This kills the horizontal scroll */
        background-color: var(--m3-surface);
    }

    /* 2. Responsive Container */
    .m3-layout {
        width: 100%;
        max-width: 100vw;
        padding: 16px;
        /* Equal padding for both sides */
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    /* 3. Card Fixes */
    .m3-card,
    .m3-panel,
    .m3-preview-section {
        width: 100% !important;
        max-width: 100%;
        min-width: 0;
        /* Critical: allows cards to shrink below their content size */
        margin-left: 0;
        margin-right: 0;
    }

    /* 4. Fix the Color Grid (This is usually what breaks the right side) */
    .color-matrix {
        display: grid;
        /* This tells the grid: 'Fill the row with as many 70px boxes as fit, then wrap' */
        grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
        gap: 8px;
        width: 100%;
    }

    /* 5. Fix the Radio Group */
    .m3-radio-group {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
        gap: 8px;
        width: 100%;
    }

    /* 6. Fix the Fieldsets (Legends often stretch the box) */
    fieldset {
        min-width: 0;
        width: 100%;
        padding: 12px;
    }

    /* 1. Adjust the Main Grid */
    .m3-layout {
        grid-template-columns: 1fr;
        /* Stack everything in one column */
        padding: 12px;
        gap: 16px;
    }

    /* 2. Re-order: Put Preview at the top */
    .m3-preview-section {
        order: -1;
        /* Moves preview section to the very top */
        position: sticky;
        top: 0;
        z-index: 10;
        background-color: var(--m3-surface);
        padding-bottom: 10px;
    }

    /* 4. Improve Touch Targets */
    .m3-radio {
        padding: 12px 0;
        /* Easier to tap radio labels */
        font-size: 15px;
    }

    .m3-chip,
    .m3-btn-filled,
    .m3-btn-tonal {
        min-height: 48px;
        /* Standard M3 touch target size */
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* 5. Optimize Grid for narrow screens */
    .m3-radio-group {
        grid-template-columns: repeat(2, 1fr);
        /* 2 columns instead of 3 */
    }

    .color-matrix {
        grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
        gap: 8px;
    }

    .m3-header h1 {
        font-size: 1.5rem;
        margin: 16px 0;
    }

    /* 1. Prevent inputs from defining the width of the parent */
    input[type="text"],
    input[type="number"] {
        width: 100% !important;
        /* Take up only available space */
        max-width: 100%;
        /* Never exceed the parent */
        box-sizing: border-box;
        /* Include padding in the width calculation */
        min-width: 0;
        /* Allow the flexbox to shrink the input */
    }

    /* 2. Fix the Row Container */
    .m3-input-row {
        display: flex;
        flex-wrap: wrap;
        /* Crucial: wrap to next line if screen is tiny */
        gap: 12px;
        width: 100%;
        /* Stay inside the card */
    }

    /* 3. Fix the Field Container */
    .m3-text-field {
        flex: 1 1 10px;
        /* Grow, shrink, but try to be at least 140px */
        min-width: 0;
        /* Allows the child input to shrink */
        display: flex;
        flex-direction: column;
    }

}

/* 6. Fix for very small phones (iPhone SE etc) */
@media (max-width: 400px) {
    .m3-button-row {
        flex-direction: column;
        /* Stack Download buttons vertically */
    }

    .m3-input-row {
        flex-direction: column;
        /* Stack Text 1 and Text 2 */
    }
}

@media (min-width: 840px) {
    :root {
        --m3-surface: #1C1B1F;
        --m3-surface-container: #2B2930;
        --m3-primary: #D0BCFF;
        --m3-on-primary: #381E72;
        --m3-secondary-container: #4A4458;
        --m3-on-secondary-container: #E8DEF8;
        --m3-outline: #938F99;
        --m3-on-surface: #E6E1E5;
        --m3-on-surface-variant: #CAC4D0;
        --m3-tertiary-container: #2D323F;
    }

    body {
        background-color: var(--m3-surface);
        color: var(--m3-on-surface);
        font-family: 'Roboto', 'Segoe UI', sans-serif;
        margin: 0;
        padding: 20px;
    }

    .m3-header h1 {
        text-align: center;
        font-weight: 300;
        letter-spacing: -0.5px;
        margin-bottom: 32px;
    }

    /* Layout Grid */
    .m3-layout {
        display: grid;
        grid-template-columns: 1fr 450px;
        gap: 24px;
        max-width: 1200px;
        margin: 0 auto;
    }

    /* Card Design */
    .m3-card {
        background: var(--m3-surface-container);
        border-radius: 24px;
        padding: 20px;
        margin-bottom: 16px;
    }

    .m3-label {
        font-size: 0.85rem;
        color: var(--m3-primary);
        margin: 0 0 16px 0;
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    /* Text Fields */
    .m3-text-field {
        background: #313033;
        padding: 8px 16px;
        border-radius: 12px 12px 0 0;
        border-bottom: 2px solid var(--m3-outline);
        display: flex;
        flex-direction: column;
        flex: 1;
    }

    .m3-text-field label {
        font-size: 11px;
        color: var(--m3-primary);
    }

    .m3-text-field input {
        background: transparent;
        border: none;
        color: white;
        font-size: 1.1rem;
        outline: none;
        padding: 4px 0;
    }

    .m3-input-row {
        display: flex;
        gap: 12px;
        margin-bottom: 16px;
    }

    /* Chips (Toggle Buttons) */
    .m3-chip-group {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .m3-chip {
        background: transparent;
        border: 1px solid var(--m3-outline);
        color: var(--m3-on-surface-variant);
        border-radius: 8px;
        padding: 6px 12px;
        font-size: 13px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .m3-chip:hover {
        background: rgba(208, 188, 255, 0.08);
        border-color: var(--m3-primary);
    }

    /* Color Matrix Grid */
    .color-matrix {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
        gap: 12px;
    }

    .color-unit {
        display: flex;
        flex-direction: column;
        align-items: center;
        background: var(--m3-tertiary-container);
        padding: 8px;
        border-radius: 12px;
    }

    .color-unit span {
        font-size: 10px;
        margin-bottom: 4px;
        opacity: 0.8;
    }

    .color-unit input[type="color"] {
        width: 40px;
        height: 30px;
        border: none;
        background: none;
        cursor: pointer;
    }

    .primary-item {
        border: 1px solid var(--m3-primary);
    }

    .stripe-item {
        border: 1px solid #ff4444;
    }

    /* Radio Presets */
    .m3-fieldset {
        border: 1px solid var(--m3-outline);
        border-radius: 16px;
        padding: 16px;
    }

    .m3-radio-group {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 10px;
    }

    .m3-radio {
        font-size: 13px;
        display: flex;
        align-items: center;
        gap: 6px;
        cursor: pointer;
    }

    .separator {
        grid-column: 1 / -1;
        margin-top: 10px;
        border-top: 1px solid var(--m3-outline);
        padding-top: 10px;
    }

    /* Preview & Export */
    .m3-preview-card {
        background: black;
        display: flex;
        justify-content: center;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    }

    #render-target {
        max-width: 100%;
        height: auto;
    }

    .m3-export-card {
        margin-top: 20px;
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .m3-button-row {
        display: flex;
        gap: 12px;
    }

    .m3-btn-filled {
        background: var(--m3-primary);
        color: var(--m3-on-primary);
        border: none;
        padding: 12px 24px;
        border-radius: 100px;
        flex: 1;
        font-weight: 600;
        cursor: pointer;
    }

    .m3-btn-tonal {
        background: var(--m3-secondary-container);
        color: var(--m3-on-secondary-container);
        border: none;
        padding: 12px 24px;
        border-radius: 100px;
        cursor: pointer;
    }

    /* Container for all switches */
    .m3-switch-group {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 16px;
        margin-top: 10px;
    }

    .m3-switch-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: rgba(255, 255, 255, 0.04);
        padding: 8px 12px;
        border-radius: 12px;
    }

    .m3-switch-item span {
        font-size: 14px;
        color: var(--m3-on-surface-variant);
    }

    /* The Switch Base */
    .m3-switch {
        position: relative;
        display: inline-block;
        width: 52px;
        height: 32px;
    }

    .m3-switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }

    /* The Track */
    .m3-slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: var(--m3-surface-container);
        border: 2px solid var(--m3-outline);
        transition: .3s;
        border-radius: 32px;
    }

    /* The Thumb */
    .m3-slider:before {
        position: absolute;
        content: "";
        height: 24px;
        width: 24px;
        left: 2px;
        bottom: 2px;
        background-color: var(--m3-outline);
        transition: .3s;
        border-radius: 50%;
    }

    /* Active State */
    input:checked+.m3-slider {
        background-color: var(--m3-primary);
        border-color: var(--m3-primary);
    }

    input:checked+.m3-slider:before {
        transform: translateX(20px);
        background-color: var(--m3-on-primary);
        height: 24px;
        width: 24px;
        bottom: 2px;
        /* Centers thumb in M3 style */
    }
}