:root {
    /* Theme Variables - Modern & Soft */
    /* Day Mode */
    --bg-app: #f9fafb;
    /* User Req: Softer White */
    --bg-dot: #e2e8f0;
    /* Light Gray Dots */
    --bg-surface: rgba(255, 255, 255, 0.85);
    /* Glass-like */
    --bg-surface-solid: #efefef;

    --text-primary: #1e293b;
    --text-secondary: #64748b;

    --border-color: rgba(0, 0, 0, 0.08);
    --border-hover: rgba(0, 0, 0, 0.15);

    --accent-color: #2563eb;
    /* Royal Blue */
    --accent-hover: #1d4ed8;
    --accent-text: #ffffff;

    --radius-lg: 16px;
    --radius-md: 10px;
    --radius-sm: 6px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.05), 0 4px 6px -2px rgba(0, 0, 0, 0.025);

    --header-height: 60px;
}

[data-theme="dark"] {
    /* Night Mode */
    --bg-app: #0f172a;
    /* Slate 900 */
    --bg-dot: #334155;
    /* Slate 700 Dots */
    --bg-surface: rgba(30, 30, 30, 0.85);
    --bg-surface-solid: #1e1e1e;

    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;

    --border-color: rgba(255, 255, 255, 0.1);
    --border-hover: rgba(255, 255, 255, 0.2);

    --accent-color: #3b82f6;
    --accent-hover: #60a5fa;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}

/* Full Screen Layout */
body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-app);
    background-image: radial-gradient(var(--bg-dot) 1px, transparent 1px);
    background-size: 20px 20px;
    color: var(--text-primary);
    height: 100vh;
    /* Fixed height */
    overflow: hidden;
    /* No scroll on body */
    display: flex;
    flex-direction: column;
    transition: background-color 0.4s ease;
}

/* --- Header --- */
.app-header {
    height: var(--header-height);
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 1.5rem;
    background: var(--bg-surface);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
}

.logo {
    font-weight: 800;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

.logo-img {
    height: 32px;
    width: auto;
    border-radius: 4px;
    /* Optional: smooth corners if it's square */
}

.logo i {
    color: var(--accent-color);
}

.theme-toggle {
    cursor: pointer;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s;
    color: var(--text-primary);
}

.theme-toggle:hover {
    background: var(--border-color);
}

.header-content {
    flex: 1;
    margin: 0 1.5rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    overflow: hidden;
}

.header-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-desc {
    font-size: 0.7rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-left: auto;
    /* Push to right */
}

.header-link {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s;
    white-space: nowrap;
}

.header-link:hover {
    color: var(--accent-color);
}

/* --- Container --- */
.app-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Hide overflow */
    padding: 0;
    /* Remove padding */
    width: 100%;
    max-width: 100%;
}

.intro-section {
    display: none;
}

/* --- Workspace --- */
.workspace {
    flex: 1;
    display: flex;
    overflow: hidden;
    /* No scroll here */
    position: relative;
}

/* Canvas Area */
.canvas-wrapper {
    flex: 1;
    background-color: transparent;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
    padding: 0;
    transition: background-color 0.3s;
}

.canvas-wrapper.mode-fixed {
    /* Dimming removed as per user request */
    cursor: not-allowed;
}

/* Fabric Container override for transform */
.canvas-container {
    /* Base style */
}

.canvas-container.bounded {
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.15);
    transition: transform 0.3s ease-out;
}

/* Tools Panel */
.tools-panel {
    width: 340px;
    background: var(--bg-surface);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    /* Scroll ONLY inside tools */
    padding: 2rem 1.75rem;
    gap: 0.5rem;
    z-index: 10;
}

/* Tool Sections */
.panel-section {
    margin-bottom: 0;
    padding-bottom: 2.5rem;
    border-bottom: none;
}

.panel-section:last-child {
    padding-bottom: 0;
}

.section-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
    padding-bottom: 0;
    border-bottom: none;
}

/* Tool Groups */
.tool-group {
    margin-bottom: 1.75rem;
    padding-bottom: 0;
    border-bottom: none;
}

.tool-group:last-child {
    margin-bottom: 0;
}

.tool-group label {
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    margin-bottom: 1rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
}

