:root {
    /* Light theme */
    --bg-color: #eef2f8;
    --bg: linear-gradient(180deg, #f8fbff 0%, #eef2f8 100%);
    --card: rgba(255, 255, 255, 0.55);
    --muted: #6b7280;
    --accent: #3b82f6;
    --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
    --radius: 16px;
    --gap: 12px;
    --glass-border: rgba(175, 175, 175, 0.35);
    --text: #0f172a;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #1e293b;
        --bg: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
        --card: rgba(30, 41, 59, 0.55);
        --muted: #94a3b8;
        --accent: #3b82f6;
        --shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
        --glass-border: rgba(255, 255, 255, 0.1);
        --text: #f1f5f9;
    }
}


html, body {
    height: 100%;
    margin: 0;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

header.appbar,
.control,
.period-control,
.chart-card,
.settings-section,
.modal,
.selected-sensor-item,
.tag {
    background: var(--card);
    border: 1px solid var(--glass-border);
    box-shadow: var(--shadow);
}

/* App layout */
.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    gap: var(--gap);
}

header.appbar {
    display: flex;
    gap: var(--gap);
    align-items: center;
    padding: 18px;
    background: var(--card);
    box-shadow: var(--shadow);
    border-radius: calc(var(--radius));
    margin: 18px;
    top: 12px;
    z-index: 50;
}

.brand {
    display: flex;
    gap: 12px;
    align-items: center;
    min-width: 180px;
}

.logo {
    width: 48px;
    height: 48px;
    border-radius: 10px;
    background: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white; /* This sets the SVG stroke color */
    box-shadow: 0 8px 20px rgba(59, 130, 246, 0.14);
}

.title {
    font-size: 1.1rem;
    font-weight: 600;
    letter-spacing: -0.02em;
}

.subtitle {
    font-size: 0.8rem;
    font-weight: 400;
    color: var(--muted);
}

.controls {
    margin-left: auto;
    display: flex;
    gap: 14px;
    align-items: flex-end;
}

.control {
    background: var(--card);
    border: 1px solid var(--glass-border);
    padding: 8px 10px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 110px;
    box-shadow: 0 6px 14px rgba(15, 23, 42, 0.04);
}

.control.inline {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
}

label.small {
    font-size: 0.75rem;
    color: var(--muted);
}

select, input[type="number"], input[type="text"] {
    border: 0;
    background: transparent;
    outline: none;
    font-size: 0.95rem;
    color: var(--text);
    width: 100%;
}

.btn {
    padding: 8px 12px;
    border-radius: 10px;
    border: 0;
    cursor: pointer;
    font-weight: 600;
    height: 46px; /* match inputs */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(2, 6, 23, 0.06);
    transition: all 0.2s ease;
}

.btn:hover {
    transform: translateY(-1px);
}


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

.btn.ghost {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--glass-border);
    color: var(--text);
}

.period-control {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--card);
    border: 1px solid var(--glass-border);
    border-radius: 10px;
    padding: 0 12px;
    height: 46px; /* match button height */
    box-shadow: 0 4px 10px rgba(2, 6, 23, 0.06);
}

.period-control label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--muted);
}

.period-control select {
    border: none;
    outline: none;
    background: transparent;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0;
}

/* Main content */
main.container {
    padding: 8px 18px 36px;
    display: flex;
    gap: 18px;
    flex-direction: column;
}

.chart-card {
    padding: 16px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    background: var(--card);
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}

.meta-line {
    color: var(--muted);
    font-size: 0.9rem;
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}

/* Chart wrapper (fixes Chart.js resize loop) */
html, body {
    height: 100%;
    overflow: hidden;
}

main.container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chart-card {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.chart-container {
    position: relative;
    flex: 1;
    height: auto;
    max-height: none;
    display: flex;
}

.no-data {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    color: var(--muted);
    font-weight: 600;
    background: var(--card);
}

canvas#chart {
    height: 100% !important;
}

/* Modal for sensors + settings */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.35);
    display: flex;
    opacity: 0;
    pointer-events: none;
    align-items: center;
    justify-content: center;
    z-index: 60;
    transition: opacity 0.3s ease;
}

.modal-backdrop[aria-hidden='false'] {
    opacity: 1;
    backdrop-filter: blur(5px);
    pointer-events: auto;
}

