/**
 * Asciistrator - ASCII Vector Graphics Engine & Editor
 * Main Stylesheet
 * 
 * Zero dependencies, pure CSS3
 */

/* ============================================
   CSS CUSTOM PROPERTIES (Design Tokens)
   ============================================ */

:root {
    /* Color Palette - Dark Theme (Default) */
    --color-bg-primary: #1a1a2e;
    --color-bg-secondary: #16213e;
    --color-bg-tertiary: #0f0f1a;
    --color-bg-elevated: #252542;
    --color-bg-hover: #2d2d4a;
    --color-bg-active: #3d3d5c;
    
    --color-text-primary: #e8e8f0;
    --color-text-secondary: #a0a0b0;
    --color-text-muted: #6a6a7a;
    --color-text-inverse: #1a1a2e;
    
    --color-border: #3a3a5a;
    --color-border-light: #4a4a6a;
    --color-border-focus: #6366f1;
    
    --color-accent: #6366f1;
    --color-accent-hover: #818cf8;
    --color-accent-active: #4f46e5;
    
    --color-success: #22c55e;
    --color-warning: #f59e0b;
    --color-error: #ef4444;
    --color-info: #3b82f6;
    
    --color-canvas-bg: #0d0d14;
    --color-canvas-grid: #2a2a3a;
    --color-canvas-text: #00ff88;
    
    --color-selection: rgba(99, 102, 241, 0.3);
    --color-selection-border: #6366f1;
    
    /* Spacing Scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 12px;
    --space-lg: 16px;
    --space-xl: 24px;
    --space-xxl: 32px;
    
    /* Typography */
    --font-family-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    --font-family-mono: 'SF Mono', 'Fira Code', 'Consolas', 'Monaco', monospace;
    
    --font-size-xs: 10px;
    --font-size-sm: 12px;
    --font-size-md: 14px;
    --font-size-lg: 16px;
    --font-size-xl: 20px;
    
    --font-weight-normal: 400;
    --font-weight-medium: 500;
    --font-weight-bold: 600;
    
    --line-height-tight: 1.2;
    --line-height-normal: 1.5;
    --line-height-relaxed: 1.75;
    
    /* Border Radius */
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 8px;
    --radius-xl: 12px;
    --radius-full: 9999px;
    
    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.4);
    --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.5);
    
    /* Transitions */
    --transition-fast: 100ms ease;
    --transition-normal: 200ms ease;
    --transition-slow: 300ms ease;
    
    /* Z-Index Scale */
    --z-base: 0;
    --z-dropdown: 100;
    --z-sticky: 200;
    --z-fixed: 300;
    --z-modal-backdrop: 400;
    --z-modal: 500;
    --z-popover: 600;
    --z-tooltip: 700;
    
    /* Layout */
    --toolbar-width: 48px;
    --panel-width: 280px;
    --menu-height: 40px;
    --status-height: 28px;
    --ruler-size: 20px;
}

/* ============================================
   THEME VARIATIONS
   ============================================ */

/* Light Theme */
[data-theme="light"] {
    --color-bg-primary: #f5f5f7;
    --color-bg-secondary: #ffffff;
    --color-bg-tertiary: #e8e8eb;
    --color-bg-elevated: #ffffff;
    --color-bg-hover: #ebebf0;
    --color-bg-active: #dddde3;
    
    --color-text-primary: #1a1a2e;
    --color-text-secondary: #505060;
    --color-text-muted: #909098;
    --color-text-inverse: #ffffff;
    
    --color-border: #d0d0d8;
    --color-border-light: #e0e0e8;
    --color-border-focus: #6366f1;
    
    --color-canvas-bg: #ffffff;
    --color-canvas-grid: #e8e8f0;
    --color-canvas-text: #1a1a2e;
    
    --color-selection: rgba(99, 102, 241, 0.2);
    
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.15);
    --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.2);
}

/* Midnight Theme */
[data-theme="midnight"] {
    --color-bg-primary: #0a0a12;
    --color-bg-secondary: #0f0f1a;
    --color-bg-tertiary: #050508;
    --color-bg-elevated: #14142a;
    --color-bg-hover: #1a1a32;
    --color-bg-active: #24243c;
    
    --color-text-primary: #c8c8d8;
    --color-text-secondary: #8888a0;
    --color-text-muted: #585870;
    
    --color-border: #252540;
    --color-border-light: #303050;
    
    --color-canvas-bg: #000005;
    --color-canvas-grid: #181828;
    --color-canvas-text: #00ffaa;
    
    --color-accent: #8b5cf6;
    --color-accent-hover: #a78bfa;
    --color-accent-active: #7c3aed;
}

/* ============================================
   RESET & BASE STYLES
   ============================================ */

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

html, body {
    height: 100%;
    height: 100dvh; /* Dynamic viewport height */
    width: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-family-ui);
    font-size: var(--font-size-md);
    line-height: var(--line-height-normal);
    color: var(--color-text-primary);
    background-color: var(--color-bg-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Focus Styles */
:focus {
    outline: none;
}

:focus-visible {
    outline: 2px solid var(--color-border-focus);
    outline-offset: 2px;
}

/* Selection */
::selection {
    background-color: var(--color-selection);
    color: var(--color-text-primary);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--color-bg-tertiary);
}

::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
    background: var(--color-border-light);
}

/* ============================================
   APP LAYOUT
   ============================================ */

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height - better for mobile */
    width: 100vw;
    overflow: hidden;
    min-height: 0;
}

/* ============================================
   MENU BAR
   ============================================ */

.menu-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--menu-height);
    padding: 0 var(--space-md);
    background-color: var(--color-bg-secondary);
    border-bottom: 1px solid var(--color-border);
    user-select: none;
    flex-shrink: 0;
    overflow: visible;
    min-width: 0;
    position: relative;
    z-index: var(--z-sticky);
}

.menu-left {
    display: flex;
    align-items: center;
    gap: var(--space-lg);
    flex-shrink: 1;
    min-width: 0;
    overflow: visible;
}

.app-logo {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-bold);
    color: var(--color-accent);
    letter-spacing: -0.5px;
}

.menu-items {
    display: flex;
    gap: var(--space-xs);
}

.menu-item {
    padding: var(--space-xs) var(--space-sm);
    background: none;
    border: none;
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.menu-item:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.menu-item-wrapper {
    position: relative;
}

.menu-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    min-width: 200px;
    background-color: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: var(--z-modal);
    padding: var(--space-xs) 0;
}

