/* --- Top Bar --- */
.topbar {
    position: sticky; top: 0; z-index: 10;
    display: grid; grid-template-columns: auto 1fr auto;
    gap: 1rem; align-items: center; padding: 0.8rem 1rem;
    background: rgba(0, 0, 0, 0.2); 
    backdrop-filter: blur(6px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.logo { font-weight: 800; letter-spacing: 0.2px; }
.logo b { color: #00ffc6; }
.actions { display: flex; gap: 0.6rem; align-items: center; }

/* --- Dashboard Dropdown Styling --- */
#dashboardSwitch {
    background-color: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(4px);
    color: #fff;
    min-width: 220px;
    text-align: left;
    border: 1px solid rgba(255, 255, 255, 0.15);
    padding-right: 2rem;
    cursor: pointer;
}

#dashboardSwitch:focus {
    border-color: #fff;
    outline: none;
    background-color: rgba(0, 0, 0, 0.4);
}

#dashboardSwitch option {
    background-color: #000;
    color: #fff;
    padding: 12px;
}

/* ============================================
   PRESENCE TOGGLE (Topbar)
   ============================================ */
.presence-toggle {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
}

.presence-toggle:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
}

.presence-toggle:focus {
    outline: none;
}

.presence-toggle:focus-visible {
    outline: 2px solid rgba(0, 255, 198, 0.5);
    outline-offset: 2px;
}

.presence-icon {
    font-size: 1.1rem;
    line-height: 1;
    transition: all 0.3s ease;
}

/* --- Presence States --- */

/* OFF State - Greyed out */
.presence-toggle.off {
    opacity: 0.5;
}

.presence-toggle.off:hover {
    opacity: 0.75;
}

/* ON State - Active/Scanning */
.presence-toggle.on {
    opacity: 1;
    background: rgba(0, 255, 198, 0.1);
    border-color: rgba(0, 255, 198, 0.3);
}

.presence-toggle.on .presence-icon {
    animation: presencePulse 2s ease-in-out infinite;
}

/* DETECTED State - Found a user */
.presence-toggle.detected {
    opacity: 1;
    background: rgba(0, 255, 198, 0.15);
    border-color: rgba(0, 255, 198, 0.5);
    box-shadow: 0 0 12px rgba(0, 255, 198, 0.3);
}

.presence-toggle.detected .presence-icon {
    animation: none;
}

/* --- Presence Pulse Animation --- */
@keyframes presencePulse {
    0%, 100% {
        opacity: 1;
        transform: scale(1);
    }
    50% {
        opacity: 0.6;
        transform: scale(0.95);
    }
}

/* --- Presence Tooltip --- */
.presence-tooltip {
    position: absolute;
    top: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: #111;
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 4px;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
    z-index: 100;
    white-space: nowrap;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

/* Tooltip Arrow */
.presence-tooltip::before {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-bottom-color: rgba(255, 255, 255, 0.15);
}

.presence-tooltip::after {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-bottom-color: #111;
}

/* Tooltip Visible */
.presence-tooltip.show {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

/* Tooltip Button */
.presence-tooltip button {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: #fff;
    padding: 8px 14px;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s ease;
}

.presence-tooltip button:hover {
    background: rgba(0, 255, 198, 0.2);
}

/* --- Presence User Badge (when detected) --- */
.presence-user-badge {
    position: absolute;
    top: calc(100% + 4px);
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 255, 198, 0.9);
    color: #000;
    font-size: 0.65rem;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 4px;
    white-space: nowrap;
    pointer-events: none;
    animation: badgeFadeIn 0.3s ease;
}

@keyframes badgeFadeIn {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-5px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

/* ============================================
   END PRESENCE STYLES
   ============================================ */

/* ============================================
   PRESENCE TRANSITION ANIMATION
   Tony Stark Level Coolâ„¢
   ============================================ */

.presence-transition {
    position: fixed;
    inset: 0;
    z-index: 9999;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
}

.presence-transition.active {
    opacity: 1;
    visibility: visible;
    pointer-events: all;
}

/* Dark overlay background - instant show, animated hide */
.presence-transition-bg {
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse at center, rgba(0, 20, 15, 0.98) 0%, rgba(0, 0, 0, 1) 100%);
}

/* Only animate opacity when fading out */
.presence-transition.fade-out .presence-transition-bg {
    opacity: 0;
    transition: opacity 0.5s ease;
}

/* Scanning line effect */
.presence-transition-scanline {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, 
        transparent 0%, 
        rgba(0, 255, 198, 0.3) 20%,
        rgba(0, 255, 198, 0.8) 50%,
        rgba(0, 255, 198, 0.3) 80%,
        transparent 100%
    );
    box-shadow: 0 0 20px rgba(0, 255, 198, 0.5), 0 0 60px rgba(0, 255, 198, 0.3);
    transform: translateY(-100%);
    opacity: 0;
}

