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

:root {
    --theme-color: #007AFF;
    --touch-target-size: 44px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    min-height: 100dvh;
    background-color: #f5f5f5;
    /* Improved touch device support */
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    /* Performance optimization: use GPU acceleration */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Remove tap highlight on touch devices */
    -webkit-tap-highlight-color: transparent;
}

@supports (height: 100dvh) {
    body {
        height: 100dvh;
    }
}

/* Global touch device improvements - disable tap highlight on all interactive elements */
button, a, input[type="button"], input[type="submit"] {
    -webkit-tap-highlight-color: transparent;
    -webkit-user-select: none;
    user-select: none;
}

/* Minimum touch target size for primary navigation buttons (44x44px recommended by WCAG) */
/* NOTE: .tool-btn size is controlled by JS updateToolbarSize() - do NOT set min-height/min-width here */
.history-btn, .feature-btn, .timer-action-btn, .timer-control-btn {
    min-height: var(--touch-target-size);
    min-width: var(--touch-target-size);
}

/* Canvas */
#background-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
}

#canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: crosshair;
    z-index: 1;
}

/* Eraser cursor */
#eraser-cursor {
    position: fixed;
    pointer-events: none;
    border: 2px dashed #bbb;
    box-sizing: border-box;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.3);
    z-index: 9999;
    transform: translate(-50%, -50%);
}

/* History controls (Undo/Redo/Zoom) */
#history-controls {
    position: fixed;
    top: 20px;
    right: 20px;
    display: flex;
    gap: 10px;
    z-index: 1000;
    cursor: move;
    /* Improved touch device support */
    touch-action: none;
    /* Performance optimization */
    will-change: transform, left, top;
}

#history-controls.top-left {
    right: auto;
    left: 20px;
}

#history-controls.bottom-right {
    top: auto;
    bottom: 100px;
}

#history-controls.bottom-left {
    top: auto;
    bottom: 100px;
    right: auto;
    left: 20px;
}

#history-controls.dragging {
    cursor: grabbing;
}

.history-btn {
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 12px;
    background-color: #ffffff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
    color: #333;
}

.history-btn:hover {
    background-color: #f0f0f0;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.history-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.history-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    transform: none;
}

.history-btn:disabled:hover {
    background-color: #ffffff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Zoom input */
.zoom-input {
    width: 70px;
    height: 48px;
    border: none;
    border-radius: 12px;
    background-color: #ffffff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    text-align: center;
    font-size: 14px;
    font-weight: 500;
    color: #333;
    padding: 0 8px;
    transition: all 0.2s ease;
}

.zoom-input:hover {
    background-color: #f0f0f0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.zoom-input:focus {
    outline: none;
    background-color: #ffffff;
    box-shadow: 0 0 0 2px var(--theme-color);
}

/* Configuration area */
#config-area {
    position: fixed;
    left: 50%;
    bottom: 100px; /* Position above toolbar (toolbar is at bottom: 20px + estimated height ~60px + gap 20px) */
    transform: translateX(-50%);
    z-index: 2000; /* Ensure it's above image controls overlay */
    padding: 10px 12px;
    background-color: #ffffff;
    border-radius: 14px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    min-width: 280px;
    max-width: min(550px, calc(100vw - 40px)); /* Responsive: fits within viewport */
    max-height: calc(100vh - 120px); /* Prevent touching top - leave space for positioning */
    overflow-y: auto; /* Add scroll if content exceeds max height */
    display: none;
    cursor: move;
    /* Improved touch device support */
    touch-action: none;
    /* Performance optimization */
    will-change: transform, left, top;
}

#config-area.show {
    display: block;
}

#config-area.dragging {
    cursor: grabbing;
}

/* Vertical config area layout (when snapped to left or right) */
#config-area.vertical {
    min-width: auto;
    max-width: 250px;
    max-height: 80vh;
    overflow-y: auto;
    overflow-x: hidden;
}

#config-area.vertical .config-panel {
    flex-direction: column;
}

#config-area.vertical .config-group {
    max-width: 100%;
}

#config-area.vertical .color-picker {
    max-width: 100%;
}

#config-area.vertical .color-picker-row {
    max-width: 100%;
}

#config-area.vertical .slider,
#config-area.vertical .slider-compact {
    max-width: 180px;
}

#config-area.vertical .pattern-grid-compact {
    grid-template-columns: repeat(2, 1fr);
}

#config-area.vertical .pen-type-grid {
    grid-template-columns: repeat(2, 1fr);
}

.config-close-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 6px;
    background-color: #f0f0f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease, color 0.2s ease;
    color: #666;
}

.config-close-btn:hover {
    background-color: #e0e0e0;
    color: #333;
}

.config-panel {
    display: none;
}

.config-panel.active {
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

/* Eraser config panel: center the size slider vertically */
#eraser-config.active {
    align-items: center;
}

/* Background config panel: center content vertically */
#background-config.active {
    align-items: center;
    gap: 12px;
}

#background-config .config-group-compact {
    gap: 4px;
}

#background-config .pattern-grid-compact {
    gap: 6px;
}

#background-config .pattern-grid-compact .pattern-option-btn {
    min-height: 30px;
    padding: 6px 8px;
    font-size: 11px;
}

#background-config .pattern-grid-compact .pattern-option-btn svg {
    width: 12px;
    height: 12px;
}

#background-config .upload-text {
    font-size: 11px;
}

.config-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    max-width: 100%;
}

.config-group label {
    font-size: 12px;
    color: #666;
    font-weight: 500;
}

/* Color picker */
.color-picker {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.color-picker-custom {
    margin-top: 8px;
}

.pen-size-compact {
    margin-top: 12px;
    width: 100%;
}

.pen-size-compact label {
    font-size: 11px;
    color: #666;
    font-weight: 500;
    display: block;
    margin-bottom: 6px;
}

.color-picker-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: 3px solid transparent;
    border-radius: 8px;
    background-color: #f8f8f8;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
    z-index: 1; /* Ensure clickable above other elements */
}

.color-picker-icon-btn:hover {
    background-color: #e8e8e8;
    border-color: #ddd;
    transform: scale(1.05);
}

.color-picker-icon-btn.active {
    border-color: var(--theme-color);
    transform: scale(1.15);
    background-color: #e8f4ff;
}

.color-picker-icon-btn svg {
    width: 24px;  /* Larger size for better visibility */
    height: 24px;
    color: #666;
    position: relative;
    z-index: 1;
    pointer-events: none;
    display: block; /* Ensure block display to remove any text baseline spacing */
    margin: 0 auto; /* Center horizontally */
}

.custom-color-picker-input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
}

.color-btn {
    width: 36px;
    height: 36px;
    border: 2px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.color-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
}

.color-btn.active {
    border-color: var(--theme-color);
    transform: scale(1.15);
    /* Use box-shadow to create visible border for light colors (yellow, white) */
    box-shadow: 0 0 0 2px var(--theme-color), 0 3px 8px rgba(0, 0, 0, 0.15);
}

