* {
    box-sizing: border-box;
}

:root {
    --bg-color: #0d0e12;
    --panel-bg: #16181d;
    --text-primary: #ffffff;
    --text-secondary: #c7c7c7;

    --accent-color: #d24470;
    /* Pink for selection/active */
    --accent-hover: #b8355e;
    --border-color: #272b36;
    --ball-color: #1c2234;
    /* Red ball */
    --ghost-color: rgba(23, 51, 97, 0.25);
    /* Blue ghosts */
    --timeline-bg: #000000;
}

body {
    margin: 0;
    padding: 0;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    min-height: 100dvh;
    /* Dynamic height to avoid clipping by browser bars */
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    /* Allow scroll if content is too tall (mobile/tablet) */
}

.app-container {
    display: flex;
    flex-direction: column;
    flex: 1;
    /* Grow to fill body min-height */
    max-width: 1400px;
    margin: 0 auto;
    width: 100%;
}

/* Header */
header {
    height: 100%;
    /* Reduced height for cleaner look */
    background-color: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Space them out */
    padding: 0.5rem 2rem;
    flex-shrink: 0;
}

.header-content {
    text-align: left;
    position: relative;
    /* Context for absolute toggle */
    width: 100%;
}

.header-controls {
    display: flex;
    gap: 10px;
    margin-top: 10px;
    align-items: center;
}

.header-content h1 {
    margin: 0;
    font-weight: 700;
}

.header-content p {
    margin: 0.25rem 0 0;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.global-controls {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

/* Name Input Styling */
.name-input {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    /* Pill shape */
    padding: 0.4rem 0.8rem;
    margin: 0;
    /* Align left */
    color: var(--text-primary);
    font-size: 0.9rem;
    font-family: inherit;
    outline: none;
    transition: all 0.2s;
    width: 200px;
}

.name-input:focus {
    border-color: var(--accent-color);
    background: rgba(255, 255, 255, 0.1);
}

.name-input:read-only {
    /* Look normal but uneditable */
    background: transparent;
    border-color: transparent;
    cursor: default;
    padding-left: 0;
    /* Remove padding to look like text */
    font-weight: 600;
}

.name-input:read-only:focus {
    border-color: transparent;
    outline: none;
    background: transparent;
}

/* Main Area */
main {
    flex: 1;
    display: flex;
    overflow: hidden;
    padding: 2rem;
}

.canvas-layout {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 3rem;
    width: 100%;
    height: 100%;
}

.canvas-section {
    flex: 0 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    background: transparent;
    position: relative;
    overflow: hidden;
    /* Allow shrinking but keep aspect ratio logic via child */
    max-width: 100%;
}

canvas {
    /* Flexible sizing */
    width: auto;
    height: auto;
    max-width: 100%;
    /* Max height calculation: 100vh - header(100) - footer(140) - padding(64) - safety buffer(20) ~ 324px */
    max-height: calc(100dvh - 330px);
    aspect-ratio: 4/3;

    box-shadow: 0 0 50px rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-color);
    background-color: #da4a83;
}

#riveTitleCanvas {
    background-color: transparent !important;
    border: none !important;
    box-shadow: none !important;
    aspect-ratio: auto !important;
    max-height: none !important;
}

.frame-indicator {
    position: absolute;
    bottom: 20px;
    /* Moved to bottom-left */
    left: 20px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
    background: transparent;
    padding: 0;
    z-index: 10;
    pointer-events: none;
    /* Let clicks pass through if needed */
}

/* Loop Button (Top Right) */
#btnLoop {
    top: 20px;
    right: 20px;
    left: auto;
}

/* Chart Button (Top Left) */
#btnChart {
    top: 20px;
    left: 20px;
    right: auto;
}

/* FPS Button (Bottom Right) */
#btnFps {
    bottom: 20px;
    right: 20px;
    top: auto;
    left: auto;
}

/* Onion Skin Button (Bottom Left) */
#btnOnion {
    bottom: 20px;
    left: 20px;
    top: auto;
    right: auto;
}



.loop-indicator {
    position: absolute;
    /* top/left/right set by ID above */
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-secondary);
    background: transparent;
    border: none;
    cursor: pointer;
    z-index: 10;
    transition: all 0.2s;
    padding: 0.2rem 0.6rem;
    border-radius: 4px;
}

.loop-indicator.active {
    background-color: var(--accent-color);
    color: white;
}