/* Controls */
.ratio-options {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.ratio-btn {
    padding: 0.65rem 0.5rem;
    border: 1.5px solid var(--border-color);
    background: var(--bg-app);
    color: var(--text-primary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
    transition: all 0.2s;
    text-align: center;
}

.ratio-btn:hover {
    border-color: var(--accent-color);
    background: rgba(37, 99, 235, 0.05);
}

.ratio-btn.active {
    background: var(--text-primary);
    color: var(--bg-surface-solid);
    border-color: var(--text-primary);
    font-weight: 600;
}

.bg-options {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.5rem;
}

.col-half {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.mt-2 {
    margin-top: 0.8rem;
}

.gradient-controls,
.color-picker-wrapper {
    margin-top: 1rem;
    padding: 1rem;
    background: var(--bg-app);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
}

.gradient-controls label {
    margin-bottom: 0.4rem;
    font-size: 0.7rem;
}

.bg-btn {
    aspect-ratio: 1;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform 0.2s;
    box-shadow: var(--shadow-sm);
    position: relative;
    /* For tooltip positioning */
}

.bg-btn:hover {
    transform: scale(1.1);
}

/* Custom Tooltip */
.bg-btn:hover::after {
    content: attr(title);
    position: absolute;
    bottom: 120%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-primary);
    color: var(--bg-surface-solid);
    padding: 0.4rem 0.6rem;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    z-index: 20;
    opacity: 0;
    animation: fadeInTooltip 0.2s forwards;
    box-shadow: var(--shadow-md);
    font-weight: 500;
}

@keyframes fadeInTooltip {
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(-2px);
    }
}

.bg-btn.active {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px var(--bg-surface-solid), 0 0 0 4px var(--accent-color);
    transform: scale(1.1);
}

.bg-btn[data-bg="none"] {
    border: 1px solid #ddd;
}

/* Texture Pattern for White Button */
.bg-texture-white {
    background-color: #faf9f6;
    background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100' height='100' filter='url(%23noise)' opacity='0.08'/%3E%3C/svg%3E");
    border: 1px solid #e2e8f0;
}

/* Main Buttons */
.primary-btn,
.export-btn {
    width: 100%;
    padding: 0.85rem 1rem;
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.6rem;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}

.primary-btn {
    background: var(--bg-app);
    border: 1.5px solid var(--border-color);
    color: var(--text-primary);
}

.primary-btn:hover {
    border-color: var(--accent-color);
    color: var(--accent-color);
    background: rgba(37, 99, 235, 0.05);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.export-btn {
    background: #15BC41;
    color: #ffffff;
    margin-top: auto;
}

.export-btn:hover {
    background: #12a838;
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* Push to bottom if needed */
.primary-btn:active,
.export-btn:active {
    transform: scale(0.98);
}

/* Inputs */
.custom-dims {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    margin-top: 1rem;
    padding: 1rem;
    background: var(--bg-app);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
}

.custom-dims input[type="number"] {
    text-align: center;
    font-weight: 500;
}

select,
input[type="number"] {
    padding: 0.65rem 0.75rem;
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-app);
    color: var(--text-primary);
    width: 100%;
    font-size: 0.85rem;
    font-weight: 500;
    transition: all 0.2s;
}

select:hover,
input[type="number"]:hover {
    border-color: var(--text-secondary);
}

select:focus,
input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.text-formatting {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.icon-row button,
.action-grid button {
    flex: 1;
    padding: 0.65rem;
    border: 1.5px solid var(--border-color);
    background: var(--bg-app);
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.9rem;
}

.icon-row button:hover,
.action-grid button:hover {
    color: var(--text-primary);
    background: rgba(0, 0, 0, 0.03);
    border-color: var(--text-secondary);
    transform: translateY(-1px);
}

.icon-row button:active,
.action-grid button:active {
    transform: translateY(0);
}

/* Upload Area */
.upload-area {
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-md);
    padding: 2rem 1rem;
    text-align: center;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s;
    background: rgba(0, 0, 0, 0.01);
}

.upload-area:hover {
    border-color: var(--accent-color);
    color: var(--accent-color);
    background: rgba(37, 99, 235, 0.05);
}

.upload-area i {
    font-size: 1.8rem;
    margin-bottom: 0.5rem;
    display: block;
}

/* Gradient Editor UI */
.gradient-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.8rem;
}

.angle-picker {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    position: relative;
    cursor: pointer;
    background: var(--bg-surface);
}

.angle-knob {
    width: 8px;
    height: 8px;
    background: var(--text-primary);
    /* Knob color */
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    /* Center pivot */
    /* Position will be set via JS: transform: translate(dx, dy) */
    pointer-events: none;
}

.gradient-editor {
    position: relative;
    height: 40px;
    margin-bottom: 0.8rem;
    user-select: none;
}

.gradient-preview {
    width: 100%;
    height: 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
    /* Default preview */
    margin-top: 10px;
    /* Space for handles on top if desired, or below.. lets put handles on track */
}

/* We overlay the track for hit testing handles */
.gradient-track {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

.gradient-handle {
    width: 14px;
    height: 14px;
    background: white;
    border: 2px solid white;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2), 0 2px 4px rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    /* Circle or Square? User image had square with pointer. Let's do rounded square */
    border-radius: 2px;
    position: absolute;
    top: 18px;
    /* Just below bar */
    transform: translateX(-50%);
    cursor: grab;
    z-index: 2;
}

.gradient-handle.selected {
    box-shadow: 0 0 0 2px var(--accent-color);
    z-index: 3;
}

.gradient-handle::before {
    /* Triangle pointer pointing up to bar */
    content: '';
    position: absolute;
    top: -6px;
    left: 50%;
    transform: translateX(-50%);
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 6px solid white;
}

/* Handle inner color preview */
.gradient-handle-color {
    width: 100%;
    height: 100%;
    background: currentColor;
    display: block;
}

.stop-details {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.calc-angle-text {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.hint-text {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 0.5rem;
    opacity: 0.7;
}

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

.disabled {
    opacity: 0.4;
    pointer-events: none;
    filter: grayscale(1);
}

.danger-btn {
    color: #ef4444 !important;
    border-color: rgba(239, 68, 68, 0.3) !important;
}

.danger-btn:hover {
    background: #ef4444 !important;
    color: white !important;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Mobile */
@media (max-width: 900px) {
    body {
        height: auto;
        overflow: auto;
    }

    /* Restore scroll on mobile */
    .app-container {
        overflow: visible;
    }

    .workspace {
        flex-direction: column;
        overflow: visible;
    }

    .tools-panel {
        width: 100%;
        height: auto;
        border-left: none;
        border-top: 1px solid var(--border-color);
        padding-bottom: 3rem;
    }

    .canvas-wrapper {
        min-height: 50vh;
        padding: 1rem;
    }
}