:root {
    /* Capture iOS safe area insets as CSS variables */
    --sat: env(safe-area-inset-top);
    --sar: env(safe-area-inset-right);
    --sab: env(safe-area-inset-bottom);
    --sal: env(safe-area-inset-left);
}

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

html, body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow-x: hidden;
    overflow-y: auto;
    background: #ffffff;
    color: #1a1a1a;
    margin: 0;
    padding: 0;
    width: 100%;
    min-height: 100vh;
}

#canvas {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
}

/* Hide UI Button (bottom right corner) */
.hide-ui-top-btn {
    position: fixed;
    bottom: 20px;
    bottom: max(20px, env(safe-area-inset-bottom));
    right: 20px;
    right: max(20px, env(safe-area-inset-right));
    width: 44px;
    height: 44px;
    background: #ffffff;
    border: none;
    border-radius: 50%;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: all 0.2s ease;
    opacity: 1;
}

.hide-ui-top-btn.hidden {
    opacity: 0;
    pointer-events: auto; /* Still allow clicks even when hidden */
}

.hide-ui-top-btn svg {
    width: 20px;
    height: 20px;
    stroke: #1a1a1a !important;
}

.hide-ui-top-btn i {
    width: 20px;
    height: 20px;
    color: #1a1a1a !important;
}

.hide-ui-top-btn:hover {
    background: #f5f5f5;
    transform: scale(1.05);
}

.hide-ui-top-btn:active {
    transform: scale(0.95);
}

/* Hide UI Modal */
.hide-ui-modal {
    position: fixed;
    bottom: 80px;
    bottom: max(80px, calc(env(safe-area-inset-bottom) + 60px));
    right: 20px;
    right: max(20px, env(safe-area-inset-right));
    background: #1a1a1a;
    color: #ffffff;
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
    font-size: 14px;
    z-index: 999;
    opacity: 0;
    transform: translateY(10px);
    transition: all 0.3s ease;
    pointer-events: none;
    max-width: 200px;
    text-align: center;
}

.hide-ui-modal.visible {
    opacity: 1;
    transform: translateY(0);
}

/* iOS App Link (web only) */
.ios-app-link {
    position: fixed;
    bottom: 20px;
    bottom: max(20px, env(safe-area-inset-bottom));
    left: 20px;
    left: max(20px, env(safe-area-inset-left));
    z-index: 998;
    transition: opacity 0.2s ease;
    display: inline-block;
}

.ios-app-link:hover {
    opacity: 0.8;
}

.ios-app-link:active {
    opacity: 0.6;
}