/* Vertical Sliders - Ruler Style */
.slider-vertical-container {
    display: flex;
    justify-content: center;
    /* Height will be set by JS setupCanvasResolution */
    /* Height will be set by JS setupCanvasResolution */
    width: 60px;
    position: relative;
    flex-shrink: 0;
    margin: 0;
    padding: 0;
}

.slider-vertical-container label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-secondary);
    white-space: nowrap;
    writing-mode: horizontal-tb;
    transform: none;

    position: absolute;
    bottom: -40px;
    left: 50%;
    transform: translateX(-50%);
}

.slider-vertical-container .value-readout {
    /* Re-add readout styling if it was missing or needs absolute positioning */
    font-family: monospace;
    font-size: 0.85rem;
    color: var(--accent-color);
    min-width: 50px;
    text-align: center;

    position: absolute;
    bottom: -20px;
    left: 50%;
    transform: translateX(-50%);
}

.slider-vertical-container input[type=range] {
    writing-mode: vertical-lr;
    /* Keeps slider vertical in modern browsers */
    direction: rtl;
    width: 40px;
    /* Wider for ruler ticks */
    /* Height set by JS */
    /* Force exact match */
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    margin: 0;
    padding: 0;
    cursor: pointer;
    flex-shrink: 0;
    display: block;
}

/* Invert Y-Position slider so Top = 0 (Min) and Bottom = 600 (Max) */
#yPosRange {
    transform: rotate(180deg);
}

/*
 * NOTE: Styling vertical ranges cross-browser is tricky.
 * Webkit vertical slider styling is limited.
 * We often need to treat it as a box or rely on defaults.
 * However, we can try to style the track if appearance is none.
 * 
 * Standard approach for custom vertical slider: 
 * transform: rotate(-90deg) is often more reliable for full styling,
 * but 'writing-mode: vertical-lr' is what we have.
 * 
 * Let's try to make it look clean with what we have.
 */

/* Track styling - Webkit */
.slider-vertical-container input[type=range]::-webkit-slider-runnable-track {
    width: 40px;
    border: none;
    border-radius: 0;

    /* Simple Dashes Design */
    background-color: transparent;
    background-image: repeating-linear-gradient(to bottom, var(--border-color) 0, var(--border-color) 1px, transparent 1px, transparent 20px);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

/* Highlight for Squash Slider: Blue dash at 1.0x (Value 1.0 in 0.1-2.0 range)
   Range span: 1.9 (2.0 - 0.1). 
   1.0 is 0.9 from min (bottom), 1.0 from max (top).
   Percentage from top = 1.0 / 1.9 = ~52.63%
*/
#squashRange::-webkit-slider-runnable-track {
    background-image:
        linear-gradient(to bottom, transparent calc(52.63% - 1px), var(--accent-color) calc(52.63% - 1px), var(--accent-color) calc(52.63% + 1px), transparent calc(52.63% + 1px)),
        repeating-linear-gradient(to bottom, var(--border-color) 0, var(--border-color) 1px, transparent 1px, transparent 20px);
}

/* Thumb styling - Webkit */
.slider-vertical-container input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 50px;
    /* Spans full width + overlap */
    height: 4px;
    /* Thin line indicator */
    border-radius: 2px;
    background: var(--accent-color);
    /* Bright blue line */
    border: none;
    box-shadow: 0 0 10px rgba(210, 68, 112, 0.8);
    margin-left: -5px;
    /* Center on 40px track: (40 - 50) / 2 = -5 */
    cursor: grab;
    transition: transform 0.1s;
}

.slider-vertical-container input[type=range]:active::-webkit-slider-thumb {
    transform: scaleY(2);
    cursor: grabbing;
}

