/* Table styles removed — will recreate table and styles from scratch. */
/* FLEX layout for chart and table side by side on wide screens (sensor.php) */
.main-flex-wrap {
    display: flex;
    flex-direction: row;
    gap: 0;
    align-items: stretch; /* make chart and table equal height */
    width: 100%;
    position: relative;
}

.main-flex-wrap .divider {
    width: 2px;
    background: var(--line);
    min-height: 420px;
    align-self: stretch;
    margin: 0 16px;
    border-radius: 2px;
}

#chartBox {
    flex: 0 1 60%;
    min-width: 320px;
    max-width: 100%;
}

#tableBox {
    flex: 1 1 40%;
    min-width: 0;
    width: 100%;
    margin-top: 0;
    display: flex;
    flex-direction: column;
}

/* Table layout removed — will recreate markup and styles later */

/* table styles consolidated at file top (cleaned) */

/* ===== Sensor data table (now re-created) ===== */
#tableBox .table-responsive {
    overflow: auto;
    width: 100%;
    flex: 1 1 auto;
    min-height: 400px;
    max-height: 600px;
}
#tableBox .sensor-data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
}
#tableBox .sensor-data-table th,
#tableBox .sensor-data-table td {
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
}
#tableBox .sensor-data-table th {
    background: #fbfdff;
    color: var(--muted);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 2;
}
#tableBox .sensor-data-table tbody tr:hover {
    background: #f8fbff;
}
#tableBox .sensor-data-table td:nth-child(1) { width: 56px; }
#tableBox .sensor-data-table td:nth-child(4) { width: 96px; }
#tableBox .sensor-data-table td:nth-child(5) { white-space: nowrap; }

/* Tables used in list views (devices.php, device.php) */
.table-wrap {
    overflow-x: auto;
}
.table-wrap table {
    width: 100%;
    border-collapse: collapse;
    table-layout: auto;
}
.table-wrap th,
.table-wrap td {
    padding: 8px 10px;
    border-bottom: 1px solid var(--line);
    text-align: left;
}

/* Page-specific table overrides */
#devicesTableWrap table {
    /* devices list: full-width table, compact cells */
    width: 100%;
}
#sensorsTableWrap table {
    /* device -> sensors list: allow columns to size naturally but keep full width */
    width: 100%;
}
#manageNamesTableWrap table {
    /* manage names: ensure inputs fill cells */
    width: 100%;
}

/* manage_names.php: keep default table sizing (no global overrides here) */
#manageNamesTableWrap table {
    width: 100%;
}
#manageNamesTableWrap input[type="text"] {
    width: 100%;
    box-sizing: border-box;
}

/* make ID column fixed width in manage_names table */
#manageNamesTableWrap .col-id {
    width: 140px;
    min-width: 140px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* make 'Inne' column wider to fit two buttons */
#manageNamesTableWrap th:nth-child(4),
#manageNamesTableWrap td:nth-child(4) {
    width: 220px;
    min-width: 220px;
    white-space: nowrap;
    text-align: center;
}

/* reduce horizontal gap for buttons inside the 'Inne' column */
#manageNamesTableWrap td:nth-child(4) .btn {
    margin-left: 6px;
    margin-right: 0;
    padding: 6px 10px;
}

/* red delete button styling inside manage_names */
#manageNamesTableWrap .btn-danger {
    background: #d32f2f;
    border-color: #d32f2f;
    color: #fff;
}
#manageNamesTableWrap .btn-danger:hover {
    background: #b71c1c;
    border-color: #b71c1c;
}

/* Small inputs / tighter spacing only inside popups (do not affect main lists) */
.map-modal-panel .table-wrap th,
.map-modal-panel .table-wrap td,
.device-info-popup .table-wrap th,
.device-info-popup .table-wrap td {
    padding: 4px 6px;
    font-size: 0.92rem;
}
.map-modal-panel input[type="text"],
.device-info-popup input[type="text"] {
    padding: 4px 6px;
    margin: 0;
    font-size: 0.92rem;
    box-sizing: border-box;
}

/* make popup tables size columns to content */
.map-modal-panel .table-wrap table,
.device-info-popup .table-wrap table {
    table-layout: auto;
    width: auto; /* allow columns to size to their content */
    max-width: 100%;
}
.map-modal-panel .table-wrap th,
.device-info-popup .table-wrap th {
    white-space: nowrap; /* keep headers compact and let columns expand */
}
.map-modal-panel .table-wrap td,
.device-info-popup .table-wrap td {
    white-space: nowrap;
}

