:root {
    --bg-color: #f0f2f5;
    --panel-bg: #ffffff;
    --text-color: #1a1a1a;
    --accent-color: #3b82f6;
    --border-color: #e5e7eb;
    --input-bg: #f9fafb;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}

.dark-theme {
    --bg-color: #0f172a;
    --panel-bg: #1e293b;
    --text-color: #f8fafc;
    --accent-color: #60a5fa;
    --border-color: #334155;
    --input-bg: #0f172a;
    --shadow: 0 10px 15px -3px rgb(0 0 0 / 0.5);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    overflow: hidden;
    transition: background-color 0.3s, color 0.3s;
    user-select: none;
    -webkit-user-select: none;
    overscroll-behavior-y: contain;
}

input, textarea, select {
    user-select: auto;
    -webkit-user-select: auto;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 1200px;
    margin: 0 auto;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.3rem 0.75rem;
    background-color: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
}

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

.pwa-btn {
    background-color: var(--accent-color);
    color: white;
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 0.5rem;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
}

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

.logo h1 {
    font-size: 1.25rem;
    font-weight: 700;
}

#theme-toggle {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 50%;
}

main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.canvas-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1rem;
    position: relative;
    background: radial-gradient(circle, var(--border-color) 1px, transparent 1px);
    background-size: 20px 20px;
}

#main-canvas {
    background-color: white;
    box-shadow: var(--shadow);
    border-radius: 50%;
    max-width: 100%;
    max-height: 100%;
    touch-action: none;
}

.canvas-info {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    background: var(--panel-bg);
    padding: 0.25rem 0.75rem;
    border-radius: 1rem;
    font-size: 0.8rem;
    border: 1px solid var(--border-color);
    pointer-events: none;
}

.history-controls {
    position: absolute;
    bottom: 1rem;
    left: 1rem;
    display: flex;
    gap: 0.5rem;
}

.history-controls button {
    /* Small square background with padding for better touch targets and visual weight */
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    padding: 8px;
    width: auto;
    height: auto;
    border-radius: 8px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 1.1rem;
    box-shadow: var(--shadow);
    transition: transform 0.06s, opacity 0.06s, background-color 0.12s;
}

.history-controls button:active {
    transform: scale(0.96);
    opacity: 0.95;
}

.history-controls button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.controls-panel {
    background-color: var(--panel-bg);
    border-top: 1px solid var(--border-color);
    height: 40%;
    display: flex;
    flex-direction: column;
    box-shadow: 0 -4px 6px -1px rgb(0 0 0 / 0.1);
}

.tabs {
    display: flex;
    overflow-x: auto;
    border-bottom: 1px solid var(--border-color);
    scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
    display: none;
}

.tab-btn {
    flex: 1;
    padding: 0.45rem 0.25rem; /* slightly reduced vertical padding */
    border: none;
    background: none;
    color: var(--text-color);
    cursor: pointer;
    white-space: nowrap;
    font-weight: 500;
    transition: 0.2s;
    opacity: 0.6;
    min-width: 48px; /* reduced min-width to avoid overflow on narrow screens */
    font-size: 0.85rem;
}

.tab-btn.active {
    opacity: 1;
    border-bottom: 3px solid var(--accent-color);
    color: var(--accent-color);
}

.tab-content {
    display: none;
    padding: 0.75rem;
    overflow-y: auto;
    flex: 1;
}

.tab-content.active {
    display: block;
}

.control-group {
    margin-bottom: 0.75rem;
}

.control-group label {
    display: block;
    font-size: 0.875rem;
    margin-bottom: 0.5rem;
    font-weight: 500;
}

input[type="text"],
input[type="number"],
select {
    width: 100%;
    padding: 0.625rem;
    border-radius: 0.5rem;
    border: 1px solid var(--border-color);
    background-color: var(--input-bg);
    color: var(--text-color);
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent-color);
}

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

.checkbox-group input {
    width: auto;
}