.menu-dropdown.show {
    display: block;
}

.menu-dropdown-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    background: none;
    border: none;
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    text-align: left;
    transition: background-color var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.menu-dropdown-item:hover {
    background-color: var(--color-bg-hover);
}

.menu-dropdown-item:active,
.menu-dropdown-item.touch-active {
    background-color: var(--color-bg-active);
}

.menu-shortcut {
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
    margin-left: var(--space-lg);
}

.menu-separator {
    height: 1px;
    background-color: var(--color-border);
    margin: var(--space-xs) 0;
}

.menu-right {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-shrink: 0;
}

.separator {
    color: var(--color-border);
    font-size: var(--font-size-sm);
}

.icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: none;
    border: none;
    color: var(--color-text-secondary);
    font-size: var(--font-size-md);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.icon-btn:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.icon-btn:active {
    background-color: var(--color-bg-active);
}

.icon-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.icon-btn.small {
    width: 24px;
    height: 24px;
    font-size: var(--font-size-sm);
}

/* Tooltip for icon buttons */
.icon-btn[title] {
    position: relative;
}

.icon-btn[title]::after {
    content: attr(title);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 6px;
    padding: 4px 8px;
    background-color: var(--color-bg-primary);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-xs);
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.2s, visibility 0.2s;
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.icon-btn[title]:hover::after {
    opacity: 1;
    visibility: visible;
    transition-delay: 0.3s;
}

.zoom-level {
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    min-width: 45px;
    text-align: center;
}

.theme-select {
    padding: var(--space-xs) var(--space-sm);
    background-color: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    cursor: pointer;
}

.theme-select option {
    background-color: var(--color-bg-secondary);
    color: var(--color-text-primary);
}

/* ============================================
   MAIN CONTENT AREA
   ============================================ */

.main-content {
    display: flex;
    flex: 1 1 0;
    overflow: hidden;
    overflow-x: clip;
    min-height: 0;
    /* 100vh - 40px menu - 28px status = calc(100vh - 68px) */
    height: calc(100vh - 68px);
    position: relative;
}

/* ============================================
   TOOLBAR (LEFT)
   ============================================ */

.toolbar {
    display: flex;
    flex-direction: column;
    width: var(--toolbar-width);
    padding: var(--space-sm);
    background-color: var(--color-bg-secondary);
    border-right: 1px solid var(--color-border);
    gap: var(--space-sm);
    flex-shrink: 0;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
    position: relative;
    z-index: 10;
}

.tool-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-bottom: var(--space-sm);
    border-bottom: 1px solid var(--color-border);
}

.tool-group:last-child {
    border-bottom: none;
}

.tool-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: none;
    border: none;
    color: var(--color-text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.tool-btn:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.tool-btn.active {
    background-color: var(--color-accent);
    color: var(--color-text-inverse);
}

.tool-btn.active:hover {
    background-color: var(--color-accent-hover);
}

/* Custom Tooltips */
.tool-btn {
    position: relative;
}

.tool-btn::after {
    content: attr(title);
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translateY(-50%);
    margin-left: 8px;
    padding: 4px 8px;
    background-color: var(--color-bg-primary);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-xs);
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.2s, visibility 0.2s;
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.tool-btn:hover::after {
    opacity: 1;
    visibility: visible;
    transition-delay: 0.5s;
}

/* Hide empty tooltips */
.tool-btn:not([title])::after,
.tool-btn[title=""]::after {
    display: none;
}

.tool-icon {
    font-size: var(--font-size-lg);
    line-height: 1;
}

.tool-colors {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-xs);
}

.color-preview {
    position: relative;
    width: 28px;
    height: 28px;
}

