* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    background: var(--html-bg, #ffffff); /* Fallback for safe areas */
}

/* Theme CSS Variables */
:root {
    --bg-primary: rgba(255, 255, 255, 0.95);
    --bg-secondary: white;
    --bg-hover: rgba(102, 126, 234, 0.1);
    --text-primary: #333;
    --text-secondary: #666;
    --text-muted: #999;
    --border-color: #eee;
    --accent: #667eea;
    --accent-secondary: #764ba2;
    --shadow: rgba(0, 0, 0, 0.1);
    --shadow-strong: rgba(0, 0, 0, 0.15);
    --overlay: rgba(0, 0, 0, 0.3);
    --modal-overlay: rgba(0, 0, 0, 0.5);
}

body.dark-mode {
    --bg-primary: rgba(30, 30, 35, 0.95);
    --bg-secondary: #1e1e23;
    --bg-hover: rgba(102, 126, 234, 0.2);
    --text-primary: #e5e5e5;
    --text-secondary: #aaa;
    --text-muted: #777;
    --border-color: #3a3a42;
    --shadow: rgba(0, 0, 0, 0.3);
    --shadow-strong: rgba(0, 0, 0, 0.4);
    --overlay: rgba(0, 0, 0, 0.5);
    --modal-overlay: rgba(0, 0, 0, 0.7);
}

/* Smooth theme transitions */
#header,
.drawer,
.tool-palette,
.modal-content,
select,
.form-group input[type="text"] {
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh; /* Full viewport including safe areas with viewport-fit=cover */
    touch-action: none;
}

#app {
    position: relative;
    height: 100vh;
}

/* Slim Header */
#header {
    display: flex;
    align-items: center;
    height: calc(44px + env(safe-area-inset-top, 0px));
    padding: env(safe-area-inset-top, 0px) 12px 0 12px;
    background: var(--bg-primary);
    box-shadow: 0 2px 8px var(--shadow);
    z-index: 100;
}

#puzzleName {
    flex: 1;
    font-weight: 600;
    font-size: 16px;
    margin-left: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-primary);
}

#headerRight {
    display: flex;
    align-items: center;
    gap: 12px;
}

#headerRight #progressText {
    font-weight: 600;
    color: var(--accent);
    font-size: 14px;
}

.header-btn {
    width: 36px;
    height: 36px;
    padding: 6px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--accent);
    box-shadow: none;
}

.header-btn svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.header-btn:hover {
    background: var(--bg-hover);
    transform: none;
    box-shadow: none;
}

/* Slide-out Drawer */
.drawer {
    position: fixed;
    top: 0;
    left: -280px;
    width: 280px;
    height: 100%;
    background: var(--bg-secondary);
    box-shadow: 2px 0 20px var(--shadow-strong);
    z-index: 200;
    transition: left 0.3s ease;
    overflow-y: auto;
}

.drawer.open {
    left: 0;
}

.drawer-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--overlay);
    z-index: 150;
}

.drawer-overlay.open {
    display: block;
}

.drawer-content {
    padding: 20px;
}

.drawer-content h3 {
    color: var(--accent);
    font-size: 14px;
    margin: 20px 0 12px 0;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.drawer-content h3:first-child {
    margin-top: 0;
}

.drawer-group {
    margin-bottom: 12px;
}

.drawer-group label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--text-primary);
}

.drawer-group select {
    width: 100%;
}

.drawer-row {
    display: flex;
    gap: 8px;
}

.drawer-row select {
    flex: 1;
}

.drawer-upload-btn {
    width: 40px;
    min-width: 40px;
    padding: 8px;
    font-size: 18px;
    font-weight: bold;
}

.drawer-content > button {
    width: 100%;
    margin-bottom: 8px;
}

.drawer-content p {
    font-size: 13px;
    color: var(--text-secondary);
    margin: 8px 0;
}

/* Floating Tool Palette */
.tool-palette {
    position: fixed;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
    right: 20px;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    border-radius: 12px;
    box-shadow: 0 4px 20px var(--shadow-strong);
    padding: 4px;
    z-index: 100;
    opacity: 0.75;
    transition: opacity 0.2s, background 0.3s;
    touch-action: none;
}

.tool-palette:hover,
.tool-palette.dragging {
    opacity: 1;
}

.tool-palette.horizontal {
    flex-direction: row;
}

.tool-palette.horizontal .tool-palette-handle {
    padding: 0 4px;
    display: flex;
    align-items: center;
    writing-mode: vertical-rl;
}

.tool-palette.horizontal .tool-divider {
    width: 1px;
    height: auto;
    margin: 4px 0;
}

.tool-palette-handle {
    text-align: center;
    color: var(--text-muted);
    font-size: 12px;
    padding: 4px 0;
    cursor: grab;
    user-select: none;
}

.tool-palette-handle:active {
    cursor: grabbing;
}

.tool-btn {
    width: 44px;
    height: 44px;
    padding: 8px;
    margin: 2px;
    border-radius: 8px;
    background: transparent;
    color: var(--accent);
    box-shadow: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tool-btn svg {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.tool-btn:hover {
    background: var(--bg-hover);
    transform: none;
    box-shadow: none;
}

.tool-btn.active {
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent-secondary) 100%);
    color: white;
}

.tool-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.tool-btn:disabled:hover {
    background: transparent;
}

.tool-divider {
    height: 1px;
    background: var(--border-color);
    margin: 4px 8px;
}