.presence-transition.active .presence-transition-scanline {
    animation: scanDown 0.8s ease-out 0.2s forwards;
}

@keyframes scanDown {
    0% {
        transform: translateY(-100%);
        opacity: 1;
    }
    100% {
        transform: translateY(100vh);
        opacity: 0;
    }
}

/* Center content container */
.presence-transition-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    opacity: 0;
}

.presence-transition.active .presence-transition-content {
    animation: contentReveal 0.6s ease-out 0.3s forwards;
}

@keyframes contentReveal {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.9);
    }
    100% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

/* User icon */
.presence-transition-icon {
    font-size: 4rem;
    margin-bottom: 1.5rem;
    opacity: 0;
    transform: scale(0.5);
    filter: drop-shadow(0 0 30px rgba(0, 255, 198, 0.5));
}

.presence-transition.active .presence-transition-icon {
    animation: iconPop 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) 0.4s forwards;
}

@keyframes iconPop {
    0% {
        opacity: 0;
        transform: scale(0.5);
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

/* Greeting text */
.presence-transition-greeting {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.greeting-text {
    font-size: 1.2rem;
    font-weight: 300;
    color: rgba(255, 255, 255, 0.6);
    letter-spacing: 0.2em;
    text-transform: uppercase;
    opacity: 0;
}

.presence-transition.active .greeting-text {
    animation: textSlideUp 0.5s ease-out 0.5s forwards;
}

.greeting-name {
    font-size: 3.5rem;
    font-weight: 800;
    color: #fff;
    letter-spacing: -0.02em;
    text-shadow: 0 0 40px rgba(0, 255, 198, 0.5);
    opacity: 0;
    background: linear-gradient(135deg, #ffffff 0%, #00ffc6 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.presence-transition.active .greeting-name {
    animation: nameReveal 0.6s ease-out 0.6s forwards;
}

@keyframes textSlideUp {
    0% {
        opacity: 0;
        transform: translateY(20px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes nameReveal {
    0% {
        opacity: 0;
        transform: translateY(30px) scale(0.9);
        filter: blur(10px);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
        filter: blur(0);
    }
}

/* Accent line under name */
.presence-transition-line {
    width: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, #00ffc6, transparent);
    margin: 1.5rem auto 0;
    box-shadow: 0 0 20px rgba(0, 255, 198, 0.5);
}

.presence-transition.active .presence-transition-line {
    animation: lineExpand 0.4s ease-out 0.8s forwards;
}

@keyframes lineExpand {
    0% {
        width: 0;
    }
    100% {
        width: 200px;
    }
}

/* Fade out animation */
.presence-transition.fade-out {
    opacity: 0;
    transition: opacity 0.5s ease;
}

.presence-transition.fade-out .presence-transition-bg {
    opacity: 0;
    transition: opacity 0.4s ease;
}

.presence-transition.fade-out .presence-transition-content {
    animation: contentFadeOut 0.4s ease-in forwards;
}

@keyframes contentFadeOut {
    0% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
    100% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(1.1);
    }
}

/* ============================================
   WIDGET ENTRANCE ANIMATIONS (after transition)
   ============================================ */

/* Disable ALL widget animations during presence switch */
body.presence-switching .mod {
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
}

/* Loading state - widgets are hidden as they're created, NO animation */
.grid.presence-loading .mod {
    opacity: 0 !important;
    visibility: hidden !important;
    animation: none !important;
}

/* Entrance animation state - override default moduleEntry */
.grid.presence-entrance .mod {
    opacity: 0;
    transform: translateY(30px) scale(0.95);
    animation: widgetSlideIn 0.5s ease-out forwards !important;
}

/* Stagger the animations */
.grid.presence-entrance .mod:nth-child(1) { animation-delay: 0.05s; }
.grid.presence-entrance .mod:nth-child(2) { animation-delay: 0.1s; }
.grid.presence-entrance .mod:nth-child(3) { animation-delay: 0.15s; }
.grid.presence-entrance .mod:nth-child(4) { animation-delay: 0.2s; }
.grid.presence-entrance .mod:nth-child(5) { animation-delay: 0.25s; }
.grid.presence-entrance .mod:nth-child(6) { animation-delay: 0.3s; }
.grid.presence-entrance .mod:nth-child(7) { animation-delay: 0.35s; }
.grid.presence-entrance .mod:nth-child(8) { animation-delay: 0.4s; }
.grid.presence-entrance .mod:nth-child(9) { animation-delay: 0.45s; }
.grid.presence-entrance .mod:nth-child(10) { animation-delay: 0.5s; }
.grid.presence-entrance .mod:nth-child(11) { animation-delay: 0.55s; }
.grid.presence-entrance .mod:nth-child(12) { animation-delay: 0.6s; }

@keyframes widgetSlideIn {
    0% {
        opacity: 0;
        transform: translateY(30px) scale(0.95);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* ============================================
   END PRESENCE TRANSITION
   ============================================ */

/* --- Main Grid Canvas --- */
.canvas-wrap {
    flex: 1; padding: 1rem 0;
    display: flex; flex-direction: column; min-height: 0;
}

.grid {
    flex: 1; display: grid;
    gap: var(--gap);
    grid-template-columns: repeat(12, 1fr);
    grid-auto-rows: minmax(0, 1fr);
    padding: 0 1rem;
    height: calc(100vh - 80px);
}

/* --- Slots & Base Modules --- */
.slot {
    border: 1px dashed var(--tileBorder);
    border-radius: 14px;
    background: rgba(0, 0, 0, 0.1); 
    display: flex; align-items: center; justify-content: center;
    color: var(--muted); cursor: pointer;
    transition: border-color .15s ease, background .15s ease;
    z-index: 1;
}

.slot:hover {
    border-color: rgba(255,255,255,0.3);
    background: rgba(0, 0, 0, 0.2); 
}

.slot.drag-over {
    border-color: rgba(255,255,255,0.5);
    background: rgba(255,255,255,0.08);
}

.mod {
    border: 1px solid var(--tileBorder);
    background: rgba(0, 0, 0, 0.15); 
    border-radius: 14px;
    position: relative;
    display: flex; flex-direction: column;
    overflow: hidden;
    z-index: 5;
    transition: all 0.2s ease;
    animation: moduleEntry 0.4s ease-out;
    container-type: inline-size; /* Enable container queries for responsive font scaling */
}

.mod:hover {
    border-color: rgba(255,255,255,0.3);
    background: rgba(0, 0, 0, 0.25); 
}

.mod.dragging {
    opacity: 0.5; border-color: rgba(255,255,255,0.5);
    cursor: grabbing;
}

/* --- Resize Handles --- */
.resize-handle {
    position: absolute; opacity: 0; z-index: 10;
    transition: opacity 0.2s ease;
}
.resize-handle:hover, .resize-handle.dragging { opacity: 1 !important; background: rgba(0,255,198,0.5); }
.mod:hover .resize-handle { opacity: 0.6; }

.resize-handle.left, .resize-handle.right { top: 0; bottom: 0; width: 12px; cursor: ew-resize; }
.resize-handle.top, .resize-handle.bottom { left: 0; right: 0; height: 12px; cursor: ns-resize; }
.resize-handle.right { right: -6px; }
.resize-handle.bottom { bottom: -6px; }

/* --- Drop Zones --- */
.drop-zone {
    position: absolute; z-index: 999;
    border: 2px dashed rgba(0,255,198,0.6);
    background: rgba(0,255,198,0.08);
    opacity: 0; transition: opacity .12s ease;
    pointer-events: none;
}
.drop-zone.active { opacity: 1; }

/* --- Mirror Mode (Hide UI) --- */
body.mirror .topbar, 
body.mirror .slot, 
body.mirror .resize-handle,
body.mirror .tools,
body.mirror .legal-links,
body.mirror #appInfoNotice,
body.mirror .mod .bar { 
    display: none !important; 
}

body.mirror .grid { padding: 2rem; }

body.mirror .mod { 
    border-color: transparent; 
    background: rgba(0, 0, 0, 0.1) !important; 
    backdrop-filter: none; 
    box-shadow: none;
}

body.mirror .mod:hover {
    background: rgba(0, 0, 0, 0.2) !important;
}

@keyframes moduleEntry {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

/* --- Responsive --- */
@media (max-width: 768px) {
    .grid { grid-template-columns: repeat(4, 1fr); }
    .topbar { grid-template-columns: 1fr; text-align: center; }
    .tagline { display: none; }
    
    /* Hide presence on mobile */
    .presence-toggle { display: none !important; }
}

/* --- Legal Footer --- */
.legal-links {
    position: fixed;
    bottom: 16px;
    right: 20px;
    z-index: 5;
    display: flex;
    gap: 8px;
    align-items: center;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.3);
    transition: color 0.2s ease;
}

.legal-links a {
    color: inherit;
    text-decoration: none;
    transition: color 0.2s ease;
}

.legal-links a:hover {
    color: var(--muted);
}

.legal-links span {
    color: var(--tileBorder);
}

body.mirror .legal-links {
    display: none !important;
}

/* ============================================
   PRESENCE ACTIVATION MODAL
   ============================================ */

.presence-activation-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9998;
    pointer-events: none;
    opacity: 0;
    background: radial-gradient(
        ellipse at center,
        rgba(0, 255, 198, 0.15) 0%,
        rgba(0, 200, 150, 0.08) 40%,
        rgba(0, 0, 0, 0.4) 100%
    );
    transition: opacity 0.4s ease;
}

.presence-activation-overlay.active {
    opacity: 1;
    animation: presenceOverlayPulse 1.5s ease-in-out infinite;
}

@keyframes presenceOverlayPulse {
    0%, 100% {
        background: radial-gradient(
            ellipse at center,
            rgba(0, 255, 198, 0.15) 0%,
            rgba(0, 200, 150, 0.08) 40%,
            rgba(0, 0, 0, 0.4) 100%
        );
    }
    50% {
        background: radial-gradient(
            ellipse at center,
            rgba(0, 255, 198, 0.25) 0%,
            rgba(0, 200, 150, 0.12) 40%,
            rgba(0, 0, 0, 0.4) 100%
        );
    }
}

.presence-activation-overlay.active.sustain {
    animation: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.presence-activation-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.8);
    z-index: 9999;
    opacity: 0;
    pointer-events: none;
    text-align: center;
    padding: 3rem 4rem;
    background: rgba(10, 30, 25, 0.9);
    border: 1px solid rgba(0, 255, 198, 0.3);
    border-radius: 16px;
    backdrop-filter: blur(20px);
    box-shadow: 
        0 0 60px rgba(0, 255, 198, 0.25),
        0 0 120px rgba(0, 200, 150, 0.1),
        inset 0 0 30px rgba(0, 255, 198, 0.05);
    transition: opacity 0.3s ease, transform 0.3s ease;
    min-width: 280px;
}

.presence-activation-modal.active {
    opacity: 1;
    pointer-events: auto;
    transform: translate(-50%, -50%) scale(1);
}

.presence-activation-modal.fade-out {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.95);
    pointer-events: none;
}

/* --- Activated View Content --- */
.presence-activation-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

.presence-activation-content.hidden {
    display: none;
}

.presence-activation-icon {
    font-size: 3rem;
    margin-bottom: 0.5rem;
    animation: presenceIconFloat 2s ease-in-out infinite;
}

@keyframes presenceIconFloat {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.presence-activation-title {
    font-size: 2.5rem;
    font-weight: 800;
    letter-spacing: 0.4em;
    text-transform: uppercase;
    color: #00ffc6;
    text-shadow: 
        0 0 20px rgba(0, 255, 198, 0.8),
        0 0 40px rgba(0, 200, 150, 0.5),
        0 0 60px rgba(0, 150, 100, 0.3);
    animation: presenceActivationTitleGlow 1.5s ease-in-out infinite alternate;
}

@keyframes presenceActivationTitleGlow {
    0% {
        text-shadow: 
            0 0 20px rgba(0, 255, 198, 0.8),
            0 0 40px rgba(0, 200, 150, 0.5),
            0 0 60px rgba(0, 150, 100, 0.3);
    }
    100% {
        text-shadow: 
            0 0 30px rgba(0, 255, 198, 1),
            0 0 60px rgba(0, 220, 170, 0.7),
            0 0 90px rgba(0, 180, 130, 0.5);
    }
}

.presence-activation-subtitle {
    font-size: 1.1rem;
    font-weight: 600;
    letter-spacing: 0.3em;
    text-transform: uppercase;
    color: rgba(0, 255, 198, 0.7);
    margin-top: 0.25rem;
}

.presence-activation-settings-link {
    margin-top: 1.5rem;
    padding: 0.6rem 1.5rem;
    background: rgba(0, 255, 198, 0.1);
    border: 1px solid rgba(0, 255, 198, 0.3);
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: inherit;
}

.presence-activation-settings-link:hover {
    background: rgba(0, 255, 198, 0.2);
    border-color: rgba(0, 255, 198, 0.5);
    color: #fff;
}

/* --- Settings View --- */
.presence-activation-settings {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.presence-activation-settings.visible {
    display: flex;
}

.presence-activation-settings-title {
    font-size: 1.2rem;
    font-weight: 700;
    color: #00ffc6;
    margin-bottom: 0.5rem;
}

.presence-activation-info {
    color: rgba(255, 255, 255, 0.7);
    font-size: 0.9rem;
    line-height: 1.5;
    max-width: 280px;
    text-align: center;
}

.presence-activation-manage-btn {
    margin-top: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: rgba(0, 255, 198, 0.15);
    border: 1px solid rgba(0, 255, 198, 0.4);
    border-radius: 8px;
    color: #00ffc6;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: inherit;
}

.presence-activation-manage-btn:hover {
    background: rgba(0, 255, 198, 0.25);
    border-color: rgba(0, 255, 198, 0.6);
}
/* --- Presence Transition Settings Button (for activation mode) --- */
.presence-transition-settings {
    margin-top: 1.5rem;
    padding: 0.6rem 1.5rem;
    background: rgba(0, 255, 198, 0.1);
    border: 1px solid rgba(0, 255, 198, 0.3);
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: inherit;
    opacity: 0;
    animation: presenceSettingsFadeIn 0.3s ease 0.8s forwards;
}

.presence-transition-settings:hover {
    background: rgba(0, 255, 198, 0.2);
    border-color: rgba(0, 255, 198, 0.5);
    color: #fff;
}

@keyframes presenceSettingsFadeIn {
    to { opacity: 1; }
}

/* Hide settings button when not in activation mode */
.presence-transition:not(.activation-mode) .presence-transition-settings {
    display: none;
}