/* Options popup: reduce spacing between edit fields and buttons */
#optionsOverlay .device-info-body > div {
    margin-bottom: 6px !important;
}
#optionsOverlay .device-info-body > div[style*="display:flex"] {
    gap: 6px !important;
    align-items: center !important;
}
#optionsOverlay .device-info-body > div[style*="flex-direction:column"] {
    gap: 6px !important;
}
#optionsOverlay .device-info-body input[type="number"],
#optionsOverlay .device-info-body select {
    padding: 6px !important;
    box-sizing: border-box !important;
}
#optionsOverlay .device-info-body .btn {
    padding: 6px 8px !important;
}
#optionsOverlay .device-info-body .muted {
    font-size: 0.82rem !important;
    margin-top: 2px !important;
}

/* avoid duplicate/invalid rules and ensure table layout is valid */
#chartBox {
    display: flex;
    flex-direction: column;
}

#chartBox .chart-box {
    flex: 1 1 auto;
    min-height: 400px;
    max-height: 600px;
}

@media (max-width: 1100px) {
    .main-flex-wrap {
        flex-direction: column;
        gap: 16px;
    }
    .main-flex-wrap .divider {
        width: 100%;
        height: 2px;
        min-height: unset;
        margin: 16px 0;
    }
    #chartBox, #tableBox {
        max-width: 100% !important;
        min-width: 0 !important;
    }
}
:root {
    --bg: #f5f7fb;
    --card: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --line: #e5e7eb;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success-bg: #e8f7ef;
    --success-text: #166534;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    color: var(--text);
    background: var(--bg);
    position: relative;
    z-index: 0;
}

.container {
    width: 98vw !important;
    /* max-width i min-width usunięte, aby panel rozciągał się do krawędzi */
        margin: 10px !important;
    padding: 0 16px !important;
    box-sizing: border-box;
        width: calc(100vw - 20px) !important;
        height: calc(100vh - 20px) !important;
}

.card {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 18px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
    position: relative;
    max-width: 100%;
    width: 100%;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 5px;
    padding-top: 0;
    flex-wrap: wrap;
}

.topbar > :not(.actions) {
    flex: 1 1 auto;
    min-width: 0;
}

.topbar > .actions {
    margin-left: auto;
    align-self: flex-start;
    justify-content: flex-end;
}

.logo {
    position: static;
    max-height: 84px !important;
    width: auto !important;
    height: auto;
    margin: 0 !important;
    vertical-align: middle;
}

.header-with-logo {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 0;
    padding-top:0px;
    padding-left: 0px;
}

@media (max-width: 768px) {
    .logo {
    }
    .container {
        margin: 12px auto;
        padding: 0 10px;
    }
}

.header-with-logo {
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    min-width: 0;
}

.header-with-logo .logo {
    flex: 0 0 auto;
}

.header-with-logo h1 {
    flex: 1 1 auto;
    min-width: 0;
    width: 100%;
}

/* Illustration next to header on device page */
.device-illustration {
    /* larger illustration on desktop */
    max-width: 320px;
    width: 220px;
    min-width: 160px;
    margin-left: 24px;
    display: inline-block;
    vertical-align: top;
}
.device-illustration img {
    width: 100%;
    height: auto;
    display: block;
}

/* hide illustration on small screens for cleaner layout */
@media (max-width: 768px) {
    .device-illustration { display: none !important; }
}

h1,
h2 {
    margin: 0;
}

h1 {
    font-size: 1.35rem;
}

h2 {
    font-size: 1.1rem;
    margin-bottom: 12px;
}

.subtitle {
    margin: 8px 0 0;
    color: var(--muted);
}

.subtitle-actions-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    margin: 8px 0 0;
}

.subtitle-actions-row .subtitle {
    margin: 0;
}

.subtitle-actions {
    margin-left: auto;
    justify-content: flex-end;
}

.actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.user-menu-wrap {
    position: relative;
    display: inline-flex;
}

.user-menu-toggle {
    min-width: 42px;
    padding: 8px 10px;
    line-height: 1;
}

.user-menu-toggle svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.user-menu-dropdown {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    min-width: 150px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.14);
    z-index: 8800;
    overflow: hidden;
}

.user-menu-wrap.open .user-menu-dropdown {
    display: block;
}

.user-menu-item {
    display: block;
    padding: 9px 12px;
    text-decoration: none;
    color: #0f172a;
    font-size: 0.9rem;
}

.user-menu-item:hover {
    background: #f8fafc;
}

.user-menu-item-danger {
    color: #b91c1c;
}

.user-menu-item-danger:hover {
    background: #fef2f2;
    color: #991b1b;
}

.btn {
    display: inline-block;
    text-decoration: none;
    border: 1px solid var(--primary);
    background: var(--primary);
    color: #fff;
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 0.92rem;
    cursor: pointer;
}

.btn:hover {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
}

.btn.btn-light {
    background: #fff;
    color: var(--primary);
}

.btn.btn-light:hover {
    background: #f0f0f0;
}

