@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&display=swap');

:root {
    --bg: #0f172a;
    --panel: #1e293b;
    --card: #0f172a;
    --border: #334155;
    --muted: #94a3b8;
    --txt: #f1f5f9;
    --accent: #22c55e;
    --accent-hover: #16a34a;
    --red: #ef4444;
    --blue: #3b82f6;
    --blue-hover: #2563eb;
    --yellow: #facc15;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

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

body {
    margin: 0;
    font-family: 'Cairo', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--txt);
    line-height: 1.5;
    direction: rtl;
}

.layout {
    max-width: 1400px;
    margin: 0 auto;
    padding: 16px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* Header */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    background: var(--panel);
    border-radius: 16px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 20px;
    font-weight: 700;
    color: #fff;
}

.logo {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--accent), var(--blue));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    box-shadow: 0 2px 10px rgba(34, 197, 94, 0.2);
    flex-shrink: 0;
}

/* Stats Panel (Default: Desktop 4 columns) */
.stats-panel {
    display: grid;
    grid-template-columns: repeat(4, 1fr); /* Force 4 columns on large screens */
    gap: 16px;
}

.stat {
    background: var(--panel);
    padding: 20px;
    border-radius: 16px;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    position: relative;
    overflow: hidden;
    transition: transform 0.2s;
    min-height: 140px; /* Consistent height */
}

.stat:hover {
    transform: translateY(-2px);
}

.stat::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: var(--border);
}

.stat--time::before { background: var(--yellow); }
.stat--temp::before { background: var(--red); }
.stat--hum::before { background: var(--blue); }
.stat--qual::before { background: var(--accent); }

.stat .label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 16px;
    color: var(--muted);
    font-weight: 600;
}

.stat .icon {
    font-size: 24px;
}

.stat .value {
    font-size: 32px;
    font-weight: 800;
    color: var(--txt);
    font-variant-numeric: tabular-nums;
    direction: ltr; /* Keep numbers consistent */
}

/* Controls */
.controls {
    background: var(--panel);
    padding: 20px;
    border-radius: 16px;
    border: 1px solid var(--border);
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 16px;
    align-items: end;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

label {
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
}

select, input {
    width: 100%;
    height: 44px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0 12px;
    color: var(--txt);
    font-family: inherit;
    font-size: 14px;
    transition: border-color 0.2s;
}

select:focus, input:focus {
    outline: none;
    border-color: var(--blue);
}

button, .btn {
    height: 44px;
    border: none;
    border-radius: 10px;
    font-weight: 700;
    font-family: inherit;
    font-size: 14px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 20px;
    transition: filter 0.2s, transform 0.1s;
    text-decoration: none;
}

button:active, .btn:active {
    transform: scale(0.98);
}

#apply {
    background: var(--accent);
    color: #fff;
}

#apply:hover {
    background: var(--accent-hover);
}

.btn {
    background: var(--blue);
    color: #fff;
}

.btn:hover {
    background: var(--blue-hover);
}

/* Status Bar */
.status {
    padding: 12px 16px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 14px;
    display: none;
    animation: fadeIn 0.3s ease;
}

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

/* Cards Grid */
.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 16px;
}

.card {
    background: var(--panel);
    border-radius: 16px;
    border: 1px solid var(--border);
    padding: 20px;
}

.card h3 {
    margin: 0 0 16px 0;
    font-size: 18px;
    color: var(--txt);
}

.barn-sim {
    position: relative;
    height: 200px;
    background: var(--bg);
    border: 2px dashed var(--border);
    border-radius: 12px;
    margin-bottom: 16px;
}

.barn-sim .dot {
    position: absolute;
    width: 20px;
    height: 20px;
    background: var(--accent);
    border: 3px solid #fff;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    transition: all 0.3s ease;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
}