.stroke-color {
    position: absolute;
    top: 0;
    left: 0;
    width: 18px;
    height: 18px;
    background-color: var(--color-canvas-text);
    border: 2px solid var(--color-border-light);
    cursor: pointer;
    z-index: 2;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.stroke-color:hover {
    transform: scale(1.1);
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.fill-color {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 18px;
    height: 18px;
    background: repeating-conic-gradient(#808080 0% 25%, transparent 0% 50%) 50% / 8px 8px;
    border: 2px solid var(--color-border);
    cursor: pointer;
    z-index: 1;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.fill-color:hover {
    transform: scale(1.1);
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

/* ============================================
   CANVAS CONTAINER
   ============================================ */

.canvas-container {
    flex: 1;
    display: grid;
    grid-template-columns: var(--ruler-size) 1fr auto;
    grid-template-rows: var(--ruler-size) 1fr auto;
    overflow: hidden;
    background-color: var(--color-bg-tertiary);
    min-height: 0; /* Critical for flex + grid combo */
    height: 100%;
}

.ruler-corner {
    background-color: var(--color-bg-secondary);
    border-right: 1px solid var(--color-border);
    border-bottom: 1px solid var(--color-border);
}

.ruler {
    position: relative;
    background-color: var(--color-bg-secondary);
    font-family: var(--font-family-mono);
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    overflow: hidden;
}

.ruler-horizontal {
    grid-column: 2;
    border-bottom: 1px solid var(--color-border);
}

.ruler-vertical {
    grid-row: 2;
    border-right: 1px solid var(--color-border);
}

.viewport {
    grid-column: 2;
    grid-row: 2;
    overflow: hidden;
    position: relative;
    cursor: crosshair;
    touch-action: none; /* Disable default touch actions for pointer events */
}

.viewport.panning {
    cursor: grab;
}

.viewport.panning:active {
    cursor: grabbing;
}

.canvas-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;
    transform-origin: 0 0;
    will-change: transform;
}

.ascii-canvas {
    font-family: 'Courier New', Courier, monospace;
    font-size: 14px;
    line-height: 1;
    letter-spacing: 0;
    color: var(--color-canvas-text);
    background-color: var(--color-canvas-bg);
    padding: var(--space-lg);
    margin: 0;
    white-space: pre;
    user-select: none;
    box-shadow: var(--shadow-xl);
    border: 1px solid var(--color-border);
    /* Ensure consistent character rendering */
    font-variant-ligatures: none;
    font-feature-settings: "liga" 0, "calt" 0, "kern" 0;
    text-rendering: geometricPrecision;
    -webkit-font-smoothing: subpixel-antialiased;
    -moz-osx-font-smoothing: auto;
    word-spacing: 0;
    tab-size: 1;
}

/* Ensure spans don't break the monospace grid */
.ascii-canvas span {
    display: inline;
    font-family: inherit;
    font-size: inherit;
    line-height: 1;
    letter-spacing: inherit;
    word-spacing: inherit;
    vertical-align: top;
}

/* Grid overlay - CSS-based for performance */
.grid-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    opacity: 0.2;
    z-index: 1;
}

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

/* Scrollbars - custom implementation for zoomed canvas */
.scrollbar {
    display: block;
    background-color: var(--color-bg-secondary);
    position: relative;
    opacity: 0;
    transition: opacity var(--transition-fast);
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.scrollbar.visible {
    opacity: 1;
}

.viewport:hover ~ .scrollbar,
.scrollbar:hover,
.scrollbar.dragging {
    opacity: 1;
}

.scrollbar-horizontal {
    grid-column: 2;
    grid-row: 3;
    height: 12px;
}

.scrollbar-vertical {
    grid-column: 3;
    grid-row: 2;
    width: 12px;
}

.scrollbar-thumb {
    position: absolute;
    background-color: var(--color-border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    min-width: 30px;
    min-height: 30px;
    transition: background-color var(--transition-fast);
    touch-action: none;
}

.scrollbar-horizontal .scrollbar-thumb {
    height: 10px;
    top: 1px;
}

.scrollbar-vertical .scrollbar-thumb {
    width: 10px;
    left: 1px;
}

.scrollbar-thumb:hover,
.scrollbar-thumb.active {
    background-color: var(--color-accent);
}

/* ============================================
   RIGHT PANEL
   ============================================ */

.panel {
    background-color: var(--color-bg-secondary);
    border-left: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    min-height: 0;
}

.panel-right {
    width: var(--panel-width);
}

.panel-tabs {
    display: flex;
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
}

.panel-tab {
    flex: 1;
    padding: var(--space-sm);
    background: none;
    border: none;
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all var(--transition-fast);
}

.panel-tab:hover {
    color: var(--color-text-primary);
    background-color: var(--color-bg-hover);
}

.panel-tab.active {
    color: var(--color-accent);
    border-bottom-color: var(--color-accent);
}

.panel-content {
    display: none;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    min-height: 0;
}

.panel-content.active {
    display: flex;
}

/* Allow panel content to scroll when needed */
.panel-content > *:last-child:not(.panel-header) {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--color-border);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-secondary);
}

.panel-actions {
    display: flex;
    gap: var(--space-xs);
}

/* Layers Panel */
.layer-list {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-xs);
}

.layer-item {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.layer-item:hover {
    background-color: var(--color-bg-hover);
}

.layer-item.active {
    background-color: var(--color-bg-active);
}

.layer-visibility,
.layer-lock {
    background: none;
    border: none;
    font-size: var(--font-size-sm);
    cursor: pointer;
    padding: 2px;
    opacity: 0.7;
    position: relative;
}

.layer-visibility:hover,
.layer-lock:hover {
    opacity: 1;
}

/* Tooltips for layer buttons */
.layer-visibility[title]::after,
.layer-lock[title]::after {
    content: attr(title);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 6px;
    padding: 4px 8px;
    background-color: var(--color-bg-primary);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-xs);
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.2s, visibility 0.2s;
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.layer-visibility[title]:hover::after,
.layer-lock[title]:hover::after {
    opacity: 1;
    visibility: visible;
    transition-delay: 0.3s;
}

.layer-name {
    flex: 1;
    font-size: var(--font-size-sm);
    color: var(--color-text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Properties Panel */
.properties-content {
    padding: var(--space-md);
    overflow-y: auto;
}

.property-group {
    margin-bottom: var(--space-md);
}

.property-group > label {
    display: block;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: var(--space-xs);
}

.property-row {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    margin-bottom: var(--space-xs);
}

.property-row label {
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    min-width: 20px;
}

.property-row input[type="number"],
.property-row input[type="text"] {
    flex: 1;
    padding: var(--space-xs) var(--space-sm);
    background-color: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    font-family: var(--font-family-mono);
    min-width: 0;
}

.property-row input:focus {
    border-color: var(--color-border-focus);
}

.property-row select {
    flex: 1;
    padding: var(--space-xs) var(--space-sm);
    background-color: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    cursor: pointer;
}

.property-row select option {
    background-color: var(--color-bg-secondary);
    color: var(--color-text-primary);
}

/* ============================================
   COMPONENTS PANEL
   ============================================ */

#panel-components.active {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#panel-components > .panel-header,
#panel-components > .components-toolbar {
    flex-shrink: 0;
}

.components-toolbar {
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--color-border);
}

.component-search {
    width: 100%;
    padding: var(--space-xs) var(--space-sm);
    background-color: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
}

.component-search:focus {
    border-color: var(--color-border-focus);
    outline: none;
}

.component-search::placeholder {
    color: var(--color-text-muted);
}

.component-libraries {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--space-xs);
    min-height: 0;
}

.component-library {
    margin-bottom: var(--space-xs);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background-color: var(--color-bg-tertiary);
    overflow: hidden;
}

.component-library-header {
    display: flex;
    align-items: center;
    padding: var(--space-xs) var(--space-sm);
    background-color: var(--color-bg-secondary);
    cursor: pointer;
    user-select: none;
    gap: var(--space-xs);
    transition: background-color var(--transition-fast);
}

.component-library-header:hover {
    background-color: var(--color-bg-hover);
}

.component-library-expand {
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    transition: transform var(--transition-fast);
}

.component-library.expanded .component-library-expand {
    transform: rotate(90deg);
}

.component-library-icon {
    font-size: var(--font-size-md);
}

.component-library-name {
    flex: 1;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.component-library-count {
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    background-color: var(--color-bg-tertiary);
    padding: 2px 6px;
    border-radius: var(--radius-full);
}

.component-library-actions {
    display: flex;
    gap: 2px;
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.component-library-header:hover .component-library-actions {
    opacity: 1;
}

.component-library-actions .icon-btn {
    width: 20px;
    height: 20px;
    font-size: var(--font-size-xs);
}

.component-library-content {
    display: none;
    padding: var(--space-xs);
}

.component-library.expanded .component-library-content {
    display: block;
}

.component-category {
    margin-bottom: var(--space-sm);
}

.component-category-header {
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: var(--space-xs) 0;
    border-bottom: 1px solid var(--color-border);
    margin-bottom: var(--space-xs);
}

.component-grid {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
}

.component-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--space-sm);
    background-color: var(--color-bg-secondary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    cursor: grab;
    transition: all var(--transition-fast);
    user-select: none;
    min-width: 0;
    overflow: hidden;
    width: 100%;
    box-sizing: border-box;
}

.component-item:hover {
    background-color: var(--color-bg-hover);
    border-color: var(--color-border-light);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

.component-item:active {
    cursor: grabbing;
    transform: translateY(0);
    box-shadow: none;
}

.component-item.dragging {
    opacity: 0.5;
}

.component-preview {
    font-family: var(--font-family-mono);
    font-size: 7px;
    line-height: 1.1;
    color: var(--color-canvas-text);
    background-color: var(--color-canvas-bg);
    padding: 2px;
    border-radius: var(--radius-sm);
    white-space: pre;
    overflow: hidden;
    width: 100%;
    max-height: 45px;
    margin-bottom: var(--space-xs);
    text-overflow: clip;
    box-sizing: border-box;
}

.component-icon {
    font-size: var(--font-size-xl);
    margin-bottom: var(--space-xs);
}

.component-name {
    font-size: var(--font-size-xs);
    color: var(--color-text-primary);
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
}

/* Component drag ghost */
.component-drag-ghost {
    position: fixed;
    pointer-events: none;
    z-index: var(--z-tooltip);
    font-family: var(--font-family-mono);
    font-size: 10px;
    line-height: 1;
    color: var(--color-canvas-text);
    background-color: var(--color-canvas-bg);
    padding: var(--space-sm);
    border: 2px solid var(--color-accent);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    opacity: 0.9;
    white-space: pre;
}

/* Touch drag ghost (visible during touch drag) */
.component-drag-ghost.touch-drag {
    opacity: 0.85;
    transform: scale(1.1);
    box-shadow: var(--shadow-xl);
}

/* Component search results */
.component-search-results {
    padding: var(--space-sm);
}

.component-search-results .component-grid {
    grid-template-columns: repeat(2, 1fr);
}

.no-results {
    text-align: center;
    padding: var(--space-lg);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

/* Create component dialog */
.create-component-form .form-group {
    margin-bottom: var(--space-md);
}

.create-component-form label {
    display: block;
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    margin-bottom: var(--space-xs);
}

.create-component-form input,
.create-component-form select,
.create-component-form textarea {
    width: 100%;
    padding: var(--space-sm);
    background-color: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
}

.create-component-form textarea {
    resize: vertical;
    min-height: 60px;
    font-family: var(--font-family-ui);
}

.create-component-form input:focus,
.create-component-form select:focus,
.create-component-form textarea:focus {
    border-color: var(--color-border-focus);
    outline: none;
}

.library-select-wrapper {
    display: flex;
    gap: var(--space-xs);
}

.library-select-wrapper select {
    flex: 1;
}

/* Export Library Dialog */
.export-library-form p {
    margin: 0 0 var(--space-sm) 0;
    color: var(--color-text-secondary);
}

.library-checkboxes {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
    max-height: 200px;
    overflow-y: auto;
}

.checkbox-option {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs);
    cursor: pointer;
    border-radius: var(--border-radius-sm);
}

.checkbox-option:hover {
    background: var(--color-hover);
}

.checkbox-option input[type="checkbox"] {
    margin: 0;
}

.checkbox-option span {
    flex: 1;
}

/* Characters Panel */
.chars-content {
    padding: var(--space-md);
    overflow-y: auto;
}

.char-category {
    margin-bottom: var(--space-md);
}

.char-category > label {
    display: block;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: var(--space-xs);
}

.char-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    font-family: var(--font-family-mono);
    font-size: var(--font-size-md);
}

.char-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-family: var(--font-family-mono);
    font-size: var(--font-size-md);
    cursor: pointer;
    transition: all var(--transition-fast);
}

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

.char-btn.active {
    background-color: var(--color-accent);
    color: var(--color-text-inverse);
    border-color: var(--color-accent);
}

/* ============================================
   STATUS BAR
   ============================================ */

.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--status-height);
    padding: 0 var(--space-md);
    background-color: var(--color-bg-secondary);
    border-top: 1px solid var(--color-border);
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    flex-shrink: 0;
}