/* Firefox styling */
.slider-vertical-container input[type=range]::-moz-range-track {
    width: 40px;
    border: none;
    border-radius: 0;
    background-color: transparent;
    background-image: repeating-linear-gradient(to bottom, var(--border-color) 0, var(--border-color) 1px, transparent 1px, transparent 20px);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

.slider-vertical-container input[type=range]::-moz-range-thumb {
    width: 50px;
    height: 4px;
    border-radius: 2px;
    background: var(--accent-color);
    border: none;
    box-shadow: 0 0 10px rgba(210, 68, 112, 0.8);
    cursor: grab;
}



/* Timeline / Footer */
footer {
    height: 140px;
    background-color: var(--panel-bg);
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Spread items */
    padding: 0 1rem;
    gap: 1rem;
}

.desktop-credits {
    background-color: #000000;
    /* Dark background */
    width: 100%;
    text-align: center;
    font-size: 0.70rem;
    opacity: 0.5;
    color: var(--text-secondary);
    padding: 8px 0;
    flex-shrink: 0;
    /* Prevent shrinking if flexbox */
}

@media (max-width: 768px) {
    .desktop-credits {
        display: none;
    }
}

/* Timeline Wrapper (Layout + Static Overlay) */
/* Timeline Wrapper (Layout + Static Overlay) */
/* Timeline Wrapper (Layout + Static Overlay) */
.timeline-wrapper {
    flex: 1;
    /* Allow growing in center */
    margin: 0 1rem;
    /* Spacing from buttons */
    position: relative;
    /* Anchor for static highlight */
    overflow: hidden;
    /* Ensure content stays inside */
    order: 2;
    /* Desktop: Center */
}

/* ... existing timeline-container styles ... */

/* Icon Button (Play) */
.icon-btn {
    background: transparent;
    color: var(--accent-color);
    border: 2px solid var(--accent-color);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
}

/* Play Button specific for Desktop positioning */
#btnPlayPause {
    order: 1;
    /* Desktop: Left */
}

.footer-controls {
    order: 3;
    /* Desktop: Right */
}

/* Wrapper handling: On Desktop, unwrap to let children flow in footer */
.mobile-controls-row {
    display: contents;
}

/* Timeline Container (Scrollable Area) */
.timeline-container {
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    scroll-behavior: auto;
    /* Hide scrollbar */
    scrollbar-width: none;
    -ms-overflow-style: none;
    cursor: grab;
    /* Indicate draggable */
    display: flex;
    /* Ensure strip is handled correctly */
}

.timeline-container:active {
    cursor: grabbing;
}

.timeline-container::-webkit-scrollbar {
    display: none;
}

/* Static Center Highlight Overlay - Attached to Wrapper */
.timeline-wrapper::after {
    content: '';
    position: absolute;
    left: 50%;
    top: calc(50% + 48px);
    /* 40px (half frame) + gap */
    transform: translate(-50%, -50%);
    width: 60px;
    /* Match frame width */
    height: 4px;
    /* Line thickness */
    background: var(--accent-color);
    border-radius: 2px;
    box-shadow: 0 0 10px rgba(210, 68, 112, 0.8);
    pointer-events: none;
    z-index: 10;
}

/* Icon Button (Play) */
.icon-btn {
    background: transparent;
    color: var(--accent-color);
    border: 2px solid var(--accent-color);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
}

.icon-btn:hover {
    background: rgba(210, 68, 112, 0.1);
    transform: scale(1.05);
}

.icon-btn svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

/* The Strip of Frames */
.frames-strip {
    display: flex;
    gap: 8px;
    /* Large padding allows first/last frames to be centered */
    padding: 20px calc(50% - 30px);
    width: max-content;
    box-sizing: border-box;
    align-items: center;
}

.frame-box {
    width: 60px;
    height: 80px;
    box-sizing: border-box;
    /* Include border in 60px width */
    background-color: #000;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    transition: all 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    -webkit-user-select: none;
    flex-shrink: 0;
    scroll-snap-align: center;
}

.frame-box.selected {
    /* No border, static highlight handles it */
    background-color: #1a1e26;
}

.frame-box.active-anim {
    /* State when playing animation */
    background-color: rgba(255, 255, 255, 0);
}



.mini-ball {
    width: 12px;
    height: 12px;
    background-color: var(--text-secondary);
    border-radius: 50%;
}

.frame-box.selected .mini-ball {
    background-color: var(--accent-color);
}

/* Hide buttons during playback */
.frames-strip.is-playing .duplicate-btn,
.frames-strip.is-playing .delete-btn {
    display: none !important;
}

/* Controls Components */
button.primary-btn {
    background-color: var(--accent-color);
    color: white;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 20px;
    /* Pill shape */
    font-weight: 600;
    cursor: pointer;
    font-size: 0.9rem;
}

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

input[type=range] {
    width: 100%;
    background: transparent;
    appearance: none;
    /* Standard property */
    -webkit-appearance: none;
}

input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: var(--accent-color);
    margin-top: -6px;
    cursor: pointer;
}