/* Sliders */
.slider {
    width: 200px;
    height: 6px;
    border-radius: 3px;
    outline: none;
    -webkit-appearance: none;
    background: #ddd;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--theme-color);
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.slider::-moz-range-thumb {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--theme-color);
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.slider::-webkit-slider-thumb:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
}

.slider::-moz-range-thumb:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
}

/* Toolbar */
#toolbar {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: flex;
    /* gap and padding are controlled by JS updateToolbarSize() for dynamic scaling */
    gap: 8px;
    padding: 8px 12px;
    background-color: #ffffff;
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    cursor: move;
    /* Improved touch device support */
    touch-action: none;
    /* Performance optimization */
    will-change: transform, left, bottom;
}

#toolbar.toolbar-loading {
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
}

#toolbar.dragging {
    cursor: grabbing;
}

/* Vertical toolbar layout (when snapped to left or right) */
#toolbar.vertical {
    flex-direction: column;
    /* gap and padding are controlled by JS updateToolbarSize() */
}

#toolbar.vertical .tool-btn {
    /* Size is controlled by JS updateToolbarSize() */
    justify-content: center;
}

/* Hide text labels in vertical mode to save space */
#toolbar.vertical .tool-btn span {
    display: none !important;
}

/* Hide text labels when user explicitly disables text via setting */
#toolbar.hide-text .tool-btn span {
    display: none !important;
}

#toolbar.hide-text .tool-btn {
    /* Size is controlled by JS, just need to center the icon */
    justify-content: center;
}

.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* gap, padding, width, height are set by JS updateToolbarSize() */
    border: 2px solid transparent;
    border-radius: 12px;
    background-color: transparent;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
    color: #666;
    box-sizing: border-box;
}

/* Button size variants - now fully controlled by JS updateToolbarSize() */
/* These are fallback defaults if JS hasn't run yet */
#toolbar.size-small .tool-btn {
    /* Sizing controlled by JS */
}

#toolbar.size-small .tool-btn svg {
    /* Sizing controlled by JS */
}

#toolbar.size-small .tool-btn span {
    /* Sizing controlled by JS */
}

#toolbar.size-large .tool-btn {
    /* Sizing controlled by JS */
}

#toolbar.size-large .tool-btn svg {
    /* Sizing controlled by JS */
}

#toolbar.size-large .tool-btn span {
    /* Sizing controlled by JS */
}

.tool-btn svg {
    transition: stroke 0.2s ease, transform 0.2s ease;
}

.tool-btn span {
    font-size: 12px;
    font-weight: 500;
}

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

.tool-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

.tool-btn.active svg {
    stroke: #ffffff;
}

/* Settings Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding: 12px;
    box-sizing: border-box;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 2000;
    display: none;
    align-items: center;
    justify-content: center;
}

.modal.show {
    display: flex;
}

body.overlay-modal-open #config-area,
body.overlay-modal-open #feature-area,
body.overlay-modal-open #time-display-area,
body.overlay-modal-open .feature-widget,
body.overlay-modal-open .timer-display-widget {
    pointer-events: none !important;
}

body.overlay-modal-open .modal.show,
body.overlay-modal-open .time-fullscreen-modal.show,
body.overlay-modal-open .timer-fullscreen-modal.show {
    pointer-events: auto;
}

.modal-content {
    background-color: #ffffff;
    border-radius: 16px;
    padding: 24px;
    min-width: 320px;
    max-width: 90%;
    max-height: calc(100vh - 24px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.settings-modal-content {
    min-width: 700px;
    max-width: 850px;
    width: 80vw;
    max-height: 85vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-right: 4px; /* Ensure space for close button */
}

.modal-draggable-header {
    cursor: move;
    user-select: none;
}

.modal-draggable-header button {
    cursor: pointer;
}

.modal-header h2 {
    font-size: 20px;
    font-weight: 600;
    color: #333;
    display: flex;
    align-items: center; /* Align help button with title text */
}

.modal-close-btn {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background-color: #f0f0f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    color: #666;
}

.modal-close-btn:hover {
    background-color: #e0e0e0;
    color: #333;
}

