/* ============================================
   Philips Hue Controller - Styles
   Mobile-first, dark-mode optimized
   ============================================ */

:root {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-card: #22223a;
    --bg-input: #2a2a45;
    --text-primary: #f0f0f5;
    --text-secondary: #a0a0b8;
    --text-muted: #6a6a82;
    --accent: #4a9eff;
    --accent-glow: rgba(74, 158, 255, 0.3);
    --success: #4ade80;
    --warning: #fbbf24;
    --danger: #f87171;
    --border: #333355;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    --radius: 16px;
    --radius-sm: 10px;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Light mode */
body.light-mode {
    --bg-primary: #f0f2f5;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-input: #f0f0f5;
    --text-primary: #1a1a2e;
    --text-secondary: #555570;
    --text-muted: #8888a0;
    --accent: #2979ff;
    --accent-glow: rgba(41, 121, 255, 0.2);
    --border: #dddde8;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

/* Reset */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    transition: var(--transition);
    -webkit-font-smoothing: antialiased;
}

/* ============================================
   Loading Overlay
   ============================================ */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 15, 26, 0.92);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    backdrop-filter: blur(8px);
    transition: opacity 0.3s;
}

.loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

#loading-text {
    margin-top: 16px;
    color: var(--text-secondary);
    font-size: 0.95rem;
}

/* ============================================
   Toast
   ============================================ */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--bg-card);
    color: var(--text-primary);
    padding: 14px 24px;
    border-radius: 12px;
    font-size: 0.9rem;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    z-index: 9998;
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    max-width: 90vw;
    text-align: center;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
}

.toast.error { border-left: 4px solid var(--danger); }
.toast.success { border-left: 4px solid var(--success); }
.toast.info { border-left: 4px solid var(--accent); }

/* ============================================
   Screens
   ============================================ */
.screen {
    display: none;
    animation: fadeIn 0.4s ease;
}

.screen.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ============================================
   Connection Screen
   ============================================ */
#connection-screen {
    padding: 24px 16px;
    max-width: 480px;
    margin: 0 auto;
}

.logo-container {
    text-align: center;
    padding: 32px 0 24px;
}

.logo-icon {
    font-size: 4rem;
    margin-bottom: 12px;
    animation: pulse 2s ease-in-out infinite;
}

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

.logo-container h1 {
    font-size: 1.8rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent), #a855f7);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: var(--text-secondary);
    margin-top: 4px;
    font-size: 0.95rem;
}

/* ============================================
   Cards
   ============================================ */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
    transition: var(--transition);
}

.info-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}

.info-card h3 {
    font-size: 1.05rem;
    margin-bottom: 12px;
    color: var(--accent);
}

.info-card ol {
    padding-left: 20px;
    color: var(--text-secondary);
    font-size: 0.9rem;
    line-height: 1.8;
}

.cors-notice {
    background: rgba(248, 113, 113, 0.1);
    border: 1px solid rgba(248, 113, 113, 0.3);
    border-radius: var(--radius-sm);
    padding: 16px;
    margin-bottom: 24px;
}

.cors-notice h4 {
    color: var(--danger);
    font-size: 0.95rem;
    margin-bottom: 6px;
}

.cors-notice p {
    color: var(--text-secondary);
    font-size: 0.85rem;
    line-height: 1.5;
}

/* ============================================
   Status Bar
   ============================================ */
.status-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 0.9rem;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
}

.status-dot.disconnected {
    background: var(--danger);
    box-shadow: 0 0 8px rgba(248, 113, 113, 0.5);
}

.status-dot.connected {
    background: var(--success);
    box-shadow: 0 0 8px rgba(74, 222, 128, 0.5);
}

.status-dot.connecting {
    background: var(--warning);
    box-shadow: 0 0 8px rgba(251, 191, 36, 0.5);
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

/* ============================================
   Forms
   ============================================ */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 6px;
    font-weight: 500;
}

input[type="text"],
select {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 1rem;
    transition: var(--transition);
    -webkit-appearance: none;
}

input[type="text"]:focus,
select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

input[readonly] {
    opacity: 0.7;
    cursor: not-allowed;
}

