:root {
    --primary: #fbbf24;
    /* Yellow */
    --primary-dark: #d97706;
    --bg: #0f172a;
    --surface: #1e293b;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --danger: #ef4444;
    --success: #22c55e;
    --radius: 12px;
    --glass: rgba(30, 41, 59, 0.7);
    --collage-gap: 0px;
    --item-radius: 0%;
    --collage-bg: #ffffff;
}



* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Inter', sans-serif;
}

body {
    background-color: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    padding: 20px;
}

#app {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

header {
    text-align: center;
}

header h1 {
    font-family: 'Outfit', sans-serif;
    font-size: 2rem;
    letter-spacing: -1px;
}

.accent {
    color: var(--primary);
}

header p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Canvas Area */
.canvas-container {
    width: 100%;
    aspect-ratio: 1/1;
    background-color: var(--surface);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.2);
    position: relative;
    border: 2px solid rgba(255, 255, 255, 0.05);
}

#collage-canvas {
    width: 100%;
    height: 100%;
    display: grid;
    position: relative;
    gap: var(--collage-gap);
    background-color: var(--collage-bg);
}


/* Navigation & Control Panels */
.controls-nav {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 10px;
}

.nav-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    padding: 5px 10px;
    transition: all 0.2s;
    text-transform: uppercase;
}

.nav-btn.active {
    color: var(--primary);
    border-bottom: 2px solid var(--primary);
}

.control-panel.hidden {
    display: none;
}

.layout-grid {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    padding: 5px 0 15px 0;
}

.layout-preview {
    min-width: 60px;
    height: 60px;
    background: var(--surface);
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    cursor: pointer;
    display: grid;
    gap: 2px;
    padding: 2px;
    transition: all 0.2s;
}

.layout-preview.active {
    border-color: var(--primary);
    background: rgba(251, 191, 36, 0.1);
}

.layout-preview div {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 1px;
}

/* Specific Layout Systems */
.collage-item {
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    cursor: grab;
    position: relative;
    border: 0px solid transparent;
    border-radius: var(--item-radius);
    overflow: hidden;
    transition: outline 0.1s, border-radius 0.2s;
}

.collage-item.active {
    outline: 4px solid var(--primary) !important;
    outline-offset: -4px;
    z-index: 10;
}


/* 1 Image */
.layout-1-sq {
    grid-template-columns: 1fr;
}

.layout-1-wide {
    grid-template-rows: 1fr 2fr 1fr;
    grid-template-columns: 1fr;
}

.layout-1-wide div {
    grid-row: 2;
}

.layout-1-tall {
    grid-template-columns: 1fr 2fr 1fr;
    grid-template-rows: 1fr;
}

.layout-1-tall div {
    grid-column: 2;
}

/* Especiales 1 Foto */
.layout-1-circle {
    place-items: center !important;
}

.layout-1-circle .collage-item,
.layout-1-circle.layout-preview div {
    width: 80% !important;
    height: 80% !important;
    border-radius: 50% !important;
    border: 4px solid var(--primary) !important;
    overflow: hidden;
}

.layout-1-sea .collage-item,
.layout-1-sea.layout-preview div {
    clip-path: polygon(0 15%, 100% 0, 100% 85%, 0 100%) !important;
    -webkit-clip-path: polygon(0 15%, 100% 0, 100% 85%, 0 100%) !important;
}


/* 2 Images */
.layout-2-v {
    grid-template-columns: 1fr 1fr;
}

.layout-2-h {
    grid-template-rows: 1fr 1fr;
}

.layout-2-v-70 {
    grid-template-columns: 2fr 1fr;
}

.layout-2-v-30 {
    grid-template-columns: 1fr 2fr;
}

.layout-2-h-70 {
    grid-template-rows: 2fr 1fr;
}

.layout-2-h-30 {
    grid-template-rows: 1fr 2fr;
}

/* 3 Images */
.layout-3-v {
    grid-template-columns: 1fr 1fr 1fr;
}

.layout-3-h {
    grid-template-rows: 1fr 1fr 1fr;
}

.layout-3-featured-l {
    grid-template-columns: 2fr 1fr;
    grid-template-rows: 1fr 1fr;
}