input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 4px;
    background: var(--border-color);
    border-radius: 2px;
}

/* Toggle Switch */
.toggle-switch {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 0.9rem;
}

.toggle-switch input {
    display: none;
}

.slider {
    width: 32px;
    height: 18px;
    background-color: var(--border-color);
    border-radius: 18px;
    position: relative;
    transition: 0.3s;
}

.slider::before {
    content: "";
    position: absolute;
    height: 14px;
    width: 14px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    border-radius: 50%;
    transition: 0.3s;
    opacity: 1;
}

/* -- Mobile / Responsive Styles -- */
@media (max-width: 768px) {

    /* Lock the body to viewport height to prevent scrolling */
    body {
        height: 100dvh;
        overflow: hidden;
    }

    .app-container {
        height: 100dvh;
        overflow: hidden;
    }

    /* Hide Header to save space */
    header {
        display: none !important;
    }

    /* Main Area: Flex grow to fill space */
    main {
        padding: 5px;
        /* Safe Area for iPhone Notch */
        padding-top: env(safe-area-inset-top, 20px);
        padding-left: env(safe-area-inset-left, 5px);
        padding-right: env(safe-area-inset-right, 5px);

        flex-direction: row;
        gap: 0;
        align-items: stretch;
    }

    .canvas-layout {
        gap: 0;
        /* Remove gaps to maximize space */
        width: 100%;
        height: 100%;
        justify-content: space-between;
        align-items: stretch;
        /* FIX: Enable full height expansion */
    }

    /* Sliders: Hide on mobile as requested */
    .slider-vertical-container {
        display: none !important;
    }

    /* Canvas Section: Center and Resize */
    .canvas-section {
        flex: 1;
        /* Take remaining width */
        margin: 0 5px;
        border-radius: 8px;
        width: 100%;
        height: 100%;
        /* FIX: Force full height */
        /* Let flex handle it */
        /* Ensure buttons inside don't get clipped weirdly, but overflow hidden is needed for canvas? */
        /* If buttons are absolute, they need space. */
    }

    canvas {
        width: 100% !important;
        height: 100% !important;
        object-fit: contain;
        /* Ensure aspect ratio */
        border: none;
        box-shadow: none;
    }

    /* Footer: Compact Layout */
    footer {
        display: flex;
        flex-direction: column;
        flex-wrap: nowrap;
        height: auto;
        /* Allow growth for dropdown */
        min-height: auto;

        /* Spacing */
        padding-top: 10px;
        padding-bottom: env(safe-area-inset-bottom, 10px);
        /* Home bar safe area */

        gap: 3px;
        /* Increased gap to separate timeline from buttons */

        background-color: var(--panel-bg);
        border-top: 1px solid var(--border-color);
        z-index: 50;
        overflow-x: hidden;
        /* Prevent horizontal scroll on footer itself */
    }

    /* Timeline: Order 1 */
    /* Timeline: Order 1 */
    .timeline-wrapper {
        width: 100%;
        height: 90px;
        /* Adjusted to fit 80px frames + padding */
        margin: 0;
        flex: 0 0 auto;
        order: 1;
        /* Ensure it doesn't overflow parent width */
        max-width: 100vw;
    }

    /* Optimize strip padding for mobile */
    .frames-strip {
        padding-top: 5px;
        padding-bottom: 5px;
        /* Keep horizontal centering logic: calc(50% - 30px) */
        padding-left: calc(50% - 30px);
        padding-right: calc(50% - 30px);
    }

    .timeline-container {
        padding-bottom: 0;
        /* Ensure scrollbar area doesn't add weird height */
    }

    /* Controls Row: Order 2 */
    .mobile-controls-row {
        display: flex;
        width: 100%;
        justify-content: space-around;
        align-items: center;
        order: 2;
        padding: 0 2px 2px 2px;
        /* Reduced bottom padding since footer has safe area */
        flex-shrink: 0;
    }

    /* Re-organize buttons into the controls row */
    #btnPlayPause {
        width: 44px;
        height: 44px;
        margin: 0;
        order: initial;
        /* Reset desktop order */
    }

    .footer-controls {
        flex-direction: row !important;
        gap: 2px;
        margin: 0;
        order: 3;
        /* Just in case */
    }

    .undo-redo-group {
        display: flex;
        gap: 15px;
        margin: 0 !important;
    }

    /* Ensure icons are touch friendly size */
    .icon-btn.small-btn {
        width: 40px;
        height: 40px;
    }

    /* File / Mode dropdown: Order 3, Static Flow */
    .mobile-file-dropdown {
        display: block !important;
        margin: 0;
        width: 100%;
        order: 3;
        border-top: 1px solid var(--border-color);
        background: rgba(0, 0, 0, 0.2);
    }

    .mobile-file-dropdown summary {
        padding: 10px;
        background: transparent;
        cursor: pointer;
        text-align: center;
        font-size: 0.9rem;
        color: var(--text-secondary);
        list-style: none;
        /* Hide default triangle if desired, or keep it */
    }

    /* When open, it just expands comfortably in the footer flow */
    .mobile-file-dropdown[open] {
        position: static;
        box-shadow: none;
        border: none;
        padding: 10px;
        background: transparent;
    }

    .mobile-file-row {
        display: flex;
        justify-content: center;
        gap: 10px;
        margin-bottom: 15px;
    }
}