/* Informational icon button used in devices table */
.btn-info-icon {
    background: transparent;
    border: none;
    color: var(--primary);
    padding: 6px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* ===== Auth pages (login / register / reset) ===== */
body.auth-page { position: relative; min-height: 100vh; }
body.auth-page::before {
    content: "";
    position: fixed;
    inset: 0;
    background: url('img/Background.webp') no-repeat center/cover;
    background-attachment: fixed;
    opacity: 0.5; /* 50% transparency */
    z-index: 0;
    pointer-events: none;
}

.auth-form {
    position: relative; /* ensure z-index applies */
    z-index: 2;
    max-width: 420px;
    margin: 40px auto;
    padding: 22px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.auth-form h1 { font-size: 1.35rem; margin-bottom: 14px; text-align: center; }
.form-group { margin-bottom: 14px; }
.form-group label { display:block; margin-bottom:6px; font-weight:500; color:var(--text); }
.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"] {
    width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 1rem; box-sizing: border-box; background: #fff; color: var(--text);
}
.form-group input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.08); }
.form-actions { text-align: center; }
.muted-center { text-align:center; color:var(--muted); font-size:0.95rem; margin-top:8px; }

.error { color: #dc2626; background: #fee2e2; padding: 12px; border-radius: 8px; margin-bottom: 16px; border-left: 4px solid #dc2626; }
.success { color: var(--success-text); background: var(--success-bg); padding: 12px; border-radius: 8px; margin-bottom: 16px; border-left: 4px solid #16a34a; }
.info { color: #1e40af; background: #dbeafe; padding: 12px; border-radius: 8px; margin-bottom: 16px; border-left: 4px solid #2563eb; font-size: 0.95rem; }
.password-requirements { background: #f3f4f6; padding: 12px; border-radius: 8px; margin-bottom: 16px; font-size: 0.85rem; color: var(--muted); }
.password-requirements ul { margin: 8px 0 0 0; padding-left: 20px; }
.code-info { background: #dbeafe; padding: 12px; border-radius: 8px; margin-bottom: 16px; font-size: 0.9rem; color: #1e40af; }
.code-info .test-code { font-family: monospace; font-weight: bold; color: #dc2626; }
.auth-link { text-align: center; margin-top: 12px; color: var(--muted); }
.auth-link a { color: var(--primary); text-decoration: none; }
.auth-link a:hover { text-decoration: underline; }
.auth-link .auth-desktop-inline-link { display: inline-block; margin-top: 18px; }

@media (max-width: 480px) {
    .auth-form { margin: 18px 12px; padding: 16px; }
}
.btn-info-icon svg {
    width: 22px;
    height: 22px;
    display: block;
}
.btn-info-icon:focus {
    outline: 2px solid rgba(37,99,235,0.25);
    outline-offset: 2px;
}

/* Simple fallback icon (rendered as text inside a circle) to avoid SVG render issues after innerHTML replace */
.btn-info-icon {
    /* ensure button layout is stable and not overridden */
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    min-width: 28px !important;
    min-height: 28px !important;
    padding: 4px !important;
    background: transparent !important;
    border: none !important;
    color: var(--primary) !important;
}

.btn-info-icon .info-mark,
.btn-info-icon svg {
    width: 22px !important;
    height: 22px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    visibility: visible !important;
    opacity: 1 !important;
}

.btn-info-icon .info-mark {
    border: 2px solid var(--primary) !important;
    border-radius: 50% !important;
    font-weight: 700 !important;
    font-size: 12px !important;
    line-height: 1 !important;
    color: var(--primary) !important;
    background: #fff !important;
}

.btn-info-icon svg { fill: none !important; stroke: var(--primary) !important; }

input,
select,
button {
    font: inherit;
}

input[type="date"] {
    border: 1px solid var(--line) !important;
    border-radius: 10px !important;
    padding: 8px 10px;
    background: #fff;
    color: var(--text);
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    outline: none;
}

input[type="date"]::-webkit-calendar-picker-indicator {
    cursor: pointer;
    opacity: 0.7;
}

input[type="date"]::-webkit-datetime-edit-fields-wrapper {
    padding: 0;
}

input[type="date"]:focus {
    border-color: var(--primary) !important;
    outline: none;
}

/* Controls used for period/date selection (match sensor.php styling) */
.range-control,
select.range-control,
input.range-control {
    border: 1px solid var(--primary) !important;
    border-radius: 10px !important;
    color: var(--primary);
    padding: 6px 10px; /* increased padding to match sensor.php controls */
    font-size: 0.9em;
    line-height: 1.2;
    background: #fff;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    box-sizing: border-box;
    min-height: 34px; /* ensure visual height matches sensor.php */
}

/* when focused, use primary outline similar to other inputs */
select.range-control:focus,
input.range-control:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(37,99,235,0.08);
}

/* Table-related global rules removed — will recreate table-specific styles later */

tbody tr:hover {
    background: #f8fbff;
}

.btn, input.btn, button.btn {
    display: inline-block;
    text-decoration: none;
    border: 1px solid var(--primary);
    background: var(--primary);
    color: #fff;
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 0.92rem;
    cursor: pointer;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    line-height: 1.2;
}
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--success-bg);
    color: var(--success-text);
    font-size: 0.9rem;
}

.muted {
    color: var(--muted);
}

.section {
    margin-top: 18px;
}

.chart-box {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
}

/* Loader overlay for charts */
.chart-loader {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: rgba(255,255,255,0.9);
    z-index: 50;
    flex-direction: row;
}
.chart-loader .chart-spinner {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 4px solid rgba(0,0,0,0.08);
    border-top-color: var(--primary);
    animation: spin 1s linear infinite;
}
.chart-loader-text { color: var(--muted); font-weight: 600; }

@keyframes spin { to { transform: rotate(360deg); } }

.chart-box canvas {
    width: 100% !important;
    height: auto !important;
    display: block;
}

/* Floating illustration overlay (desktop only) */
.floating-illustration {
    position: absolute;
    top: 28px;
    right: 28px;
    width: 300px;
    max-width: 34%;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px;
    box-shadow: 0 8px 24px rgba(15,23,42,0.08);
    z-index: 120;
    display: flex;
    align-items: center;
    justify-content: center;
}
.floating-illustration .floating-illustration-body img {
    width: 100%;
    height: auto;
    display: block;
}
.floating-illustration .floating-illustration-thumb {
    border: none;
    background: transparent;
    padding: 0;
    display: block;
    cursor: zoom-in;
}
.floating-illustration .floating-illustration-thumb img {
    width: 100%;
    height: auto;
    display: block;
    border-radius: 8px;
}
.floating-illustration-close {
    position: absolute;
    top: 6px;
    right: 8px;
    border: none;
    background: transparent;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    color: var(--muted);
}

/* Image lightbox */
.image-lightbox {
    position: fixed;
    inset: 0;
    z-index: 1400;
    display: flex;
    align-items: center;
    justify-content: center;
}
.image-lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.6);
}
.image-lightbox-panel {
    position: relative;
    z-index: 1410;
    max-width: 90vw;
    max-height: 90vh;
    background: transparent;
    border-radius: 8px;
    overflow: hidden;
    display:flex;
    align-items:center;
    justify-content:center;
}

/* Anchored lightbox (appear near a trigger button instead of center) */
.image-lightbox.anchored {
    display: block; /* we position panel absolutely */
}
.image-lightbox.anchored .image-lightbox-backdrop {
    display: none; /* hide full-screen backdrop when anchored */
}
.image-lightbox.anchored .image-lightbox-panel {
    /* size to image while respecting viewport limits */
    width: auto;
    height: auto;
    max-width: 80vw;
    max-height: 80vh;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 18px 42px rgba(0,0,0,0.36);
    padding: 0; /* header + image will define padding */
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.image-lightbox-body img {
    width: auto;
    height: auto;
    max-width: calc(80vw - 32px);
    max-height: calc(80vh - 64px);
    display: block;
    border-radius: 0 0 8px 8px;
    object-fit: contain;
}
.image-lightbox-close {
    position: absolute;
    top: 10px;
    right: 12px;
    z-index: 1420;
    background: #fff;
    border: 0;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(0,0,0,0.12);
}

/* smaller thumbnail on narrow screens, and hidden overlay */
@media (max-width: 1024px) {
    .floating-illustration { width: 220px; max-width: 38%; }
}
@media (max-width: 768px) {
    .floating-illustration { display: none !important; }
}

/* hide floating illustration on small screens */
@media (max-width: 768px) {
    .floating-illustration { display: none !important; }
}

/* Floating Action Button (FAB) - bottom right */
.floating-action-button {
    position: absolute; /* positioned inside .card near the table */
    left: 24px; /* align left above the table */
    top: 220px; /* adjust vertical position as needed */
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1500;
    box-shadow: 0 8px 20px rgba(15,23,42,0.14);
    border: none;
    cursor: pointer;
    font-size: 18px;
}
.floating-action-button:active { transform: translateY(1px); }
.floating-action-button:focus { outline: none; box-shadow: 0 12px 24px rgba(15,23,42,0.22), 0 0 0 4px rgba(37,99,235,0.08); }

/* Inline illustration/info button styled as blue circular icon with white 'i' */
#openIllustrationInline,
.btn-info-icon.inline-illustration,
.btn-info-icon.icon-circle {
    background: var(--primary) !important;
    color: #fff !important;
    border-radius: 50% !important;
    width: 40px !important;
    height: 40px !important;
    min-width: 40px !important;
    min-height: 40px !important;
    padding: 0 !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    border: none !important;
    font-weight: 700 !important;
    font-size: 16px !important;
    box-shadow: 0 6px 14px rgba(15,23,42,0.12);
}
.btn-info-icon.inline-illustration .info-mark { border: none !important; background: transparent !important; color: #fff !important; }

@media (max-width: 768px) {
    .floating-action-button { display: none !important; }
}

body.mobile-device .container {
    margin: 12px auto;
    padding: 0 10px;
}

/* --- Custom: inline illustration button and anchored popup header --- */
/* Anchored panel header (show a small top bar with close button) */
.image-lightbox.anchored .image-lightbox-panel {
    padding: 0 !important; /* panel uses header + image sizing */
}
.image-lightbox.anchored .image-lightbox-header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--line);
    background: linear-gradient(180deg, #ffffff, #fbfdff);
}
.image-lightbox.anchored .image-lightbox-close {
    margin: 0;
    top: auto !important;
    right: auto !important;
    position: relative !important;
}
.image-lightbox.anchored .image-lightbox-panel > * { position: relative; z-index: 1420; }

/* Inline illustration button - circular style, placed inline near other actions */
#openIllustrationInline {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
    line-height: 1;
    border: 1px solid var(--primary);
    background: #fff;
    color: var(--primary);
}
#openIllustrationInline:hover { background: #f8fbff; }

/* Ensure anchored panel content has some top-padding for header overlap */
.image-lightbox.anchored .image-lightbox-body { padding-top: 6px; }

body.mobile-device .card {
    border-radius: 12px;
    padding: 12px;
}

body.mobile-device h1 {
    font-size: 1.15rem;
}

body.mobile-device h2 {
    font-size: 1rem;
}

body.mobile-device .subtitle {
    font-size: 0.92rem;
}

body.mobile-device .actions {
    width: 100%;
}

body.mobile-device .actions .btn,
body.mobile-device .actions input[type="date"],
body.mobile-device .btn {
    width: 100%;
    text-align: center;
}

body.mobile-device .btn {
    padding: 10px 12px;
}

/* Mobile topbar layout: actions on first row, header below */
body.mobile-device .topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 8px;
}

