:root {
    --bg-primary: #12141a;
    --bg-secondary: #1e222b;
    --bg-elevated: #282d38;
    --text-primary: #f0f2f5;
    --text-muted: #9aa3b2;
    --accent-color: #3b82f6;
    --accent-hover: #2563eb;
    --accent-glow: rgba(59, 130, 246, 0.35);
    --success-color: #22c55e;
    --danger-color: #ef4444;
    --draw-color: #8b5cf6;
    --border-subtle: rgba(255, 255, 255, 0.08);
    --shadow-toolbar: 0 4px 24px rgba(0, 0, 0, 0.45);
    --touch-min: 44px;
    --toolbar-gap: 8px;
    --radius-sm: 8px;
    --radius-md: 12px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
    height: 100%;
}

body {
    font-family: 'Inter', 'Roboto', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    height: 100%;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    touch-action: manipulation;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
}

/* ——— Toolbar ——— */
.app-toolbar {
    flex-shrink: 0;
    z-index: 300;
    position: relative;
    background: linear-gradient(180deg, var(--bg-secondary) 0%, #181c24 100%);
    border-bottom: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-toolbar);
    pointer-events: auto;
    isolation: isolate;
}

.toolbar-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--toolbar-gap);
    padding: 10px 12px;
}

.toolbar-row-main {
    justify-content: space-between;
}

.toolbar-row-draw {
    border-top: 1px solid var(--border-subtle);
    background: rgba(0, 0, 0, 0.15);
    padding-top: 8px;
    padding-bottom: 8px;
}

.toolbar-group {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--toolbar-gap);
}

.toolbar-group-end {
    justify-content: flex-end;
}

.app-title {
    font-family: 'Oswald', sans-serif;
    font-size: 1.35rem;
    font-weight: 500;
    color: var(--text-primary);
    letter-spacing: 0.04em;
    white-space: nowrap;
    flex-shrink: 0;
}

/* Unified tool buttons — hit area matches visual */
.tool-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: var(--touch-min);
    min-width: var(--touch-min);
    padding: 10px 14px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    background: var(--bg-elevated);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.8125rem;
    font-weight: 500;
    line-height: 1.2;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    transition: background 0.15s, border-color 0.15s, box-shadow 0.15s, transform 0.1s;
    position: relative;
    overflow: visible;
}

.tool-btn::after {
    content: '';
    position: absolute;
    inset: -6px;
    z-index: -1;
}

.tool-btn:hover {
    background: #323845;
    border-color: rgba(255, 255, 255, 0.14);
}

.tool-btn:active {
    transform: scale(0.97);
}

.tool-btn.active {
    background: linear-gradient(135deg, var(--accent-color), var(--accent-hover));
    border-color: transparent;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1), 0 4px 14px var(--accent-glow);
    color: #fff;
}

.tool-btn-success {
    background: linear-gradient(135deg, #16a34a, var(--success-color));
    border-color: transparent;
    color: #fff;
}

.tool-btn-success:hover {
    filter: brightness(1.08);
}

.tool-btn-accent {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    border-color: transparent;
    color: #fff;
}

.tool-btn-draw {
    background: #2a2438;
    border-color: rgba(139, 92, 246, 0.25);
}

.tool-btn-draw.active,
.tool-btn-draw[aria-expanded='true'] {
    background: linear-gradient(135deg, #7c3aed, var(--draw-color));
    border-color: transparent;
    box-shadow: 0 4px 14px rgba(139, 92, 246, 0.35);
    color: #fff;
}

.tool-icon {
    font-size: 1.1rem;
    line-height: 1;
    flex-shrink: 0;
}

.tool-label {
    white-space: nowrap;
}

.tool-control-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    display: none;
}

input[type='file'] {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

label.tool-btn {
    cursor: pointer;
}

/* Shapes dropdown */
.shapes-dropdown {
    position: relative;
    display: inline-flex;
}

.shapes-dropdown-panel {
    display: none;
    position: fixed;
    z-index: 500;
    min-width: 200px;
    max-width: calc(100vw - 16px);
    max-height: min(45vh, 240px);
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-toolbar);
    padding: 6px;
    -webkit-overflow-scrolling: touch;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
}

.shapes-dropdown.open .shapes-dropdown-panel {
    display: grid;
}

.shape-option {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
    width: 100%;
    min-height: 40px;
    padding: 8px 10px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.75rem;
    font-weight: 500;
    text-align: left;
    cursor: pointer;
    touch-action: manipulation;
}

.shape-option:hover,
.shape-option:focus-visible {
    background: rgba(59, 130, 246, 0.2);
    outline: none;
}

.shape-option.active {
    background: var(--accent-color);
    color: #fff;
}

/* Zoom */
.zoom-container {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: var(--touch-min);
    padding: 0 4px;
}

.zoom-slider {
    width: 100px;
    height: 28px;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    cursor: pointer;
    touch-action: manipulation;
}

.zoom-slider::-webkit-slider-runnable-track {
    height: 6px;
    background: var(--bg-elevated);
    border-radius: 3px;
    border: 1px solid var(--border-subtle);
}

.zoom-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 28px;
    height: 28px;
    margin-top: -11px;
    background: linear-gradient(135deg, var(--accent-color), var(--accent-hover));
    border-radius: 50%;
    border: 2px solid #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

.zoom-value {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    min-width: 42px;
    text-align: center;
}

.color-picker {
    width: var(--touch-min);
    height: var(--touch-min);
    padding: 4px;
    border: 2px solid var(--border-subtle);
    border-radius: var(--radius-md);
    cursor: pointer;
    touch-action: manipulation;
    background: var(--bg-elevated);
}

.size-slider {
    width: 100px;
    height: 28px;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    touch-action: manipulation;
}

.size-slider::-webkit-slider-runnable-track {
    height: 6px;
    background: var(--bg-elevated);
    border-radius: 3px;
    border: 1px solid var(--border-subtle);
}

.size-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 28px;
    height: 28px;
    margin-top: -11px;
    background: var(--draw-color);
    border-radius: 50%;
    border: 2px solid #fff;
}