.modal-title-group {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.modal-reset-size-btn,
.modal-keep-centered-btn {
    display: none;
    border: 1px solid #d9d9df;
    border-radius: 999px;
    background: #f7f7fa;
    color: #555;
    font-size: 12px;
    line-height: 1;
    padding: 6px 10px;
    cursor: pointer;
    white-space: nowrap;
}

.modal-reset-size-btn.show,
.modal-keep-centered-btn.show {
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.modal-keep-centered-btn.active {
    background: rgba(0, 122, 255, 0.12);
    border-color: rgba(0, 122, 255, 0.45);
    color: #0a63c9;
}

.resizable-modal-content {
    position: relative;
}

.resizable-modal-content.modal-custom-sized {
    margin: 0;
}

.resizable-modal-content.modal-resizing {
    user-select: none;
}

.resizable-modal-content.modal-dragging {
    user-select: none;
}

.modal-resize-handle {
    position: absolute;
    width: 18px;
    height: 18px;
    z-index: 2;
    touch-action: none;
}

.modal-resize-handle::before {
    content: '';
    position: absolute;
    inset: 3px;
    border-radius: 50%;
    background: rgba(0, 122, 255, 0.16);
    border: 1px solid rgba(0, 122, 255, 0.45);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.resizable-modal-content:hover .modal-resize-handle::before,
.resizable-modal-content.modal-resizing .modal-resize-handle::before {
    opacity: 1;
}

.modal-resize-handle.top-left {
    top: 4px;
    left: 4px;
    cursor: nwse-resize;
}

.modal-resize-handle.top-right {
    top: 4px;
    right: 4px;
    cursor: nesw-resize;
}

.modal-resize-handle.bottom-left {
    bottom: 4px;
    left: 4px;
    cursor: nesw-resize;
}

.modal-resize-handle.bottom-right {
    right: 4px;
    bottom: 4px;
    cursor: nwse-resize;
}

.help-inline-container {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    width: 100%;
}

.help-inline-container > label {
    flex: 1 1 auto;
    margin-bottom: 0;
}

.help-modal-content {
    width: min(480px, 92vw);
    max-width: min(480px, 92vw);
    min-height: 0;
    max-height: min(72vh, calc(100vh - 24px));
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.help-modal-content .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    justify-content: center;
}

.help-content {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: stretch;
    overflow: auto;
    padding: 2px 6px 2px 0;
    text-align: left;
    line-height: 1.75;
    font-size: 14px;
    color: #2f3542;
    word-break: break-word;
}

.help-content > div {
    width: 100%;
    margin: 0;
}

.help-content > div + div {
    margin-top: 6px;
}

.help-content b {
    color: #1f2d3d;
}

.help-content a {
    color: var(--theme-color, #007aff);
}

/* Settings layout with sidebar */
.settings-layout {
    display: flex;
    gap: 0;
    height: 100%;
    overflow: hidden;
}

.settings-sidebar {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 140px;
    width: 180px;
    border-right: 2px solid #f0f0f0;
    padding-right: 20px;
    overflow-y: auto;
    max-height: 75vh;
}

.settings-tab-icon {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 12px 8px;
    border: none;
    background-color: transparent;
    color: #666;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 8px;
}

.settings-tab-icon:hover {
    background-color: #f8f8f8;
    color: #333;
}

.settings-tab-icon.active {
    background-color: var(--theme-color);
    color: #ffffff;
}

.settings-tab-icon.active svg {
    stroke: #ffffff;
}

.settings-tab-icon svg {
    stroke: currentColor;
}

.settings-content {
    flex: 1;
    padding-left: 32px;
    padding-right: 16px;
    overflow-y: auto;
    max-height: 75vh; /* Increased from 70vh for longer scrollbar */
}

.settings-content h3 {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    margin-bottom: 16px;
}

/* Settings tabs - deprecated but keeping for compatibility */
.settings-tabs {
    display: none;
}

.settings-tab {
    display: none;
}

.settings-tab-content {
    display: none;
}

.settings-tab-content.active {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* About page styles */
.about-info {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.about-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.about-section h4 {
    font-size: 14px;
    font-weight: 600;
    color: #333;
    margin: 0;
}

.about-section p {
    font-size: 13px;
    color: #666;
    line-height: 1.6;
    margin: 0;
}

.about-section ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.about-section ul li {
    font-size: 13px;
    color: #666;
    line-height: 1.8;
    padding-left: 20px;
    position: relative;
}

.about-section ul li::before {
    content: "•";
    position: absolute;
    left: 8px;
    color: var(--theme-color);
}

.about-section a {
    color: var(--theme-color);
    text-decoration: none;
    font-weight: 500;
}

.about-section a:hover {
    text-decoration: underline;
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.settings-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.settings-group.collapsible {
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 6px 10px;
    background: #fafafa;
    min-height: auto; /* Compact when collapsed */
}

/* When expanded, use minimal padding to maximize content space */
.settings-group.collapsible:not(.collapsed) {
    padding: 10px 12px;
    min-height: auto;
}

/* Settings groups with sliders need slightly more space */
.settings-group.collapsible:not(.collapsed):has(.slider) {
    padding: 10px 12px;
    min-height: auto;
}

/* Collapsed state should be compact */
.settings-group.collapsible.collapsed {
    padding: 6px 10px;
    min-height: auto;
}

.settings-group-header {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
}

.collapse-toggle {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: transform 0.2s ease;
    flex-shrink: 0;
}

.collapse-toggle svg {
    width: 12px;
    height: 12px;
}

.settings-group.collapsed .collapse-toggle {
    transform: rotate(-90deg);
}

.settings-group-content {
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: max-height 0.3s ease, opacity 0.3s ease;
    overflow: hidden;
}

.settings-group.collapsed .settings-group-content {
    max-height: 0 !important;
    opacity: 0;
    margin-top: 0;
}

.settings-group label {
    font-size: 14px;
    color: #333;
    font-weight: 500;
}

.button-size-options {
    display: flex;
    gap: 12px;
}

.eraser-shape-options {
    display: flex;
    gap: 8px;
}

.eraser-shape-btn {
    flex: 1;
    padding: 8px 12px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 12px;
    font-weight: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.eraser-shape-btn svg {
    flex-shrink: 0;
}

.eraser-shape-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.eraser-shape-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

.select-mode-options {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.select-mode-btn {
    flex: 0 1 auto;
    padding: 8px 10px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 11px;
    font-weight: 500;
    white-space: nowrap;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.select-mode-btn svg {
    flex-shrink: 0;
}

.select-mode-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.select-mode-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

.button-size-options-4 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

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

.button-size-options.pattern-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.size-option-btn {
    flex: 1;
    padding: 12px 16px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 14px;
    font-weight: 500;
}

.size-option-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.size-option-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

.position-option-btn,
.display-option-btn {
    flex: 1;
    padding: 12px 16px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 14px;
    font-weight: 500;
}

.position-option-btn:hover,
.display-option-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.position-option-btn.active,
.display-option-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

/* Toolbar customization list */
.toolbar-customization-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
}

.toolbar-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: #f8f9fa;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    cursor: grab;
    transition: background-color 0.2s, box-shadow 0.2s;
}

.toolbar-item:hover {
    background: #f0f0f0;
}

.toolbar-item.dragging {
    opacity: 0.5;
    cursor: grabbing;
}

.toolbar-item .drag-handle {
    color: #999;
    cursor: grab;
    font-size: 14px;
    user-select: none;
}

.toolbar-item input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.toolbar-item span:last-child {
    flex: 1;
    font-size: 14px;
    color: #333;
}

/* Control button settings */
.control-button-settings {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
}

.control-button-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px;
    background: #f8f9fa;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    cursor: grab;
    transition: background-color 0.2s, transform 0.2s;
}

.control-button-item:active {
    cursor: grabbing;
}

.control-button-item.dragging {
    opacity: 0.5;
    background: #e3f2fd;
}

.control-button-item.drag-over {
    border-color: #4285f4;
    background: #e8f0fe;
}

.control-button-item .drag-handle {
    color: #999;
    font-size: 14px;
    padding: 4px 8px;
    cursor: grab;
    user-select: none;
}

.control-button-item .drag-handle:active {
    cursor: grabbing;
}

.control-button-item .checkbox-label {
    flex: 1;
    padding: 8px 12px;
    margin: 0;
    background: transparent;
    border: none;
}

.control-button-settings .checkbox-label {
    padding: 8px 12px;
    background: #f8f9fa;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
}

.pattern-option-btn {
    flex: 1;
    padding: 8px 12px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
}

.pattern-option-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.pattern-option-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

.pen-type-btn {
    flex: 1;
    padding: 7px 8px;
    border: 2px solid #ddd;
    border-radius: 6px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 11px;
    font-weight: 500;
    white-space: nowrap;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    touch-action: manipulation;
}

.pen-type-btn svg {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
}

.pen-type-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.pen-type-btn.active {
    background-color: rgba(0, 122, 255, 0.08);
    color: var(--theme-color);
    border-color: var(--theme-color);
    box-shadow: 0 4px 10px rgba(0, 122, 255, 0.14);
}

.font-management-list {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.font-management-item {
    display: grid;
    grid-template-columns: auto auto 1fr auto auto;
    align-items: center;
    gap: 8px;
    border: 1px solid #e5e5ea;
    border-radius: 8px;
    padding: 6px 8px;
    background: #fff;
}

.font-management-item .drag-handle {
    color: #999;
    cursor: grab;
    user-select: none;
}

.font-management-item.dragging {
    opacity: 0.6;
}

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

.font-action-btn {
    border: 1px solid #d1d1d6;
    border-radius: 6px;
    background: #f7f7fa;
    color: #333;
    font-size: 12px;
    padding: 4px 8px;
    cursor: pointer;
}

.font-preview-sample {
    grid-column: 1 / -1;
    display: none;
    font-size: 15px;
    color: #444;
    padding: 6px 8px;
    border-radius: 6px;
    background: #f7f7fa;
    border: 1px dashed #d1d1d6;
}

.font-alias-input {
    border: 1px solid #d1d1d6;
    border-radius: 6px;
    height: 28px;
    font-size: 12px;
    padding: 2px 6px;
    grid-column: 3 / 6;
}

.settings-hint {
    margin: 4px 0 0 0;
    font-size: 12px;
    color: #999;
}

/* Pagination controls */
#pagination-controls {
    position: fixed;
    top: 80px;
    right: 20px;
    z-index: 1000;
    display: none;
    cursor: move;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background-color: #ffffff;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

#pagination-controls.show {
    display: flex;
}

#pagination-controls.top-left {
    right: auto;
    left: 20px;
}

#pagination-controls.bottom-right {
    top: auto;
    bottom: 170px;  /* Position above the history controls */
}

#pagination-controls.bottom-left {
    top: auto;
    bottom: 170px;  /* Position above the history controls */
    right: auto;
    left: 20px;
}

.page-nav-btn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    background-color: #f0f0f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    color: #333;
}

.page-nav-btn:hover {
    background-color: #e0e0e0;
}

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

.page-input {
    width: 50px;
    height: 36px;
    border: 1px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    text-align: center;
    font-size: 14px;
    font-weight: 500;
    color: #333;
    padding: 0 8px;
}

.page-input:focus {
    outline: none;
    border-color: var(--theme-color);
}

#page-total {
    font-size: 14px;
    font-weight: 500;
    color: #666;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.checkbox-label span {
    font-size: 14px;
    color: #333;
    font-weight: 500;
}

.settings-cache-options {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 10px;
}

/* Confirm Modal */
.confirm-modal-content {
    max-width: 400px;
    width: min(400px, 100%);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.confirm-modal-content .modal-body {
    min-height: 0;
    overflow-y: auto;
}

.confirm-message {
    font-size: 16px;
    color: #666;
    margin-bottom: 24px;
    line-height: 1.5;
}

.confirm-message.compact {
    margin-bottom: 0;
}

.app-confirm-options {
    display: none;
    flex-direction: column;
    gap: 10px;
}

.app-confirm-options.show {
    display: flex;
}

.app-confirm-option {
    align-items: flex-start;
    padding: 10px 12px;
    border: 1px solid #e5e5ea;
    border-radius: 10px;
    background: #fafafa;
}

.app-confirm-footer {
    display: none;
    margin-bottom: 0;
    font-size: 14px;
}

.app-confirm-footer.show {
    display: block;
}

.confirm-buttons {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}

.confirm-btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

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

.cancel-btn:hover {
    background-color: #e0e0e0;
}

.ok-btn {
    background-color: #FF3B30;
    color: #ffffff;
}

.ok-btn:hover {
    background-color: #E02020;
}

/* Announcement modal styles */
.announcement-modal-content {
    max-width: 500px;
    width: min(500px, 100%);
    height: auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.announcement-modal-content .modal-body {
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.announcement-content {
    flex: 1 1 auto;
    min-height: 0;
    font-size: 15px;
    color: #333;
    line-height: 1.8;
    white-space: pre-wrap;
    overflow-y: auto;
}

.announcement-buttons {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    justify-content: flex-end;
    margin-top: auto;
    padding-top: 16px;
    border-top: 1px solid #eee;
    background-color: #ffffff;
    flex-shrink: 0;
}

.announcement-btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.announcement-btn.ok-btn {
    background-color: var(--theme-color);
    color: #ffffff;
}

.announcement-btn.ok-btn:hover {
    opacity: 0.9;
}

.announcement-btn.no-show-btn {
    background-color: #f0f0f0;
    color: #333;
}

.announcement-btn.no-show-btn:hover {
    background-color: #e0e0e0;
}

.announcement-install-btn {
    margin-right: auto;
}

#pwa-announcement-status {
    flex-shrink: 0;
}

.settings-announcement-content {
    font-size: 15px;
    color: #333;
    line-height: 1.8;
    white-space: pre-wrap;
}

.announcement-info {
    padding: 16px;
    background-color: #f8f9fa;
    border-radius: 8px;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .settings-modal-content {
        min-width: 90%;
        max-width: 95%;
    }
    
    .settings-layout {
        flex-direction: column;
    }
    
    .settings-sidebar {
        flex-direction: row;
        border-right: none;
        border-bottom: 2px solid #f0f0f0;
        padding-right: 0;
        padding-bottom: 12px;
        min-width: 100%;
        overflow-x: auto;
    }
    
    .settings-content {
        padding-left: 0;
        padding-top: 16px;
    }
    
    #config-area {
        min-width: 280px;
        padding: 12px 16px;
        bottom: 90px;
    }

    .config-panel.active {
        flex-direction: column;
        gap: 16px;
        align-items: stretch;
    }

    .slider {
        width: 100%;
    }

    #toolbar {
        gap: 8px;
        padding: 10px 16px;
    }

    #toolbar.size-small {
        padding: 8px 12px;
        gap: 6px;
    }

    #toolbar.size-large {
        padding: 12px 20px;
        gap: 10px;
    }

    .tool-btn {
        /* Size controlled by JS */
    }

    .tool-btn span {
        font-size: 11px;
    }
    
    /* Hide the Move (pan) button on mobile devices - pan is done via pinch gestures */
    #pan-btn {
        display: none;
    }

    #history-controls {
        top: 10px;
        right: 10px;
        gap: 8px;
    }

    .history-btn {
        width: 40px;
        height: 40px;
    }

    .history-btn svg {
        width: 20px;
        height: 20px;
    }

    .modal-content {
        min-width: 280px;
    }
}