/* Default state: Hide mobile dropdown */
.mobile-file-dropdown {
    display: none;
}

/* Footer Controls Styling (Desktop Default) */
.footer-controls {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    /* Align to right/center */
    gap: 8px;
}

.undo-redo-group {
    display: flex;
    gap: 8px;
    margin-bottom: 5px;
    /* Spacing above onion skin on desktop */
}

.icon-btn.small-btn {
    width: 32px;
    height: 32px;
    border-width: 1px;
}

.icon-btn.small-btn svg {
    width: 18px;
    height: 18px;
}

.icon-btn:disabled {
    opacity: 0.3;
    pointer-events: none;
    border-color: var(--border-color);
    color: var(--text-secondary);
}

.frame-number {
    position: absolute;
    bottom: 4px;
    /* Moved to bottom */
    left: 6px;
    font-size: 0.7rem;
    color: var(--text-secondary);
    pointer-events: none;
    /* Prevent interference */
}

.duplicate-btn {
    position: absolute;
    top: 4px;
    left: 6px;

    font-size: 0.9rem;
    line-height: 1;
    color: var(--text-secondary);

    cursor: pointer;
    display: none;
    z-index: 10;
    font-weight: bold;
}

.duplicate-btn:hover {
    color: var(--accent-color);
    /* Blue for duplicate */
}

/* Delete Button (Top Right) */
.delete-btn {
    position: absolute;
    top: 4px;
    right: 6px;

    font-size: 0.9rem;
    /* Slightly larger than number for clickability */
    line-height: 1;
    color: var(--text-secondary);

    cursor: pointer;
    display: none;
    z-index: 10;
    font-weight: bold;
}

.delete-btn:hover {
    color: #ef4444;
    /* Red delete */
}

@media (hover: hover) {

    .frame-box:hover .delete-btn,
    .frame-box:hover .duplicate-btn {
        display: block;
    }
}

/* On Mobile/Touch, show buttons when selected */
.frame-box.selected .delete-btn,
.frame-box.selected .duplicate-btn {
    display: block;
}

input:checked+.slider {
    background-color: var(--accent-color);
}

input:checked+.slider::before {
    transform: translateX(14px);
}

/* Mode Toggle (Segmented Control) */
.mode-toggle {
    display: inline-flex;
    background: #1a1d23;
    border-radius: 20px;
    padding: 3px;
    /* margin-left: 15px; Removed for flex gap */
    vertical-align: middle;
    border: 2px solid var(--accent-color);
    margin-left: 10px;
}

/* Tablet Layout: 769px - 1200px */
@media (min-width: 769px) and (max-width: 1200px) {
    .header-controls .mode-toggle {
        position: absolute;
        top: 0;
        left: 13rem;
        right: auto;
        width: auto;
        margin: 0;
        /* Remove default margins */
    }
}

.desktop-only {
    display: inline-flex;
}

.mobile-only {
    display: none;
}

@media (max-width: 768px) {
    .desktop-only {
        display: none !important;
    }

    .mobile-only {
        display: flex !important;
    }
}

.mode-btn {
    border: none;
    background: transparent;
    color: var(--text-secondary);
    padding: 5px 12px;
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 16px;
    transition: all 0.2s ease;
}

.mode-btn:hover {
    color: var(--text-primary);
}

.mode-btn.active {
    background: var(--accent-color);
    color: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}