body.mobile-device .topbar > .actions {
    order: -1;
    width: 100%;
    margin-left: 0;
    align-self: flex-start;
    justify-content: flex-end;
    flex-wrap: nowrap;
    gap: 6px;
}

body.mobile-device .topbar > :not(.actions) {
    width: 100%;
    min-width: 0;
}

body.mobile-device .topbar > .actions .btn,
body.mobile-device .topbar > .actions input[type="date"] {
    width: auto;
    padding: 7px 9px;
    font-size: 0.84rem;
    white-space: nowrap;
}

body.mobile-device .topbar > .actions .user-menu-wrap {
    display: inline-flex;
    width: auto;
}

body.mobile-device .topbar > .actions .user-menu-toggle,
body.mobile-device .topbar > .actions .alert-center-topbtn {
    width: auto;
    min-width: 38px;
    padding: 7px 9px;
    line-height: 1;
}

/* mobile table styles removed */

@media (max-width: 768px) {
    .container {
        margin: 12px auto;
        padding: 0 10px;
    }

    .card {
        border-radius: 12px;
        padding: 12px;
    }

    h1 {
        font-size: 1.15rem;
    }

    h2 {
        font-size: 1rem;
    }

    .subtitle {
        font-size: 0.92rem;
    }

    .subtitle-actions-row {
        align-items: stretch;
    }

    .subtitle-actions {
        width: 100%;
        margin-left: 0;
    }

    .actions {
        width: 100%;
    }

    .actions .btn,
    .actions input[type="date"] {
        width: 100%;
        text-align: center;
    }

    .btn {
        width: 100%;
        text-align: center;
        padding: 10px 12px;
    }

    /* removed table styles in this media query */
}