@media (max-height: 720px), (max-width: 480px) {
    .modal {
        padding: 8px;
    }

    .modal-content {
        padding: 18px;
        max-height: calc(100vh - 16px);
    }

    .announcement-content,
    .settings-announcement-content {
        font-size: 14px;
        line-height: 1.65;
    }

    .announcement-buttons {
        gap: 10px;
        padding-top: 12px;
    }

    .announcement-btn {
        padding: 10px 16px;
        font-size: 13px;
    }

    .text-input-modal-content {
        min-width: 0;
    }
}

/* Performance optimizations */
#canvas {
    will-change: contents;
}

.tool-btn,
.history-btn,
.color-btn {
    -webkit-tap-highlight-color: transparent;
}

/* Pattern preferences styles */
.pattern-preferences {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 10px;
}

.pattern-preferences .checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background-color: #f5f5f5;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.pattern-preferences .checkbox-label:hover {
    background-color: #e8e8e8;
}

.pattern-preferences input[type="checkbox"] {
    cursor: pointer;
}

/* Image pattern button styling */
#image-pattern-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

#image-pattern-btn svg {
    flex-shrink: 0;
}

#image-size-group {
    margin-top: 15px;
}

/* Image Controls Overlay */
.image-controls-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1500;
    pointer-events: none;
}

