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

html, body {
    width: 100%;
    overflow: hidden;
    background: #000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    /* Dynamic viewport height for mobile browsers */
    height: 100vh;
    height: 100dvh;
}

.viewport {
    position: relative;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    /* Safe area for notched phones */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.viewport video,
.viewport canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.viewport video {
    z-index: 1;
}

#overlay {
    z-index: 2;
    pointer-events: none;
}

#capture {
    display: none;
}

/* Controls bar at bottom */
.controls {
    position: absolute;
    bottom: max(16px, env(safe-area-inset-bottom, 16px));
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    /* Ensure it's always above other elements */
    max-width: calc(100% - 32px);
    width: auto;
}

.controls input {
    width: 280px;
    min-width: 120px;
    flex: 1;
    padding: 12px 14px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 16px; /* iOS zoom prevention: must be >= 16px */
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.controls input:focus {
    border-color: #4ade80;
}

.controls input::placeholder {
    color: rgba(255, 255, 255, 0.4);
}

.btn {
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    background: #4ade80;
    color: #000;
    transition: transform 0.1s, opacity 0.2s;
    white-space: nowrap;
    /* Minimum touch target */
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-appearance: none;
    appearance: none;
}

.btn:active {
    transform: scale(0.96);
    opacity: 0.9;
}

/* Status indicator top-left */
.status {
    position: absolute;
    top: max(12px, env(safe-area-inset-top, 12px));
    left: max(12px, env(safe-area-inset-left, 12px));
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.9);
    pointer-events: none;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.status-dot.online {
    background: #4ade80;
    box-shadow: 0 0 6px #4ade80;
}

.status-dot.offline {
    background: #ef4444;
}

.status-dot.connecting {
    background: #fbbf24;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Click to start hint overlay */
.start-hint {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.4);
    color: #fff;
    font-size: 1.2rem;
    cursor: pointer;
    transition: opacity 0.3s;
    text-align: center;
    padding: 20px;
}

.start-hint.hidden {
    opacity: 0;
    pointer-events: none;
}

/* ==================== MOBILE ==================== */
@media (max-width: 640px) {
    .controls {
        width: calc(100% - 24px);
        max-width: none;
        bottom: max(12px, env(safe-area-inset-bottom, 12px));
        padding: 8px 10px;
        gap: 6px;
    }

    .controls input {
        width: auto;
        min-width: 0;
        font-size: 16px;
        padding: 10px 12px;
    }

    .btn {
        padding: 10px 14px;
        font-size: 15px;
    }

    .status {
        font-size: 0.8rem;
        padding: 5px 8px;
    }

    .start-hint {
        font-size: 1rem;
    }
}

/* ==================== SMALL PHONES ==================== */
@media (max-width: 360px) {
    .controls {
        width: calc(100% - 16px);
        bottom: max(8px, env(safe-area-inset-bottom, 8px));
        padding: 6px 8px;
    }

    .controls input {
        padding: 8px 10px;
    }

    .btn {
        padding: 8px 12px;
        font-size: 14px;
    }
}

/* Landscape phone */
@media (max-height: 400px) and (orientation: landscape) {
    .controls {
        bottom: max(8px, env(safe-area-inset-bottom, 8px));
        padding: 6px 10px;
    }

    .status {
        top: max(8px, env(safe-area-inset-top, 8px));
    }
}