.primary-btn, .secondary-btn {
    width: 100%;
    padding: 0.5rem; /* slightly reduced padding to fit tighter layouts */
    border-radius: 0.5rem;
    border: none;
    font-weight: 600;
    cursor: pointer;
    margin-bottom: 0.4rem;
    transition: opacity 0.2s;
    font-size: 0.85rem; /* slightly smaller font for compactness */
    min-height: 40px; /* maintain touch-friendly height while avoiding overflow */
    box-sizing: border-box;
}

.primary-btn {
    background-color: var(--accent-color);
    color: white;
}

.secondary-btn {
    background-color: var(--border-color);
    color: var(--text-color);
}

.primary-btn:active, .secondary-btn:active {
    opacity: 0.8;
}

.icon-btn {
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    border-radius: 0.5rem;
    padding: 0.35rem; /* reduced padding for more compact layout */
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
    min-width: 32px; /* smaller min-width so icons don't force overflow */
    font-size: 0.9rem;
    box-sizing: border-box;
}

.icon-btn:hover {
    background-color: var(--border-color);
}

.active-style {
    background-color: var(--accent-color) !important;
    color: white !important;
    border-color: var(--accent-color) !important;
}

.layer-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.layer-item {
    background-color: var(--input-bg);
    padding: 0.75rem;
    border-radius: 0.5rem;
    border: 1px solid var(--border-color);
    position: relative;
}

.layer-item.selected {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 1px var(--accent-color);
}

.layer-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.remove-layer {
    background: #ef4444;
    color: white;
    border: none;
    /* Force consistent size and alignment with other layer action buttons */
    width: 36px;
    min-width: 36px;
    height: 36px;
    padding: 0.15rem;
    border-radius: 0.5rem;
    cursor: pointer;
    font-size: 0.92rem;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    vertical-align: middle;
    line-height: 1;
}

.export-buttons {
    display: flex;
    gap: 0.5rem;
    align-items: center; /* ensure children align vertically */
}

.export-note {
    font-size: 0.75rem;
    opacity: 0.7;
    margin-top: 0.5rem;
    text-align: center;
}

footer {
    padding: 0.5rem;
    text-align: center;
    font-size: 0.8rem;
    background-color: var(--panel-bg);
    border-top: 1px solid var(--border-color);
}

footer a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 600;
}

/* Modal Styling */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.7);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal {
    background: var(--panel-bg);
    padding: 1.5rem;
    border-radius: 1rem;
    width: 90%;
    max-width: 400px;
    max-height: 85vh;
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
}

.modal h3 {
    margin-bottom: 1rem;
    flex-shrink: 0;
}

#modal-body {
    overflow-y: auto;
    flex: 1;
    margin-bottom: 1.5rem;
    padding-right: 4px;
}

/* Custom Scrollbar for Modal Body */
#modal-body::-webkit-scrollbar {
    width: 4px;
}
#modal-body::-webkit-scrollbar-track {
    background: transparent;
}
#modal-body::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.modal p {
    margin-bottom: 1rem;
    font-size: 0.95rem;
    line-height: 1.4;
}

.modal-buttons {
    flex-shrink: 0;
    display: flex;
    gap: 0.75rem;
}

.modal-buttons button {
    flex: 1;
    margin-bottom: 0;
}

/* Responsive adjustments */
@media (min-width: 768px) {
    main {
        flex-direction: row;
    }
    .controls-panel {
        width: 350px;
        height: 100%;
        border-top: none;
        border-left: 1px solid var(--border-color);
    }
}

.range-with-val {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.range-with-val span {
    min-width: 3rem;
    text-align: right;
    font-size: 0.75rem;
}

/* Layer Order Modal Styles */
.reorder-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 1rem;
    padding-bottom: 1rem;
}

.reorder-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: var(--input-bg);
    padding: 0.5rem;
    border-radius: 0.5rem;
    border: 1px solid var(--border-color);
}

.reorder-preview {
    width: 40px;
    height: 40px;
    object-fit: contain;
    background: #ccc;
    border-radius: 4px;
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    overflow: hidden;
    margin-right: 8px; /* separa la miniatura de los botones */
}