.image-controls-box {
    position: absolute;
    border: 2px solid var(--theme-color);
    pointer-events: all;
    cursor: move;
    box-shadow: 0 0 0 1px rgba(0, 122, 255, 0.1);
    transform-origin: center center;
    touch-action: none;
    --resize-handle-size: 12px;
    --resize-handle-offset: -6px;
    --floating-control-size: 36px;
    --floating-control-icon-size: 18px;
    --handle-connector-top: 32px;
    --handle-connector-height: 12px;
    --toolbar-button-size: 44px;
    --toolbar-gap: 8px;
    --toolbar-padding-y: 10px;
    --toolbar-padding-x: 10px;
}

#selection-controls-box.text-selection-only {
    /* Text selections now show the same border and resize handles as other types */
}

#selection-controls-box.text-selection-only .resize-handle {
    /* Show resize handles for text selections */
}

/* Resize handles */
.resize-handle {
    position: absolute;
    background-color: var(--theme-color);
    border: 2px solid white;
    width: var(--resize-handle-size);
    height: var(--resize-handle-size);
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.resize-handle.top-left {
    top: var(--resize-handle-offset);
    left: var(--resize-handle-offset);
    cursor: nw-resize;
}

.resize-handle.top-right {
    top: var(--resize-handle-offset);
    right: var(--resize-handle-offset);
    cursor: ne-resize;
}

.resize-handle.bottom-left {
    bottom: var(--resize-handle-offset);
    left: var(--resize-handle-offset);
    cursor: sw-resize;
}

.resize-handle.bottom-right {
    bottom: var(--resize-handle-offset);
    right: var(--resize-handle-offset);
    cursor: se-resize;
}

.resize-handle.top {
    top: var(--resize-handle-offset);
    left: 50%;
    transform: translateX(-50%);
    cursor: n-resize;
}

.resize-handle.bottom {
    bottom: var(--resize-handle-offset);
    left: 50%;
    transform: translateX(-50%);
    cursor: s-resize;
}

.resize-handle.left {
    left: var(--resize-handle-offset);
    top: 50%;
    transform: translateY(-50%);
    cursor: w-resize;
}

.resize-handle.right {
    right: var(--resize-handle-offset);
    top: 50%;
    transform: translateY(-50%);
    cursor: e-resize;
}

/* Rotation handle */
.rotate-handle {
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    width: var(--floating-control-size);
    height: var(--floating-control-size);
    --floating-handle-bg: var(--theme-color);
    background-color: var(--floating-handle-bg);
    border-radius: 50%;
    cursor: grab;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease;
}

.rotate-handle:hover {
    transform: translateX(-50%) scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.rotate-handle:active {
    cursor: grabbing;
}

/* Flip handles */
.flip-handle {
    position: absolute;
    top: -40px;
    width: var(--floating-control-size);
    height: var(--floating-control-size);
    --floating-handle-bg: var(--theme-color);
    background-color: var(--floating-handle-bg);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease;
    z-index: 10;
}

.flip-handle.flip-horizontal {
    left: calc(50% + 50px);
    transform: translateX(-50%);
}

.flip-handle.flip-vertical {
    left: calc(50% + 90px);
    transform: translateX(-50%);
}

.flip-handle:hover {
    transform: translateX(-50%) scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.flip-handle:active {
    transform: translateX(-50%) scale(0.95);
}

.flip-handle.active {
    --floating-handle-bg: #34C759;
    background-color: var(--floating-handle-bg);
}

.flip-handle::after {
    content: '';
    position: absolute;
    top: var(--handle-connector-top);
    left: 50%;
    width: 2px;
    height: var(--handle-connector-height);
    background-color: var(--floating-handle-bg);
    transform: translateX(-50%);
    pointer-events: none;
}

.flip-handle.active::after {
    background-color: var(--floating-handle-bg);
}

/* Selection transform handles (rotate 90, flip) */
.selection-transform-handle {
    position: absolute;
    top: -40px;
    width: var(--floating-control-size);
    height: var(--floating-control-size);
    --floating-handle-bg: var(--theme-color);
    background-color: var(--floating-handle-bg);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease;
    z-index: 10;
    pointer-events: all;
}

#selection-rotate90-handle {
    left: calc(50% + 40px);
    transform: translateX(-50%);
}

#selection-flip-h-handle {
    left: calc(50% + 76px);
    transform: translateX(-50%);
}

.selection-transform-handle:hover {
    transform: translateX(-50%) scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.selection-transform-handle:active {
    transform: translateX(-50%) scale(0.95);
}

.selection-transform-handle::after {
    content: '';
    position: absolute;
    top: var(--handle-connector-top);
    left: 50%;
    width: 2px;
    height: var(--handle-connector-height);
    background-color: var(--floating-handle-bg);
    transform: translateX(-50%);
    pointer-events: none;
}

.selection-transform-handle svg {
    width: var(--floating-control-icon-size);
    height: var(--floating-control-icon-size);
}

/* Image controls toolbar */
.image-controls-toolbar {
    position: absolute;
    bottom: -60px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: var(--toolbar-gap);
    padding: var(--toolbar-padding-y) var(--toolbar-padding-x);
    background-color: white;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    max-width: calc(100% - 8px);
    box-sizing: border-box;
}

.selection-layer-menu-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.selection-layer-menu {
    position: absolute;
    bottom: 54px;
    left: 50%;
    transform: translateX(-50%);
    display: none;
    flex-direction: column;
    gap: 6px;
    padding: 8px;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
    z-index: 20;
}

.selection-layer-menu.show {
    display: flex;
}

.selection-layer-item {
    border: none;
    background-color: #f2f2f2;
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 12px;
    color: #333;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s ease;
}

.selection-layer-item:hover {
    background-color: #e4e4e4;
    transform: translateY(-1px);
}

.selection-layer-item:active {
    transform: translateY(0);
}

.image-control-btn {
    width: var(--toolbar-button-size);
    height: var(--toolbar-button-size);
    border: none;
    border-radius: 10px;
    background-color: #f0f0f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.12s ease, color 0.12s ease, transform 0.12s ease, box-shadow 0.12s ease;
    color: #333;
}

#selection-rotate-handle {
    --floating-handle-bg: #0A84FF;
}

#selection-rotate90-handle {
    --floating-handle-bg: #FF9500;
}

#selection-flip-h-handle,
#flip-horizontal-handle {
    --floating-handle-bg: #34C759;
}

#flip-vertical-handle {
    --floating-handle-bg: #AF52DE;
}

.rotate-handle svg,
.flip-handle svg,
.image-control-btn svg {
    width: var(--floating-control-icon-size);
    height: var(--floating-control-icon-size);
}