@media (max-width: 420px) {
    .container {
        padding: 0 8px;
    }

    .card {
        padding: 10px;
    }

    h1 {
        font-size: 1.02rem;
    }

    .subtitle,
    .muted {
        font-size: 0.82rem;
    }

    .btn,
    input[type="date"] {
        padding: 8px 10px;
    }
}

body.modal-open {
    overflow: hidden;
}

/* Trend arrow styling — make arrows more visible */
.trend-arrow {
    font-weight: 700;
    font-size: 1.25em;
    margin-left: 6px;
    vertical-align: middle;
    line-height: 1;
}

.map-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
}

.map-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
}

.map-modal-panel {
    position: relative;
    z-index: 1;
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 0;
    background: var(--card);
    border: 0;
    border-radius: 0;
    padding: 14px;
    box-shadow: 0 20px 45px rgba(15, 23, 42, 0.28);
}

.map-layout {
    position: relative;
    display: block;
    flex: 1;
    min-height: 0;
}

.map-editor-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 20;
    width: min(22vw, 280px);
    min-width: 180px;
    max-width: calc(100% - 20px);
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
    box-shadow: 0 12px 26px rgba(15, 23, 42, 0.2);
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 0;
    overflow: auto;
    transform: translateX(calc(-100% - 14px));
    transition: transform 0.2s ease;
}