.layout-3-featured-l div:nth-child(1) {
    grid-row: span 2;
}

.layout-3-featured-r {
    grid-template-columns: 1fr 2fr;
    grid-template-rows: 1fr 1fr;
}

.layout-3-featured-r div:nth-child(1) {
    grid-row: span 2;
    grid-column: 2;
}

.layout-3-featured-r div:nth-child(2) {
    grid-column: 1;
    grid-row: 1;
}

.layout-3-featured-r div:nth-child(3) {
    grid-column: 1;
    grid-row: 2;
}

.layout-3-featured-t {
    grid-template-rows: 2fr 1fr;
    grid-template-columns: 1fr 1fr;
}

.layout-3-featured-t div:nth-child(1) {
    grid-column: span 2;
}

.layout-3-featured-b {
    grid-template-rows: 1fr 2fr;
    grid-template-columns: 1fr 1fr;
}

.layout-3-featured-b div:nth-child(1) {
    grid-row: 2;
    grid-column: span 2;
}

.layout-3-featured-b div:nth-child(2) {
    grid-row: 1;
    grid-column: 1;
}

.layout-3-featured-b div:nth-child(3) {
    grid-row: 1;
    grid-column: 2;
}

/* 4 Images */
.layout-4-grid {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}

.layout-4-v {
    grid-template-columns: 1fr 1fr 1fr 1fr;
}

.layout-4-h {
    grid-template-rows: 1fr 1fr 1fr 1fr;
}

.layout-4-featured-tl {
    grid-template-columns: 2fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
}

.layout-4-featured-tl div:nth-child(1) {
    grid-row: span 2;
    grid-column: span 1;
}

.layout-4-featured-tl div:nth-child(2) {
    grid-column: 2;
    grid-row: 1;
}

.layout-4-featured-tl div:nth-child(3) {
    grid-column: 2;
    grid-row: 2;
}

.layout-4-featured-tl div:nth-child(4) {
    grid-column: span 2;
    grid-row: 3;
}


.layout-4-strips {
    grid-template-columns: 1fr 1fr 1fr 1fr;
}


.collage-item.active {
    outline: 3px solid var(--primary);
    outline-offset: -3px;
    z-index: 5;
}

/* Pan Handle (Joystick) */
.pan-handle {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.9);
    border: 2px solid #000;
    border-radius: 50%;
    color: #000;
    display: none;
    justify-content: center;
    align-items: center;
    cursor: all-scroll;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.collage-item.active .pan-handle {
    display: flex;
}

/* Real-time Drag Ghost */
.drag-ghost {
    position: fixed;
    width: 100px;
    height: 100px;
    background-size: cover;
    background-position: center;
    border-radius: 12px;
    pointer-events: none;
    z-index: 9999;
    opacity: 0.8;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    border: 3px solid var(--primary);
    transform: translate(-50%, -50%) scale(1.1);
    display: none;
}

.collage-item.dragging-source {
    opacity: 0.3;
}


.collage-item.dragging {
    opacity: 0.6;
    transform: scale(0.95);
    z-index: 1000;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8);
    pointer-events: none;
    /* Let the mouse events pass to the slot underneath */
}


#image-editor-overlay {
    margin-top: -10px;
    margin-bottom: 20px;
    background: var(--glass);
    padding: 15px 20px;
    border-radius: var(--radius);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 10;
    backdrop-filter: blur(10px);
}


.editor-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.zoom-box {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
}

.zoom-box label {
    margin-bottom: 0;
    font-size: 0.7rem;
}

#zoom-slider {
    flex: 1;
    accent-color: var(--primary);
}

.swap-box {
    display: flex;
    gap: 8px;
}

.btn-icon.circle {
    background: var(--bg);
    border-radius: 50%;
    width: 36px;
    height: 36px;
}


.placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
    cursor: pointer;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.placeholder:hover {
    background: rgba(255, 255, 255, 0.05);
}

#image-input {
    position: absolute;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