.image-control-btn:hover {
    background-color: #e0e0e0;
    transform: translateY(-2px);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.image-control-btn:active {
    transform: translateY(0);
}

.image-done-btn {
    background-color: #34C759;
    color: white;
}

.image-done-btn:hover {
    background-color: #28A745;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(52, 199, 89, 0.3);
}

/* Canvas mode buttons */
.button-size-options-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.canvas-mode-btn {
    padding: 12px 16px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 14px;
    font-weight: 500;
}

.canvas-mode-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.canvas-mode-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

/* Canvas presets */
.canvas-presets {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.canvas-preset-btn {
    padding: 10px 12px;
    border: 3px solid #ddd;
    border-radius: 8px;
    background-color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #666;
    font-size: 12px;
    font-weight: 500;
}

.canvas-preset-btn:hover {
    background-color: #f0f0f0;
    border-color: #ccc;
}

.canvas-preset-btn.active {
    background-color: var(--theme-color);
    color: #ffffff;
    border-color: var(--theme-color);
}

/* Canvas custom size */
.canvas-custom-size {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    background-color: #f8f8f8;
    border-radius: 8px;
}

.custom-size-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.custom-size-row label {
    min-width: 50px;
    font-size: 13px;
    color: #666;
}

.custom-size-row input[type="number"],
.custom-size-row select {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 13px;
    background-color: #ffffff;
}

.custom-size-row span {
    font-size: 13px;
    color: #666;
}

/* Compact pen and background config layouts */
.color-picker-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.color-picker-main {
    display: flex;
    gap: 6px;
}

.color-and-size-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 8px;
}

.pen-size-inline {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
}

.pen-size-inline label {
    font-size: 11px;
    color: #666;
    font-weight: 500;
}

.slider-compact {
    width: 150px;
}

.config-group-compact {
    gap: 6px;
}

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

.pattern-grid-compact .pattern-option-btn {
    padding: 8px 10px;
    font-size: 11px;
}

.compact-sliders {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 16px;
}

.compact-slider-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.compact-slider-item label {
    font-size: 11px;
    color: #666;
    font-weight: 500;
}

/* Responsive sizing for controls based on screen size */
@media (max-width: 1024px) {
    .color-btn {
        width: 32px;
        height: 32px;
    }
    
    .color-picker-icon-btn {
        width: 36px;
        height: 36px;
    }
    
    .slider-compact {
        width: 120px;
    }
    
    #history-controls {
        gap: 8px;
    }
    
    .history-btn {
        width: 44px;
        height: 44px;
    }
    
    .zoom-input {
        width: 65px;
        height: 44px;
        font-size: 13px;
    }
}

@media (max-width: 768px) {
    .color-btn {
        width: 28px;
        height: 28px;
    }
    
    .color-picker-icon-btn {
        width: 32px;
        height: 32px;
    }
    
    .slider-compact {
        width: 100px;
    }
    
    .pattern-grid-compact {
        grid-template-columns: repeat(3, 1fr);
    }
    
    #history-controls {
        gap: 6px;
    }
    
    .history-btn {
        width: 40px;
        height: 40px;
    }
    
    .zoom-input {
        width: 60px;
        height: 40px;
        font-size: 12px;
    }
    
    #config-area {
        min-width: 280px;
        padding: 12px 18px;
    }
    
    .tool-btn {
        /* Size controlled by JS */
    }
    
    .tool-btn svg {
        /* Size controlled by JS */
    }
    
    .tool-btn span {
        /* Size controlled by JS */
    }
}

@media (min-width: 1920px) {
    /* Larger screens - scale up controls */
    .color-btn {
        width: 40px;
        height: 40px;
    }
    
    .color-picker-icon-btn {
        width: 44px;
        height: 44px;
    }
    
    .history-btn {
        width: 52px;
        height: 52px;
    }
    
    .zoom-input {
        width: 75px;
        height: 52px;
        font-size: 15px;
    }
    
    #config-area {
        min-width: 350px;
        padding: 18px 28px;
        max-width: 800px; /* Larger max-width for big screens */
    }
    
    .tool-btn {
        /* Size controlled by JS */
    }
    
    /* Settings groups with more space on large screens */
    .settings-group.collapsible:not(.collapsed) {
        padding: 16px 20px;
    }
}

/* Canvas Image Selection Overlay */
.canvas-image-selection {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 900;
}

.selection-box {
    position: absolute;
    border: 2px solid var(--theme-color);
    box-shadow: 0 0 0 1px rgba(0, 122, 255, 0.1);
    pointer-events: all;
    cursor: move;
}

.selection-box .resize-handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: var(--theme-color);
    border: 2px solid white;
    border-radius: 50%;
    pointer-events: all;
}

.selection-box .resize-handle.top-left {
    top: -6px;
    left: -6px;
    cursor: nwse-resize;
}

.selection-box .resize-handle.top-right {
    top: -6px;
    right: -6px;
    cursor: nesw-resize;
}

.selection-box .resize-handle.bottom-left {
    bottom: -6px;
    left: -6px;
    cursor: nesw-resize;
}

.selection-box .resize-handle.bottom-right {
    bottom: -6px;
    right: -6px;
    cursor: nwse-resize;
}

.selection-box .resize-handle.top {
    top: -6px;
    left: 50%;
    transform: translateX(-50%);
    cursor: ns-resize;
}

.selection-box .resize-handle.bottom {
    bottom: -6px;
    left: 50%;
    transform: translateX(-50%);
    cursor: ns-resize;
}

.selection-box .resize-handle.left {
    top: 50%;
    left: -6px;
    transform: translateY(-50%);
    cursor: ew-resize;
}

.selection-box .resize-handle.right {
    top: 50%;
    right: -6px;
    transform: translateY(-50%);
    cursor: ew-resize;
}

.selection-box .rotate-handle {
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    width: 32px;
    height: 32px;
    background: var(--theme-color);
    border: 2px solid white;
    border-radius: 50%;
    cursor: grab;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: all;
}

.selection-box .rotate-handle:active {
    cursor: grabbing;
}