.map-layout.sidebar-open .map-editor-sidebar {
    transform: translateX(0);
}

.map-sidebar-overlay {
    display: none !important;
    pointer-events: none;
}

.map-splitter {
    display: none !important;
}

.map-splitter:hover {
    background: #cbd5e1;
}

.map-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.map-close {
    min-width: 130px;
}

.map-panel-toggle {
    min-width: 120px;
}

.map-controls {
    display: grid;
    gap: 10px;
    margin: 0;
}

.map-view-actions {
    margin: 10px 0 0;
}

#toggleMapEdit,
#toggleSensorMapEdit {
    display: block;
    width: 100%;
}

.map-modal [hidden] {
    display: none !important;
}

.map-controls label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
}

.map-controls input[type="file"],
.map-controls select {
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 8px 10px;
    background: #fff;
    color: var(--text);
}

.map-controls input[type="range"] {
    width: 100%;
}

.map-controls input[type="color"] {
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 4px;
    background: #fff;
    width: 72px;
    height: 40px;
}

.map-canvas-wrap {
    position: relative;
    min-height: 0;
    border: 1px dashed var(--line);
    border-radius: 12px;
    padding: 8px;
    background: #f8fafc;
    overflow: auto;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.map-canvas-stage {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    flex: 0 0 auto;
}

.map-placeholder {
    color: var(--muted);
    text-align: center;
    padding: 30px 10px;
}

.map-loading {
    position: absolute;
    inset: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: rgba(248, 250, 252, 0.88);
    color: var(--muted);
    border-radius: 8px;
    z-index: 4;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 600ms ease, visibility 600ms ease;
}

.map-loading.is-visible {
    opacity: 1;
    visibility: visible;
}

.map-loading-spinner {
    width: 30px;
    height: 30px;
    border: 3px solid #dbeafe;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: map-loading-spin 0.8s linear infinite;
}

@keyframes map-loading-spin {
    to {
        transform: rotate(360deg);
    }
}

#mapImage {
    display: block;
    width: auto;
    max-width: none;
    height: auto;
    border-radius: 8px;
    user-select: none;
}

.map-markers {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.map-marker {
    position: absolute;
    transform: translate(-50%, -50%);
    border: 1px solid var(--primary);
    background: rgba(37, 99, 235, 0.92);
    color: #fff;
    border-radius: 999px;
    padding: 4px 8px;
    font-size: 0.78rem;
    white-space: nowrap;
    pointer-events: auto;
    cursor: grab;
    touch-action: none;
}

.map-marker.is-dragging {
    cursor: grabbing;
    opacity: 0.9;
}

.map-marker-sensor {
    border-radius: 12px;
    min-width: 0;
    max-width: none;
    padding: 8px 10px;
    text-align: center;
    white-space: normal;
    line-height: 1.2;
    box-sizing: border-box;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.map-marker[class*="shape-cloud-"] {
    overflow: visible;
    border-radius: 16px !important;
    z-index: 1;
    isolation: isolate;
}

.map-marker[class*="shape-cloud-"]::after {
    content: '';
    position: absolute;
    width: 16px;
    height: 16px;
    background: inherit;
    pointer-events: none;
    z-index: -1;
}

.map-marker.shape-cloud-bottom::after {
    left: 50%;
    bottom: -10px;
    transform: translateX(-50%) rotate(45deg);
    border-left: inherit;
    border-bottom: inherit;
    border-top: 0;
    border-right: 0;
}

.map-marker.shape-cloud-top::after {
    left: 50%;
    top: -10px;
    transform: translateX(-50%) rotate(45deg);
    border-right: inherit;
    border-top: inherit;
    border-left: 0;
    border-bottom: 0;
}

.map-marker.shape-cloud-left::after {
    left: -10px;
    top: 50%;
    transform: translateY(-50%) rotate(45deg);
    border-left: inherit;
    border-top: inherit;
    border-right: 0;
    border-bottom: 0;
}

.map-marker.shape-cloud-right::after {
    right: -10px;
    top: 50%;
    transform: translateY(-50%) rotate(45deg);
    border-right: inherit;
    border-bottom: inherit;
    border-left: 0;
    border-top: 0;
}

.map-marker.map-marker-sensor[class*="shape-cloud-"] {
    overflow: visible;
}

.map-marker[class*="shape-flag-"] {
    overflow: visible;
    z-index: 1;
    isolation: isolate;
    border-radius: 2px !important;
}

.map-marker[class*="shape-flag-"]::before {
    content: '';
    position: absolute;
    pointer-events: none;
    z-index: -1;
    top: 0;
    width: 4px;
    height: calc(100% + 56px);
    background: #374151;
    border-radius: 2px;
}

.map-marker[class*="shape-flag-"]::after {
    content: '';
    position: absolute;
    pointer-events: none;
    z-index: -1;
    width: 8px;
    height: 8px;
    background: #374151;
    border-radius: 50%;
    top: calc(100% + 54px);
}

.map-marker.shape-flag-right::before {
    left: -4px;
}

.map-marker.shape-flag-right::after {
    left: -6px;
}

.map-marker.shape-flag-left::before {
    right: -4px;
}

.map-marker.shape-flag-left::after {
    right: -6px;
}

.sensor-marker-title {
    display: block;
    font-weight: 700;
    font-size: 1.02em;
    margin-bottom: 2px;
}

.sensor-marker-row {
    display: block;
    font-size: 0.9em;
    word-break: break-word;
}

.sensor-marker-time {
    font-size: 0.86em;
    opacity: 0.9;
    margin-top: 2px;
}

.marker-context-menu {
    position: fixed;
    z-index: 1200;
    min-width: 150px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.22);
    padding: 6px;
}

.marker-context-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--muted);
    padding: 6px 8px 8px;
    cursor: move;
    user-select: none;
}