.status-left,
.status-right {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

#status-zoom {
    cursor: pointer;
    padding: 2px 4px;
    border-radius: var(--radius-sm);
    transition: background-color var(--transition-fast);
}

#status-zoom:hover {
    background-color: var(--color-bg-hover);
}

/* ============================================
   DIALOGS
   ============================================ */

.dialogs-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: var(--z-modal);
}

.dialog-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    pointer-events: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn var(--transition-fast);
    z-index: 1000;
}

.dialog {
    background-color: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    min-width: 400px;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    animation: slideUp var(--transition-normal);
}

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

.dialog-title {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-medium);
}

.dialog-close {
    background: none;
    border: none;
    color: var(--color-text-secondary);
    font-size: var(--font-size-xl);
    cursor: pointer;
    padding: var(--space-xs);
    line-height: 1;
}

.dialog-close:hover {
    color: var(--color-text-primary);
}

.dialog-content {
    padding: var(--space-lg);
    overflow-y: auto;
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-sm);
    padding: var(--space-md) var(--space-lg);
    border-top: 1px solid var(--color-border);
}

/* Form elements inside dialogs */
.form-group {
    margin-bottom: var(--space-md);
}

.form-group label {
    display: block;
    margin-bottom: var(--space-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-primary);
}

.form-group input,
.form-group select {
    width: 100%;
    padding: var(--space-sm);
    background-color: var(--color-bg-secondary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-base);
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--color-accent);
}

.btn-group {
    display: flex;
    gap: var(--space-xs);
    flex-wrap: wrap;
}

.btn-small {
    padding: var(--space-xs) var(--space-sm);
    font-size: var(--font-size-sm);
}

/* ============================================
   CONTEXT MENU
   ============================================ */

.context-menu {
    position: fixed;
    background-color: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    min-width: 180px;
    padding: var(--space-xs);
    z-index: var(--z-popover);
}

.context-menu.hidden {
    display: none;
}