select {
    padding: 8px 12px;
    border: 2px solid var(--accent);
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    background: var(--bg-secondary);
    color: var(--text-primary);
    transition: all 0.3s;
}

select:hover {
    border-color: var(--accent-secondary);
}

button {
    padding: 10px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

button:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

button:active {
    transform: translateY(0);
}

button.active {
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
}

#referenceBtn.active {
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}

#uploadBtn {
    padding: 8px 12px;
    min-width: 36px;
    font-size: 18px;
    font-weight: bold;
}


#canvasContainer {
    position: absolute;
    top: calc(44px + env(safe-area-inset-top, 0px)); /* Below header */
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}

#puzzleCanvas {
    display: block;
    cursor: grab;
    touch-action: none;
}

#puzzleCanvas:active {
    cursor: grabbing;
}


@media (max-width: 768px) {
    #header {
        padding: 0 8px;
    }

    #puzzleName {
        font-size: 14px;
        margin-left: 8px;
    }

    .drawer {
        width: 260px;
        left: -260px;
    }

    .tool-palette {
        bottom: calc(20px + env(safe-area-inset-bottom, 0px));
        right: 10px;
    }

    .tool-btn {
        width: 40px;
        height: 40px;
        padding: 6px;
    }

    .tool-btn svg {
        width: 22px;
        height: 22px;
    }
}

/* Loading indicator */
.loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 18px;
    color: #667eea;
    font-weight: 600;
}

/* Loading overlay */
.loading-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}

.loading-overlay.active {
    display: flex;
}

.loading-content {
    text-align: center;
    color: white;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

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

.loading-text {
    font-size: 18px;
    font-weight: 600;
}

/* Selection box styling */
.selection-box {
    position: absolute;
    border: 2px dashed #667eea;
    background: rgba(102, 126, 234, 0.1);
    pointer-events: none;
    z-index: 1000;
}

/* Modal styling */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--modal-overlay);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: 12px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 10px 40px var(--shadow-strong);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
    margin: 0;
    color: var(--text-primary);
    font-size: 20px;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    line-height: 1;
    box-shadow: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close svg {
    width: 24px;
    height: 24px;
}

.modal-close:hover {
    color: var(--text-primary);
    transform: none;
    box-shadow: none;
}

.modal-body {
    padding: 20px;
}

/* Settings sections */
.settings-section {
    margin-bottom: 25px;
    padding-bottom: 25px;
    border-bottom: 1px solid var(--border-color);
}

.settings-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.settings-section h3 {
    margin: 0 0 15px 0;
    color: var(--accent);
    font-size: 16px;
}

.form-group {
    margin-bottom: 15px;
}

.form-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
    color: var(--text-primary);
    font-size: 14px;
}

.form-group input[type="text"] {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    transition: border-color 0.3s;
}

.form-group input[type="text"]:focus {
    border-color: var(--accent);
    outline: none;
}

.form-group input[type="color"] {
    width: 60px;
    height: 40px;
    padding: 2px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    background: var(--bg-secondary);
}

.settings-note {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 10px;
}

/* Color pickers row in settings */
.color-pickers-row {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
}

.color-picker-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.color-picker-item span {
    font-size: 11px;
    color: var(--text-secondary);
    text-align: center;
}

.color-picker-item input[type="color"] {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 2px solid var(--border-color);
    padding: 0;
    cursor: pointer;
    overflow: hidden;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background: none;
}

.color-picker-item input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

.color-picker-item input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 50%;
}

.color-picker-item input[type="color"]::-moz-color-swatch {
    border: none;
    border-radius: 50%;
}

/* Button row in settings */
.button-row {
    display: flex;
    gap: 8px;
}

.button-row button {
    flex: 1;
}

/* Auto-save note */
.auto-save-note {
    text-align: center;
    font-style: italic;
    margin-top: 12px;
}

/* Button styles */
.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-secondary {
    background: #f0f0f0;
    color: #333;
}

.btn-secondary:hover {
    background: #e0e0e0;
}

.btn-danger {
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a5a 100%);
    color: white;
}

.btn-danger:hover {
    background: linear-gradient(135deg, #ee5a5a 0%, #dd4a4a 100%);
}

/* Backup list styles */
.backup-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.backup-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid var(--border-color);
}

.backup-item:last-child {
    border-bottom: none;
}

.backup-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.backup-date {
    font-weight: 600;
    color: var(--text-primary);
}

.backup-progress {
    font-size: 12px;
    color: var(--accent);
}

.restore-btn {
    padding: 6px 12px;
    font-size: 12px;
}

/* Uploaded images list */
.uploaded-image-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid var(--border-color);
}

.uploaded-image-item:last-child {
    border-bottom: none;
}

.uploaded-image-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.uploaded-image-name {
    font-weight: 600;
    color: var(--text-primary);
}

.uploaded-image-date {
    font-size: 12px;
    color: var(--text-muted);
}

.delete-image-btn {
    padding: 6px 12px;
    font-size: 12px;
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a5a 100%);
}

/* Disabled button styling */
button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Remote user highlight styling (drawn on canvas, but reference styles) */
.remote-selection {
    stroke-dasharray: 8 4;
}

/* Mobile responsive modal */
@media (max-width: 768px) {
    .modal-content {
        width: 95%;
        max-height: 90vh;
    }

    .modal-header {
        padding: 15px;
    }

    .modal-body {
        padding: 15px;
    }

    .settings-section {
        margin-bottom: 20px;
        padding-bottom: 20px;
    }
}
