/**
 * StickerCanvas v1.0.0
 * NetDynamic.Net
 *
 * TikTok sticker canvas compositor
 */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #1a1a2e;
    touch-action: none;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
}

/* Canvas Wrapper - Full Screen */
#canvasWrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

/* Layout Background Guide (not included in export) */
.layout-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    opacity: 0.4;
    pointer-events: none;
    z-index: 1;
}

/* The Actual Canvas - ONLY this gets screenshotted */
#stickerCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    background: transparent;
}

/* Individual Sticker */
.sticker {
    position: absolute;
    cursor: move;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    transform-origin: center center;
    transition: box-shadow 0.15s ease;
}

.sticker img {
    display: block;
    width: 100%;
    height: auto;
    pointer-events: none;
    -webkit-user-drag: none;
}

.sticker.selected {
    box-shadow: 0 0 0 3px #00d4ff, 0 0 20px rgba(0, 212, 255, 0.5);
}

.sticker.dragging {
    opacity: 0.85;
    z-index: 9999 !important;
}

/* Circle Avatar Style */
.sticker.circle-avatar {
    border-radius: 50%;
    border: 4px solid #FFD700;
    box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
    overflow: hidden;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

.sticker.circle-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center center;
    display: block;
}

.sticker.circle-avatar.selected {
    box-shadow: 0 0 0 3px #00d4ff, 0 0 10px rgba(255, 215, 0, 0.5), 0 0 20px rgba(0, 212, 255, 0.5);
}

/* Settings Cog Button */
.cog-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 50px;
    height: 50px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    font-size: 24px;
    cursor: pointer;
    z-index: 100;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.2s ease;
}

.cog-btn:hover {
    background: rgba(0, 0, 0, 0.8);
    transform: rotate(45deg);
}

.cog-btn:active {
    transform: scale(0.9) rotate(45deg);
}

/* Export FAB Button */
.export-fab {
    position: fixed;
    bottom: 30px;
    right: 20px;
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, #00d4ff, #0099cc);
    color: #fff;
    font-size: 28px;
    cursor: pointer;
    z-index: 100;
    box-shadow: 0 4px 20px rgba(0, 212, 255, 0.4);
    transition: all 0.2s ease;
}

.export-fab:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 30px rgba(0, 212, 255, 0.6);
}

.export-fab:active {
    transform: scale(0.95);
}

.export-fab:disabled {
    opacity: 0.7;
    cursor: wait;
}

/* Settings Panel - Slide from right */
.settings-panel {
    position: fixed;
    top: 0;
    right: -350px;
    width: 320px;
    max-width: 90vw;
    height: 100%;
    background: #1a1a2e;
    z-index: 150;
    transition: right 0.3s ease;
    display: flex;
    flex-direction: column;
    box-shadow: -5px 0 30px rgba(0, 0, 0, 0.5);
}

.settings-panel.active {
    right: 0;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    background: #252542;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.panel-header h3 {
    color: #fff;
    font-size: 18px;
    font-weight: 600;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 15px;
}

.panel-section {
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.panel-section:last-child {
    border-bottom: none;
}

.panel-section h4 {
    color: #888;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 12px;
}

.panel-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    background: rgba(255,255,255,0.1);
    color: #fff;
    transition: all 0.2s ease;
    margin-bottom: 10px;
}

.panel-btn:last-child {
    margin-bottom: 0;
}

.panel-btn:hover {
    background: rgba(255,255,255,0.2);
}

.panel-btn:active {
    transform: scale(0.98);
}