/* Button styles for insert and selection tools */
.button-primary {
    background: #0066FF;
    color: white;
    border: none;
    border-radius: 8px;
    padding: 12px 20px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.button-primary:hover {
    background: #0052CC;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 102, 255, 0.3);
}

.button-primary:active {
    transform: translateY(0);
}

.button-secondary {
    background: #f0f0f0;
    color: #333;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.button-secondary:hover:not(:disabled) {
    background: #e0e0e0;
    border-color: #ccc;
}

.button-secondary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.button-secondary:active:not(:disabled) {
    transform: translateY(1px);
}

/* Export Modal Styles */
.export-modal-content {
    max-width: 500px;
    width: 90%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.export-modal-content .modal-body {
    min-height: 0;
    overflow-y: auto;
}

.export-options {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.export-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.export-group label {
    font-weight: 500;
    font-size: 14px;
    color: #333;
}

.export-format-btn {
    background: white;
    border: 3px solid #e0e0e0;
    border-radius: 8px;
    padding: 12px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.export-format-btn:hover {
    border-color: #0066ff;
    background: #f0f7ff;
}

.export-format-btn.active {
    border-color: #0066ff;
    background: #0066ff;
    color: white;
}

.export-filename-input {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.2s ease;
}

.export-filename-input:focus {
    outline: none;
    border-color: #0066ff;
}

.export-actions {
    display: flex;
    gap: 10px;
    margin-top: 10px;
}

.export-actions button {
    flex: 1;
}

/* Selection Action Buttons */
.selection-action-buttons {
    position: absolute;
    top: -45px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 5px;
    background: rgba(0, 0, 0, 0.8);
    padding: 5px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    z-index: 10;
}

.selection-action-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 6px;
    padding: 8px 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    color: white;
}

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

.selection-action-btn:active {
    transform: scale(0.95);
}

.selection-action-btn svg {
    display: block;
}

/* Right-click context menu for images */
.image-context-menu {
    position: fixed;
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    padding: 8px;
    z-index: 10000;
    min-width: 150px;
}

.context-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s ease;
    color: #333;
    font-size: 14px;
}

.context-menu-item:hover {
    background: #f0f0f0;
}

.context-menu-item svg {
    flex-shrink: 0;
}

.context-menu-item span {
    flex: 1;
}

/* Icon button for adjustment */
.icon-btn {
    background: #f0f0f0;
    color: #333;
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    min-height: 32px;
}

.icon-btn:hover {
    background: #e0e0e0;
    border-color: #ccc;
    transform: translateY(-1px);
}

.icon-btn:active {
    transform: translateY(0);
}

.icon-btn.active {
    background: var(--theme-color);
    color: white;
    border-color: var(--theme-color);
}

.icon-btn.active:hover {
    background: var(--theme-color);
    opacity: 0.9;
}

.icon-btn svg {
    display: block;
}

/* Text Input Modal */
.text-input-modal-content {
    min-width: 400px;
    max-width: 500px;
    width: min(500px, 100%);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.text-input-modal-content .modal-body {
    min-height: 0;
    overflow-y: auto;
}

.text-input-area {
    width: 100%;
    min-height: 120px;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-family: inherit;
    font-size: 16px;
    resize: vertical;
    margin-bottom: 16px;
}

.text-input-area:focus {
    outline: none;
    border-color: var(--theme-color);
}

.text-input-controls {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 20px;
}

.text-control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.text-control-group label {
    font-size: 14px;
    font-weight: 500;
    color: #333;
}

.text-input-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* Format Select Styles */
.format-select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 14px;
    background-color: white;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s;
}

.format-select:hover {
    border-color: #999;
}

.format-select:focus {
    border-color: var(--theme-color, #007AFF);
}

/* Size Input Styles */
.size-input {
    width: 70px;
    padding: 6px 10px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
    outline: none;
    transition: border-color 0.2s;
}

.size-input:hover {
    border-color: #999;
}

.size-input:focus {
    border-color: var(--theme-color, #007AFF);
}

/* Time Display Area Styles */
#time-display-area {
    position: fixed;
    left: 50%;
    bottom: 100px;
    transform: translateX(-50%);
    z-index: 999;
    padding: 16px 20px;
    background-color: #ffffff;
    border-radius: 16px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    min-width: 240px;
    cursor: move;
    max-width: 280px;
    display: none;
    touch-action: none;
    will-change: transform, left, top;
}

/* Panel drag handle - a visible area for dragging panels */
.panel-drag-handle {
    position: absolute;
    top: 0;
    left: 0;
    right: 40px; /* Leave room for close button */
    height: 24px;
    cursor: move;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(to bottom, rgba(0,0,0,0.02), transparent);
    border-radius: 16px 0 0 0;
}

.panel-drag-handle .drag-handle-dots {
    color: #999;
    font-size: 12px;
    letter-spacing: 2px;
    user-select: none;
    pointer-events: none;
}

.panel-drag-handle:hover {
    background: linear-gradient(to bottom, rgba(0,0,0,0.05), transparent);
}

.panel-drag-handle:hover .drag-handle-dots {
    color: #666;
}

#time-display-area.show {
    display: block;
}

#time-display-area.dragging {
    cursor: grabbing;
}

/* When docked to edge (vertical mode) */
#time-display-area.vertical {
    min-width: auto;
    max-width: 240px;
    max-height: 80vh;
    overflow-y: auto;
    overflow-x: hidden;
}

#time-display-area.vertical .config-group {
    max-width: 100%;
}

#time-display-area.vertical label {
    font-size: 11px;
}

#time-display-area .config-group label {
    font-size: 12px;
    color: #666;
    font-weight: 500;
    display: block;
    margin-bottom: 8px;
}

.time-display-options-row {
    display: flex;
    align-items: flex-start;
    gap: 12px;
}

.time-display-checkboxes {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.time-display-settings-icon-btn {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    padding: 6px;
    border: none;
    border-radius: 6px;
    background-color: #f0f0f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    color: #666;
}

.time-display-settings-icon-btn:hover {
    background-color: #e0e0e0;
    color: #333;
}

/* Smooth transform transition for double-tap zoom */
.smooth-transform {
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* ============================================
   Additional Responsive & Touch Improvements
   ============================================ */

/* Portrait prompt overlay for touch devices */
#portrait-orientation-overlay {
    position: fixed;
    inset: 0;
    z-index: 6000;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(15, 18, 24, 0.92);
    color: #ffffff;
    padding: 24px;
    text-align: center;
}

#portrait-orientation-overlay .overlay-content {
    max-width: 360px;
}

#portrait-orientation-overlay .overlay-title {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 10px;
}

#portrait-orientation-overlay .overlay-tip {
    font-size: 15px;
    line-height: 1.6;
    opacity: 0.92;
}

/* 1024px keeps tablet/phone portrait covered, while leaving desktop layouts unaffected. */
@media (orientation: portrait) and (max-width: 1024px) {
    #portrait-orientation-overlay {
        display: flex;
    }
}