.card .actions {
    margin-top: 16px;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.card .actions button {
    flex: 1;
}

/* Table Section */
.table-section {
    background: var(--panel);
    border-radius: 16px;
    border: 1px solid var(--border);
    padding: 16px; 
    overflow: hidden;
}

.table-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0;
    padding-bottom: 8px; 
}

table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    min-width: 600px; 
}

th {
    background: var(--bg);
    color: var(--muted);
    font-weight: 600;
    font-size: 12px;
    text-align: center;
    padding: 12px 8px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

td {
    padding: 12px 8px;
    border-bottom: 1px solid var(--border);
    text-align: center;
    font-size: 13px;
    color: var(--txt);
    white-space: nowrap;
}

tbody tr:last-child td {
    border-bottom: none;
}

tbody tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

/* Pagination */
.pagination {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-top: 20px;
    flex-wrap: wrap;
}

.pagination .page {
    min-width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
}

.pagination .page:hover {
    border-color: var(--muted);
}

.pagination .page.active {
    background: var(--blue);
    border-color: var(--blue);
    color: #fff;
}

.pagination .page.disabled {
    opacity: 0.5;
    pointer-events: none;
}

/* Footer */
.footer {
    text-align: center;
    padding: 20px;
    color: var(--muted);
    font-size: 13px;
    margin-top: auto;
}

.footer a {
    color: var(--accent);
    text-decoration: none;
}

/* Floating Actions */
.whatsapp {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: #25D366;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    box-shadow: 0 4px 12px rgba(37, 211, 102, 0.4);
    text-decoration: none;
    transition: transform 0.2s;
    z-index: 999;
}

.chart-fab {
    position: fixed;
    bottom: 20px;
    left: 20px;
    height: 48px;
    padding: 0 20px;
    border-radius: 24px;
    background: var(--blue);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    text-decoration: none;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
    transition: transform 0.2s;
    z-index: 999;
}

.whatsapp:active, .chart-fab:active {
    transform: scale(0.95);
}

/* --- Mobile Optimizations --- */
@media (max-width: 768px) {
    .layout {
        padding: 12px;
        gap: 12px;
    }

    .header {
        padding: 12px;
        flex-direction: column;
        gap: 10px;
        text-align: center;
    }

    /* Optimized Grid for Mobile */
    .stats-panel {
        grid-template-columns: 1fr 1fr; /* 2 Columns */
        gap: 12px;
    }

    /* Order & Size adjustments */
    .stat--time {
        grid-column: 1 / -1; /* Time takes full width */
        order: 1;
    }

    .stat--temp {
        order: 2;
    }

    .stat--hum {
        order: 3;
    }

    .stat--qual {
        grid-column: 1 / -1; /* Quality takes full width */
        order: 4;
    }

    .stat {
        padding: 16px;
        min-height: 120px;
    }

    .stat .value {
        font-size: 28px;
    }
    
    .stat--time .value {
        font-size: 20px; /* Smaller time font */
    }

    /* Controls */
    .controls {
        grid-template-columns: 1fr 1fr;
        padding: 16px;
    }
    
    .controls button, .controls #export {
        grid-column: 1 / -1;
    }
    
    .field:has(#from), .field:has(#to) {
        grid-column: span 1;
    }

    .card .form-grid {
        grid-template-columns: 1fr;
    }
    
    .field[style*="grid-column"] {
        grid-column: 1 / -1 !important;
    }
}

@media (max-width: 480px) {
    .stats-panel {
        /* For very small screens, still keep 2 cols for Temp/Hum, but adjust text */
        gap: 8px;
    }

    .stat .value {
        font-size: 24px;
    }

    .controls {
        grid-template-columns: 1fr; 
    }
    
    .field:has(#from), .field:has(#to) {
        grid-column: 1;
    }

    .chart-fab span {
        display: none;
    }
    
    .chart-fab {
        width: 48px;
        height: 48px;
        padding: 0;
        justify-content: center;
        border-radius: 50%;
    }
    
    .chart-fab::before {
        content: '📊';
        font-size: 20px;
    }
}