/* Floating Toolbar - Modern Minimalist */
.toolbar {
    position: absolute;
    top: max(20px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    background: #ffffff;
    border-radius: 10px;
    padding: 6px;
    display: flex;
    gap: 2px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 0 0 1px rgba(0, 0, 0, 0.06);
    z-index: 1000;
    align-items: center;
    max-width: calc(100vw - max(40px, env(safe-area-inset-left) + 20px) - max(40px, env(safe-area-inset-right) + 20px));
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}

/* Hide scrollbar but keep functionality */
.toolbar::-webkit-scrollbar {
    display: none;
}

.toolbar {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

.toolbar-section {
    display: flex;
    gap: 2px;
    align-items: center;
    flex-shrink: 0; /* Prevent sections from shrinking in scrollable toolbar */
}

.toolbar-divider {
    width: 1px;
    height: 24px;
    background: rgba(0, 0, 0, 0.1);
    margin: 0 6px;
}

.toolbar-divider-small {
    width: 1px;
    height: 20px;
    background: rgba(0, 0, 0, 0.08);
    margin: 0 4px;
}

/* Tool Buttons */
.tool-btn {
    min-width: 36px;
    height: 36px;
    padding: 0 8px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: #52525b;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 2px;
    position: relative;
    flex-shrink: 0; /* Prevent buttons from shrinking in scrollable toolbar */
}

.tool-btn i {
    width: 18px;
    height: 18px;
    stroke-width: 2;
}

.tool-btn .chevron {
    width: 12px;
    height: 12px;
    opacity: 0.5;
}

.tool-btn:hover {
    background: rgba(0, 0, 0, 0.06);
    color: #18181b;
}

.tool-btn.active {
    background: #18181b;
    color: #ffffff;
}

/* Icon Buttons */
.icon-btn {
    min-width: 32px;
    height: 32px;
    padding: 0 6px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: #52525b;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 2px;
    flex-shrink: 0; /* Prevent buttons from shrinking in scrollable toolbar */
}

.icon-btn i {
    width: 16px;
    height: 16px;
    stroke-width: 2;
}

.icon-btn .chevron {
    width: 10px;
    height: 10px;
    opacity: 0.5;
}

.icon-btn:hover {
    background: rgba(0, 0, 0, 0.06);
    color: #18181b;
}

.icon-btn.active {
    background: rgba(0, 0, 0, 0.08);
    color: #18181b;
}

/* Chevron-only button */
.icon-btn.chevron-only {
    min-width: 16px;
    padding: 0 2px;
}

.icon-btn.chevron-only i {
    width: 12px;
    height: 12px;
    opacity: 0.6;
}

/* Toggle buttons */
.toggle-btn.active {
    background: #10b981;
    color: #ffffff;
}

.toggle-btn.active:hover {
    background: #059669;
}

/* Font Controls */
select {
    height: 32px;
    padding: 0 8px;
    background: transparent;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    color: #18181b;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
}

select:hover {
    background: rgba(0, 0, 0, 0.04);
    border-color: rgba(0, 0, 0, 0.15);
}

select:focus {
    outline: none;
    border-color: rgba(0, 0, 0, 0.25);
    background: rgba(0, 0, 0, 0.02);
}

#fontSelect {
    min-width: 100px;
}

input[type="number"] {
    width: 50px;
    height: 32px;
    padding: 0 8px;
    background: transparent;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    color: #18181b;
    font-size: 13px;
    text-align: center;
    transition: all 0.15s;
}

input[type="number"]:hover {
    border-color: rgba(0, 0, 0, 0.15);
    background: rgba(0, 0, 0, 0.04);
}

input[type="number"]:focus {
    outline: none;
    border-color: rgba(0, 0, 0, 0.25);
    background: rgba(0, 0, 0, 0.02);
}

/* Text Settings Section */
.text-settings {
    display: none;
}

.text-settings.active {
    display: flex;
}

/* Popovers */
.popover {
    position: fixed;
    z-index: 1001;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.08);
    padding: 12px;
    min-width: 240px;
}

.popover-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Color Picker */
.color-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
}

.color-swatch {
    width: 32px;
    height: 32px;
    border: 2px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
    padding: 0;
}

.color-swatch:hover {
    transform: scale(1.1);
    border-color: rgba(0, 0, 0, 0.2);
}

.color-swatch.active {
    border-color: #18181b;
    border-width: 3px;
}

.rainbow-swatch {
    position: relative;
}

.rainbow-swatch::after {
    content: '✨';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 14px;
    pointer-events: none;
}

/* Emoji Picker */
.emoji-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 4px;
    max-height: 300px;
    overflow-y: auto;
}

.emoji-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
    padding: 0;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.emoji-btn:hover {
    background: rgba(0, 0, 0, 0.06);
    transform: scale(1.1);
}

.emoji-btn.active {
    background: rgba(0, 0, 0, 0.1);
}

.popover-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: #18181b;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
    width: 100%;
    text-align: left;
}

.popover-btn i {
    width: 16px;
    height: 16px;
    stroke-width: 2;
    color: #52525b;
}

.popover-btn:hover {
    background: rgba(0, 0, 0, 0.06);
}

.popover-btn span {
    font-weight: 500;
}

.settings-row {
    margin-bottom: 12px;
}

.settings-row:last-child {
    margin-bottom: 0;
}

.settings-row label {
    display: block;
    font-size: 11px;
    color: #52525b;
    margin-bottom: 6px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.settings-row label span {
    float: right;
    font-weight: 600;
    color: #18181b;
}

.settings-row input[type="range"] {
    width: 100%;
    height: 4px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 2px;
    outline: none;
    -webkit-appearance: none;
}

.settings-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    background: #18181b;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.15s;
}

.settings-row input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

.settings-row input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    background: #18181b;
    border-radius: 50%;
    cursor: pointer;
    border: none;
    transition: all 0.15s;
}

.settings-row input[type="range"]::-moz-range-thumb:hover {
    transform: scale(1.2);
}

/* Show UI Button */
.show-ui-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 10px 20px;
    background: #ffffff;
    border: none;
    border-radius: 8px;
    color: #18181b;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(0, 0, 0, 0.05);
    z-index: 1001;
    transition: all 0.3s ease;
    opacity: 0;
    pointer-events: none;
}

