/* ========== الأساسيات ========== */
:root {
    --primary: #2563EB;
    --primary-dark: #1d4ed8;
    --accent: #14B8A6;
    --warning: #F59E0B;
    --danger: #EF4444;
    --bg: #f1f5f9;
    --surface: #ffffff;
    --text: #0f172a;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -2px rgba(0,0,0,0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -4px rgba(0,0,0,0.1);
    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --transition: all 0.2s ease;
    --bottom-nav-height: 64px;
    --topbar-height: 56px;
}

[data-theme="dark"] {
    --bg: #0f172a;
    --surface: #1e293b;
    --text: #f1f5f9;
    --text-secondary: #94a3b8;
    --border: #334155;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    padding-top: var(--topbar-height);
    padding-bottom: calc(var(--bottom-nav-height) + 20px);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
    transition: background 0.3s, color 0.3s;
}

/* ========== شريط علوي (Top App Bar) ========== */
.topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--topbar-height);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    z-index: 1000;
    box-shadow: var(--shadow-sm);
    backdrop-filter: blur(10px);
}

.topbar .page-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text);
}

.topbar .actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.topbar .icon-btn {
    background: none;
    border: none;
    font-size: 1.4rem;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text);
    cursor: pointer;
    transition: var(--transition);
    position: relative;
    text-decoration: none;
}

.topbar .icon-btn:hover {
    background: var(--border);
}

.topbar .badge {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 16px;
    height: 16px;
    background: var(--danger);
    border-radius: 50%;
    font-size: 10px;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ========== شريط سفلي (Bottom Navigation) ========== */
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--bottom-nav-height);
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-around;
    z-index: 1000;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
    backdrop-filter: blur(10px);
}

.bottom-nav a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    text-decoration: none;
    color: var(--text-secondary);
    font-size: 0.7rem;
    font-weight: 500;
    transition: var(--transition);
    flex: 1;
    height: 100%;
    position: relative;
}

.bottom-nav a .nav-icon {
    font-size: 1.4rem;
    transition: var(--transition);
}

.bottom-nav a.active {
    color: var(--primary);
}

.bottom-nav a.active .nav-icon {
    transform: translateY(-2px);
}

.bottom-nav a.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 24px;
    height: 3px;
    background: var(--primary);
    border-radius: 0 0 4px 4px;
}

/* زر الإضافة العائم (FAB) */
.fab {
    position: fixed;
    bottom: calc(var(--bottom-nav-height) + 16px);
    right: 16px;
    width: 48px;
    height: 48px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    z-index: 999;
    transition: var(--transition);
    text-decoration: none;
}

.fab:hover {
    background: var(--primary-dark);
    transform: scale(1.05);
}

/* ========== الحاويات والبطاقات ========== */
.container {
    max-width: 600px;
    margin: 0 auto;
    padding: 16px;
}

.card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    transition: var(--transition);
    margin-bottom: 12px;
}

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

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.stat-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 16px 12px;
    text-align: center;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}

.stat-card .stat-icon {
    font-size: 1.8rem;
    margin-bottom: 4px;
}

.stat-card .stat-value {
    font-size: 1.6rem;
    font-weight: 800;
    color: var(--text);
}

.stat-card .stat-label {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

/* ========== الأجهزة ========== */
.device-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 10px;
}

.device-card {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px 10px;
    text-align: center;
    cursor: pointer;
    transition: var(--transition);
    position: relative;
}

.device-card.on {
    border-color: var(--accent);
    background: linear-gradient(135deg, #f0fdfa 0%, #e6fffa 100%);
}

[data-theme="dark"] .device-card.on {
    background: linear-gradient(135deg, #134e4a 0%, #0f766e 100%);
    border-color: var(--accent);
}

.device-card .device-icon {
    font-size: 2.2rem;
    margin-bottom: 6px;
}

.device-card .device-name {
    font-weight: 600;
    font-size: 0.8rem;
    margin-bottom: 8px;
}

/* مفتاح التبديل */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

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

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #cbd5e1;
    border-radius: 24px;
    transition: var(--transition);
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 2px;
    bottom: 2px;
    background: white;
    border-radius: 50%;
    transition: var(--transition);
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

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

input:checked + .toggle-slider:before {
    transform: translateX(20px);
}

/* ========== الأزرار ========== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius);
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    white-space: nowrap;
}

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

.btn-primary {
    background: var(--primary);
    color: white;
}

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

.btn-danger {
    background: var(--danger);
    color: white;
}

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

.btn-sm {
    padding: 6px 12px;
    font-size: 0.75rem;
    border-radius: 8px;
}

.btn-block {
    width: 100%;
}

.btn-icon {
    width: 36px;
    height: 36px;
    padding: 0;
    border-radius: 50%;
}

/* ========== النماذج ========== */
.form-group {
    margin-bottom: 14px;
}

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

.form-control {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font-size: 0.9rem;
    transition: var(--transition);
    -webkit-appearance: none;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

select.form-control {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: left 12px center;
    padding-left: 32px;
}

/* ========== صفحة المصادقة ========== */
.auth-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
    padding-bottom: 0;
}

.auth-card {
    background: var(--surface);
    border-radius: var(--radius-xl);
    padding: 32px 24px;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 25px 50px rgba(0,0,0,0.3);
}

.auth-card h1 {
    text-align: center;
    margin-bottom: 24px;
    font-size: 1.5rem;
    font-weight: 800;
}

/* ========== التحكم الصوتي ========== */
.voice-area {
    background: linear-gradient(135deg, #667eea 0%, #14B8A6 100%);
    border-radius: var(--radius-xl);
    padding: 40px 20px;
    text-align: center;
    color: white;
}

.voice-btn {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 4px solid rgba(255,255,255,0.5);
    background: rgba(255,255,255,0.2);
    color: white;
    font-size: 2rem;
    cursor: pointer;
    transition: var(--transition);
    backdrop-filter: blur(10px);
}

.voice-btn:hover {
    background: rgba(255,255,255,0.3);
    transform: scale(1.05);
}

.voice-btn.listening {
    animation: pulse 1.5s infinite;
    border-color: var(--warning);
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(255,255,255,0.5); }
    70% { box-shadow: 0 0 0 20px rgba(255,255,255,0); }
    100% { box-shadow: 0 0 0 0 rgba(255,255,255,0); }
}

/* ========== الإشعارات (Toast) ========== */
.toast {
    position: fixed;
    top: 70px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 20px;
    border-radius: var(--radius);
    color: white;
    font-weight: 600;
    font-size: 0.85rem;
    z-index: 2000;
    animation: slideDown 0.3s ease;
    box-shadow: var(--shadow-lg);
    max-width: 90%;
    text-align: center;
}

@keyframes slideDown {
    from { transform: translateX(-50%) translateY(-100%); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ========== جداول ========== */
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.table th, .table td {
    padding: 10px 12px;
    text-align: right;
    border-bottom: 1px solid var(--border);
}

.table th {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
}

/* ========== أزرار الإدارة ========== */
.admin-links {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.admin-links .btn {
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 0.8rem;
}

.admin-links .btn.active {
    background: var(--primary);
    color: white;
}

/* ========== سكرول مخفي للجوال ========== */
::-webkit-scrollbar {
    width: 3px;
}
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

/* ========== Responsive ========== */
@media (min-width: 768px) {
    .container {
        padding: 24px;
    }
    .device-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    }
}