.context-menu-item {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.context-menu-item:hover {
    background-color: var(--color-bg-hover);
}

.context-menu-item.disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.context-menu-item .shortcut {
    margin-left: auto;
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
}

.context-menu-separator {
    height: 1px;
    background-color: var(--color-border);
    margin: var(--space-xs) 0;
}

/* ============================================
   BUTTONS
   ============================================ */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-xs);
    padding: var(--space-sm) var(--space-md);
    border: none;
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    transition: all var(--transition-fast);
}

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

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

.btn-secondary {
    background-color: var(--color-bg-tertiary);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
}

.btn-secondary:hover {
    background-color: var(--color-bg-hover);
}

/* ============================================
   ANIMATIONS
   ============================================ */

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ============================================
   CONTEXT MENU
   ============================================ */

.context-menu {
    position: fixed;
    min-width: 180px;
    background-color: var(--color-bg-elevated);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: var(--z-modal);
    padding: var(--space-xs) 0;
}

.context-menu.hidden {
    display: none;
}

.context-menu-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    background: none;
    border: none;
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    text-align: left;
}

.context-menu-item:hover {
    background-color: var(--color-bg-hover);
}

.context-menu-shortcut {
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
    margin-left: var(--space-lg);
}

.context-menu-separator {
    height: 1px;
    background-color: var(--color-border);
    margin: var(--space-xs) 0;
}

/* ============================================
   RULER TICKS
   ============================================ */

.ruler-ticks {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.ruler-tick {
    position: absolute;
    background-color: var(--color-text-muted);
}

/* Horizontal ruler ticks */
.ruler-horizontal .ruler-tick {
    width: 1px;
    height: 4px;
    bottom: 0;
}

.ruler-horizontal .ruler-tick.minor {
    height: 3px;
    opacity: 0.5;
}

.ruler-horizontal .ruler-tick.medium {
    height: 6px;
}

.ruler-horizontal .ruler-tick.major {
    height: 10px;
    background-color: var(--color-text-secondary);
}

/* Vertical ruler ticks */
.ruler-vertical .ruler-tick {
    width: 4px;
    height: 1px;
    right: 0;
}

.ruler-vertical .ruler-tick.minor {
    width: 3px;
    opacity: 0.5;
}

.ruler-vertical .ruler-tick.medium {
    width: 6px;
}

.ruler-vertical .ruler-tick.major {
    width: 10px;
    background-color: var(--color-text-secondary);
}

/* Ruler labels */
.ruler-label {
    position: absolute;
    font-size: 9px;
    font-weight: 500;
    color: var(--color-text-secondary);
    white-space: nowrap;
    pointer-events: none;
}

.ruler-horizontal .ruler-label {
    bottom: 11px;
    left: 2px;
    transform: translateX(-50%);
}

.ruler-vertical .ruler-label {
    right: 11px;
    top: 2px;
    transform: translateY(-50%);
    writing-mode: horizontal-tb;
}

/* Ruler position indicator (follows mouse) */
.ruler-position-indicator {
    position: absolute;
    background-color: var(--color-accent);
    pointer-events: none;
    z-index: 10;
    display: none;
}

.ruler-horizontal .ruler-position-indicator {
    width: 1px;
    height: 100%;
    top: 0;
}

.ruler-vertical .ruler-position-indicator {
    width: 100%;
    height: 1px;
    left: 0;
}

/* Show position value on hover */
.ruler-position-indicator::after {
    content: attr(data-value);
    position: absolute;
    background-color: var(--color-accent);
    color: white;
    font-size: 9px;
    padding: 1px 4px;
    border-radius: 2px;
    white-space: nowrap;
}

.ruler-horizontal .ruler-position-indicator::after {
    top: 2px;
    left: 50%;
    transform: translateX(-50%);
}

.ruler-vertical .ruler-position-indicator::after {
    left: 2px;
    top: 50%;
    transform: translateY(-50%);
}

/* Selection range indicator on rulers */
.ruler-selection-range {
    position: absolute;
    background-color: var(--color-accent);
    opacity: 0.3;
    pointer-events: none;
    z-index: 5;
    display: none;
}

.ruler-horizontal .ruler-selection-range {
    height: 100%;
    top: 0;
}

.ruler-vertical .ruler-selection-range {
    width: 100%;
    left: 0;
}

/* Canvas bounds indicator on rulers */
.ruler-canvas-bounds {
    position: absolute;
    pointer-events: none;
    z-index: 1;
}

.ruler-horizontal .ruler-canvas-bounds {
    height: 3px;
    bottom: 0;
    background: linear-gradient(to right, var(--color-accent), var(--color-accent));
    opacity: 0.6;
}

.ruler-vertical .ruler-canvas-bounds {
    width: 3px;
    right: 0;
    background: linear-gradient(to bottom, var(--color-accent), var(--color-accent));
    opacity: 0.6;
}

/* Guide lines created from rulers */
.guide-line {
    background-color: var(--color-accent);
    opacity: 0.8;
}

.guide-line.guide-horizontal {
    height: 1px;
    background: repeating-linear-gradient(
        to right,
        var(--color-accent),
        var(--color-accent) 4px,
        transparent 4px,
        transparent 8px
    );
}

.guide-line.guide-vertical {
    width: 1px;
    background: repeating-linear-gradient(
        to bottom,
        var(--color-accent),
        var(--color-accent) 4px,
        transparent 4px,
        transparent 8px
    );
}

/* Rulers hidden state */
.canvas-container.rulers-hidden .ruler,
.canvas-container.rulers-hidden .ruler-corner {
    display: none;
}

.canvas-container.rulers-hidden {
    grid-template-columns: 0 1fr auto;
    grid-template-rows: 0 1fr auto;
}

.canvas-container.rulers-hidden .viewport {
    grid-column: 1 / -1;
    grid-row: 1 / -1;
}

/* Ruler hover states */
.ruler:hover {
    background-color: var(--color-bg-tertiary);
}

.ruler-horizontal {
    cursor: col-resize;
}

.ruler-vertical {
    cursor: row-resize;
}

/* Ruler corner styling */
.ruler-corner {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: var(--color-text-muted);
    transition: background-color var(--transition-fast);
}

.ruler-corner:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text);
}