.show-ui-btn:hover {
    background: #f8f8f8;
    transform: translateY(-1px);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12), 0 0 0 1px rgba(0, 0, 0, 0.05);
    opacity: 1;
}

.show-ui-btn::before {
    content: '';
    position: absolute;
    top: -20px;
    right: -20px;
    bottom: -20px;
    left: -20px;
    pointer-events: auto;
}

.show-ui-btn:hover,
.show-ui-btn:hover::before {
    opacity: 1;
    pointer-events: auto;
}

/* Settings Panel */
.settings-panel {
    position: fixed;
    top: 80px;
    right: 20px;
    width: 320px;
    max-height: calc(100vh - 100px);
    background: #ffffff;
    border-radius: 10px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 0 0 1px rgba(0, 0, 0, 0.06);
    z-index: 999;
    display: none;
    flex-direction: column;
}

.settings-panel.visible {
    display: flex;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.settings-header h2 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
    color: #18181b;
}

.close-btn {
    background: none;
    border: none;
    font-size: 24px;
    color: #a1a1aa;
    cursor: pointer;
    padding: 0;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.15s;
}

.close-btn:hover {
    background: rgba(0, 0, 0, 0.06);
    color: #18181b;
}

.settings-content {
    overflow-y: auto;
    padding: 0;
}

.settings-section {
    padding: 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

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

.settings-section h3 {
    font-size: 13px;
    font-weight: 600;
    margin: 0 0 16px 0;
    color: #18181b;
    text-transform: none;
}

.settings-section .expandable-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    user-select: none;
    margin-bottom: 0;
    transition: color 0.2s ease;
}

.settings-section .expandable-header:hover {
    color: #000000;
}

.settings-section .expandable-header .chevron-icon {
    width: 16px;
    height: 16px;
    transition: transform 0.3s ease;
}

.settings-section .expandable-header.collapsed .chevron-icon {
    transform: rotate(-90deg);
}

.settings-section .expandable-content {
    max-height: 500px;
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.3s ease, margin-top 0.3s ease;
    opacity: 1;
    margin-top: 16px;
}

.settings-section .expandable-content.collapsed {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
}

.settings-section .control-group {
    margin-bottom: 16px;
}

.settings-section .control-group:last-child {
    margin-bottom: 0;
}

.settings-section label {
    display: block;
    font-size: 12px;
    color: #52525b;
    margin-bottom: 6px;
    font-weight: 500;
}

.settings-section input[type="text"] {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    font-size: 13px;
    color: #18181b;
    background: transparent;
    transition: all 0.15s;
}

.settings-section input[type="text"]:hover {
    border-color: rgba(0, 0, 0, 0.15);
    background: rgba(0, 0, 0, 0.02);
}

.settings-section input[type="text"]:focus {
    outline: none;
    border-color: rgba(0, 0, 0, 0.25);
    background: rgba(0, 0, 0, 0.02);
}

.settings-section textarea {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    font-size: 13px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    resize: vertical;
    min-height: 80px;
}

.settings-section textarea:hover {
    border-color: rgba(0, 0, 0, 0.15);
    background: rgba(0, 0, 0, 0.02);
}

.settings-section textarea:focus {
    outline: none;
    border-color: rgba(0, 0, 0, 0.25);
    background: rgba(0, 0, 0, 0.02);
}

.settings-section input[type="range"] {
    width: 100%;
    height: 4px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 2px;
    outline: none;
    -webkit-appearance: none;
}

.settings-section input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    background: #18181b;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.15s;
}

.settings-section input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

.settings-section input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    background: #18181b;
    border-radius: 50%;
    cursor: pointer;
    border: none;
    transition: all 0.15s;
}

.settings-section input[type="range"]::-moz-range-thumb:hover {
    transform: scale(1.2);
}

.settings-section input[type="checkbox"] {
    width: 16px;
    height: 16px;
    margin-right: 8px;
    cursor: pointer;
    vertical-align: middle;
}

.settings-section .slot-nav-btn {
    padding: 6px 12px;
    background: #ffffff;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    font-size: 12px;
    color: #18181b;
    cursor: pointer;
    transition: all 0.2s ease;
    font-weight: 500;
}

.settings-section .slot-nav-btn:hover {
    background: rgba(0, 0, 0, 0.02);
    border-color: rgba(0, 0, 0, 0.15);
}

.settings-section .slot-nav-btn:active {
    background: rgba(0, 0, 0, 0.05);
    transform: scale(0.98);
}

.settings-section label span {
    font-weight: 600;
    color: #18181b;
}
