:root {
    --bg-color: #0f172a;
    --sidebar-bg: rgba(30, 41, 59, 0.8);
    --accent-color: #38bdf8;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border-color: rgba(255, 255, 255, 0.1);
    --glass-blur: 16px;
    --sidebar-width: 320px;
    --viewport-pad: clamp(12px, 2vw, 24px);
    --floor-ratio: 1617 / 2100;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
    overscroll-behavior: none;
}

.app-container {
    display: flex;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
    backdrop-filter: blur(var(--glass-blur));
    border-right: 1px solid var(--border-color);
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    z-index: 1000;
    box-shadow: 10px 0 30px rgba(0, 0, 0, 0.3);
    overflow-y: auto;
    scrollbar-width: none;
}

.sidebar::-webkit-scrollbar { display: none; }

.sidebar-header {
    padding: 0 0.5rem 1rem 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h1 {
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: 3px;
    text-transform: uppercase;
    background: linear-gradient(90deg, var(--accent-color), #0ea5e9);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.sidebar-header p {
    font-size: 0.85rem;
    font-weight: 400;
    color: var(--text-secondary);
    margin-top: 2px;
}

.sidebar-section {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 1.25rem;
    transition: transform 0.3s ease, background 0.3s ease;
}

.sidebar-section:hover {
    background: rgba(255, 255, 255, 0.05);
}

.sidebar-section h3 {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-section h3::before {
    content: '';
    width: 4px;
    height: 4px;
    background: var(--accent-color);
    border-radius: 50%;
    box-shadow: 0 0 8px var(--accent-color);
}

/* Toggle Switch */
.toggle-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.85rem;
    color: var(--text-primary);
}

.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 22px;
}

.switch input { opacity: 0; width: 0; height: 0; }

.slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #334155;
    transition: .3s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
}

input:checked + .slider { background-color: var(--accent-color); }
input:checked + .slider:before { transform: translateX(22px); }

/* Inventory */
.inventory-items {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.85rem;
}

.inventory-item {
    background: rgba(15, 23, 42, 0.4);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 1.25rem 1rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    cursor: grab;
    touch-action: manipulation;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    height: 120px;
}

.inventory-item:hover {
    background: rgba(56, 189, 248, 0.06);
    transform: translateY(-4px);
    border-color: var(--accent-color);
    box-shadow: 0 10px 20px -10px rgba(56, 189, 248, 0.3);
}

.inventory-item:active { cursor: grabbing; transform: scale(0.95); }

.inventory-item img {
    filter: drop-shadow(0 6px 10px rgba(0,0,0,0.4));
    height: 52px;
    width: auto;
    transition: transform 0.2s ease;
}

.inventory-item:hover img {
    transform: scale(1.08);
}

.inventory-item span {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-primary);
}

.action-btn.delete-all {
    width: auto;
    margin-top: 0.85rem;
    padding: 0.45rem 0.85rem;
    font-size: 0.72rem;
    border-radius: 8px;
    background: rgba(239, 68, 68, 0.05);
    border-color: rgba(239, 68, 68, 0.1);
    color: #fca5a5;
    margin-left: auto;
    margin-right: auto;
}

.action-btn.delete-all:hover {
    background: rgba(239, 68, 68, 0.15);
    border-color: #ef4444;
    color: #ef4444;
}

/* Measure Tool Buttons */
.measure-buttons {
    display: flex;
    gap: 0.5rem;
}

.measure-buttons .action-btn { flex: 1; }

.action-btn.measure.active {
    background: rgba(245, 158, 11, 0.15);
    border-color: #f59e0b;
    color: #fbbf24;
}

/* Save & Load UI */
.save-new {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    margin-bottom: 1rem;
}

.save-name-input {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: rgba(15, 23, 42, 0.4);
    color: var(--text-primary);
    font-family: 'Outfit', sans-serif;
    font-size: 0.8rem;
    outline: none;
    transition: all 0.3s;
}

.save-name-input:focus {
    border-color: var(--accent-color);
    background: rgba(15, 23, 42, 0.6);
}

.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0.6rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    font-family: 'Outfit', sans-serif;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    min-height: 44px;
    touch-action: manipulation;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.action-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
}

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

.action-btn.save {
    width: auto;
    align-self: flex-end;
    padding: 0.45rem 1rem;
    font-size: 0.75rem;
    background: rgba(56, 189, 248, 0.05);
    border-color: rgba(56, 189, 248, 0.15);
    color: var(--accent-color);
}

.action-btn.save:hover {
    background: rgba(56, 189, 248, 0.12);
    border-color: var(--accent-color);
    color: var(--accent-color);
}