/* Ensure the thumbnail shown in each layer header (preview wrapper created in JS)
   also has the same spacing from the action buttons. Targets the first child
   preview container inside the layer header. */
.layer-item .layer-item-header > div:first-child > div {
    margin-right: 6px;
    margin-left: -8px;
}

.reorder-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.reorder-info {
    flex: 1;
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-color);
}

.reorder-btns {
    display: flex;
    gap: 0.25rem;
}

/* Ensure layer action buttons stay compact, equal height and aligned to the same baseline */
.layer-item .secondary-btn,
.layer-item .icon-btn,
.layer-item .remove-layer {
    /* Force exact metrics for all action buttons (override inline styles if present) */
    width: 36px !important;
    min-width: 36px !important;
    height: 36px !important;
    padding: 0 !important;
    font-size: 0.92rem;
    box-sizing: border-box;

    /* Center contents and ensure identical baseline/vertical alignment */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    vertical-align: middle;
    line-height: 1;
}

/* Ensure any icon-sized buttons inside layer headers match exact metrics */
.layer-item .secondary-btn,
.layer-item .icon-btn {
    /* keep gap control but do not override the enforced metrics above */
    gap: 0;
}

/* Keep reorder buttons small but flexible */
.reorder-btns button {
    width: auto;
    height: auto;
    padding: 4px 6px;
    margin: 0;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    font-size: 1rem;
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
}

.reorder-btns button:disabled {
    opacity: 0.2;
    cursor: not-allowed;
}

/* Make export buttons (PNG / PDF) and the combine button share identical size and alignment */
.export-buttons .primary-btn,
.export-buttons .secondary-btn,
#combine-designs-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem;
    min-height: 44px; /* uniform touch target */
    border-radius: 0.5rem;
    font-weight: 600;
    cursor: pointer;
    margin-top: 0.5rem;
    width: 100%;
    box-sizing: border-box;
    transition: opacity 0.2s;
}

/* Keep the combine button visual style consistent with other secondary buttons */
#combine-designs-btn {
    background-color: var(--border-color);
    color: var(--text-color);
    border: none;
}

/* Eyedropper magnifier styles */
#eyedropper-magnifier {
    position: fixed;
    z-index: 1200;
    pointer-events: none;
    width: 96px;
    height: 96px;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 6px 18px rgba(0,0,0,0.45);
    border: 2px solid rgba(255,255,255,0.9);
    background: #222;
    display: flex;
    align-items: center;
    justify-content: center;
    transform: translate(-50%, -50%);
}

#eyedropper-magnifier canvas {
    width: 96px;
    height: 96px;
    display: block;
    image-rendering: pixelated;
    background: transparent;
    border-radius: 6px;
}

#eyedropper-magnifier .eyecross {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 0;
    height: 0;
    transform: translate(-50%, -50%);
    pointer-events: none;
}

/* Slightly reduce on very small screens so magnifier doesn't cover too much */
@media (max-width: 420px) {
    #eyedropper-magnifier {
        width: 80px;
        height: 80px;
    }
    #eyedropper-magnifier canvas {
        width: 80px;
        height: 80px;
    }
}

/* Ensure the crop and delete buttons have consistent larger touch area and spacing */
.layer-item .remove-layer,
.layer-item .icon-btn.crop-btn {
    height: 39px !important;
    margin-bottom: 7px !important;
}

/* Image crop modal / canvas */
.crop-canvas-wrap {
    display: flex;
    gap: 0.5rem;
    flex-direction: column;
    align-items: center;
}
.crop-canvas {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    max-width: 100%;
    touch-action: none;
}
.crop-controls {
    display: flex;
    gap: 0.5rem;
    width: 100%;
    justify-content: space-between;
    margin-top: 0.5rem;
}
.crop-tip {
    font-size: 0.85rem;
    opacity: 0.8;
}
.crop-handle {
    width: 12px;
    height: 12px;
    background: rgba(255,255,255,0.9);
    border: 1px solid rgba(0,0,0,0.3);
    border-radius: 2px;
    position: absolute;
    transform: translate(-50%, -50%);
    touch-action: none;
}