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

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: fixed;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    touch-action: manipulation; /* Allow all touch gestures, prevent double-tap zoom */
}

.app-container {
    width: 100vw;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.screen {
    flex: 1;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding-top: calc(15px + env(safe-area-inset-top, 0)); /* Safe area for notch/Dynamic Island */
    padding-left: max(20px, env(safe-area-inset-left, 0));
    padding-right: max(20px, env(safe-area-inset-right, 0));
    padding-bottom: calc(100px + env(safe-area-inset-bottom, 0)); /* Tab bar + safe area */
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: auto; /* Allow all touch interactions within screens */
    min-height: 0; /* Important for flex scrolling */
}

.screen.active {
    display: flex;
}

/* Drawing screen needs different padding - safe areas applied to child elements */
#drawing-screen.screen {
    padding: 0;
    padding-top: env(safe-area-inset-top, 0); /* Safe area for notch */
    padding-bottom: env(safe-area-inset-bottom, 0); /* Safe area for home indicator */
}

.screen-title {
    font-size: clamp(24px, 5vw, 36px);
    font-weight: bold;
    margin-bottom: 20px;
    flex-shrink: 0;
}

/* Tab Bar */
.tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    background: #f8f8f8;
    border-top: 1px solid #ccc;
    /* Height includes safe area for home indicator */
    height: auto;
    min-height: 70px;
    flex-shrink: 0;
    /* Just use safe area padding, no extra */
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
    z-index: 1000;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
}

.tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s;
    user-select: none;
    padding: 8px 4px;
}

.tab:active {
    transform: scale(0.95);
}

.tab.active {
    color: #007AFF;
}

.tab-icon {
    font-size: clamp(20px, 4vw, 28px);
    margin-bottom: 4px;
}

.tab-label {
    font-size: clamp(10px, 2vw, 12px);
}

/* Buttons Screen */
#buttons-screen {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.counter-display {
    font-size: clamp(50px, 12vw, 80px);
    font-weight: bold;
    margin-bottom: 20px;
    flex-shrink: 0;
}

.buttons-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    justify-content: center;
    max-width: min(600px, 90vw);
    margin-bottom: 20px;
    flex-shrink: 0;
}

.fidget-button {
    border: none;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    color: white;
    font-weight: bold;
    display: flex;
    flex-direction: column;
    gap: 5px;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    position: relative;
    overflow: hidden;
}

.fidget-button svg {
    width: 32px;
    height: 32px;
    stroke-width: 2;
}

.fidget-button.small svg {
    width: 24px;
    height: 24px;
}

.fidget-button.large svg {
    width: 40px;
    height: 40px;
}

.fidget-button:active {
    transform: scale(0.9);
}

.fidget-button::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.5);
    transform: translate(-50%, -50%);
    transition: width 0.6s, height 0.6s;
}

.fidget-button.ripple::before {
    width: 300px;
    height: 300px;
}

@keyframes bounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

@keyframes wiggle {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-10deg); }
    75% { transform: rotate(10deg); }
}

@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.15); opacity: 0.8; }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-10px); }
    75% { transform: translateX(10px); }
}

@keyframes jello {
    0%, 100% { transform: skewX(0deg) skewY(0deg); }
    25% { transform: skewX(-12.5deg) skewY(-12.5deg); }
    50% { transform: skewX(6.25deg) skewY(6.25deg); }
    75% { transform: skewX(-3.125deg) skewY(-3.125deg); }
}

@keyframes glow {
    0%, 100% { box-shadow: 0 4px 12px rgba(0,0,0,0.3); }
    50% { box-shadow: 0 0 30px currentColor, 0 4px 12px rgba(0,0,0,0.3); }
}

@keyframes rainbow {
    0% { filter: hue-rotate(0deg); }
    100% { filter: hue-rotate(360deg); }
}

.fidget-button.animate-bounce {
    animation: bounce 0.5s ease;
}

.fidget-button.animate-spin {
    animation: spin 0.5s ease;
}