/* ============================================
   Buttons
   ============================================ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    -webkit-user-select: none;
    user-select: none;
    min-height: 48px; /* Touch-friendly */
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--accent);
    color: white;
    box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-primary:hover {
    box-shadow: 0 6px 20px var(--accent-glow);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-secondary {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-accent {
    background: linear-gradient(135deg, var(--accent), #7c3aed);
    color: white;
    box-shadow: 0 4px 16px var(--accent-glow);
}

.btn-full {
    width: 100%;
}

.btn-small {
    padding: 10px 16px;
    font-size: 0.85rem;
    min-height: 40px;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
}

/* ============================================
   App Header
   ============================================ */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-left h2 {
    font-size: 1.15rem;
    font-weight: 700;
}

.connection-badge {
    font-size: 0.75rem;
    color: var(--success);
    display: flex;
    align-items: center;
    gap: 4px;
}

.header-right {
    display: flex;
    gap: 4px;
}

.icon-btn {
    width: 42px;
    height: 42px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 1.1rem;
    cursor: pointer;
    transition: var(--transition);
}

.icon-btn:active {
    transform: scale(0.92);
    background: var(--accent);
}

/* ============================================
   Tab Navigation
   ============================================ */
.tab-nav {
    display: flex;
    gap: 4px;
    padding: 8px 16px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.tab-btn {
    flex: 1;
    padding: 10px 16px;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
    min-height: 42px;
}

.tab-btn.active {
    background: var(--accent);
    color: white;
}

.tab-btn:not(.active):active {
    background: var(--bg-input);
}

/* ============================================
   Tab Content
   ============================================ */
.tab-content {
    display: none;
    padding: 16px;
    animation: fadeIn 0.3s ease;
    padding-bottom: 100px;
}

.tab-content.active {
    display: block;
}

/* ============================================
   Controls Bar
   ============================================ */
.controls-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.controls-bar .btn {
    flex: 1;
}

/* ============================================
   Light/Group Cards
   ============================================ */
.cards-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
}

@media (min-width: 480px) {
    .cards-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 768px) {
    .cards-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

.light-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    transition: var(--transition);
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.light-card:active {
    transform: scale(0.98);
}

.light-card.on {
    border-color: var(--accent);
    box-shadow: 0 0 20px var(--accent-glow);
}

.light-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--accent);
    opacity: 0;
    transition: var(--transition);
}

.light-card.on::before {
    opacity: 1;
}

.light-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.light-name {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 70%;
}

.light-status {
    font-size: 0.75rem;
    padding: 4px 10px;
    border-radius: 20px;
    font-weight: 600;
}

.light-status.on {
    background: rgba(74, 222, 128, 0.15);
    color: var(--success);
}

.light-status.off {
    background: rgba(248, 113, 113, 0.15);
    color: var(--danger);
}

.light-preview {
    width: 100%;
    height: 48px;
    border-radius: 8px;
    margin-bottom: 12px;
    background: #333;
    transition: background 0.5s ease;
}

.light-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.light-controls input[type="range"] {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-input);
    border-radius: 3px;
    outline: none;
}

.light-controls input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 2px 8px var(--accent-glow);
}

.brightness-val {
    font-size: 0.8rem;
    color: var(--text-secondary);
    min-width: 36px;
    text-align: right;
}

/* Quick toggle button on card */
.quick-toggle {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid var(--border);
    background: var(--bg-input);
    color: var(--text-secondary);
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
}

.quick-toggle.on {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
}

/* ============================================
   Modal
   ============================================ */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 500;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    backdrop-filter: blur(4px);
    animation: fadeIn 0.2s ease;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: var(--radius) var(--radius) 0 0;
    width: 100%;
    max-width: 500px;
    max-height: 85vh;
    overflow-y: auto;
    animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg-secondary);
    z-index: 1;
}

.modal-header h3 {
    font-size: 1.1rem;
}

.close-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-input);
    border: none;
    border-radius: 50%;
    color: var(--text-primary);
    font-size: 1rem;
    cursor: pointer;
}

.modal-body {
    padding: 20px;
}

.control-section {
    margin-bottom: 24px;
}

.control-section label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 10px;
    font-weight: 500;
}

/* Power Button */
.power-btn {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 3px solid var(--border);
    background: var(--bg-input);
    color: var(--text-muted);
    font-size: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    cursor: pointer;
    transition: var(--transition);
}

.power-btn.on {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
    box-shadow: 0 0 30px var(--accent-glow);
}

/* Color Picker */
.color-picker-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

input[type="color"] {
    width: 100%;
    height: 50px;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    background: transparent;
    padding: 0;
}

input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 2px;
}

input[type="color"]::-webkit-color-swatch {
    border: 2px solid var(--border);
    border-radius: 8px;
}

.preset-colors {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.color-preset {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 2px solid var(--border);
    cursor: pointer;
    transition: var(--transition);
}

.color-preset:active {
    transform: scale(0.9);
}

/* Slider group */
.slider-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.slider-group input[type="range"] {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-input);
    border-radius: 3px;
    outline: none;
}

.slider-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 2px 8px var(--accent-glow);
}

.slider-group span {
    font-size: 0.8rem;
    color: var(--text-muted);
    min-width: 30px;
}

.value-label {
    display: block;
    text-align: center;
    font-size: 0.9rem;
    color: var(--accent);
    margin-top: 6px;
    font-weight: 600;
}

/* ============================================
   Automation
   ============================================ */
.automation-card h3 {
    font-size: 1.1rem;
    margin-bottom: 4px;
}

.card-subtitle {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 16px;
}

.timers-list {
    margin-top: 20px;
}

.timer-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    margin-bottom: 8px;
    transition: var(--transition);
}

.timer-item.expired {
    opacity: 0.5;
    border-color: var(--danger);
}

.timer-info {
    flex: 1;
}

.timer-target-name {
    font-weight: 600;
    font-size: 0.9rem;
}

.timer-countdown {
    font-size: 0.8rem;
    color: var(--accent);
    margin-top: 2px;
}

.timer-cancel-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: rgba(248, 113, 113, 0.15);
    color: var(--danger);
    font-size: 1rem;
    cursor: pointer;
    transition: var(--transition);
}

/* ============================================
   Empty State
   ============================================ */
.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
}

.empty-state .icon {
    font-size: 3rem;
    margin-bottom: 12px;
}

.empty-state p {
    font-size: 0.95rem;
}

/* ============================================
   Scrollbar
   ============================================ */
::-webkit-scrollbar {
    width: 4px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

/* ============================================
   Safe area for notched phones
   ============================================ */
@supports (padding: max(0px)) {
    .app-header {
        padding-top: max(12px, env(safe-area-inset-top));
    }
    .modal-body {
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
}