.marker-context-close {
    border: 1px solid var(--line);
    background: #fff;
    color: var(--muted);
    border-radius: 8px;
    width: 24px;
    height: 24px;
    line-height: 1;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
}

.marker-context-close:hover {
    background: #f1f5f9;
}

.marker-menu-fields {
    display: grid;
    grid-template-columns: auto auto;
    gap: 6px;
    margin-bottom: 6px;
}

.marker-menu-fields label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 0.8rem;
    color: var(--muted);
}

.marker-menu-fields input[type="color"],
.marker-menu-fields select,
.marker-menu-fields input[type="number"] {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--text);
}

.marker-menu-fields input[type="color"] {
    width: 46px;
    height: 28px;
    padding: 2px;
}

.marker-menu-fields select {
    padding: 5px 8px;
}

.marker-menu-fields input[type="number"] {
    padding: 4px 6px;
    width: 62px;
}

.marker-menu-item {
    width: 100%;
    text-align: left;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text);
    border-radius: 8px;
    padding: 8px 10px;
    cursor: pointer;
}

.marker-menu-item:hover {
    background: #f1f5f9;
}

.marker-menu-item.danger {
    color: #b91c1c;
}

#markerContextMenu {
    min-width: 0;
    width: 250px;
}

#markerContextMenu .marker-menu-fields {
    gap: 5px;
}

#markerContextMenu .marker-menu-fields label {
    font-size: 0.76rem;
}

#markerContextMenu .marker-menu-fields input[type="number"] {
    width: 58px;
}

#sensorMarkerContextMenu {
    min-width: 0;
    width: 250px;
}

#sensorMarkerContextMenu .marker-menu-fields {
    gap: 5px;
}

#sensorMarkerContextMenu .marker-menu-fields label {
    font-size: 0.76rem;
}

#sensorMarkerContextMenu .marker-menu-fields input[type="number"] {
    width: 58px;
}

@media (max-width: 768px) {
    .map-layout {
        display: block;
    }

    .map-splitter {
        display: none;
    }

    .map-editor-sidebar {
        width: min(80vw, 300px);
        min-width: 0;
    }

    .map-controls {
        gap: 8px;
    }

    .map-controls label,
    .map-close {
        width: 100%;
    }

    .map-modal-panel {
        width: 100vw;
        max-width: 100vw;
        height: 100vh;
        max-height: 100vh;
        margin: 0;
        padding: 12px;
    }
}

/* ===== Info icon button ===== */
.btn-info-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--primary);
    padding: 4px;
    border-radius: 50%;
    vertical-align: middle;
    margin-left: 6px;
    transition: background 0.15s, color 0.15s;
}

.btn-info-icon:hover {
    background: var(--primary);
    color: #fff;
}

/* ===== Device info popup ===== */
.device-info-overlay {
    position: fixed;
    inset: 0;
    z-index: 9000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.device-info-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
}

.device-info-popup {
    position: relative;
    background: var(--card);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
    width: min(420px, 92vw);
    max-height: 80vh;
    overflow-y: auto;
    animation: diPopIn 0.2s ease-out;
}