.panel-btn.primary {
    background: linear-gradient(135deg, #00d4ff, #0099cc);
}

.panel-btn.primary:hover {
    background: linear-gradient(135deg, #00e5ff, #00aadd);
}

.panel-btn.danger {
    background: rgba(255, 70, 70, 0.2);
    color: #ff6b6b;
}

.panel-btn.danger:hover {
    background: rgba(255, 70, 70, 0.4);
}

.panel-btn:disabled {
    opacity: 0.6;
    cursor: wait;
}

/* Emoji Input */
.emoji-input-group {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    width: 100%;
    max-width: 100%;
}

.emoji-input-group input {
    flex: 1;
    min-width: 0;
    width: 60px;
    padding: 10px 12px;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 8px;
    background: rgba(255,255,255,0.05);
    color: #fff;
    font-size: 20px;
    text-align: center;
    outline: none;
}

.emoji-input-group input:focus {
    border-color: #00d4ff;
}

.emoji-input-group input::placeholder {
    font-size: 12px;
    color: #555;
}

.panel-btn-small {
    flex-shrink: 0;
    padding: 10px 16px;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    background: linear-gradient(135deg, #00d4ff, #0099cc);
    color: #fff;
    transition: all 0.2s ease;
}

.panel-btn-small:hover {
    background: linear-gradient(135deg, #00e5ff, #00aadd);
}

/* View Mode Toggle */
.view-mode-toggle {
    background: rgba(255,255,255,0.05);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 12px;
}

.view-mode-toggle label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    color: #fff;
    font-size: 14px;
}

.view-mode-toggle input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.view-mode-toggle small {
    display: block;
    color: #666;
    font-size: 11px;
    margin-top: 6px;
    margin-left: 28px;
}

.view-mode-toggle.active {
    background: rgba(0, 212, 255, 0.2);
    border: 1px solid #00d4ff;
}

/* View Mode Active Indicator */
body.view-mode-active::before {
    content: 'VIEW MODE';
    position: fixed;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 212, 255, 0.9);
    color: #000;
    padding: 4px 12px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: bold;
    z-index: 99999;
    pointer-events: none;
}

/* Gifters Config */
.gifters-config {
    margin-bottom: 12px;
}

.gifters-config label {
    display: block;
    color: #888;
    font-size: 12px;
    margin-bottom: 6px;
}

.gifters-config input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 8px;
    background: rgba(255,255,255,0.05);
    color: #fff;
    font-size: 13px;
    outline: none;
}

.gifters-config input:focus {
    border-color: #00d4ff;
}

.gifters-config input::placeholder {
    color: #555;
}

/* Collapsible Section */
.panel-section.collapsible {
    padding-bottom: 0;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    padding: 10px 0;
    margin: -10px 0 0 0;
}

.section-header:hover h4 {
    color: #aaa;
}

.toggle-icon {
    color: #666;
    font-size: 12px;
    transition: transform 0.2s ease;
}

.panel-section.collapsible.open .toggle-icon {
    transform: rotate(180deg);
}

.section-content {
    display: none;
    padding-top: 15px;
    padding-bottom: 20px;
}

.panel-section.collapsible.open .section-content {
    display: block;
}

/* Gifters List */
.gifters-list {
    max-height: 200px;
    overflow-y: auto;
    margin-top: 10px;
}

.gifters-list .empty {
    color: #555;
    font-size: 13px;
    text-align: center;
    padding: 15px;
}

.gifter-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: rgba(255,255,255,0.05);
    border-radius: 8px;
    margin-bottom: 6px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.gifter-item:hover {
    background: rgba(0, 212, 255, 0.2);
}

.gifter-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,0.1);
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: #666;
}

.gifter-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.gifter-info {
    flex: 1;
    min-width: 0;
}

.gifter-name {
    color: #fff;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.gifter-flag {
    font-size: 16px;
    flex-shrink: 0;
}

/* Screen Info */
.screen-info {
    background: rgba(255,255,255,0.05);
    border-radius: 8px;
    padding: 12px;
}

.screen-info p {
    color: #aaa;
    font-size: 13px;
    margin-bottom: 6px;
}

.screen-info p:last-child {
    margin-bottom: 0;
}

.screen-info span {
    color: #00d4ff;
    font-weight: 600;
}

/* Close Button */
.close-btn {
    background: none;
    border: none;
    color: #888;
    font-size: 28px;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.close-btn:hover {
    color: #fff;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.7);
    z-index: 200;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.modal.active {
    display: flex;
}

.modal-content {
    background: #252542;
    border-radius: 16px;
    width: 90%;
    max-width: 400px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    background: #1a1a2e;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.modal-header h3 {
    color: #fff;
    font-size: 18px;
    font-weight: 600;
}

.modal-body {
    padding: 20px;
}

.slider-group {
    margin-bottom: 20px;
}

.slider-group label {
    display: block;
    color: #aaa;
    font-size: 13px;
    margin-bottom: 8px;
}

.slider-group input[type="range"] {
    width: calc(100% - 60px);
    height: 6px;
    -webkit-appearance: none;
    background: rgba(255,255,255,0.1);
    border-radius: 3px;
    outline: none;
    vertical-align: middle;
}

.slider-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    background: #00d4ff;
    border-radius: 50%;
    cursor: pointer;
}

.slider-group span {
    display: inline-block;
    width: 50px;
    text-align: right;
    color: #fff;
    font-size: 14px;
    vertical-align: middle;
    margin-left: 10px;
}

.modal-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 20px;
}