/* Saves List UI */
.saves-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 180px;
    overflow-y: auto;
    padding-right: 4px;
}

.saves-list::-webkit-scrollbar { width: 4px; }
.saves-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.save-entry {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid var(--border-color);
    transition: all 0.25s ease;
}

.save-entry:hover {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(255, 255, 255, 0.15);
    transform: scale(1.02);
}

.save-entry-name {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.save-entry-meta {
    font-size: 0.65rem;
    color: var(--text-secondary);
    grid-column: 1;
    margin-top: -2px;
}

.save-entry-actions {
    grid-row: 1 / 3;
    grid-column: 2;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.save-icon-btn {
    width: 28px;
    height: 28px;
    min-width: 40px;
    min-height: 40px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid transparent;
    color: var(--text-secondary);
    cursor: pointer;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.save-icon-btn svg { width: 14px; height: 14px; }

.save-icon-btn.load-entry:hover {
    background: rgba(74, 222, 128, 0.1);
    color: #4ade80;
    border-color: rgba(74, 222, 128, 0.2);
}

.save-icon-btn.rename-entry:hover {
    background: rgba(56, 189, 248, 0.1);
    color: var(--accent-color);
    border-color: rgba(56, 189, 248, 0.2);
}

.save-icon-btn.delete-entry:hover {
    background: rgba(239, 68, 68, 0.1);
    color: #f87171;
    border-color: rgba(239, 68, 68, 0.2);
}

.save-status {
    font-size: 0.75rem;
    margin-top: 0.5rem;
    text-align: center;
    height: 1.2rem;
    transition: opacity 0.4s;
    font-weight: 500;
}

/* Main Viewport */
.viewport {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at center, #1e293b 0%, #080c14 100%);
    overflow: auto;
    padding: var(--viewport-pad);
    min-width: 0;
    min-height: 0;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
}

.floor-container {
    position: relative;
    width: min(800px, 72dvh, calc(100vw - var(--sidebar-width) - var(--viewport-pad) - var(--viewport-pad)));
    height: auto;
    aspect-ratio: var(--floor-ratio);
    flex-shrink: 0;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    background-color: white;
    box-shadow: 0 40px 100px -20px rgba(0, 0, 0, 0.7);
    border-radius: 8px;
    transition: background-image 0.4s ease;
}

.floor-container.empty { background-image: url('Empty Floor.png'); }
.floor-container.full { background-image: url('Full Floor.png'); }

/* Placed Items */
.placed-item {
    position: absolute;
    cursor: move;
    transform-origin: center center;
    user-select: none;
    touch-action: none;
    z-index: 10;
}

.placed-item img {
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none;
    filter: drop-shadow(0 8px 16px rgba(0,0,0,0.3));
}

.placed-item.selected {
    outline: 3px solid var(--accent-color);
    outline-offset: 6px;
    z-index: 20;
}

/* Rulers */
.ruler {
    position: absolute;
    z-index: 5;
    pointer-events: none;
}

.ruler-top {
    top: 0;
    left: 0;
    width: 100%;
    height: 20px;
}

.ruler-left {
    top: 0;
    left: 0;
    width: 20px;
    height: 100%;
}

.ruler-tick {
    position: absolute;
    background: rgba(0, 0, 0, 0.45);
}

.ruler-top .ruler-tick {
    top: 0;
    width: 1px;
    height: 12px;
}

.ruler-top .ruler-tick.major {
    height: 20px;
    background: rgba(0, 0, 0, 0.75);
}

.ruler-left .ruler-tick {
    left: 0;
    height: 1px;
    width: 12px;
}

.ruler-left .ruler-tick.major {
    width: 20px;
    background: rgba(0, 0, 0, 0.75);
}

.ruler-label {
    position: absolute;
    font-family: 'Outfit', sans-serif;
    font-size: 8px;
    font-weight: 600;
    color: rgba(0, 0, 0, 0.82);
    pointer-events: none;
}

.ruler-top .ruler-label {
    top: 1px;
}

.ruler-left .ruler-label {
    left: 2px;
}

/* Measure SVG overlay */
.measure-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 30;
    pointer-events: none;
}

.floor-container.measuring {
    cursor: crosshair;
    touch-action: none;
}

.floor-container.measuring .placed-item {
    pointer-events: none;
}

.floor-container.measuring .measure-svg {
    pointer-events: auto;
}

.measure-line {
    stroke: #f59e0b;
    stroke-width: 2;
    stroke-dasharray: 6 3;
}

.measure-endpoint {
    fill: #f59e0b;
    stroke: white;
    stroke-width: 1.5;
}

.measure-label-bg {
    fill: rgba(15, 23, 42, 0.9);
    rx: 4;
    ry: 4;
}

.measure-label-text {
    font-family: 'Outfit', sans-serif;
    font-size: 12px;
    font-weight: 600;
    fill: #f59e0b;
    text-anchor: middle;
    dominant-baseline: central;
}

/* Measure toggle */
.measure-buttons {
    display: flex;
    gap: 0.5rem;
}

.action-btn.measure.active {
    background: rgba(245, 158, 11, 0.2);
    border-color: #f59e0b;
    color: #f59e0b;
}

/* Controls */
.item-controls {
    position: fixed;
    display: flex;
    align-items: center;
    gap: 10px;
    z-index: 10000;
    pointer-events: auto;
    transition: opacity 0.2s, transform 0.2s;
    background: rgba(15, 23, 42, 0.95);
    padding: 8px 14px;
    border-radius: 14px;
    backdrop-filter: blur(12px);
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 30px -5px rgba(0,0,0,0.6);
}

.item-controls.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
}

/* Rotation Control */
.rotation-control {
    display: flex;
    align-items: center;
    gap: 8px;
}

.degree-input {
    font-family: 'Outfit', sans-serif;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--accent-color);
    background: rgba(56, 189, 248, 0.12);
    padding: 4px 6px;
    border-radius: 6px;
    width: 48px;
    text-align: center;
    border: 1px solid transparent;
    outline: none;
    transition: border-color 0.2s, background 0.2s;
    -moz-appearance: textfield;
}