.ruler-corner::after {
    content: '⊞';
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

.hidden {
    display: none !important;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.no-select {
    user-select: none;
}

/* ============================================
   RESPONSIVE ADJUSTMENTS
   ============================================ */

/* Tablet breakpoint */
@media (max-width: 1200px) {
    :root {
        --panel-width: 240px;
    }
}

/* Small tablet / large phone breakpoint */
@media (max-width: 900px) {
    :root {
        --panel-width: 200px;
        --toolbar-width: 40px;
    }
    
    .tool-btn {
        width: 28px;
        height: 28px;
    }
    
    .tool-icon {
        font-size: var(--font-size-md);
    }
}

/* Medium screens - compact menu */
@media (max-width: 1100px) {
    .menu-item {
        padding: var(--space-xs) var(--space-xs);
        font-size: var(--font-size-xs);
    }
    
    .menu-right {
        gap: var(--space-xs);
    }
    
    .menu-right .icon-btn {
        width: 24px;
        height: 24px;
    }
    
    .zoom-level {
        font-size: var(--font-size-xs);
        min-width: 36px;
    }
    
    .theme-select {
        font-size: var(--font-size-xs);
        padding: 2px 4px;
        max-width: 60px;
    }
    
    .menu-left {
        gap: var(--space-sm);
    }
    
    .app-logo {
        font-size: var(--font-size-md);
    }
}

/* Mobile breakpoint */
@media (max-width: 768px) {
    :root {
        --panel-width: 100%;
        --toolbar-width: 100%;
        --menu-height: 48px;
        --status-height: 32px;
    }
    
    /* Hide elements on mobile */
    .hide-mobile {
        display: none !important;
    }
    
    /* App layout */
    #app {
        display: flex;
        flex-direction: column;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for mobile */
        height: -webkit-fill-available; /* iOS Safari fallback */
    }
    
    /* Menu bar mobile */
    .menu-bar {
        height: var(--menu-height);
        padding: 0 var(--space-sm);
        position: relative;
    }
    
    .menu-left {
        gap: var(--space-sm);
    }
    
    .app-logo {
        font-size: var(--font-size-sm);
    }
    
    /* Hide menu items, show mobile toggle */
    .menu-items {
        display: none;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background: var(--color-bg-secondary);
        border-bottom: 1px solid var(--color-border);
        flex-direction: column;
        padding: var(--space-sm);
        z-index: var(--z-modal);
        box-shadow: var(--shadow-lg);
        max-height: 70vh;
        overflow-y: auto;
    }
    
    .menu-items.open {
        display: flex;
    }
    
    .menu-items .menu-item {
        padding: var(--space-md);
        justify-content: flex-start;
        border-radius: var(--radius-sm);
        min-height: 44px;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }
    
    .menu-items .menu-item:hover {
        background: var(--color-bg-hover);
    }
    
    .menu-items .menu-item:active {
        background: var(--color-bg-active);
    }
    
    /* Mobile dropdown menus - full width slide down */
    .menu-items .menu-item-wrapper {
        width: 100%;
    }
    
    .menu-items .menu-item-wrapper .menu-dropdown {
        position: static;
        width: 100%;
        min-width: auto;
        background: var(--color-bg-tertiary);
        border: none;
        border-radius: 0;
        box-shadow: none;
        margin: 0;
        padding: 0;
        z-index: auto;
    }
    
    .menu-items .menu-item-wrapper .menu-dropdown:not(.show) {
        display: none;
    }
    
    .menu-items .menu-item-wrapper .menu-dropdown.show {
        display: block !important;
    }
    
    .menu-items .menu-dropdown-item {
        padding: var(--space-md) var(--space-lg);
        min-height: 48px;
        font-size: var(--font-size-md);
    }
    
    .menu-items .menu-shortcut {
        display: none;
    }
    
    /* Mobile menu toggle - inline with logo */
    .mobile-menu-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        background: transparent;
        border: none;
        color: var(--color-text-primary);
        font-size: var(--font-size-lg);
        cursor: pointer;
        border-radius: var(--radius-sm);
        flex-shrink: 0;
        margin-right: var(--space-xs);
    }
    
    .mobile-menu-toggle:hover,
    .mobile-menu-toggle:active {
        background: var(--color-bg-hover);
    }
    
    .menu-right {
        gap: var(--space-xs);
    }
    
    .menu-right .icon-btn {
        width: 32px;
        height: 32px;
        min-width: 32px;
    }
    
    /* Main content mobile layout */
    .main-content {
        flex: 1;
        display: flex;
        flex-direction: column;
        position: relative;
        overflow: hidden;
    }
    
    /* Toolbar mobile - horizontal at bottom */
    .toolbar {
        position: fixed;
        bottom: var(--status-height);
        left: 0;
        right: 0;
        width: 100%;
        height: 48px;
        min-height: 48px;
        max-height: none;
        flex-direction: row;
        flex-wrap: nowrap;
        padding: 6px 8px;
        gap: 6px;
        z-index: var(--z-fixed);
        transform: translateY(100%);
        transition: transform var(--transition-normal);
        overflow-x: auto;
        overflow-y: visible;
        border-top: 1px solid var(--color-border);
        border-right: none;
        justify-content: flex-start;
        align-items: center;
        -webkit-overflow-scrolling: touch;
        box-sizing: border-box;
        touch-action: pan-x;
    }
    
    .toolbar.open {
        transform: translateY(0);
    }
    
    .toolbar .tool-group {
        flex-direction: row;
        flex-wrap: nowrap;
        flex-shrink: 0;
        border-bottom: none;
        padding: 2px;
        gap: 2px;
        background: var(--color-bg-elevated);
        border-radius: var(--radius-sm);
        border: 1px solid var(--color-border);
        height: 34px;
        align-items: center;
        touch-action: pan-x;
    }
    
    .toolbar .tool-btn {
        width: 28px;
        height: 28px;
        flex-shrink: 0;
        touch-action: manipulation;
    }
    
    .toolbar .tool-icon {
        font-size: var(--font-size-sm);
    }
    
    .toolbar .tool-colors {
        flex-direction: row;
        align-items: center;
    }
    
    .toolbar .tool-colors .color-preview {
        width: 24px;
        height: 24px;
    }
    
    .toolbar .tool-colors .stroke-color,
    .toolbar .tool-colors .fill-color {
        width: 16px;
        height: 16px;
    }
    
    /* Mobile toolbar toggle */
    .mobile-toolbar-toggle {
        display: flex;
        position: fixed;
        bottom: calc(var(--status-height) + var(--space-sm));
        left: var(--space-sm);
        width: 44px;
        height: 44px;
        border-radius: var(--radius-full);
        background: var(--color-accent);
        color: white;
        border: none;
        font-size: var(--font-size-xl);
        cursor: pointer;
        z-index: var(--z-fixed);
        box-shadow: var(--shadow-lg);
        align-items: center;
        justify-content: center;
        transition: background var(--transition-fast);
    }
    
    .mobile-toolbar-toggle:hover,
    .mobile-toolbar-toggle:active {
        background: var(--color-accent-hover);
    }
    
    .mobile-toolbar-toggle.active {
        background: var(--color-accent-active);
    }
    
    /* Canvas container mobile */
    .canvas-container {
        flex: 1;
        margin: 0;
        border-radius: 0;
    }
    
    /* Hide rulers on mobile */
    .ruler, .ruler-corner {
        display: none;
    }
    
    .viewport {
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    
    /* Panel mobile - slide from right */
    .panel-right {
        position: fixed;
        top: var(--menu-height);
        right: 0;
        bottom: var(--status-height);
        width: 85%;
        max-width: 320px;
        transform: translateX(100%);
        transition: transform var(--transition-normal);
        z-index: var(--z-modal);
        box-shadow: var(--shadow-xl);
    }
    
    .panel-right.open {
        transform: translateX(0);
    }
    
    .panel-tabs {
        padding: var(--space-xs);
    }
    
    .panel-tab {
        padding: var(--space-sm) var(--space-md);
        font-size: var(--font-size-sm);
    }
    
    /* Mobile panel toggle */
    .mobile-panel-toggle {
        display: flex;
        position: fixed;
        bottom: calc(var(--status-height) + var(--space-sm));
        right: var(--space-sm);
        width: 44px;
        height: 44px;
        border-radius: var(--radius-full);
        background: var(--color-bg-elevated);
        color: var(--color-text-primary);
        border: 1px solid var(--color-border);
        font-size: var(--font-size-xl);
        cursor: pointer;
        z-index: var(--z-fixed);
        box-shadow: var(--shadow-lg);
        align-items: center;
        justify-content: center;
        transition: background var(--transition-fast);
    }
    
    .mobile-panel-toggle:hover,
    .mobile-panel-toggle:active {
        background: var(--color-bg-hover);
    }
    
    .mobile-panel-toggle.active {
        background: var(--color-accent);
        color: white;
        border-color: var(--color-accent);
    }
    
    /* Status bar mobile */
    .status-bar {
        height: auto;
        min-height: var(--status-height);
        padding: var(--space-xs) var(--space-sm);
        padding-bottom: max(var(--space-xs), env(safe-area-inset-bottom, 0px));
        font-size: var(--font-size-xs);
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: var(--z-sticky);
        flex-wrap: wrap;
        gap: var(--space-xs);
        background-color: var(--color-bg-secondary);
    }
    
    .status-left, .status-right {
        gap: var(--space-xs);
        flex-wrap: wrap;
    }
    
    .status-right {
        justify-content: flex-end;
    }
    
    /* Ensure main content doesn't overlap status bar */
    .canvas-container {
        padding-bottom: calc(var(--status-height) + var(--space-sm));
    }
    
    /* Scrollbars - very thin on mobile to save space */
    .scrollbar {
        --scrollbar-size: 3px;
    }
    
    .scrollbar-horizontal {
        height: 3px;
    }
    
    .scrollbar-vertical {
        width: 3px;
    }
    
    .scrollbar-thumb {
        min-width: 16px;
        min-height: 16px;
    }
    
    /* Hide native scrollbars on mobile */
    ::-webkit-scrollbar {
        width: 2px;
        height: 2px;
    }
    
    /* Dialogs mobile */
    .dialog {
        width: 95% !important;
        max-width: none !important;
        margin: var(--space-sm);
    }
    
    /* Context menu mobile */
    .context-menu {
        min-width: 180px;
        max-width: calc(100vw - var(--space-lg));
    }
    
    .context-menu-item {
        padding: var(--space-md) var(--space-lg);
    }
}

/* Small mobile breakpoint */
@media (max-width: 480px) {
    :root {
        --menu-height: 44px;
    }
    
    .hide-mobile-sm {
        display: none !important;
    }
    
    .app-logo {
        font-size: var(--font-size-xs);
    }
    
    .menu-right .icon-btn {
        width: 28px;
        height: 28px;
        min-width: 28px;
    }
    
    .zoom-level {
        font-size: var(--font-size-xs);
        min-width: 40px;
    }
    
    .toolbar {
        height: 44px;
        min-height: 44px;
        padding: 4px 6px;
        gap: 4px;
    }
    
    .toolbar .tool-group {
        padding: 2px;
        gap: 2px;
        height: 32px;
    }
    
    .toolbar .tool-btn {
        width: 26px;
        height: 26px;
    }
    
    .toolbar .tool-icon {
        font-size: var(--font-size-xs);
    }
    
    .panel-right {
        width: 100%;
        max-width: none;
    }
    
    .mobile-toolbar-toggle,
    .mobile-panel-toggle {
        width: 40px;
        height: 40px;
        font-size: var(--font-size-lg);
    }
}

/* Desktop - hide mobile elements */
@media (min-width: 769px) {
    .mobile-menu-toggle,
    .mobile-toolbar-toggle,
    .mobile-panel-toggle {
        display: none;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets */
    .tool-btn {
        min-width: 44px;
        min-height: 44px;
    }
    
    .icon-btn {
        min-width: 44px;
        min-height: 44px;
    }
    
    .menu-item {
        min-height: 44px;
        padding: var(--space-md) var(--space-lg);
    }
    
    .context-menu-item {
        min-height: 44px;
        padding: var(--space-md) var(--space-lg);
    }
    
    .panel-tab {
        min-height: 44px;
    }
    
    /* Disable hover states that don't apply to touch */
    .tool-btn:hover,
    .icon-btn:hover,
    .menu-item:hover {
        background: inherit;
    }
    
    .tool-btn:active,
    .icon-btn:active,
    .menu-item:active {
        background: var(--color-bg-active);
    }
    
    /* Prevent text selection on interactive elements */
    button, .tool-btn, .menu-item, .panel-tab {
        -webkit-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
    }
    
    /* Smooth scrolling in panels */
    .panel-content,
    .layer-list,
    .properties-content,
    .chars-content,
    .component-libraries {
        -webkit-overflow-scrolling: touch;
    }
    
    /* ASCII canvas touch handling */
    .ascii-canvas {
        touch-action: none;
    }
    
    .viewport {
        touch-action: none;
    }
}

/* Landscape mobile */
@media (max-width: 900px) and (orientation: landscape) {
    :root {
        --menu-height: 32px;
        --status-height: 20px;
    }
    
    /* Optimize menu bar for landscape */
    .menu-bar {
        padding: 0 var(--space-xs);
        gap: var(--space-xs);
        overflow: visible;
    }
    
    .menu-left {
        gap: var(--space-xs);
        flex-shrink: 1;
        min-width: 0;
        overflow: hidden;
    }
    
    .app-logo {
        font-size: var(--font-size-xs);
        white-space: nowrap;
    }
    
    .menu-right {
        gap: 2px;
        flex-shrink: 0;
    }
    
    .menu-right .icon-btn {
        width: 26px;
        height: 26px;
        min-width: 26px;
        flex-shrink: 0;
    }
    
    .menu-right .separator {
        display: none;
    }
    
    /* Hide theme select on landscape mobile */
    .theme-select {
        display: none !important;
    }
    
    .mobile-menu-toggle {
        width: 28px;
        height: 28px;
        font-size: var(--font-size-sm);
    }
    
    .toolbar {
        height: 40px;
        min-height: 40px;
        max-height: 40vh;
    }
    
    .toolbar .tool-btn {
        width: 26px;
        height: 26px;
    }
    
    .toolbar .tool-group {
        height: 30px;
    }
    
    .panel-right {
        width: 50%;
        max-width: 300px;
    }
    
    /* Smaller status bar for landscape */
    .status-bar {
        font-size: 10px;
        padding: 2px var(--space-sm);
    }
}

/* Safe area insets for notched devices */
@supports (padding: env(safe-area-inset-bottom)) {
    .status-bar {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .toolbar {
        padding-bottom: calc(var(--space-sm) + env(safe-area-inset-bottom));
    }
    
    @media (max-width: 768px) {
        .mobile-toolbar-toggle,
        .mobile-panel-toggle {
            bottom: calc(var(--status-height) + var(--space-sm) + env(safe-area-inset-bottom));
        }
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --color-border: #666;
        --color-border-light: #888;
    }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    * {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
    }
}

/* Touch feedback */
.touch-active {
    background: var(--color-bg-active) !important;
}

/* Disable hover effects on touch devices */
@media (hover: none) and (pointer: coarse) {
    .menu-dropdown-item:hover,
    .menu-item:hover,
    .tool-btn:hover,
    .icon-btn:hover {
        background: transparent;
    }
    
    .menu-dropdown-item:active,
    .menu-item:active,
    .tool-btn:active,
    .icon-btn:active {
        background: var(--color-bg-active);
    }
}

/* Panel backdrop for mobile */
@media (max-width: 768px) {
    .panel-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: calc(var(--z-modal) - 1);
        opacity: 0;
        pointer-events: none;
        transition: opacity var(--transition-normal);
    }
    
    .panel-backdrop.visible {
        opacity: 1;
        pointer-events: auto;
    }
}
/* ============================================
   SMART GUIDES & SNAPPING
   ============================================ */

/* Clickable status bar items */
.status-snap.clickable {
    cursor: pointer;
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    user-select: none;
}

.status-snap.clickable:hover {
    background-color: var(--color-bg-hover);
}

.status-snap.active {
    color: var(--color-accent);
    font-weight: 500;
}

/* Canvas resize handles */
.canvas-resize-handle {
    position: absolute;
    width: 8px;
    height: 8px;
    background: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: 2px;
    pointer-events: auto;
    transition: background-color var(--transition-fast), transform var(--transition-fast);
    z-index: 100;
}

.canvas-resize-handle:hover {
    background: var(--color-accent);
    transform: scale(1.2);
}

/* Canvas size indicator tooltip */
#canvas-size-indicator {
    position: fixed;
    background: rgba(0, 0, 0, 0.85);
    color: #fff;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-family: var(--font-mono);
    z-index: 1000;
    pointer-events: none;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* ============================================
   MODAL DIALOGS
   ============================================ */

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-modal);
    backdrop-filter: blur(2px);
}

