/* ============================================
   Grapes Dryer IoT - Main Stylesheet
   Clean, modern design for desktop & mobile
   ============================================ */

/* CSS Variables */
:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --secondary: #64748b;
    --success: #22c55e;
    --warning: #f59e0b;
    --danger: #ef4444;
    --dark: #1e293b;
    --light: #f8fafc;
    --white: #ffffff;
    --border: #e2e8f0;
    --shadow: rgba(0, 0, 0, 0.1);
    --radius: 12px;
    --radius-sm: 8px;
    --sidebar-width: 260px;
    --sidebar-collapsed: 70px;
}

/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--light);
    color: var(--dark);
    line-height: 1.6;
}

/* ============================================
   Login Page
   ============================================ */
.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
}

.login-box {
    background: var(--white);
    border-radius: var(--radius);
    padding: 40px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 20px 60px var(--shadow);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h1 {
    font-size: 28px;
    margin-bottom: 8px;
}

.login-header p {
    color: var(--secondary);
    font-size: 14px;
}

.login-form .form-group {
    margin-bottom: 20px;
}

.login-form label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    font-size: 14px;
    color: var(--secondary);
}

.login-form input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 15px;
    transition: border-color 0.2s;
}

.login-form input:focus {
    outline: none;
    border-color: var(--primary);
}

/* ============================================
   Dashboard Layout
   ============================================ */
.dashboard-container {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--dark);
    color: var(--white);
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transition: width 0.3s ease;
    z-index: 100;
}

.sidebar.collapsed {
    width: var(--sidebar-collapsed);
}

.sidebar.collapsed .sidebar-header h2,
.sidebar.collapsed .sidebar-nav span {
    display: none;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-header h2 {
    font-size: 18px;
}

.sidebar-toggle {
    background: none;
    border: none;
    color: var(--white);
    font-size: 20px;
    cursor: pointer;
    display: none;
}

.sidebar-nav {
    flex: 1;
    padding: 20px 0;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    color: rgba(255,255,255,0.7);
    text-decoration: none;
    transition: all 0.2s;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
    background: rgba(255,255,255,0.1);
    color: var(--white);
}

.sidebar-nav a.active {
    border-left: 3px solid var(--primary);
}

.sidebar-footer {
    padding: 20px;
    border-top: 1px solid rgba(255,255,255,0.1);
}

.user-info {
    margin-bottom: 12px;
    color: rgba(255,255,255,0.7);
    font-size: 14px;
}

/* Main Content */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    transition: margin-left 0.3s ease;
}

.sidebar.collapsed + .main-content {
    margin-left: var(--sidebar-collapsed);
}

.top-bar {
    background: var(--white);
    padding: 20px 30px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 50;
}

.top-bar h1 {
    font-size: 24px;
    font-weight: 600;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 15px;
}

/* ============================================
   Stats Cards
   ============================================ */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    padding: 30px;
}

.stat-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: 0 2px 10px var(--shadow);
}

.stat-icon {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
}

.stat-icon.users { background: #dbeafe; }
.stat-icon.devices { background: #fef3c7; }
.stat-icon.online { background: #dcfce7; }
.stat-icon.unassigned { background: #fee2e2; }

.stat-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--dark);
}

.stat-label {
    color: var(--secondary);
    font-size: 14px;
}

/* ============================================
   Content Cards
   ============================================ */
.content-card {
    background: var(--white);
    border-radius: var(--radius);
    margin: 0 30px 30px;
    box-shadow: 0 2px 10px var(--shadow);
    overflow: hidden;
}

.card-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.card-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.card-body {
    padding: 24px;
}

.content-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 30px;
    padding: 0 30px 30px;
}

/* ============================================
   Device Cards
   ============================================ */
.devices-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
    padding: 30px;
}

.device-card {
    background: var(--white);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: 0 2px 10px var(--shadow);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.device-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 25px var(--shadow);
}

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

.device-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.status {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
}

.status.online {
    background: #dcfce7;
    color: #16a34a;
}

.status.offline {
    background: #fee2e2;
    color: #dc2626;
}

.device-info {
    margin-bottom: 16px;
}

.device-info p {
    margin: 4px 0;
    font-size: 14px;
    color: var(--secondary);
}

.device-config {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.config-item {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
}

.config-item .value {
    font-weight: 600;
    color: var(--primary);
}

/* ============================================
   Tables
   ============================================ */
.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th,
.data-table td {
    padding: 14px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.data-table th {
    background: var(--light);
    font-weight: 600;
    font-size: 13px;
    text-transform: uppercase;
    color: var(--secondary);
}

.data-table tr:hover {
    background: var(--light);
}

.data-table tr:last-child td {
    border-bottom: none;
}

.unassigned {
    color: var(--secondary);
    font-style: italic;
}

/* ============================================
   Buttons
   ============================================ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
    gap: 8px;
}

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

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

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

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

.btn-outline:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.btn-block {
    width: 100%;
}

.btn-sm {
    padding: 6px 14px;
    font-size: 13px;
}

.btn-link {
    color: var(--primary);
    text-decoration: none;
    font-weight: 500;
}

.btn-link:hover {
    text-decoration: underline;
}

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

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    font-size: 14px;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary);
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.mt-3 {
    margin-top: 16px;
}

.text-center {
    text-align: center;
}

/* ============================================
   Modal
   ============================================ */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}

.modal.show {
    display: flex;
}

.modal-content {
    background: var(--white);
    border-radius: var(--radius);
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--secondary);
}

.modal-body {
    padding: 24px;
}