.degree-input::-webkit-outer-spin-button,
.degree-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.degree-input:focus {
    border-color: var(--accent-color);
    background: rgba(56, 189, 248, 0.2);
}

.rotation-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 120px;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.1);
    outline: none;
    cursor: pointer;
}

.rotation-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent-color);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(56, 189, 248, 0.4);
    transition: transform 0.15s, box-shadow 0.15s;
}

.rotation-slider::-webkit-slider-thumb:hover {
    transform: scale(1.2);
    box-shadow: 0 2px 10px rgba(56, 189, 248, 0.6);
}

.rotation-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent-color);
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 6px rgba(56, 189, 248, 0.4);
}

.rotation-slider::-moz-range-track {
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.1);
}

.control-btn {
    width: 32px;
    height: 32px;
    min-width: 44px;
    min-height: 44px;
    border-radius: 8px;
    border: none;
    color: white;
    cursor: pointer;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.control-btn.copy {
    background: var(--accent-color);
}

.control-btn.copy:hover {
    background: #7dd3fc;
    transform: scale(1.1);
}

.control-btn.delete {
    background: #ef4444;
}

.control-btn.delete:hover {
    background: #f87171;
    transform: scale(1.1);
}

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

/* Confirm Modal */
.confirm-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(6px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 99999;
    animation: fadeIn 0.2s ease;
}

.confirm-modal-overlay.hidden {
    display: none;
}

.confirm-modal {
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 2rem 2.5rem;
    text-align: center;
    box-shadow: 0 25px 60px -10px rgba(0, 0, 0, 0.7);
    animation: modalPop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
    min-width: 300px;
}

.confirm-modal-msg {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 1.1rem;
}

.confirm-modal-input {
    width: 100%;
    margin-bottom: 1.25rem;
    padding: 0.75rem 0.9rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: rgba(15, 23, 42, 0.58);
    color: var(--text-primary);
    font-family: 'Outfit', sans-serif;
    font-size: 0.95rem;
    font-weight: 500;
    outline: none;
    text-align: left;
    transition: border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.confirm-modal-input.hidden {
    display: none;
}

.confirm-modal-input:focus {
    background: rgba(15, 23, 42, 0.78);
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.14);
}

.confirm-modal-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
}

.confirm-btn {
    font-family: 'Outfit', sans-serif;
    font-size: 0.85rem;
    font-weight: 500;
    padding: 0.6rem 1.5rem;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    cursor: pointer;
    transition: all 0.2s ease;
}

.confirm-yes {
    background: rgba(239, 68, 68, 0.2);
    color: #f87171;
    border-color: rgba(239, 68, 68, 0.3);
}

.confirm-yes:hover {
    background: rgba(239, 68, 68, 0.35);
    border-color: #f87171;
    transform: translateY(-1px);
}

.confirm-modal-overlay.rename-mode .confirm-yes {
    background: rgba(56, 189, 248, 0.16);
    color: var(--accent-color);
    border-color: rgba(56, 189, 248, 0.35);
}

