/**
 * SmartPID PWA - Modern Dark Theme
 * Features: Glassmorphism, Micro-animations, Vibrant colors
 */

/* ============================================================================
   CSS Variables (Design System)
   ============================================================================ */

:root {
    /* Colors - Dark Theme */
    --bg-primary: #0f0f1e;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #16213e;
    --bg-card: rgba(26, 26, 46, 0.7);
    --bg-card-hover: rgba(26, 26, 46, 0.85);

    /* Glassmorphism */
    --glass-bg: rgba(255, 255, 255, 0.05);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);

    /* Accent Colors */
    --accent-primary: #00d4ff;
    --accent-secondary: #667eea;
    --accent-success: #4ade80;
    --accent-warning: #fbbf24;
    --accent-error: #f87171;

    /* Text Colors */
    --text-primary: #ffffff;
    --text-secondary: #e0e0e0;
    --text-tertiary: #a0a0a0;
    --text-muted: #666;

    /* Gradients */
    --gradient-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --gradient-accent: linear-gradient(135deg, #00d4ff 0%, #0099ff 100%);
    --gradient-success: linear-gradient(135deg, #4ade80 0%, #22c55e 100%);
    --gradient-warning: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
    --gradient-error: linear-gradient(135deg, #f87171 0%, #ef4444 100%);

    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;

    /* Border Radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --radius-pill: 999px;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 300ms ease;
    --transition-slow: 500ms ease;

    /* Shadows */
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.3);
    --shadow-glow: 0 0 20px rgba(0, 212, 255, 0.3);
}

/* ============================================================================
   Global Styles
   ============================================================================ */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow-x: hidden;
    min-height: 100vh;

    /* Animated gradient background */
    background: linear-gradient(135deg, #0f0f1e 0%, #1a1a2e 50%, #16213e 100%);
    background-size: 200% 200%;
    animation: gradientShift 15s ease infinite;
}

@keyframes gradientShift {

    0%,
    100% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }
}

/* ============================================================================
   Typography
   ============================================================================ */

h1,
h2,
h3,
h4,
h5,
h6 {
    font-weight: 700;
    line-height: 1.2;
    margin-bottom: var(--spacing-md);
}

h1 {
    font-size: 2.5rem;
}

h2 {
    font-size: 2rem;
}

h3 {
    font-size: 1.5rem;
}

h4 {
    font-size: 1.25rem;
}

/* ============================================================================
   Header
   ============================================================================ */

.header {
    padding: var(--spacing-lg) var(--spacing-md);
    background: var(--glass-bg);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--glass-border);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-md);
}

.header-content {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--spacing-md);
}

.logo {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
}

.logo-icon {
    width: 40px;
    height: 40px;
    background: var(--gradient-primary);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    box-shadow: var(--shadow-glow);
}

.logo-text h1 {
    font-size: 1.5rem;
    margin: 0;
    background: var(--gradient-accent);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.logo-text p {
    font-size: 0.75rem;
    color: var(--text-tertiary);
    margin: 0;
}

.connection-info {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
}

.status-indicator {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    position: relative;
}

.status-indicator.connected {
    background: var(--accent-success);
    box-shadow: 0 0 10px var(--accent-success);
    animation: pulse 2s ease infinite;
}

.status-indicator.disconnected {
    background: var(--text-muted);
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

#device-name {
    font-weight: 600;
    color: var(--text-secondary);
}

/* ============================================================================
   Buttons
   ============================================================================ */

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-normal);
    display: inline-flex;
    align-items: center;
    gap: var(--spacing-sm);
    text-decoration: none;
    font-family: inherit;
}

.btn-primary {
    background: var(--gradient-primary);
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn-secondary {
    background: var(--glass-bg);
    color: var(--text-primary);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
}

.btn-secondary:hover {
    background: var(--glass-border);
    border-color: var(--accent-primary);
}

.btn-success {
    background: var(--gradient-success);
    color: white;
}

.btn-warning {
    background: var(--gradient-warning);
    color: white;
}

.btn-error {
    background: var(--gradient-error);
    color: white;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none !important;
}

.btn:active {
    transform: scale(0.98);
}

/* ============================================================================
   Container & Layout
   ============================================================================ */

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: var(--spacing-lg);
}

.grid {
    display: grid;
    gap: var(--spacing-lg);
}

.grid-2 {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}

.grid-3 {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

/* ============================================================================
   Cards
   ============================================================================ */

.card {
    background: var(--bg-card);
    backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    padding: var(--spacing-lg);
    box-shadow: var(--shadow-md);
    transition: all var(--transition-normal);
}

.card:hover {
    background: var(--bg-card-hover);
    border-color: var(--accent-primary);
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--spacing-lg);
}