/* Modal dialog wrapper */
.modal-dialog {
    width: 100%;
    max-width: 500px;
}

/* Show modal helper */
.modal[style*="flex"] {
    display: flex !important;
}

/* Device Details Modal */
.device-details {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.detail-row .label {
    font-weight: 500;
    color: var(--secondary);
}

.sensor-readings {
    text-align: center;
}

.sensor-readings h3 {
    font-size: 16px;
    margin-bottom: 16px;
}

.readings-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.reading-card {
    padding: 20px;
    border-radius: var(--radius);
    text-align: center;
}

.reading-card.temp {
    background: linear-gradient(135deg, #f97316, #ef4444);
    color: var(--white);
}

.reading-card.humidity {
    background: linear-gradient(135deg, #3b82f6, #06b6d4);
    color: var(--white);
}

.reading-label {
    font-size: 13px;
    opacity: 0.9;
}

.reading-value {
    font-size: 36px;
    font-weight: 700;
}

.last-update {
    margin-top: 12px;
    font-size: 13px;
    color: var(--secondary);
}

.control-panel {
    margin-top: 20px;
}

.control-panel h3 {
    font-size: 16px;
    margin-bottom: 16px;
}

/* ============================================
   Alert
   ============================================ */
.alert {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    margin-top: 16px;
    font-size: 14px;
}

.alert-error {
    background: #fee2e2;
    color: #dc2626;
}

/* ============================================
   Loading & Empty States
   ============================================ */
.loading {
    text-align: center;
    padding: 60px;
    color: var(--secondary);
}

.empty-state {
    text-align: center;
    padding: 60px;
    color: var(--secondary);
    grid-column: 1 / -1;
}

/* ============================================
   Action Buttons
   ============================================ */
.action-buttons {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

/* ============================================
   API Docs
   ============================================ */
.docs-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 40px 20px;
}

.docs-header {
    background: var(--white);
    padding: 40px;
    border-radius: var(--radius);
    margin-bottom: 30px;
    box-shadow: 0 2px 10px var(--shadow);
}

.docs-header h1 {
    font-size: 28px;
    margin-bottom: 8px;
}

.docs-header p {
    color: var(--secondary);
}

.doc-section {
    background: var(--white);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 2px 10px var(--shadow);
}

.doc-section h2 {
    font-size: 20px;
    margin-bottom: 16px;
}

.api-endpoint {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 20px;
    overflow: hidden;
}

.endpoint-header {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.endpoint-header.post {
    background: #fef3c7;
}

.endpoint-header.get {
    background: #dcfce7;
}

.method {
    padding: 4px 12px;
    border-radius: 4px;
    font-weight: 700;
    font-size: 12px;
}

.endpoint-header.post .method {
    background: #f59e0b;
    color: var(--white);
}

.endpoint-header.get .method {
    background: #22c55e;
    color: var(--white);
}

.path {
    font-family: monospace;
    font-size: 14px;
}

.request-body,
.response-body {
    padding: 16px;
    background: var(--light);
    border-top: 1px solid var(--border);
}

.request-body h4,
.response-body h4 {
    font-size: 13px;
    margin-bottom: 8px;
    color: var(--secondary);
}

pre {
    background: var(--dark);
    color: #a5b4fc;
    padding: 16px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    font-size: 13px;
    line-height: 1.5;
}

code {
    font-family: 'Consolas', 'Monaco', monospace;
}

.response-table {
    width: 100%;
    border-collapse: collapse;
}

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

.response-table th {
    background: var(--light);
    font-weight: 600;
}

/* ============================================
   Common Styles
   ============================================ */
a {
    color: var(--primary);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

h1, h2, h3, h4, h5, h6 {
    color: var(--dark);
    font-weight: 600;
}

p {
    color: var(--secondary);
}

/* Table common */
.table {
    width: 100%;
    border-collapse: collapse;
}

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

.table th {
    font-size: 12px;
    text-transform: uppercase;
    color: var(--secondary);
    font-weight: 600;
    background: var(--light);
}

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

.table tr:hover {
    background: var(--light);
}

/* Modal dialog */
.modal-dialog {
    width: 100%;
    max-width: 500px;
    padding: 20px;
}

/* Card */
.card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    overflow: hidden;
}

.card-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--dark);
    margin: 0;
}

/* Stat number & label */
.stat-number {
    font-size: 28px;
    font-weight: 700;
    color: var(--dark);
    display: block;
}

.stat-label {
    font-size: 13px;
    color: var(--secondary);
}

/* Content grid */
.content-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        width: var(--sidebar-width);
    }

    .sidebar.active {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0;
    }

    .sidebar-toggle {
        display: block;
    }

    .stats-grid {
        grid-template-columns: 1fr;
        padding: 20px;
    }

    .devices-grid {
        grid-template-columns: 1fr;
        padding: 20px;
    }

    .content-card {
        margin: 0 20px 20px;
    }

    .content-row {
        grid-template-columns: 1fr;
        padding: 0 20px 20px;
    }

    .top-bar {
        padding: 15px 20px;
    }

    .top-bar h1 {
        font-size: 20px;
    }

    .data-table {
        font-size: 13px;
    }

    .data-table th,
    .data-table td {
        padding: 10px 12px;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    .readings-grid {
        grid-template-columns: 1fr;
    }

    .modal-content {
        margin: 20px;
    }
}

@media (max-width: 480px) {
    .login-box {
        padding: 30px 20px;
    }

    .device-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .device-config {
        grid-template-columns: 1fr;
    }

    .header-actions {
        flex-direction: column;
        gap: 10px;
    }
}