/* ——— Canvas ——— */
.canvas-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: #0a0c10;
    cursor: grab;
    min-height: 0;
    touch-action: none;
    z-index: 1;
}

.canvas-container.dragging { cursor: grabbing; }
.canvas-container.move-mode { cursor: grab; }
.canvas-container.image-mode { cursor: default; }
.canvas-container.draw-mode { cursor: crosshair; }
.canvas-container.text-mode { cursor: text; }

.canvas-viewport {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    width: 100%;
    height: 100%;
}

.media-layer, .text-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
}

.media-layer { z-index: 10; }
.text-layer { z-index: 20; }

.draggable-image, .draggable-video {
    position: absolute;
    user-select: none;
    -webkit-user-drag: none;
    cursor: move;
    border: 2px solid transparent;
    touch-action: none;
}

.draggable-image.selected, .draggable-video.selected {
    border-color: var(--accent-color);
}

#drawingCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 30;
    pointer-events: none;
}

.canvas-container.draw-mode #drawingCanvas {
    pointer-events: auto;
}

/* ——— Overlays ——— */
.notification {
    position: fixed;
    top: calc(80px + var(--safe-top));
    right: 16px;
    background: var(--success-color);
    color: white;
    padding: 14px 20px;
    border-radius: var(--radius-md);
    z-index: 1000;
    font-size: 0.875rem;
    box-shadow: var(--shadow-toolbar);
}

.notification.error { background: var(--danger-color); }
.notification.info { background: var(--accent-color); }

.online-counter {
    position: fixed;
    bottom: calc(16px + var(--safe-bottom));
    right: 16px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    padding: 10px 14px;
    border-radius: 999px;
    z-index: 100;
    font-size: 0.8125rem;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: var(--shadow-toolbar);
}

.online-dot {
    width: 8px;
    height: 8px;
    background: var(--success-color);
    border-radius: 50%;
}

.upload-progress-container {
    position: fixed;
    bottom: calc(16px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    padding: 16px 20px;
    border-radius: var(--radius-md);
    z-index: 1000;
    display: none;
    flex-direction: column;
    gap: 10px;
    box-shadow: var(--shadow-toolbar);
}

.upload-progress-bar {
    width: 200px;
    max-width: 70vw;
    height: 8px;
    background: var(--bg-secondary);
    border-radius: 4px;
    overflow: hidden;
}

.upload-progress-fill {
    height: 100%;
    background: var(--success-color);
    width: 0%;
    transition: width 0.3s;
}

.text-element {
    position: absolute;
    padding: 6px 8px;
    color: white;
    border: 1px dashed rgba(255, 255, 255, 0.35);
    border-radius: 4px;
    cursor: move;
    min-width: 80px;
    min-height: 28px;
    touch-action: none;
}

.text-element.editing {
    cursor: text;
    border-style: solid;
    background: rgba(0, 0, 0, 0.55);
}

.text-element.selected {
    border-color: var(--accent-color);
}

.connection-status {
    position: fixed;
    top: calc(12px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 10000;
    padding: 12px 20px;
    background: var(--danger-color);
    color: white;
    border-radius: var(--radius-md);
    opacity: 0;
    transition: opacity 0.3s;
    font-size: 0.875rem;
    pointer-events: none;
}

.connection-status.visible { opacity: 1; }
.connection-status.connected { background: var(--success-color); }

/* ——— Mobile ——— */
@media (max-width: 768px) {
    .toolbar-row-main {
        flex-direction: column;
        align-items: stretch;
    }

    .app-title {
        text-align: center;
        order: -1;
        font-size: 1.2rem;
        padding-bottom: 4px;
    }

    .toolbar-group,
    .toolbar-group-end {
        justify-content: center;
        width: 100%;
    }

    .tool-label {
        display: none;
    }

    .tool-btn {
        min-width: var(--touch-min);
        padding: 10px 12px;
    }

    .tool-btn::after {
        inset: -8px;
    }

    .zoom-slider,
    .size-slider {
        width: 80px;
    }

    .shapes-dropdown-panel {
        max-height: min(40vh, 220px);
    }

    .online-counter {
        font-size: 0.75rem;
        padding: 8px 12px;
    }
}

@media (max-width: 400px) {
    .toolbar-row-draw {
        justify-content: center;
    }
}