/* Extra small screens (phones in portrait) */
@media (max-width: 480px) {
    #toolbar {
        gap: 4px;
        padding: 8px 12px;
        max-width: calc(100vw - 20px);
        overflow-x: auto;
        flex-wrap: nowrap;
    }
    
    #toolbar .tool-btn {
        /* Size controlled by JS */
        flex-shrink: 0;
    }
    
    #toolbar .tool-btn svg {
        /* Size controlled by JS */
    }
    
    #toolbar .tool-btn span {
        /* Size controlled by JS */
    }
    
    #config-area {
        min-width: auto;
        width: calc(100vw - 20px);
        max-width: none;
        padding: 10px 12px;
        bottom: 80px;
        left: 10px;
        right: 10px;
        transform: none;
    }
    
    .config-panel.active {
        flex-direction: column;
        gap: 12px;
    }
    
    .color-btn {
        width: 26px;
        height: 26px;
    }
    
    .color-picker-icon-btn {
        width: 30px;
        height: 30px;
    }
    
    #history-controls {
        top: 8px;
        right: 8px;
        gap: 4px;
        flex-wrap: wrap;
        max-width: 180px;
    }
    
    .history-btn {
        width: 36px;
        height: 36px;
        min-height: var(--touch-target-size);
        min-width: var(--touch-target-size);
        padding: 4px;
    }
    
    .zoom-input {
        width: 50px;
        height: 36px;
        font-size: 10px;
        min-height: var(--touch-target-size);
    }
    
    /* Make sliders wider for easier touch */
    .slider,
    .slider-compact {
        width: 100%;
        min-width: 100px;
        height: 8px;
    }
    
    .slider::-webkit-slider-thumb,
    .slider-compact::-webkit-slider-thumb {
        width: 18px;
        height: 18px;
    }
    
    .slider::-moz-range-thumb,
    .slider-compact::-moz-range-thumb {
        width: 18px;
        height: 18px;
    }
    
    .pen-type-grid,
    .shape-type-grid,
    .button-size-options {
        grid-template-columns: repeat(2, 1fr);
        gap: 4px;
    }
    
    .pen-type-btn,
    .shape-type-btn,
    .select-mode-btn {
        padding: 5px 4px;
        font-size: 10px;
    }
}

/* Landscape orientation on phones */
@media (max-height: 500px) and (orientation: landscape) {
    #toolbar {
        bottom: 10px;
        padding: 6px 12px;
        gap: 6px;
    }
    
    #toolbar .tool-btn {
        /* Size controlled by JS */
    }
    
    #toolbar .tool-btn span {
        display: none;
    }
    
    #config-area {
        bottom: 60px;
        max-height: calc(100vh - 80px);
        overflow-y: auto;
    }
    
    #history-controls {
        top: 5px;
        right: 5px;
    }
}

/* Touch device specific improvements */
@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets on touch devices */
    button, 
    .color-btn,
    .pen-type-btn,
    .shape-type-btn,
    .select-mode-btn,
    .feature-btn,
    .config-close-btn,
    .sound-preset-btn,
    [role="button"] {
        min-height: var(--touch-target-size);
        min-width: var(--touch-target-size);
    }
    
    /* Prevent zoom on double-tap for buttons */
    button,
    input,
    select,
    textarea {
        touch-action: manipulation;
    }
    
    /* Increase spacing between interactive elements */
    .color-picker,
    .color-picker-row,
    .color-picker-main {
        gap: 8px;
    }
    
    .button-size-options {
        gap: 8px;
    }
    
    /* Reset hover states for touch devices - these effects don't apply well to touch */
    .tool-btn:hover,
    .history-btn:hover,
    .color-btn:hover,
    .feature-btn:hover {
        transform: none;
        box-shadow: none;
    }
    
    /* Add active state feedback for touch */
    .tool-btn:active,
    .history-btn:active,
    .color-btn:active,
    .feature-btn:active {
        transform: scale(0.95);
        opacity: 0.8;
    }
    
    /* Slider improvements for touch */
    .slider,
    .slider-compact {
        height: 10px;
    }
    
    .slider::-webkit-slider-thumb,
    .slider-compact::-webkit-slider-thumb {
        width: 20px;
        height: 20px;
    }
    
    .slider::-moz-range-thumb,
    .slider-compact::-moz-range-thumb {
        width: 20px;
        height: 20px;
    }
}

/* High DPI / Retina displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    /* Use subpixel rendering for sharper text */
    body {
        -webkit-font-smoothing: subpixel-antialiased;
    }
    
    /* Sharper borders */
    .selection-box,
    .resize-handle {
        border-width: 1px;
    }
}

/* Wide screens (ultra-wide monitors) */
@media (min-width: 2560px) {
    #config-area {
        min-width: 400px;
        max-width: 900px;
    }
    
    #toolbar {
        gap: 16px;
        padding: 16px 28px;
    }
    
    .tool-btn {
        /* Size controlled by JS */
    }
    
    .history-btn {
        width: 56px;
        height: 56px;
    }
    
    .zoom-input {
        width: 80px;
        height: 56px;
        font-size: 16px;
    }
}

/* Safe area insets for notched devices */
@supports (padding: env(safe-area-inset-bottom)) {
    #toolbar {
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }
    
    #history-controls {
        top: calc(20px + env(safe-area-inset-top));
        right: calc(20px + env(safe-area-inset-right));
    }
    
    @media (max-width: 768px) {
        #history-controls {
            top: calc(10px + env(safe-area-inset-top));
            right: calc(10px + env(safe-area-inset-right));
        }
    }
}

/* High-resolution touchscreen optimizations (4K displays, large tablets, interactive whiteboards) */
@media (min-width: 1200px) and (min-resolution: 150dpi),
       (min-width: 1200px) and (-webkit-min-device-pixel-ratio: 1.5),
       (min-width: 1200px) and (min-resolution: 1.5dppx) {
    :root {
        --touch-target-size: 52px;
    }
    
    /* Button size controlled by JS */
    .tool-btn {
        /* Size controlled by JS updateToolbarSize() */
    }
    
    .tool-btn svg {
        /* Size controlled by JS */
    }
    
    .tool-btn span {
        /* Size controlled by JS */
    }
    
    .history-btn {
        width: 52px;
        height: 52px;
    }
    
    /* Larger sliders for easier touch interaction */
    .slider,
    .slider-compact {
        height: 10px;
    }
    
    .slider::-webkit-slider-thumb,
    .slider-compact::-webkit-slider-thumb {
        width: 22px;
        height: 22px;
    }
    
    .slider::-moz-range-thumb,
    .slider-compact::-moz-range-thumb {
        width: 22px;
        height: 22px;
    }
    
    /* Larger color buttons */
    .color-btn {
        width: 40px;
        height: 40px;
    }
    
    .color-picker-icon-btn {
        width: 44px;
        height: 44px;
    }
    
    /* Larger input fields */
    .zoom-input {
        width: 70px;
        height: 52px;
        font-size: 16px;
    }
    
    /* Larger modal buttons */
    .confirm-btn {
        min-height: 48px;
        padding: 14px 28px;
        font-size: 17px;
    }
    
    /* Larger config panel options */
    .pen-type-btn,
    .shape-type-btn,
    .select-mode-btn,
    .eraser-type-btn {
        padding: 10px;
        font-size: 12px;
    }
    
    /* Larger checkboxes and radio buttons */
    input[type="checkbox"],
    input[type="radio"] {
        width: 22px;
        height: 22px;
    }
    
    /* Larger select dropdowns */
    select.format-select {
        min-height: 48px;
        padding: 12px 16px;
        font-size: 16px;
    }
    
    /* Larger feature buttons in More panel */
    .feature-btn {
        padding: 16px;
        min-width: 90px;
    }
    
    .feature-btn svg {
        width: 30px;
        height: 30px;
    }
    
    .feature-btn span {
        font-size: 13px;
    }
}