.modal {
    width: min(900px, 92vw);
    max-height: 85vh;
    overflow-y: auto;
    background: var(--bg);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.modal-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.settings-section {
    padding: 16px;
    border-radius: var(--radius);
}

.section-title {
    margin: 0 0 12px 0;
    font-size: 0.9rem;
    font-weight: 600;
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
    align-items: end;
}

.sensor-config-layout {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 16px;
}

@media (max-width: 900px) {
    .sensor-config-layout {
        grid-template-columns: 1fr;
    }
}

.tag-cloud {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    padding: 4px;
}

.tag {
    padding: 8px 12px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.25);
    border: 1px solid var(--glass-border);
    color: var(--text);
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.25s ease;
}

.tag:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.1);
}

/* Disabled tag */
.tag:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
    background: rgba(255, 255, 255, 0.12);
    box-shadow: none;
}

/* Selected Sensors List */
.selected-sensors-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 150px;
}

.placeholder-text {
    text-align: center;
    color: var(--muted);
    font-size: 0.9rem;
    padding: 40px 0;
}

.selected-sensor-item {
    display: grid;
    grid-template-columns: auto 1fr auto auto auto;
    gap: 10px;
    padding: 12px;
    border-radius: 12px;
    background: var(--card);
    border: 1px solid var(--glass-border);
    box-shadow: var(--shadow);
    cursor: grab;
    transition: all 0.2s ease;
    align-items: center;
}

.selected-sensor-item.dragging {
    opacity: 0.65;
    background: rgba(59, 130, 246, 0.2);
    border: 1px solid rgba(59, 130, 246, 0.4);
}

.drag-handle {
    cursor: grab;
    color: var(--muted);
    padding: 6px;
    border-radius: 8px;
    transition: background 0.2s ease;
}

.drag-handle:hover {
    background: rgba(255, 255, 255, 0.12);
}

.sensor-name {
    font-weight: 600;
    font-size: 0.95rem;
}

.sensor-control {
    display: flex;
    align-items: center;
    gap: 6px;
}

.sensor-control label {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--muted);
}

.sensor-control input {
    width: 60px;
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid var(--glass-border);
}

.sensor-control input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
    outline: none;
}

.remove-sensor-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--muted);
    padding: 6px;
    border-radius: 50%;
    transition: all 0.25s ease;
}

.remove-sensor-btn:hover {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.15);
}


/* Loading overlay */
#loading-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    opacity: 0;
    transition: 0.15s;
    z-index: 70;
}

.loading-box {
    background: var(--bg-color);
    padding: 14px;
    border-radius: 10px;
    box-shadow: var(--shadow);
}

#download {
    width: 46px;
    height: 46px;
    padding: 8px;
}

/* Responsive tweaks */
@media (max-width: 900px) {
    header.appbar {
        margin: 12px;
        padding: 14px;
    }

    .controls {
        width: 100%;
        justify-content: flex-end;
    }

    .brand-title {
        display: none;
    }
}

.sensor-controls-wrapper {
    display: flex;
    gap: 10px;
}

@media (max-width: 600px) {
    .brand {
        display: none;
    }

    .chart-container {
        height: 62vh;
    }

    html, body {
        overflow-y: auto;
    }

    /* Make the modal taller, wider, and with less padding on mobile */
    .modal {
        width: 95vw;
        max-height: 88vh;
        padding: 16px;
        gap: 16px;
    }

    .selected-sensor-item {
        grid-template-columns: auto 1fr auto; /* Adjust to a 3-column layout */
        grid-template-areas:
            "handle name remove"
            "handle controls remove"; /* New 2-row layout */
        row-gap: 8px;
        padding: 12px 10px;
    }

    /* Assign elements to grid areas */
    .selected-sensor-item .drag-handle {
        grid-area: handle;
        align-self: center;
    }

    .selected-sensor-item .sensor-name {
        grid-area: name;
        align-self: end;
    }

    .selected-sensor-item .remove-sensor-btn {
        grid-area: remove;
        align-self: center;
    }

    /* Create a sub-grid for the min/max controls to sit side-by-side */
    .selected-sensor-item .sensor-control {
        grid-area: controls;

    }

    /* This is a new wrapper we'll add in the next step */
    .sensor-controls-wrapper {
        grid-area: controls;
        align-self: start;
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}