/* ==========================================================================
   CSS Variables & Design Tokens
   ========================================================================== */

   :root {
    /* Color Palette - Red Theme */
    --color-primary: #dc2626;
    --color-error: #ef4444;
    --color-success: #10b981;
    --color-background: #e5e5e5;
    --color-white: #ffffff;
    --color-black: #1a1a1a;
    --color-border: #d0d0d0;
    --color-hover: #f0f0f0;
    --color-hover-light: #f8f9fa;
    
    /* Shadow System */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 2px 4px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 2px 8px rgba(0, 0, 0, 0.15);
    
    /* Spacing Scale */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 12px;
    --spacing-lg: 16px;
    --spacing-xl: 20px;
    --spacing-page-top: 80px;
    
    /* Border Radius */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    
    /* Transitions */
    --transition-fast: 0.1s ease-out;
    --transition-base: 0.2s;
    --transition-slow: 0.3s ease;
  }
  
  /* ==========================================================================
     Reset & Base Styles
     ========================================================================== */
  
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  
  html, body {
    touch-action: pan-x pan-y; /* Allow panning but prevent browser's default pinch zoom */
    -ms-touch-action: pan-x pan-y;
  }
  
  body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
      "Helvetica Neue", Arial, sans-serif;
    overflow: hidden;
    background: var(--color-background);
  }
  
  /* ==========================================================================
     Layout Components
     ========================================================================== */
  
  .app-container {
    display: flex;
    height: 100vh;
  }
  
  /* ==========================================================================
     Sidebar Components
     ========================================================================== */
  
  .left-sidebar {
    width: 80px;
    background: transparent;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-lg) 0;
    gap: var(--spacing-md);
    overflow-y: auto;
    transition: transform var(--transition-slow);
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 1000;
    pointer-events: none;
  }
  
  /* ==========================================================================
     Home Button (Top Left)
     ========================================================================== */
  
  .home-button-top {
    position: fixed;
    top: var(--spacing-lg);
    left: var(--spacing-lg);
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-white);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    color: var(--color-black);
    text-decoration: none;
    cursor: pointer;
    transition: all var(--transition-base);
    z-index: 1004;
  }
  
  .home-button-top:hover {
    background: var(--color-hover);
    transform: scale(1.05);
  }
  
  .home-button-top:active {
    transform: scale(0.95);
  }
  
  .home-button-top svg {
    width: 22px;
    height: 22px;
  }
  
  .left-sidebar.collapsed {
    transform: translateX(-80px);
  }
  
  .sidebar-toggle {
    position: fixed;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 64px;
    background: var(--color-white);
    border: 1px solid var(--color-border);
    border-left: none;
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 11;
    box-shadow: var(--shadow-sm);
    transition: all var(--transition-slow);
  }
  
  .sidebar-toggle:hover {
    background: var(--color-hover-light);
  }
  
  .sidebar-toggle.active {
    left: 80px;
  }
  
  .sidebar-toggle svg {
    width: 16px;
    height: 16px;
    transition: transform var(--transition-slow);
  }
  
  .sidebar-toggle.active svg {
    transform: rotate(180deg);
  }
  
  /* ==========================================================================
     Tool Sections & Buttons
     ========================================================================== */
  
  .tool-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--spacing-sm);
    gap: var(--spacing-sm);
    background: var(--color-white);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
  }
  
  .tool-button {
    width: 48px;
    height: 48px;
    background: var(--color-white);
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-black);
    transition: all var(--transition-base);
    position: relative;
    box-shadow: var(--shadow-sm);
    outline: none;
    pointer-events: auto;
    z-index: 1001;
  }
  
  .tool-button:hover {
    background: var(--color-hover);
  }
  
  .tool-button.active,
  #gridToggle.active {
    background: var(--color-primary);
    color: var(--color-white);
  }
  
  .tool-button svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
  }
  
  /* ==========================================================================
     Custom Tooltips
     ========================================================================== */
  
  .custom-tooltip {
    position: fixed;
    background: var(--color-black);
    color: var(--color-white);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: 11px;
    font-weight: 500;
    white-space: nowrap;
    pointer-events: none;
    z-index: 10000;
    opacity: 0;
    transform: translateY(-4px);
    transition: opacity var(--transition-fast), transform var(--transition-fast);
    box-shadow: var(--shadow-lg);
    line-height: 1.3;
  }
  
  .custom-tooltip.visible {
    opacity: 1;
    transform: translateY(0);
  }
  
  .tooltip-shortcut {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    font-size: 10px;
    font-weight: 600;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, monospace;
    letter-spacing: 0.3px;
  }
  
  /* ==========================================================================
     Canvas & Page Components
     ========================================================================== */
  
  .canvas-container {
    flex: 1;
    position: relative;
    overflow: auto;
    background: var(--color-background);
    display: flex;
    justify-content: center;
    z-index: 1;
    width: 100%;
    touch-action: pan-x pan-y; /* Allow panning but prevent browser's default pinch-zoom */
  }
  
  .pages-container {
    touch-action: pan-x pan-y; /* Prevent pinch zoom on the pages container as well */
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--spacing-page-top) var(--spacing-xl);
    gap: calc(var(--spacing-xl) * 0.5);
    z-index: 1001;
    margin: 0 auto;
  }

  .pages-container::after {
    content: '';
    display: block;
    height: var(--spacing-page-top);
    width: 100%;
    flex-shrink: 0;
  }

  .page-wrapper {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: var(--spacing-sm);
  }
  
  .page {
    position: relative;
    width: 1360px;
    height: 1870px;
    margin: 0 auto;
    background: var(--color-white);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
  }
  
  .page.grid-enabled {
    background-image: 
      linear-gradient(to right, var(--color-border) 1px, transparent 1px),
      linear-gradient(to bottom, var(--color-border) 1px, transparent 1px);
    background-size: 34px 34px;
  }

  .page-number {
    font-size: 11px;
    font-weight: 600;
    color: rgba(0, 0, 0, 0.4);
    background: rgba(255, 255, 255, 0.7);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    pointer-events: none;
    z-index: 10;
    user-select: none;
    white-space: nowrap;
    margin-top: var(--spacing-sm);
  }
  
  .page-canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: default;
    background: transparent;
    pointer-events: none;
  }
  
  /* Crosshair cursor when shapes tool is active */
  body.shapes-tool-active .page,
  body.shapes-tool-active .page * {
    cursor: crosshair;
  }
  
  /* But keep default cursor for interactive elements */
  body.shapes-tool-active .page .image-hitbox,
  body.shapes-tool-active .page .image-hitbox *,
  body.shapes-tool-active .page .page-delete-btn,
  body.shapes-tool-active .page .page-number {
    cursor: default;
  }
  
  /* Rotation handle needs its own cursor */
  body.shapes-tool-active .page .image-rotation-handle {
    cursor: default !important;
  }
  
  body.shapes-tool-active .page .image-rotation-handle.rotating {
    cursor: grabbing !important;
  }
  
  #zoomLayer {
    transform-origin: 0 0;
    transition: transform var(--transition-fast);
    width: 100%;
    min-height: 100%;
  }
  
  /* ==========================================================================
     Bottom Toolbar (moved to top right)
     ========================================================================== */
  
  .bottom-toolbar {
    position: fixed;
    top: var(--spacing-lg);
    right: var(--spacing-lg);
    display: flex;
    gap: var(--spacing-md);
    z-index: 1003;
  }
  
  .undo-redo-group,
  .zoom-export-group {
    display: flex;
    align-items: center;
    background: var(--color-white);
    border-radius: var(--radius-lg);
    padding: var(--spacing-sm);
    box-shadow: var(--shadow-lg);
    gap: var(--spacing-xs);
  }
  
  .undo-redo-group .tool-button,
  .zoom-export-group .tool-button {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition-base);
    pointer-events: auto;
  }

  .undo-redo-group .tool-button:hover,
  .zoom-export-group .tool-button:hover {
    background: var(--color-hover);
  }

  .undo-redo-group .tool-button:active,
  .zoom-export-group .tool-button:active {
    transform: scale(0.95);
    opacity: 0.8;
  }

  .undo-redo-group .tool-button.disabled,
  .zoom-export-group .tool-button.disabled {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
  }

  .undo-redo-group .tool-button.disabled:hover {
    background: transparent;
  }
  
  .divider {
    width: 1px;
    height: 32px;
    background: var(--color-border);
    margin: 0 var(--spacing-xs);
  }
  
  /* ==========================================================================
     Zoom Display
     ========================================================================== */
  
  .zoom-display {
    color: var(--color-black);
    font-size: 13px;
    width: 45px;
    text-align: center;
    font-family: system-ui, sans-serif;
    font-weight: 500;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: var(--spacing-xs) 2px;
    cursor: text;
    user-select: text;
  }
  
  .zoom-display:hover {
    border-color: var(--color-border);
    background: var(--color-hover-light);
  }
  
  .zoom-display:focus {
    outline: none;
    border-color: var(--color-primary);
  }
  
  /* ==========================================================================
     Text Editing
     ========================================================================== */
  
  .text-box {
    position: absolute;
    min-width: 100px;
    min-height: 30px;
    padding: var(--spacing-xs) var(--spacing-sm);
    border: 2px dashed var(--color-primary);
    background: transparent;
    cursor: text;
    font-family: inherit;
    font-size: 16px;
    resize: both;
    overflow: hidden;
    z-index: 1000;
  }
  
  .text-box:focus {
    border-color: var(--color-primary);
  }
  
  .text-box.editing {
    border-style: solid;
  }
  
  /* ==========================================================================
     Shapes Menu
     ========================================================================== */
  
  .shapes-menu {
    position: fixed;
    background: var(--color-white);
    border-radius: var(--radius-lg);
    padding: var(--spacing-sm);
    box-shadow: var(--shadow-lg);
    display: none;
    flex-direction: column;
    gap: var(--spacing-xs);
    z-index: 1002;
    pointer-events: auto;
  }
  
  .shapes-menu.active {
    display: flex;
  }
  
  .shape-option {
    width: 48px;
    height: 48px;
    background: transparent;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-base);
  }
  
  .shape-option:hover {
    background: var(--color-hover);
  }
  
  .shape-option.active {
    background: var(--color-primary);
    color: var(--color-white);
  }
  
  /* ==========================================================================
     Page Management Components
     ========================================================================== */
  
  .add-page-btn {
    position: absolute;
    bottom: var(--spacing-md);
    left: var(--spacing-md);
    width: 48px;
    height: 48px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    border: 1px solid var(--color-border);
    background: var(--color-white);
    box-shadow: var(--shadow-md);
    cursor: pointer;
    pointer-events: auto;
    z-index: 1000;
    transition: all var(--transition-base);
  }

  .add-page-btn:hover {
    background: var(--color-success);
    border-color: var(--color-success);
    color: var(--color-white);
    box-shadow: var(--shadow-lg);
    transform: scale(1.05);
  }
  
  .add-page-btn svg {
    width: 24px;
    height: 24px;
    stroke-width: 2;
    flex-shrink: 0;
  }
  
  .page-delete-btn {
    position: absolute;
    top: var(--spacing-md);
    right: var(--spacing-md);
    width: 48px;
    height: 48px;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: all var(--transition-base);
    box-shadow: var(--shadow-md);
    z-index: 1000;
    pointer-events: auto;
  }
  
  .page-delete-btn:hover {
    background: var(--color-error);
    border-color: var(--color-error);
    color: var(--color-white);
    box-shadow: 0 2px 8px rgba(255, 68, 68, 0.3);
  }
  
  .page-delete-btn svg {
    width: 22px;
    height: 22px;
    stroke-width: 2.5;
  }
  
  /* ==========================================================================
     Responsive Design
     ========================================================================== */
  
  @media (max-height: 600px) {
    .sidebar-toggle {
      display: flex;
    }
  }
  
  @media (max-height: 500px) {
    .left-sidebar {
      justify-content: flex-start;
      padding-top: 60px;
    }
  }

  /* ==========================================================================
     Imported Images
     ========================================================================== */

  .imported-image {
    object-fit: contain;
    max-width: none;
    max-height: none;
  }

  .image-hitbox {
    pointer-events: none;
    border: 2px solid var(--color-primary);
    border-radius: var(--radius-sm);
    background: transparent;
  }

  .image-hitbox .image-handle {
    pointer-events: auto;
    transition: all var(--transition-fast);
    background: var(--color-white);
    border: 2px solid var(--color-primary);
    border-radius: 50%;
    box-shadow: var(--shadow-sm);
  }

  /* Hide handles marked as hidden for lines */
  .image-hitbox .image-handle.line-handle-hidden {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
  }

  .image-hitbox .image-handle:hover {
    background: var(--color-primary);
    border-color: var(--color-primary);
    box-shadow: var(--shadow-md);
    transform: scale(1.2);
  }

  .image-hitbox .image-handle:active {
    transform: scale(1.1);
    box-shadow: var(--shadow-sm);
  }

  .image-hitbox .image-rotation-handle {
    pointer-events: auto;
    transition: all var(--transition-fast);
    background: var(--color-white);
    border: 2px solid var(--color-primary);
    border-radius: 50%;
    box-shadow: var(--shadow-sm);
    color: var(--color-primary);
    user-select: none;
    width: 24px !important;
    height: 24px !important;
    position: absolute;
    left: calc(50% - 12px); /* Center: 50% minus half of 24px width */
    top: -40px;
    transform: none;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    margin: 0;
    padding: 0;
    cursor: default; /* Default cursor on hover */
  }
  
  .image-hitbox .image-rotation-handle svg {
    display: block;
    margin: 0;
    padding: 0;
    flex-shrink: 0;
  }

  .image-hitbox .image-rotation-handle:hover {
    background: var(--color-hover);
    box-shadow: var(--shadow-md);
    transform: scale(1.15);
  }

  .image-hitbox .image-rotation-handle.rotating {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: var(--color-white);
    box-shadow: var(--shadow-md);
    transform: scale(1.05);
    cursor: grabbing !important; /* Grabbing hand when clicked/rotating */
  }

  .rotation-degree-display {
    position: absolute;
    background: rgba(0, 0, 0, 0.85);
    color: var(--color-white);
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    pointer-events: none;
    user-select: none;
    z-index: 1005;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    display: none;
  }