.fidget-button.animate-wiggle {
    animation: wiggle 0.5s ease;
}

.fidget-button.animate-pulse {
    animation: pulse 0.5s ease;
}

.fidget-button.animate-shake {
    animation: shake 0.5s ease;
}

.fidget-button.animate-jello {
    animation: jello 0.6s ease;
}

.fidget-button.animate-glow {
    animation: glow 0.6s ease;
}

.fidget-button.animate-rainbow {
    animation: rainbow 1s linear;
}

.fidget-button.circle {
    border-radius: 50%;
}

.fidget-button.square {
    border-radius: 10px;
}

.fidget-button.rounded {
    border-radius: 30px;
}

.fidget-button.small {
    width: 60px;
    height: 60px;
    font-size: 10px;
}

.fidget-button.medium {
    width: 90px;
    height: 90px;
    font-size: 12px;
}

.fidget-button.large {
    width: 120px;
    height: 120px;
    font-size: 14px;
}

.fidget-button.wide {
    width: 150px;
    height: 70px;
    font-size: 12px;
}

.fidget-button.tall {
    width: 70px;
    height: 150px;
    font-size: 12px;
}

.reset-btn {
    margin-top: 15px;
    background: rgba(255,255,255,0.3);
    padding: 12px 30px;
    border: none;
    border-radius: 20px;
    color: white;
    font-size: clamp(14px, 3vw, 16px);
    cursor: pointer;
    flex-shrink: 0;
}

/* Spinner Screen */
#spinner-screen {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.spinner-selector {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 90vw;
    flex-shrink: 0;
}

.spinner-type-btn {
    padding: 10px 20px;
    background: rgba(255,255,255,0.2);
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 20px;
    color: white;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
    font-size: clamp(12px, 2.5vw, 14px);
    white-space: nowrap;
}

.spinner-type-btn:hover {
    background: rgba(255,255,255,0.3);
    transform: translateY(-2px);
}

.spinner-type-btn.active {
    background: white;
    color: #f5576c;
    border-color: white;
}

.spinner-container {
    position: relative;
    width: min(250px, 60vw);
    height: min(250px, 60vw);
    flex-shrink: 0;
    margin: 20px 0;
}

.spinner {
    width: 100%;
    height: 100%;
    position: relative;
    transition: transform 0.05s linear;
    cursor: grab;
}

.spinner:active {
    cursor: grabbing;
}

.spinner-design {
    width: 100%;
    height: 100%;
    position: relative;
}

/* Classic Spinner */
.spinner-classic {
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea, #764ba2, #f5576c);
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}

.spinner-classic .arm {
    position: absolute;
    width: 100px;
    height: 20px;
    background: white;
    border-radius: 10px;
    top: 50%;
    left: 50%;
    transform-origin: 0 50%;
}

.spinner-classic .arm:nth-child(1) { transform: translate(0, -50%) rotate(0deg); }
.spinner-classic .arm:nth-child(2) { transform: translate(0, -50%) rotate(120deg); }
.spinner-classic .arm:nth-child(3) { transform: translate(0, -50%) rotate(240deg); }