@keyframes diPopIn {
    from { opacity: 0; transform: scale(0.92); }
    to { opacity: 1; transform: scale(1); }
}

.device-info-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px 12px;
    border-bottom: 1px solid var(--line);
}

.device-info-header h3 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--text);
}

.device-info-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--muted);
    line-height: 1;
    padding: 0 4px;
}

.device-info-close:hover {
    color: var(--text);
}

.device-info-body {
    padding: 16px 20px 20px;
}

/* Fullscreen override for combined chart modal */
#combinedChartOverlay {
    align-items: stretch;
    justify-content: stretch;
}
#combinedChartOverlay .device-info-popup {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    padding: 0;
    margin: 0;
    box-shadow: none;
    display: flex;
    flex-direction: column;
    background: var(--card);
}
#combinedChartOverlay .device-info-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--line);
}
#combinedChartOverlay .device-info-body {
    padding: 10px;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden; /* chart will manage its own scrolling/resizing */
}

/* Ensure chart-box inside fullscreen modal fills remaining space */
#combinedChartOverlay .chart-box {
    flex: 1 1 auto;
    padding: 8px;
    min-height: 0; /* allow flex child to shrink to available space */
}

/* Make canvas fill its container height */
#combinedChartOverlay .chart-box canvas,
#combinedChartOverlay canvas {
    width: 100% !important;
    height: 100% !important;
    display: block;
}

.device-info-table {
    width: 100%;
    border-collapse: collapse;
}

.device-info-table th,
.device-info-table td {
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid var(--line);
    font-size: 0.95rem;
}

.device-info-table th {
    color: var(--muted);
    font-weight: 500;
    width: 45%;
    white-space: nowrap;
}

.device-info-table td {
    color: var(--text);
    word-break: break-word;
}

.device-info-table tr:last-child th,
.device-info-table tr:last-child td {
    border-bottom: none;
}

/* Table header / cloned header styles removed. */

/* Compact temperature summary boxes and position period selector to their right */
/* Target the grid of small temp boxes (the first grid after an hr) */
hr + div[style*="display: grid"] {
    display: flex !important;
    flex-wrap: wrap;
    gap: 10px;
    padding: 0 15px;
    align-items: center; /* vertically center boxes */
    justify-content: flex-start; /* keep boxes aligned side-by-side */
}
hr + div[style*="display: grid"] > div {
    padding: 8px 10px !important;
    font-size: 0.88rem !important;
    border-radius: 8px !important;
    flex: 0 0 auto;
    min-width: 120px;
    max-width: 220px;
}

/* Place the moved period form to the right inside the .temp-controls-row */
#periodForm {
    margin-left: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-right: 12px;
    justify-content: flex-end; /* align form to right on wide screens */
}
/* ensure the date controls inside the form align nicely */
#dateRangeControls {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

/* Slightly reduce input sizes inside the small boxes */
hr + div[style*="display: grid"] strong {
    font-size: 0.98rem;
}
hr + div[style*="display: grid"] div {
    font-size: 0.88rem;
}

/* Temp box grid layout (2 columns x 2 rows) */
.temp-box {
    box-sizing: border-box;
}
.temp-box .temp-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 12px;
    align-items: center;
}
.temp-box .temp-grid div {
    white-space: nowrap;
}
.temp-box.temp1 { /* optional fine-tuning */ }
.temp-box.temp2 { /* optional fine-tuning */ }

/* Responsiveness: on small screens, restore normal flow */
@media (max-width: 800px) {
    #periodForm {
        margin-left: 0;
        justify-content: flex-start;
        padding: 0 15px;
    }
    hr + div[style*="display: grid"] {
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    }
}
@media (max-width: 1100px) {
    /* keep selector inline on narrower layouts */
    #periodSelect,
    #dateRangeControls {
        position: static;
        right: auto;
        top: auto;
    }
}

/* Generic modal used for addDeviceModal */
.modal {
    position: fixed;
    inset: 0;
    display: none; /* JS toggles inline style */
    align-items: center;
    justify-content: center;
    z-index: 1200;
}
.modal .modal-content {
    background: var(--card);
    border-radius: 12px;
    box-shadow: 0 18px 50px rgba(15,23,42,0.28);
    width: 100%;
    max-width: 520px;
    margin: 16px;
    border: 1px solid var(--line);
    overflow: hidden;
}
.modal .modal-header {
    display:flex;
    align-items:center;
    justify-content:space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--line);
}
.modal .modal-body { padding: 14px 16px; }
.modal .modal-footer { padding: 12px 16px; display:flex; justify-content:flex-end; gap:8px; border-top:1px solid var(--line); }
.modal .modal-close { background: none; border: none; font-size: 20px; cursor: pointer; }