.modal-dialog {
    background: var(--color-bg-secondary);
    border-radius: 8px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    max-width: 500px;
    width: 90%;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: modalFadeIn 0.2s ease-out;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(-10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

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

.modal-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-primary);
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--color-text-muted);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background-color 0.15s, color 0.15s;
}

.modal-close:hover {
    background: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
    color: var(--color-text-primary);
}

.modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid var(--color-border);
}

.modal-footer .btn {
    padding: 8px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
}

.modal-footer .btn-primary {
    background: var(--color-accent);
    color: white;
    border: none;
}

.modal-footer .btn-primary:hover {
    background: var(--color-accent-hover);
}

.modal-footer .btn-secondary {
    background: var(--color-bg-tertiary);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
}

.modal-footer .btn-secondary:hover {
    background: var(--color-bg-hover);
}

/* ============================================
   COMMAND PALETTE
   ============================================ */

.command-palette-overlay {
    align-items: flex-start;
    padding-top: 15vh;
}

.command-palette {
    background: var(--color-bg-secondary);
    border-radius: 8px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    width: 90%;
    max-width: 550px;
    overflow: hidden;
    animation: modalFadeIn 0.15s ease-out;
}

.command-palette-input-wrapper {
    padding: 12px;
    border-bottom: 1px solid var(--color-border);
}

.command-palette-input {
    width: 100%;
    padding: 10px 12px;
    font-size: 16px;
    border: none;
    background: var(--color-bg-tertiary);
    color: var(--color-text-primary);
    border-radius: 6px;
    outline: none;
}

.command-palette-input::placeholder {
    color: var(--color-text-muted);
}

.command-palette-results {
    max-height: 400px;
    overflow-y: auto;
    padding: 8px 0;
}

.command-palette-category {
    padding: 8px 16px 4px;
    font-size: 11px;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.command-palette-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    cursor: pointer;
    transition: background-color 0.1s;
}

.command-palette-item:hover,
.command-palette-item.selected {
    background: var(--color-bg-hover);
}

.command-palette-item .command-label {
    font-size: 14px;
    color: var(--color-text-primary);
}

.command-palette-item .command-shortcut {
    font-size: 12px;
    color: var(--color-text-muted);
    padding: 2px 6px;
    background: var(--color-bg-tertiary);
    border-radius: 4px;
    font-family: var(--font-family-mono);
}