.confirm-modal-overlay.rename-mode .confirm-yes:hover {
    background: rgba(56, 189, 248, 0.28);
    border-color: var(--accent-color);
}

.confirm-no {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-secondary);
}

.confirm-no:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    transform: translateY(-1px);
}

.saves-empty {
    color: var(--text-secondary);
    font-size: 0.78rem;
    padding: 0.7rem;
    text-align: center;
}

@media (max-width: 900px) {
    body {
        position: fixed;
        inset: 0;
        width: 100%;
    }

    .app-container {
        flex-direction: column;
    }

    .viewport {
        order: 1;
        flex: 1 1 auto;
        align-items: center;
        justify-content: center;
        padding: 12px;
    }

    .floor-container {
        width: min(calc(100vw - 24px), 51dvh);
        aspect-ratio: var(--floor-ratio);
        box-shadow: 0 24px 60px -24px rgba(0, 0, 0, 0.75);
    }

    .sidebar {
        order: 2;
        width: 100%;
        height: clamp(190px, 31dvh, 280px);
        flex-direction: row;
        align-items: stretch;
        gap: 0.75rem;
        padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
        border-right: 0;
        border-top: 1px solid var(--border-color);
        box-shadow: 0 -12px 34px rgba(0, 0, 0, 0.35);
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x proximity;
    }

    .sidebar-header {
        flex: 0 0 116px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: 0.75rem;
        border-bottom: 0;
        border-right: 1px solid var(--border-color);
        scroll-snap-align: start;
    }

    .sidebar-header h1 {
        font-size: 1rem;
        letter-spacing: 2px;
    }

    .sidebar-header p {
        font-size: 0.72rem;
    }

    .sidebar-section {
        flex: 0 0 min(250px, 74vw);
        max-height: calc(100% - 2px);
        overflow: auto;
        padding: 0.8rem;
        border-radius: 8px;
        scroll-snap-align: start;
    }

    .sidebar-section h3 {
        font-size: 0.64rem;
        letter-spacing: 1.3px;
        margin-bottom: 0.7rem;
    }

    .inventory-items {
        grid-template-columns: repeat(2, minmax(86px, 1fr));
        gap: 0.55rem;
    }

    .inventory-item {
        height: 92px;
        padding: 0.65rem 0.5rem;
        border-radius: 8px;
    }

    .inventory-item img {
        height: 42px;
    }

    .inventory-item span {
        font-size: 0.72rem;
        text-align: center;
    }

    .action-btn.delete-all {
        width: 100%;
        margin-top: 0.6rem;
    }

    .save-new {
        flex-direction: row;
        gap: 0.5rem;
        margin-bottom: 0.65rem;
    }

    .save-name-input {
        min-width: 0;
        font-size: 16px;
    }

    .action-btn.save {
        align-self: stretch;
        min-width: 72px;
        padding-inline: 0.75rem;
    }

    .saves-list {
        max-height: 118px;
    }

    .save-entry {
        border-radius: 8px;
        padding: 0.6rem 0.65rem;
    }

    .measure-buttons {
        gap: 0.5rem;
    }

    .instructions {
        display: none;
    }

    .item-controls {
        max-width: calc(100vw - 16px);
        gap: 8px;
        padding: 8px;
        border-radius: 10px;
    }

    .rotation-control {
        min-width: 0;
    }

    .degree-input {
        font-size: 16px;
        height: 40px;
    }

    .rotation-slider {
        width: clamp(96px, 34vw, 150px);
    }

    .confirm-modal {
        width: min(calc(100vw - 32px), 360px);
        min-width: 0;
        padding: 1.25rem;
        border-radius: 12px;
    }
}

@media (max-width: 520px) {
    .floor-container {
        width: min(calc(100vw - 24px), 48dvh);
    }

    .sidebar {
        height: clamp(190px, 32dvh, 300px);
    }

    .sidebar-section {
        flex-basis: min(224px, 78vw);
    }
}

@media (max-width: 900px) and (orientation: landscape) {
    .viewport {
        padding: 8px 12px;
    }

    .floor-container {
        width: min(calc(100vw - 24px), 42dvh);
    }

    .sidebar {
        height: clamp(120px, 34dvh, 160px);
    }
}

@media (pointer: coarse) {
    .inventory-item:hover,
    .save-entry:hover,
    .control-btn.copy:hover,
    .control-btn.delete:hover,
    .control-btn:hover,
    .action-btn:hover {
        transform: none;
    }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes modalPop {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}