.card-title {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.card-body {
    color: var(--text-secondary);
}

/* ============================================================================
   Dashboard Cards
   ============================================================================ */

.status-card {
    text-align: center;
}

.status-value {
    font-size: 3rem;
    font-weight: 700;
    background: var(--gradient-accent);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin: var(--spacing-md) 0;
    animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.status-label {
    font-size: 0.875rem;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 600;
}

.status-unit {
    font-size: 1.5rem;
    color: var(--text-tertiary);
}

/* ============================================================================
   Chart Container
   ============================================================================ */

.chart-container {
    position: relative;
    height: 300px;
    margin-top: var(--spacing-md);
}

#temperature-chart {
    width: 100% !important;
    height: 100% !important;
}

/* ============================================================================
   Power Gauge
   ============================================================================ */

.power-gauge-container {
    margin-top: var(--spacing-md);
}

.gauge {
    width: 100%;
    height: 40px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: var(--radius-pill);
    overflow: hidden;
    position: relative;
    border: 1px solid var(--glass-border);
}

.gauge-fill {
    height: 100%;
    background: var(--gradient-success);
    transition: width var(--transition-normal);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: var(--spacing-md);
    color: white;
    font-weight: 700;
}

#power-output {
    position: absolute;
    top: 50%;
    right: var(--spacing-md);
    transform: translateY(-50%);
    color: white;
    font-weight: 700;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

/* ============================================================================
   FSM State Badge
   ============================================================================ */

.fsm-badge {
    display: inline-block;
    padding: 8px 16px;
    border-radius: var(--radius-pill);
    font-weight: 700;
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.fsm-badge.idle {
    background: rgba(160, 160, 160, 0.2);
    color: var(--text-tertiary);
}

.fsm-badge.running {
    background: var(--gradient-success);
    color: white;
    animation: pulse 2s ease infinite;
}

.fsm-badge.paused {
    background: var(--gradient-warning);
    color: white;
}

/* ============================================================================
   Control Panel
   ============================================================================ */

.control-panel {
    transition: opacity var(--transition-normal);
}

.control-panel.disabled {
    opacity: 0.5;
    pointer-events: none;
}

.control-buttons {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: var(--spacing-md);
    margin-top: var(--spacing-lg);
}

/* ============================================================================
   Form Elements
   ============================================================================ */

.form-group {
    margin-bottom: var(--spacing-lg);
}

.form-label {
    display: block;
    font-weight: 600;
    margin-bottom: var(--spacing-sm);
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.form-input,
.form-select {
    width: 100%;
    padding: 12px 16px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    transition: all var(--transition-fast);
}

.form-input:focus,
.form-select:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(0, 212, 255, 0.1);
}

.form-input::placeholder {
    color: var(--text-muted);
}

/* Fix dropdown option visibility */
.form-select option {
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 12px;
}

/* Input with unit */
.input-group {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
}

.input-unit {
    color: var(--text-tertiary);
    font-weight: 600;
}

/* ============================================================================
   Config Panel
   ============================================================================ */

.config-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--spacing-md);
}

/* ============================================================================
   Toast Notifications
   ============================================================================ */

.toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    padding: 16px 24px;
    background: var(--bg-card);
    backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    color: var(--text-primary);
    font-weight: 600;
    z-index: 1000;
    opacity: 0;
    transform: translateX(400px);
    transition: all var(--transition-normal);
}

.toast.show {
    opacity: 1;
    transform: translateX(0);
}

.toast-success {
    border-left: 4px solid var(--accent-success);
}

.toast-warning {
    border-left: 4px solid var(--accent-warning);
}

.toast-error {
    border-left: 4px solid var(--accent-error);
}

.toast-info {
    border-left: 4px solid var(--accent-primary);
}

/* ============================================================================
   Error Banner
   ============================================================================ */

.error-banner {
    background: var(--gradient-error);
    color: white;
    padding: var(--spacing-md) var(--spacing-lg);
    text-align: center;
    font-weight: 600;
}

/* ============================================================================
   Install Button (PWA)
   ============================================================================ */

#install-btn {
    position: fixed;
    bottom: 20px;
    left: 20px;
    background: var(--gradient-primary);
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: var(--radius-pill);
    font-size: 1rem;
    font-weight: 600;
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    display: none;
    align-items: center;
    gap: var(--spacing-sm);
    z-index: 1000;
    transition: all var(--transition-normal);
}

#install-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

/* ============================================================================
   Responsive Design
   ============================================================================ */

@media (max-width: 768px) {
    .container {
        padding: var(--spacing-md);
    }

    .header {
        padding: var(--spacing-md);
    }

    .logo-text h1 {
        font-size: 1.25rem;
    }

    .status-value {
        font-size: 2.5rem;
    }

    .chart-container {
        height: 250px;
    }

    .control-buttons {
        grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    }

    .grid-2,
    .grid-3 {
        grid-template-columns: 1fr;
    }

    #install-btn {
        bottom: 10px;
        left: 10px;
        padding: 10px 20px;
        font-size: 0.875rem;
    }
}

@media (max-width: 480px) {
    h1 {
        font-size: 2rem;
    }

    h2 {
        font-size: 1.5rem;
    }

    .btn {
        padding: 10px 20px;
        font-size: 0.875rem;
    }

    .status-value {
        font-size: 2rem;
    }
}

/* ============================================================================
   Animations
   ============================================================================ */

@keyframes slideInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.animate-in {
    animation: slideInUp 0.5s ease;
}