.spinner-classic .center {
    position: absolute;
    width: 60px;
    height: 60px;
    background: white;
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

/* Galaxy Spinner */
.spinner-galaxy {
    border-radius: 50%;
    background: radial-gradient(circle, #4a0080 0%, #1a0033 50%, #000 100%);
    box-shadow: 0 0 40px rgba(138,43,226,0.6), 0 10px 30px rgba(0,0,0,0.3);
}

.spinner-galaxy .star {
    position: absolute;
    width: 6px;
    height: 6px;
    background: white;
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(255,255,255,0.8);
}

.spinner-galaxy .center-star {
    position: absolute;
    width: 40px;
    height: 40px;
    background: radial-gradient(circle, #fff, #ffeb3b);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 30px rgba(255,235,59,0.8);
    z-index: 10;
}

.spinner-galaxy .orbit {
    position: absolute;
    border: 2px solid rgba(138,43,226,0.4);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.spinner-galaxy .planet {
    position: absolute;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, #ffeb3b, #ff9800);
    box-shadow: 0 0 15px rgba(255,152,0,0.6);
}

/* Ninja Star Spinner */
.spinner-ninja {
    background: transparent;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.spinner-ninja .blade {
    position: absolute;
    width: 0;
    height: 0;
    border-style: solid;
}

.spinner-ninja .blade:nth-child(1) {
    border-width: 0 20px 80px 20px;
    border-color: transparent transparent #7f8c8d transparent;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
}

.spinner-ninja .blade:nth-child(2) {
    border-width: 20px 0 20px 80px;
    border-color: transparent transparent transparent #7f8c8d;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}

.spinner-ninja .blade:nth-child(3) {
    border-width: 80px 20px 0 20px;
    border-color: #7f8c8d transparent transparent transparent;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}

.spinner-ninja .blade:nth-child(4) {
    border-width: 20px 80px 20px 0;
    border-color: transparent #7f8c8d transparent transparent;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
}

.spinner-ninja .center-hole {
    position: absolute;
    width: 50px;
    height: 50px;
    background: #2c3e50;
    border: 4px solid #95a5a6;
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: inset 0 2px 8px rgba(0,0,0,0.5), 0 4px 12px rgba(0,0,0,0.3);
    z-index: 10;
}

/* Flower Spinner */
.spinner-flower {
    border-radius: 50%;
    background: transparent;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.spinner-flower .petal {
    position: absolute;
    width: 60px;
    height: 100px;
    background: linear-gradient(to bottom, #ff69b4, #ff1493);
    border-radius: 50% 50% 50% 0;
    top: 50%;
    left: 50%;
    transform-origin: 50% 100%;
    box-shadow: 0 4px 12px rgba(255,20,147,0.3);
}

.spinner-flower .petal:nth-child(1) { transform: translate(-50%, -100%) rotate(0deg); }
.spinner-flower .petal:nth-child(2) { transform: translate(-50%, -100%) rotate(45deg); }
.spinner-flower .petal:nth-child(3) { transform: translate(-50%, -100%) rotate(90deg); }
.spinner-flower .petal:nth-child(4) { transform: translate(-50%, -100%) rotate(135deg); }
.spinner-flower .petal:nth-child(5) { transform: translate(-50%, -100%) rotate(180deg); }
.spinner-flower .petal:nth-child(6) { transform: translate(-50%, -100%) rotate(225deg); }
.spinner-flower .petal:nth-child(7) { transform: translate(-50%, -100%) rotate(270deg); }
.spinner-flower .petal:nth-child(8) { transform: translate(-50%, -100%) rotate(315deg); }

.spinner-flower .center-circle {
    position: absolute;
    width: 80px;
    height: 80px;
    background: radial-gradient(circle, #ffd700, #ffed4e);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 12px rgba(255,215,0,0.5);
    z-index: 10;
}

/* Spectrum Spinner */
.spinner-spectrum {
    border-radius: 50%;
    background: conic-gradient(
        from 0deg,
        #ff0000 0deg,
        #ff7f00 51.43deg,
        #ffff00 102.86deg,
        #00ff00 154.29deg,
        #0000ff 205.71deg,
        #4b0082 257.14deg,
        #9400d3 308.57deg,
        #ff0000 360deg
    );
    box-shadow: 0 10px 40px rgba(255,255,255,0.3);
    position: relative;
}

.spinner-spectrum .prism-ring {
    position: absolute;
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 3px solid rgba(255,255,255,0.5);
}

.spinner-spectrum .prism-ring:nth-child(1) { width: 60px; height: 60px; }
.spinner-spectrum .prism-ring:nth-child(2) { width: 100px; height: 100px; }
.spinner-spectrum .prism-ring:nth-child(3) { width: 140px; height: 140px; }
.spinner-spectrum .prism-ring:nth-child(4) { width: 180px; height: 180px; }

.spinner-spectrum .rainbow-center {
    position: absolute;
    width: 70px;
    height: 70px;
    background: radial-gradient(circle, #ffffff, #f0f0f0);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
    z-index: 10;
}

.speed-display {
    margin-top: 20px;
    font-size: 24px;
    font-weight: bold;
    color: white;
}

.spinner-info {
    margin-top: 10px;
    font-size: 16px;
    color: rgba(255,255,255,0.8);
}

/* Sliders Screen */
#sliders-screen {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.sliders-container {
    width: 100%;
    max-width: min(400px, 90vw);
    flex-shrink: 0;
    touch-action: auto; /* Ensure touch events work on sliders */
}

.slider-row {
    margin-bottom: 30px;
}

.slider-label {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    font-size: 18px;
    font-weight: bold;
    color: white;
}

.slider {
    width: 100%;
    height: 40px;
    -webkit-appearance: none;
    appearance: none;
    background: rgba(255,255,255,0.3);
    border-radius: 20px;
    outline: none;
    touch-action: auto; /* Allow slider interaction on touch devices */
    pointer-events: auto; /* Ensure touch events are captured */
    position: relative; /* Ensure proper stacking context */
    z-index: 1; /* Bring above any background elements */
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 30px;
    height: 30px;
    background: white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    pointer-events: auto;
    touch-action: auto;
}

.slider::-moz-range-thumb {
    width: 30px;
    height: 30px;
    background: white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    border: none;
}

.color-preview {
    width: 200px;
    height: 100px;
    border-radius: 20px;
    margin-top: 30px;
    border: 3px solid white;
    box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}

/* Pop It Screen */
#popit-screen {
    background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}

.pop-count {
    font-size: 28px;
    font-weight: bold;
    color: white;
    margin-bottom: 20px;
}

.pop-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(45px, 50px));
    gap: 10px;
    max-width: min(400px, 90vw);
    justify-content: center;
    flex-shrink: 0;
}

.pop-bubble {
    width: 100%;
    aspect-ratio: 1;
    min-width: 45px;
    max-width: 50px;
    border-radius: 50%;
    background: rgba(255,255,255,0.3);
    border: 3px solid rgba(255,255,255,0.6);
    cursor: pointer;
    transition: all 0.2s;
}

.pop-bubble.popped {
    background: #FF6B6B;
    transform: scale(0.85);
}

.pop-reset {
    margin-top: 30px;
    padding: 15px 40px;
    background: white;
    border: none;
    border-radius: 25px;
    font-size: 18px;
    font-weight: bold;
    color: #fa709a;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

/* Drawing Screen */
#drawing-screen {
    background: #f5f5f5;
    padding: 0;
    overflow: hidden; /* Prevent scroll on drawing screen */
}

.color-picker {
    display: flex;
    gap: clamp(10px, 2vw, 15px);
    padding: 15px;
    padding-left: max(15px, env(safe-area-inset-left, 0));
    padding-right: max(15px, env(safe-area-inset-right, 0));
    background: white;
    border-bottom: 1px solid #ddd;
    align-items: center;
    flex-wrap: wrap;
    flex-shrink: 0;
    position: relative;
    z-index: 10;
}

.color-option {
    width: clamp(35px, 8vw, 40px);
    height: clamp(35px, 8vw, 40px);
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid transparent;
    transition: all 0.2s;
    flex-shrink: 0;
}

.color-option.selected {
    border-color: #007AFF;
    transform: scale(1.1);
}

.clear-btn {
    margin-left: auto;
    padding: 8px 20px;
    background: #FF3B30;
    color: white;
    border: none;
    border-radius: 15px;
    font-size: 16px;
    cursor: pointer;
}

.canvas-container {
    flex: 1;
    width: 100%;
    background: white;
    position: relative;
    overflow: hidden;
    min-height: 0;
}

#drawCanvas {
    width: 100%;
    height: 100%;
    touch-action: none;
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: crosshair;
}