.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px;
    border: none;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    background: rgba(255,255,255,0.1);
    color: #fff;
    transition: all 0.2s ease;
}

.action-btn:hover {
    background: rgba(255,255,255,0.2);
}

.action-btn.danger {
    background: rgba(255, 70, 70, 0.2);
    color: #ff6b6b;
    grid-column: span 2;
}

.action-btn.danger:hover {
    background: rgba(255, 70, 70, 0.4);
}

.action-btn.gold {
    background: linear-gradient(135deg, #FFD700, #FFA500);
    color: #000;
    font-weight: 600;
}

.action-btn.gold:hover {
    background: linear-gradient(135deg, #FFE44D, #FFB732);
}

.action-btn.gold.active {
    background: linear-gradient(135deg, #32CD32, #228B22);
    color: #fff;
}

/* Export Overlay */
.overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.95);
    z-index: 300;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}

.overlay.active {
    display: flex;
}

.overlay-content {
    text-align: center;
    padding: 20px;
    max-width: 100%;
    max-height: 100%;
}

.overlay-content p {
    color: #888;
    margin-bottom: 15px;
    font-size: 14px;
}

.overlay-content img {
    max-width: 90vw;
    max-height: 70vh;
    border-radius: 8px;
    /* Checkerboard for transparency preview */
    background: repeating-conic-gradient(#333 0% 25%, #444 0% 50%) 50% / 20px 20px;
}

.overlay-content button {
    margin-top: 20px;
}

/* Ctrl Button (for overlay close) */
.ctrl-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 12px 24px;
    border: none;
    border-radius: 25px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    background: rgba(255,255,255,0.15);
    color: #fff;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.2s ease;
}

.ctrl-btn:hover {
    background: rgba(255,255,255,0.25);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    padding: 12px 24px;
    background: rgba(0, 200, 100, 0.95);
    color: #fff;
    border-radius: 25px;
    font-size: 14px;
    opacity: 0;
    transition: all 0.3s ease;
    z-index: 400;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.error {
    background: rgba(255, 70, 70, 0.95);
}

/* Loading spinner */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Responsive */
@media (max-width: 400px) {
    .settings-panel {
        width: 100%;
        right: -100%;
    }

    .cog-btn {
        top: 15px;
        right: 15px;
        width: 44px;
        height: 44px;
        font-size: 20px;
    }

    .export-fab {
        bottom: 20px;
        right: 15px;
        width: 54px;
        height: 54px;
        font-size: 24px;
    }
}

/* Safe area for notched phones */
@supports (padding-top: env(safe-area-inset-top)) {
    .cog-btn {
        top: calc(20px + env(safe-area-inset-top));
    }

    .panel-header {
        padding-top: calc(20px + env(safe-area-inset-top));
    }
}

/* Button Row */
.btn-row {
    display: flex;
    gap: 8px;
    margin-bottom: 10px;
}

.btn-row .panel-btn {
    flex: 1;
    margin-bottom: 0;
}

/* File Manager Modal */
.modal-content.file-manager {
    max-width: 350px;
    max-height: 80vh;
}

.file-list {
    max-height: 60vh;
    overflow-y: auto;
    padding-right: 5px;
}

.file-list .loading {
    text-align: center;
    color: #888;
    padding: 20px;
}

.file-list .empty {
    text-align: center;
    color: #666;
    padding: 30px;
}

.file-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 4px;
}

.file-item {
    position: relative;
    aspect-ratio: 1;
    border-radius: 8px;
    overflow: hidden;
    background: rgba(0,0,0,0.3);
}

.file-thumb {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.file-actions {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: none;
    gap: 2px;
    padding: 4px;
    background: linear-gradient(transparent, rgba(0,0,0,0.8));
}

.file-item.selected {
    outline: 3px solid #00d4ff;
}

.file-item.selected .file-actions {
    display: flex;
}

.file-actions button {
    flex: 1;
    padding: 8px;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
}

.file-actions .add-btn {
    background: #00d4ff;
    color: #000;
}

.file-actions .delete-btn {
    background: rgba(255,70,70,0.8);
    color: #fff;
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .export-fab {
        bottom: calc(30px + env(safe-area-inset-bottom));
    }

    .toast {
        bottom: calc(100px + env(safe-area-inset-bottom));
    }
}