/* ============================================================================
   Utility Classes
   ============================================================================ */

.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

.mb-sm {
    margin-bottom: var(--spacing-sm);
}

.mb-md {
    margin-bottom: var(--spacing-md);
}

.mb-lg {
    margin-bottom: var(--spacing-lg);
}

.mt-sm {
    margin-top: var(--spacing-sm);
}

.mt-md {
    margin-top: var(--spacing-md);
}

.mt-lg {
    margin-top: var(--spacing-lg);
}

/* ============================================================================
   PWA Install Button
   ============================================================================ */
#install-btn {
    display: none;
    /* Hidden by default, shown via JS */
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2000;
    padding: 12px 24px;
    background: var(--gradient-primary);
    color: #fff;
    border: none;
    border-radius: 50px;
    font-weight: 600;
    font-size: 1rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    animation: slideUp 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#install-btn:active {
    transform: translateX(-50%) scale(0.95);
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translate(-50%, 20px);
    }

    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

/* ============================================================================
   MOBILE RESPONSIVE OPTIMIZATIONS
   ============================================================================ */
@media (max-width: 768px) {

    /* --- GLOBAL SPACING --- */
    :root {
        --spacing-lg: 16px;
        --spacing-md: 12px;
        --radius-lg: 12px;
    }

    .container {
        padding: 12px;
        padding-bottom: 80px;
        /* Space for fixed Install button */
    }

    /* --- COMPACT HEADER --- */
    .header {
        padding: 8px 16px;
    }

    .header-content {
        flex-wrap: nowrap;
        gap: 8px;
    }

    .logo-img {
        width: 32px !important;
        height: 32px !important;
        margin-right: 6px !important;
    }

    .header h1 {
        font-size: 1.1rem;
        margin: 0;
        line-height: 1.2;
    }

    .header p {
        display: none;
        /* Hide subtitle to save vertical space */
    }

    .connection-info {
        padding: 0;
        background: transparent;
        border: none;
    }

    #device-name {
        display: none;
        /* Hide device text, keep status dot */
    }

    #connect-btn,
    #disconnect-btn {
        padding: 6px 12px;
        font-size: 0.8rem;
        min-width: auto;
    }

    /* --- HORIZONTAL KPI ROW (Status Cards) --- */
    /* Override grid to force 3 columns instead of stacking */
    .grid-3 {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr !important;
        gap: 8px;
    }

    .status-card {
        padding: 12px 4px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-height: 80px;
    }

    .status-card .status-label {
        font-size: 0.65rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 4px;
        opacity: 0.7;
    }

    .status-card .status-value {
        font-size: 1.1rem;
        font-weight: 700;
        display: flex;
        align-items: baseline;
        gap: 2px;
    }

    .status-unit {
        font-size: 0.7rem;
    }

    #fsm-state {
        font-size: 0.65rem;
        padding: 4px 6px;
    }

    /* --- COMPACT GAUGE --- */
    .power-gauge-container {
        min-height: 24px;
        height: 24px;
        margin-top: 4px;
    }

    #power-gauge {
        height: 10px;
        border-radius: 5px;
    }

    #power-gauge .gauge-fill {
        border-radius: 5px;
    }

    #power-output {
        font-size: 0.75rem;
        top: -18px;
        /* Move label above */
    }

    /* Compact header for Gauge on mobile instead of hiding */
    section:has(#power-gauge) .card-header {
        display: flex;
        padding: 4px 12px;
        min-height: auto;
    }

    section:has(#power-gauge) .card-title {
        font-size: 0.8rem;
        margin: 0;
    }

    /* --- RESIZABLE CHART --- */
    .chart-container {
        height: 180px !important;
        /* Force shorter height (was 220px) */
    }

    /* --- COMPACT CONTROLS --- */
    /* SetPoint Input Row */
    #control-panel .form-group .input-group {
        display: flex;
    }

    #setpoint-input {
        flex: 1;
        font-size: 1.1rem;
    }

    /* Buttons Grid 2x2 */
    .control-buttons {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .control-buttons button {
        width: 100%;
        margin: 0;
        padding: 12px;
        font-size: 0.9rem;
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 6px;
    }
}

/* ============================================================================
   Profile Editor Table
   ============================================================================ */

.profile-table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.profile-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

.profile-table th {
    padding: var(--spacing-sm) var(--spacing-md);
    text-align: left;
    color: var(--text-tertiary);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid var(--glass-border);
}

.profile-table td {
    padding: var(--spacing-xs) var(--spacing-sm);
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    color: var(--text-secondary);
    vertical-align: middle;
}

.profile-table td:first-child {
    color: var(--text-tertiary);
    font-weight: 600;
    width: 40px;
    text-align: center;
}

.profile-input {
    width: 100%;
    padding: 6px 8px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.875rem;
    transition: border-color var(--transition-fast);
    -moz-appearance: textfield;
}

.profile-input::-webkit-outer-spin-button,
.profile-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.profile-input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.15);
}

.profile-table tr:hover {
    background: rgba(255, 255, 255, 0.02);
}