/* Text Overlays */
.text-overlay {
    position: absolute;
    cursor: move;
    user-select: none;
    padding: 10px;
    font-weight: 800;
    font-size: 24px;
    color: #ffff00;
    text-shadow:
        -2px -2px 0 #000,
        2px -2px 0 #000,
        -2px 2px 0 #000,
        2px 2px 0 #000;
    text-transform: uppercase;
    text-align: center;
    width: auto;
    min-width: 50px;
    word-wrap: break-word;
    white-space: pre-wrap;
    /* Crucial for multi-line */
    line-height: 1.1;
    display: inline-block;
    box-sizing: border-box;
}


.text-overlay.selected {
    background: rgba(0, 0, 0, 0.4);
    outline: 2px dashed rgba(255, 255, 255, 0.5);
}

/* Handles */
.handle {
    position: absolute;
    background: white;
    border: 2px solid #000;
    width: 24px;
    height: 24px;
    display: none;
    z-index: 10;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
}

.text-overlay.selected .handle {
    display: flex;
}

.handle-scale {
    top: -12px;
    right: -12px;
    cursor: nesw-resize;
    border-radius: 4px;
}

.handle-width {
    top: 50%;
    left: -20px;
    /* Moved further out to avoid accidental clicks */
    transform: translateY(-50%);
    width: 10px;
    height: 25px;
    cursor: ew-resize;
    border-radius: 2px;
}


.handle svg {
    width: 16px;
    height: 16px;
}


/* Controls */
.controls {
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: var(--glass);
    backdrop-filter: blur(10px);
    padding: 20px;
    border-radius: var(--radius);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

label {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.preset-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.preset-chip {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
}

.preset-chip:hover {
    border-color: var(--primary);
    background: rgba(251, 191, 36, 0.1);
}

.text-input-wrapper {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.text-tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.alignment-tools {
    display: flex;
    gap: 5px;
    background: rgba(0, 0, 0, 0.2);
    padding: 4px;
    border-radius: 8px;
}

.align-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 6px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    transition: all 0.2s;
}

.align-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text);
}

.align-btn.active {
    background: var(--primary);
    color: #000;
}

#custom-text {

    width: 100%;
    padding: 12px 15px;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 0.9rem;
    outline: none;
    resize: none;
}

#custom-text:focus {
    border-color: var(--primary);
}


input[type="text"] {
    flex: 1;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    padding: 10px 14px;
    color: var(--text);
    font-size: 0.9rem;
}

input[type="text"]:focus {
    outline: none;
    border-color: var(--primary);
}

.btn-primary,
.btn-secondary,
.btn-success,
.btn-danger {
    border: none;
    border-radius: 8px;
    padding: 10px 18px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
    font-size: 0.9rem;
}

.btn-primary {
    background: var(--primary);
    color: #000;
}

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.btn-success {
    background: var(--success);
    color: #000;
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-top: 10px;
}

#install-btn {
    grid-column: span 2;
    background: #4f46e5;
    /* Indigo */
    color: white;
    padding: 15px;
    font-size: 1rem;
    animation: pulse-border 2s infinite;
}

@keyframes pulse-border {
    0% {
        box-shadow: 0 0 0 0 rgba(79, 70, 229, 0.4);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(79, 70, 229, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(79, 70, 229, 0);
    }
}


/* Modals */
.hidden {
    display: none !important;
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-content {
    background: var(--surface);
    padding: 24px;
    border-radius: var(--radius);
    max-width: 90%;
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.preset-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.btn-icon {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--primary);
}

.modal-content.wide {
    max-width: 600px;
    width: 95%;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.close-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 2rem;
    cursor: pointer;
}

.preset-manager {
    display: flex;
    flex-direction: column;
    gap: 20px;
    text-align: left;
}

.add-preset-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#new-preset-content {
    min-height: 80px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    color: var(--text);
    resize: vertical;
}


.presets-table-wrapper {
    max-height: 300px;
    overflow-y: auto;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    padding: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

th {
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-muted);
    text-align: left;
}

td {
    font-size: 0.9rem;
    white-space: pre-wrap;
    /* Crucial for showing multi-line in table */
}


.btn-delete {
    color: var(--danger);
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
}

#output-canvas {

    max-width: 100%;
    height: auto;
    border-radius: 8px;
}

.modal-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

#download-link {
    text-decoration: none;
    display: inline-block;
}