/**
 * Tên file: widget.css
 * Phiên bản: Final v2.0 (Đã dọn dẹp, sửa lỗi và hợp nhất indicator)
 * Ngày: 03/10/2025
 */

/* === GridStack & Generic Widget Styles === */
.grid-stack {
    background-image: linear-gradient(rgba(203, 213, 225, 0.4) 1px, transparent 1px), linear-gradient(90deg, rgba(203, 213, 225, 0.4) 1px, transparent 1px);
    background-size: 50px 50px;
    transition: background-image 0.3s;
    padding: 10px;
    min-height: calc(100vh - 200px);
    /* Responsive adjustment for small screens */
}

.dark .grid-stack {
    background-image: linear-gradient(rgba(75, 85, 99, 0.4) 1px, transparent 1px), linear-gradient(90deg, rgba(75, 85, 99, 0.4) 1px, transparent 1px);
}

/* Responsive GridStack adjustments for small resolutions */
@media (max-width: 768px) {
    .grid-stack {
        padding: 8px;
    }
}

@media (max-width: 640px) {
    .grid-stack {
        padding: 6px;
    }
}

@media (max-width: 480px) {
    .grid-stack {
        padding: 4px;
    }
}

.grid-stack.grid-hidden {
    background-image: none !important;
}

.grid-stack-item-content {
    display: flex;
    flex-direction: column;
    background-color: rgba(255, 255, 255, 0.95);
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    overflow: hidden;
    box-shadow: 0 1px 3px 0 rgba(0,0,0,0.1);
    transition: box-shadow 0.2s, border-color 0.2s;
    /* Ensure proper scaling on small screens */
    min-height: 0;
    min-width: 0;
}

.dark .grid-stack-item-content {
    background-color: rgba(31, 41, 55, 0.95);
    border-color: #4b5563;
}

.grid-stack-item-content.alerting-warning {
    border: 3px solid #f59e0b !important;
    box-shadow: 0 0 15px rgba(245, 158, 11, 0.5) !important;
    animation: pulse-warning 2s ease-in-out infinite !important;
}

.grid-stack-item-content.alerting-critical {
    border: 3px solid #ef4444 !important;
    box-shadow: 0 0 15px rgba(239, 68, 68, 0.6) !important;
    animation: pulse-critical 1.5s ease-in-out infinite !important;
}

@keyframes pulse-warning {
    0%, 100% {
        box-shadow: 0 0 15px rgba(245, 158, 11, 0.5);
        border-color: #f59e0b;
    }
    50% {
        box-shadow: 0 0 25px rgba(245, 158, 11, 0.8);
        border-color: #fb923c;
    }
}

@keyframes pulse-critical {
    0%, 100% {
        box-shadow: 0 0 15px rgba(239, 68, 68, 0.6);
        border-color: #ef4444;
    }
    50% {
        box-shadow: 0 0 25px rgba(239, 68, 68, 0.9);
        border-color: #f87171;
    }
}

.chart-card-header {
    padding: 0.5rem 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #e5e7eb;
    cursor: move;
    flex-shrink: 0;
    position: relative;
    /* Responsive padding for small screens */
}

.dark .chart-card-header {
    border-color: #4b5563;
}

/* Responsive header adjustments */
@media (max-width: 768px) {
    .chart-card-header {
        padding: 0.375rem 0.5rem;
    }

    .chart-card-header h3 {
        font-size: clamp(0.75rem, 3vw, 0.875rem);
    }

    .chart-card-header .header-buttons {
        gap: 0px;
    }

    .chart-card-header .header-buttons button {
        padding: 0.25rem !important;
    }
}

@media (max-width: 640px) {
    .chart-card-header {
        padding: 0.3rem 0.5rem;
    }

    .chart-card-header h3 {
        font-size: clamp(0.7rem, 2.5vw, 0.875rem);
        padding-left: 8px;
    }

    .chart-card-header .widget-type-indicator {
        font-size: 9px;
        padding: 2px 6px;
        gap: 4px;
    }

    .widget-type-indicator svg {
        width: 10px;
        height: 10px;
    }
}

@media (max-width: 480px) {
    .chart-card-header {
        padding: 0.25rem 0.4rem;
    }

    .chart-card-header h3 {
        font-size: clamp(0.65rem, 2vw, 0.875rem);
        padding-left: 6px;
    }

    .chart-card-header .header-buttons {
        gap: -2px;
    }

    .chart-card-header .header-buttons button {
        width: 24px;
        height: 24px;
        padding: 0.15rem !important;
    }

    .chart-card-header .header-buttons svg {
        width: 14px;
        height: 14px;
    }
}

.chart-card-header h3 {
    font-size: 0.875rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-grow: 1;
    padding-left: 14px;
}

.chart-card-header .header-buttons {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 2px;
}

.chart-card-body {
    padding: 5px; /* KHẮC PHỤC: Đồng nhất padding thành 5px */
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    min-height: 0;
    /* Responsive padding for small screens */
}

    .chart-card-body > .chart-container {
        position: relative;
        flex-grow: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 0;
        min-width: 0;
    }
        /* CẢI TIẾN: Ghi đè style cho container của datatable để nó lấp đầy không gian */
        .chart-card-body > .chart-container.is-datatable {
            align-items: stretch;
            justify-content: flex-start;
        }

/* Responsive body adjustments */
@media (max-width: 768px) {
    .chart-card-body {
        padding: 4px;
    }
}

@media (max-width: 640px) {
    .chart-card-body {
        padding: 3px;
    }
}

@media (max-width: 480px) {
    .chart-card-body {
        padding: 2px;
    }
}

/* CẢI TIẾN: Đảm bảo wrapper của DataTable lấp đầy container để scrollY: '100%' hoạt động. */
.chart-container.is-datatable .dataTables_wrapper {
    height: 100%;
}

/* ĐẢM BẢO: Panel chính - wrapper và container đều height 100% */
.apiResponseTableWrapper, #api-response-table, #api-data-path-selector-container {
    height: 100%;
    min-height: 0;
}

/* ĐẢM BẢO: Modal - container và bảng đều height 100% */
#modal-api-result-table-container, #modal-api-response-table {
    height: 100%;
    min-height: 0;
}

/* KHẮC PHỤC: Khôi phục lại logic CSS cũ để giải quyết lỗi chiều cao và scrollbar. */
/* Logic này định vị tuyệt đối nội dung chính, cho phép .chart-container co giãn đúng. */
.chart-card-body canvas, .chart-card-body .table-container, .chart-card-body .seven-segment-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
} 

/* === Hợp nhất Widget Type và Signal Indicator === */
.chart-card-header .widget-type-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    order: 0;
    flex-shrink: 0;
    position: static;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 3px 8px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 500;
    cursor: help;
    border: 1px solid rgba(255, 255, 255, 0.2);
}
.widget-last-updated {
    position: absolute;
    bottom: 2px; /* Điều chỉnh lại vị trí cho phù hợp với padding mới */
    right: 5px;
    font-size: 0.65rem; /* ~10px */
    color: #9ca3af; /* gray-400 */
    z-index: 5;
    pointer-events: none;
}

.widget-last-updated span {
    display: inline-flex;
    align-items: center;
    gap: 3px;
}

.dark .widget-last-updated {
    color: #6b7280; /* gray-500 */
}

/* API Refresh Button Styling */
.refresh-api-widget-btn {
    transition: all 0.2s ease;
}

.refresh-api-widget-btn:hover {
    transform: rotate(90deg);
}

.refresh-api-widget-btn:active {
    animation: spin 0.5s ease;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.dark .chart-card-header .widget-type-indicator {
    background: rgba(255, 255, 255, 0.8);
    color: #1f2937;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.widget-type-indicator svg {
    width: 12px;
    height: 12px;
}

.dark .widget-type-indicator svg {
    color: #1f2937;
}

/* === ĐỊNH DẠNG HOÀN CHỈNH CHO SIGNAL DOT === */

/* 1. Kiểu dáng cơ bản của chấm tín hiệu */
.signal-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background-color: #9ca3af; /* Màu mặc định: Xám (không có dữ liệu) */
    transition: background-color 0.3s ease, box-shadow 0.3s ease;
    flex-shrink: 0; /* Đảm bảo dot không bị co lại */
}

    /* 2. Các trạng thái của chấm tín hiệu */
    .signal-dot.connected {
        background-color: #10b981; /* Màu xanh: Đã kết nối & có dữ liệu */
        animation: pulse-green 2s infinite; /* Hiệu ứng thở nhẹ nhàng */
    }

    .signal-dot.disconnected {
        background-color: #ef4444; /* Màu đỏ: Mất kết nối */
        animation: none; /* Tắt mọi hiệu ứng */
    }

    .signal-dot.warning {
        background-color: #f59e0b; /* Màu cam: Cảnh báo */
        animation: pulse-orange 1.5s infinite;
    }

    /* 3. Hiệu ứng "blinking" khi có dữ liệu mới (thay thế cho @keyframes blip cũ) */
    .signal-dot.blinking {
        /* Animation này sẽ chạy 1 lần và ghi đè lên animation pulse */
        animation: blink-effect 0.7s ease-out !important;
    }

/* 4. Định nghĩa các keyframes cho animation */

/* Hiệu ứng "thở" cho trạng thái Connected (màu xanh) */
@keyframes pulse-green {
    0% {
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
    }

    70% {
        box-shadow: 0 0 0 5px rgba(16, 185, 129, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
    }
}

/* Hiệu ứng "thở" cho trạng thái Warning (màu cam) */
@keyframes pulse-orange {
    0% {
        box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7);
    }

    70% {
        box-shadow: 0 0 0 5px rgba(245, 158, 11, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(245, 158, 11, 0);
    }
}

/* Hiệu ứng "nháy" một lần khi có dữ liệu mới */
@keyframes blink-effect {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.6);
        box-shadow: 0 0 8px currentColor;
    }

    100% {
        transform: scale(1);
    }
}

@keyframes blip {
    0%, 100% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.5);
        opacity: 0.7;
    }
}


/* === On/Off Widget === */
.on-off-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    padding: 1rem;
}

.on-off-container {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.on-off-widget {
    border: none;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.15s ease-out;
    color: #374151;
    background: #f3f4f6;
}

.dark .on-off-widget {
    background: #4b5563;
    color: #e5e7eb;
}

.on-off-widget:active {
    transform: translateY(2px);
}

.on-off-widget .on-off-label {
    font-size: 0.75rem;
    margin-top: 0.5rem;
    color: #6b7280;
}

.dark .on-off-widget .on-off-label {
    color: #e5e7eb;
}

.on-off-grid.style-round .on-off-widget {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1), inset 0 2px 4px rgba(255,255,255,0.2);
}

.dark .on-off-grid.style-round .on-off-widget {
    box-shadow: 0 4px 6px rgba(0,0,0,0.2), inset 0 2px 4px rgba(255,255,255,0.05);
}

.on-off-grid.style-round .on-off-widget:active {
    box-shadow: 0 2px 3px rgba(0,0,0,0.1), inset 0 2px 4px rgba(0,0,0,0.2);
}

.on-off-grid.style-square .on-off-widget {
    width: 80px;
    height: 80px;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1), inset 0 2px 4px rgba(255,255,255,0.2);
}

.dark .on-off-grid.style-square .on-off-widget {
    box-shadow: 0 4px 6px rgba(0,0,0,0.2), inset 0 2px 4px rgba(255,255,255,0.05);
}

.on-off-grid.style-square .on-off-widget:active {
    box-shadow: 0 2px 3px rgba(0,0,0,0.1), inset 0 2px 4px rgba(0,0,0,0.2);
}

.on-off-grid.style-group {
    flex-direction: column;
    gap: 0.5rem;
}

    .on-off-grid.style-group .on-off-widget {
        width: 100%;
        padding: 0.75rem 1rem;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.08);
    }

        .on-off-grid.style-group .on-off-widget .on-off-label {
            margin: 0;
            font-size: 0.875rem;
        }

.on-off-widget.on {
    background: #22c55e;
    color: white;
    box-shadow: 0 2px 4px rgba(34,197,94,0.4), inset 0 -3px 0 rgba(0,0,0,0.2);
}

    .on-off-widget.on:active {
        box-shadow: 0 1px 2px rgba(34,197,94,0.4), inset 0 1px 0 rgba(0,0,0,0.2);
    }

/* === Các định dạng widget khác (Gauge, GPS, AI, v.v...) === */
/* Phần này không thay đổi, có thể giữ nguyên từ file gốc của bạn */

/* === 7-Segment LED Widget === */
.seven-segment-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 8px;
    box-sizing: border-box;
    overflow: hidden;
    --digit-width: 60px;
    --digit-height: 120px;
    --segment-thickness: 10px;
    --segment-length: 40px;
    --segment-radius: 4px;
    --led-on-color: #ef4444;
    --led-off-color: #3f3f46;
    --led-off-opacity: 0.15;
    --led-glow: 0 0 8px var(--led-on-color);
    --widget-bg: #18181b;
    --decimal-size: calc(var(--segment-thickness) * 1.2);
}

.seven-segment-header {
    width: 100%;
    padding: 4px 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--led-on-color);
    margin-bottom: 4px;
    flex-shrink: 0;
}

.seven-segment-header .header-title {
    flex: 1;
}

.seven-segment-header .header-unit {
    font-size: 1.25rem;
    margin-left: 8px;
}

.seven-segment-display {
    display: flex;
    gap: calc(var(--digit-width) * 0.1);
    align-items: center;
    justify-content: center;
    max-height: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    flex-shrink: 1;
    overflow: hidden;
}

.digit {
    width: var(--digit-width);
    height: var(--digit-height);
    position: relative;
    flex-shrink: 0;
}

.segment {
    background-color: var(--led-off-color, #e5e7eb);
    opacity: var(--led-off-opacity, 0.05);
    position: absolute;
    border-radius: var(--segment-radius);
    transition: background-color 0.3s, opacity 0.3s, box-shadow 0.3s;
}

.dark .segment {
    background-color: var(--led-off-color, #1f2937);
    opacity: var(--led-off-opacity, 0.1);
}

.segment.on {
    background-color: var(--led-on-color, #ef4444);
    box-shadow: var(--led-glow, 0 0 calc(var(--segment-thickness) * 1.5) var(--led-on-color, #ef4444));
    opacity: 1;
}

/* Responsive segment positioning using CSS custom properties */
.segment.a {
    width: var(--segment-length);
    height: var(--segment-thickness);
    top: 0;
    left: calc(50% - var(--segment-length) / 2);
}

.segment.b {
    width: var(--segment-thickness);
    height: calc((var(--digit-height) - var(--segment-thickness)) / 2);
    top: calc(var(--segment-thickness) * 0.5);
    right: 0;
}

.segment.c {
    width: var(--segment-thickness);
    height: calc((var(--digit-height) - var(--segment-thickness)) / 2);
    top: calc(50% + var(--segment-thickness) / 2);
    right: 0;
}

.segment.d {
    width: var(--segment-length);
    height: var(--segment-thickness);
    bottom: 0;
    left: calc(50% - var(--segment-length) / 2);
}

.segment.e {
    width: var(--segment-thickness);
    height: calc((var(--digit-height) - var(--segment-thickness)) / 2);
    top: calc(50% + var(--segment-thickness) / 2);
    left: 0;
}

.segment.f {
    width: var(--segment-thickness);
    height: calc((var(--digit-height) - var(--segment-thickness)) / 2);
    top: calc(var(--segment-thickness) * 0.5);
    left: 0;
}

.segment.g {
    width: var(--segment-length);
    height: var(--segment-thickness);
    top: calc(50% - var(--segment-thickness) / 2);
    left: calc(50% - var(--segment-length) / 2);
}

/* Decimal point styling */
.decimal-point {
    width: var(--decimal-size);
    height: var(--decimal-size);
    border-radius: 50%;
    align-self: flex-end;
    margin-bottom: calc(var(--segment-thickness) * 1.5);
    margin-left: 2px;
    margin-right: 2px;
    background-color: var(--led-off-color, #e5e7eb);
    opacity: var(--led-off-opacity, 0.1);
    transition: background-color 0.3s, box-shadow 0.3s, opacity 0.3s;
    flex-shrink: 0;
}

    .decimal-point.on {
        background-color: var(--led-on-color, #ef4444);
        box-shadow: var(--led-glow, 0 0 calc(var(--segment-thickness) * 0.8) var(--led-on-color, #ef4444));
        opacity: 1;
    }

/* === LED THEMES === */

/* Theme 1: Classic Red LED */
.seven-segment-container[data-theme="classic-red"] {
    --led-on-color: #ef4444;
    --led-off-color: #3f3f46;
    --led-off-opacity: 0.15;
    --led-glow: 0 0 8px #ef4444, 0 0 12px #ef4444;
    --widget-bg: #18181b;
}

/* Theme 2: Monochrome LCD */
.seven-segment-container[data-theme="mono-lcd"] {
    --led-on-color: #d1d5db;
    --led-off-color: #374151;
    --led-off-opacity: 0.1;
    --led-glow: none;
    --widget-bg: #1f2937;
}

/* Theme 3: Plasma Blue */
.seven-segment-container[data-theme="plasma-blue"] {
    --led-on-color: #38bdf8;
    --led-off-color: #1e3a8a;
    --led-off-opacity: 0.1;
    --led-glow: 0 0 8px #38bdf8, 0 0 16px #38bdf8;
    --widget-bg: #0c142b;
}

/* Theme 4: Custom Color (will be set via JavaScript) */
.seven-segment-container[data-theme="custom"] {
    --led-off-opacity: 0.12;
    --led-glow: 0 0 8px var(--led-on-color), 0 0 12px var(--led-on-color);
    --widget-bg: #1a1a1a;
}

/* Theme 5: Green Matrix */
.seven-segment-container[data-theme="green-matrix"] {
    --led-on-color: #10b981;
    --led-off-color: #064e3b;
    --led-off-opacity: 0.12;
    --led-glow: 0 0 8px #10b981, 0 0 12px #10b981;
    --widget-bg: #022c22;
}

/* Theme 6: Orange Warning */
.seven-segment-container[data-theme="orange-warning"] {
    --led-on-color: #f59e0b;
    --led-off-color: #451a03;
    --led-off-opacity: 0.15;
    --led-glow: 0 0 8px #f59e0b, 0 0 12px #f59e0b;
    --widget-bg: #1c1917;
}

    /* Enhanced visual effects for better LED appearance */
    .seven-segment-container[data-theme="classic-red"] .segment.on,
    .seven-segment-container[data-theme="plasma-blue"] .segment.on,
    .seven-segment-container[data-theme="custom"] .segment.on,
    .seven-segment-container[data-theme="green-matrix"] .segment.on,
    .seven-segment-container[data-theme="orange-warning"] .segment.on {
        opacity: var(--led-brightness, 1);
        box-shadow: var(--led-glow);
    }

.seven-segment-container[data-theme="mono-lcd"] .segment.on {
    opacity: var(--led-brightness, 1);
    box-shadow: none;
}

/* Negative number styling */
.seven-segment-container .negative-sign {
    width: var(--segment-length);
    height: var(--segment-thickness);
    background-color: var(--led-on-color);
    border-radius: var(--segment-radius);
    margin-right: calc(var(--digit-width) * 0.15);
    margin-bottom: calc(var(--digit-height) * 0.45);
    box-shadow: var(--led-glow);
    opacity: var(--led-brightness, 1);
    flex-shrink: 0;
}

/* Header visibility control */
.seven-segment-header.hidden {
    display: none !important;
}

/* Brightness control via classes */
.segment.brightness-100 {
    filter: brightness(1);
}

.segment.brightness-90 {
    filter: brightness(0.9);
}

.segment.brightness-80 {
    filter: brightness(0.8);
}

.segment.brightness-70 {
    filter: brightness(0.7);
}

.segment.brightness-60 {
    filter: brightness(0.6);
}

.segment.brightness-50 {
    filter: brightness(0.5);
}

.segment.brightness-off {
    filter: brightness(0.3);
}

/* Glow control */
.segment.no-glow {
    box-shadow: none !important;
}

.seven-segment-container.no-glow .segment.on {
    box-shadow: none !important;
}

/* Transition control */
.segment {
    transition: background-color 200ms ease, opacity 200ms ease, box-shadow 200ms ease, filter 200ms ease;
}

.segment.fast-transition {
    transition: background-color 100ms ease, opacity 100ms ease, box-shadow 100ms ease, filter 100ms ease;
}

.segment.slow-transition {
    transition: background-color 400ms ease, opacity 400ms ease, box-shadow 400ms ease, filter 400ms ease;
}

.segment.no-transition {
    transition: none;
}

/* === Progress Bar, Temp, Humidity, Command Widgets === */
.progress-bar-container {
    width: 90%;
    height: 25px;
    background-color: #e5e7eb;
    border-radius: 0.5rem;
    overflow: hidden;
    position: relative;
}

.dark .progress-bar-container {
    background-color: #4b5563;
}

.progress-bar-fill {
    height: 100%;
    background-color: #3b82f6;
    border-radius: 0.5rem;
    transition: width 0.5s ease-in-out;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 0.75rem;
    font-weight: bold;
}

.temp-humidity-widget {
    font-size: 2.8rem;
    font-weight: 700;
    line-height: 1;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

    .temp-humidity-widget .unit {
        font-size: 1.5rem;
        font-weight: 500;
        color: #6b7280;
    }

.dark .temp-humidity-widget .unit {
    color: #9ca3af;
}

.command-widget-container {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 1rem;
    width: 100%;
    height: 100%;
    justify-content: center;
}

.command-widget-input {
    width: 100%;
    padding: 0.5rem;
    border-radius: 0.375rem;
    border: 1px solid #d1d5db;
}

.dark .command-widget-input {
    background-color: #4b5563;
    border-color: #6b7280;
    color: #e5e7eb;
}

.command-widget-btn {
    width: 100%;
    padding: 0.5rem;
    border-radius: 0.375rem;
    background-color: #3b82f6;
    color: white;
    font-weight: 600;
    transition: background-color 0.2s;
}

    .command-widget-btn:hover {
        background-color: #2563eb;
    }

/* === Gauge Widget === */
.gauge-needle {
    position: absolute;
    width: 2px;
    height: 38%;
    background-color: #dc2626; /* red-600 */
    bottom: 50%;
    left: 50%;
    margin-left: -1px; /* Căn giữa kim */
    transform-origin: bottom center;
    transition: transform 0.5s cubic-bezier(0.23, 1, 0.32, 1); /* Hiệu ứng nảy */
    z-index: 10;
    border-radius: 2px;
    box-shadow: 0 0 3px rgba(0,0,0,0.3);
}

.dark .gauge-needle {
    background-color: #f87171; /* red-400 */
}

.chart-container.is-gauge::after {
    content: '';
    position: absolute;
    width: 12px;
    height: 12px;
    background-color: #374151; /* gray-700 */
    border-radius: 50%;
    bottom: 50%;
    left: 50%;
    transform: translate(-50%, 50%);
    border: 2px solid white;
    z-index: 11;
    box-shadow: 0 0 5px rgba(0,0,0,0.2);
}

.dark .chart-container.is-gauge::after {
    background-color: #f9fafb; /* gray-50 */
    border-color: #1f2937;
}

.gauge-value-display {
    position: absolute;
    bottom: 12%;
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
    font-weight: bold;
    z-index: 10;
}

    .gauge-value-display .value {
        font-size: 1.8rem;
        line-height: 1;
    }

    .gauge-value-display .unit {
        font-size: 0.9rem;
        color: #6b7280; /* gray-500 */
        margin-left: 4px;
    }

.dark .gauge-value-display .unit {
    color: #9ca3af; /* gray-400 */
}

/* === Gauge-Advance Widget Type Styles === */
/* Speedometer Type */
[data-gauge-type="speedometer"] .gauge-needle-container {
    --needle-color: #ef4444 !important;
    width: 3px !important;
}

[data-gauge-type="speedometer"] .gauge-needle-pointer {
    background-color: #ef4444 !important; /* red-500 */
}

.dark [data-gauge-type="speedometer"] .gauge-needle-container {
    --needle-color: #f87171 !important;
}

.dark [data-gauge-type="speedometer"] .gauge-needle-pointer {
    background-color: #f87171 !important; /* red-400 */
}

[data-gauge-type="speedometer"] .gauge-needle-container::after {
    background-color: #ef4444 !important;
    border-color: #ef4444 !important;
}

[data-gauge-type="speedometer"] .chart-container.is-gauge::after {
    background-color: #ef4444 !important;
    border-color: #ef4444 !important;
}

.dark [data-gauge-type="speedometer"] .gauge-needle-container::after {
    background-color: #f87171 !important;
    border-color: #1f2937;
}

.dark [data-gauge-type="speedometer"] .chart-container.is-gauge::after {
    background-color: #f87171 !important;
    border-color: #1f2937;
}

/* Powermeter Type */
[data-gauge-type="powermeter"] .gauge-needle-container {
    --needle-color: #f59e0b !important;
    width: 4px !important;
}

[data-gauge-type="powermeter"] .gauge-needle-pointer {
    background-color: #f59e0b !important; /* amber-500 */
}

.dark [data-gauge-type="powermeter"] .gauge-needle-container {
    --needle-color: #fbbf24 !important;
}

.dark [data-gauge-type="powermeter"] .gauge-needle-pointer {
    background-color: #fbbf24 !important; /* amber-400 */
}

[data-gauge-type="powermeter"] .gauge-needle-container::after {
    background-color: #f59e0b !important;
    border-color: #f59e0b !important;
}

[data-gauge-type="powermeter"] .chart-container.is-gauge::after {
    background-color: #f59e0b !important;
    border-color: #f59e0b !important;
}

.dark [data-gauge-type="powermeter"] .gauge-needle-container::after {
    background-color: #fbbf24 !important;
    border-color: #1f2937;
}

.dark [data-gauge-type="powermeter"] .chart-container.is-gauge::after {
    background-color: #fbbf24 !important;
    border-color: #1f2937;
}

/* EV Battery Type */
[data-gauge-type="evbattery"] .gauge-needle-container {
    --needle-color: #60a5fa !important;
    width: 3px !important;
}

[data-gauge-type="evbattery"] .gauge-needle-pointer {
    background-color: #60a5fa !important; /* blue-400 */
}

.dark [data-gauge-type="evbattery"] .gauge-needle-container {
    --needle-color: #93c5fd !important;
}

.dark [data-gauge-type="evbattery"] .gauge-needle-pointer {
    background-color: #93c5fd !important; /* blue-300 */
}

[data-gauge-type="evbattery"] .gauge-needle-container::after {
    background-color: #60a5fa !important;
    border-color: #60a5fa !important;
}

[data-gauge-type="evbattery"] .chart-container.is-gauge::after {
    background-color: #60a5fa !important;
    border-color: #60a5fa !important;
}

.dark [data-gauge-type="evbattery"] .gauge-needle-container::after {
    background-color: #93c5fd !important;
    border-color: #1f2937;
}

.dark [data-gauge-type="evbattery"] .chart-container.is-gauge::after {
    background-color: #93c5fd !important;
    border-color: #1f2937;
}

/* Classic Type */
[data-gauge-type="classic"] .chart-card-body {
    background: linear-gradient(135deg, #fdf6e3 0%, #eee8d5 100%) !important;
    border-radius: 8px;
    box-shadow: inset 0 0 15px rgba(0,0,0,0.2);
}

.dark [data-gauge-type="classic"] .chart-card-body {
    background: linear-gradient(135deg, #2d3748 0%, #1a202c 100%) !important;
}

[data-gauge-type="classic"] .gauge-needle-container {
    --needle-color: #c0392b !important;
    width: 2px !important;
}

[data-gauge-type="classic"] .gauge-needle-pointer {
    background-color: #c0392b !important; /* classic red */
}

.dark [data-gauge-type="classic"] .gauge-needle-container {
    --needle-color: #e74c3c !important;
}

.dark [data-gauge-type="classic"] .gauge-needle-pointer {
    background-color: #e74c3c !important;
}

[data-gauge-type="classic"] .gauge-needle-container::after {
    background-color: #b58900 !important; /* classic yellow */
    border-color: #b58900 !important;
}

[data-gauge-type="classic"] .chart-container.is-gauge::after {
    background-color: #b58900 !important; /* classic yellow */
    border-color: #b58900 !important;
}

.dark [data-gauge-type="classic"] .gauge-needle-container::after {
    background-color: #d68910 !important;
    border-color: #1f2937;
}

.dark [data-gauge-type="classic"] .chart-container.is-gauge::after {
    background-color: #d68910 !important;
    border-color: #1f2937;
}

[data-gauge-type="classic"] .gauge-value-display {
    font-family: serif !important;
}

/* === GPS Map Widget === */
.gps-marker-icon {
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.gps-marker-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid white;
}

.status-normal .gps-marker-dot {
    background-color: #3b82f6;
}
/* blue-500 */
.status-normal {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
}

.status-lost-signal .gps-marker-dot {
    background-color: #f59e0b;
}
/* amber-500 */
.status-lost-signal {
    box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.5);
}

.status-critical .gps-marker-dot {
    background-color: #ef4444;
}
/* red-500 */
.status-critical {
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.5);
}

    .status-critical .gps-marker-pulse {
        position: absolute;
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #ef4444;
        animation: pulse 1.5s infinite;
    }

.status-warning .gps-marker-dot {
    background-color: #3b82f6;
}
/* blue-500 */
.status-warning {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
}

    .status-warning .gps-marker-dot-inner {
        position: absolute;
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background-color: #ef4444; /* red-500 */
        z-index: 1;
    }

@keyframes pulse {
    0% {
        transform: scale(0.5);
        opacity: 0.7;
    }

    70% {
        transform: scale(1);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 0;
    }
}

/* === AI Chat Widget === */
.ai-chat-widget-container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.ai-chat-history {
    flex-grow: 1;
    overflow-y: auto;
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.ai-chat-message {
    max-width: 85%;
    padding: 0.5rem 0.75rem;
    border-radius: 0.75rem;
    line-height: 1.4;
}

    .ai-chat-message.user {
        background-color: #3b82f6; /* blue-500 */
        color: white;
        align-self: flex-end;
        border-bottom-right-radius: 0.25rem;
    }

    .ai-chat-message.ai {
        background-color: #f3f4f6;
        align-self: flex-start;
        border-bottom-left-radius: 0.25rem;
    }

.dark .ai-chat-message.ai {
    background-color: #374151; /* gray-700 */
}

.ai-chat-message.ai-thinking {
    color: #9ca3af;
    font-style: italic;
    align-self: flex-start;
}

.ai-chat-input-area {
    display: flex;
    padding: 0.5rem;
    border-top: 1px solid #e5e7eb;
}

.dark .ai-chat-input-area {
    border-top-color: #4b5563;
}

.ai-chat-input {
    flex-grow: 1;
    border: none;
    background: transparent;
    padding: 0.5rem;
    outline: none;
}

.ai-chat-send-btn {
    padding: 0.5rem;
    border-radius: 50%;
    color: #3b82f6;
}

    .ai-chat-send-btn:hover {
        background-color: #eff6ff; /* blue-50 */
    }

.dark .ai-chat-send-btn:hover {
    background-color: #1e3a8a; /* blue-900 */
}

.ai-chat-send-btn:disabled {
    color: #9ca3af;
    cursor: not-allowed;
}

/* === Widget Configuration Modal Styles === */
.widget-config-section {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    padding: 16px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #f9fafb;
}

/* Dark mode support for config section */
.dark .widget-config-section {
    border-color: #4b5563;
    background: #1f2937;
}

.widget-config-input {
    transition: all 0.2s ease;
    border: 1px solid #d1d5db;
    background: white;
    color: #374151;
}

/* Dark mode support for inputs */
.dark .widget-config-input {
    border-color: #4b5563;
    background: #374151;
    color: #f9fafb;
}

.widget-config-input:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Dark mode focus states */
.dark .widget-config-input:focus {
    border-color: #60a5fa;
    box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.1);
}

.metric-checkbox {
    padding: 8px 12px;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    background: white;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #374151;
}

/* Dark mode support for metric checkboxes */
.dark .metric-checkbox {
    border-color: #4b5563;
    background: #374151;
    color: #f9fafb;
}

.metric-checkbox:hover {
    background: #f3f4f6;
    border-color: #3b82f6;
}

/* Dark mode hover states */
.dark .metric-checkbox:hover {
    background: #4b5563;
    border-color: #60a5fa;
}

.metric-checkbox.checked {
    color: #3b82f6;
    font-weight: 500;
}

/* Dark mode checked state */
.dark .metric-checkbox.checked {
    color: #60a5fa;
}

/* Widget Toolbox */
.widget-tool-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    padding: 8px 4px;
    width: 100%;
    background-color: #f3f4f6;
    border-radius: 0.375rem;
    border: 1px solid #e5e7eb;
    cursor: grab;
    transition: background-color 0.2s, transform 0.2s;
    user-select: none;
    text-align: center;
}
.widget-tool-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 6px -1px rgba(0,0,0,.1);
}
.dark .widget-tool-item {
    background-color: #374151;
    border-color: #4b5563;
}
.widget-tool-item:active {
    cursor: grabbing;
}

#widget-toolbox {
    position: absolute;
    right: 1.5rem;
    top: 1.5rem;
    width: 250px;
    max-height: calc(100% - 3rem);
    z-index: 200; /* Must be above enhanced-header (100) */
    transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
    transform: translateX(0);
}
#widget-toolbox.collapsed {
    transform: translateX(calc(100% + 1.5rem));
}
#widget-toolbox-toggle {
    position: absolute;
    top: 50%;
    left: -1.25rem;
    transform: translateY(-50%);
    z-index: 21;
}

.grid-stack.dashboard-locked .grid-stack-item {
    cursor: default !important;
}

/* CẢI TIẾN: Ẩn các nút cấu hình khi ở chế độ xuất bản */
.grid-stack-item.mode-published .config-button {
    display: none;
}

.widget-lock-btn.locked, #dashboard-lock-btn.locked {
    color: #3b82f6; /* blue-500 */
}

/* CẢI TIẾN: Ẩn các nút chỉ dành cho chế độ thiết kế khi ở chế độ xuất bản */
.grid-stack-item.mode-published .randomize-data-btn,
.grid-stack-item.mode-published .copy-widget-btn {
    display: none;
}

/* Chart Expand Modal */
#chart-expand-modal {
    z-index: 1040;
}

/* Signal LED on Widget Header */
.widget-signal-led {
    position: absolute;
    left: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #9ca3af; /* gray-400 */
    transition: background-color 0.3s;
}

.alerting-warning .widget-signal-led {
    background-color: #f59e0b; /* yellow-500 */
}

.alerting-critical .widget-signal-led {
    background-color: #ef4444; /* red-500 */
}

.widget-signal-led.blinking {
    animation: blip 1s ease-out;
}

/* === GAUGE ADVANCE WIDGET STYLES === */

/* Gauge container và SVG */
.gauge-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.gauge-svg {
    width: 100%;
    height: 100%;
    transform: rotate(-135deg); /* Bắt đầu từ góc dưới bên trái */
}

/* Gauge arcs */
.gauge-arc {
    fill: none;
    stroke-linecap: round;
    transition: stroke-dashoffset 0.5s cubic-bezier(0.23, 1, 0.32, 1), stroke 0.3s;
}

/* Needle và pivot */
.gauge-needle {
    transform-origin: center;
    transition: transform 0.8s cubic-bezier(0.34, 1.56, 0.64, 1); /* Hiệu ứng nảy */
    stroke-linecap: round;
}

.gauge-center-pivot {
    transition: fill 0.3s;
}

/* Digital display */
.digital-display {
    position: absolute;
    text-align: center;
    top: 60%; 
    left: 50%;
    transform: translate(-50%, -50%);
    line-height: 1;
    width: 100%;
}

/* === GAUGE STYLES === */

/* 1. SPEEDOMETER */
[data-style="speedometer"] .gauge-arc-bg { stroke: #e5e7eb; }
.dark [data-style="speedometer"] .gauge-arc-bg { stroke: #374151; }
[data-style="speedometer"] .gauge-needle { stroke: #ef4444; stroke-width: 3; }
[data-style="speedometer"] .gauge-center-pivot { fill: #374151; }
.dark [data-style="speedometer"] .gauge-center-pivot { fill: #f9fafb; }
[data-style="speedometer"] .gauge-tick { stroke: #9ca3af; }
.dark [data-style="speedometer"] .gauge-tick { stroke: #6b7280; }
[data-style="speedometer"] .gauge-label { fill: #4b5563; }
.dark [data-style="speedometer"] .gauge-label { fill: #d1d5db; }

/* 2. POWER METER */
[data-style="powermeter"] .gauge-arc-bg { stroke: #d1d5db; }
.dark [data-style="powermeter"] .gauge-arc-bg { stroke: #1f2937; }
[data-style="powermeter"] .gauge-arc { stroke-linecap: butt; }
[data-style="powermeter"] .gauge-needle { stroke: #f59e0b; stroke-width: 4; }
[data-style="powermeter"] .gauge-center-pivot { fill: #f59e0b; }
[data-style="powermeter"] .gauge-tick { stroke: #6b7280; }
.dark [data-style="powermeter"] .gauge-tick { stroke: #9ca3af; }
[data-style="powermeter"] .gauge-label { fill: #111827; font-weight: 700; }
.dark [data-style="powermeter"] .gauge-label { fill: #f9fafb; }

/* 3. EV BATTERY */
[data-style="evbattery"] .gauge-arc-bg { stroke: #e5e7eb; }
.dark [data-style="evbattery"] .gauge-arc-bg { stroke: #374151; }
[data-style="evbattery"] .gauge-needle { stroke: #60a5fa; stroke-width: 3; }
[data-style="evbattery"] .gauge-center-pivot { fill: #60a5fa; }
[data-style="evbattery"] .gauge-tick { display: none; } /* Ẩn vạch chia */
[data-style="evbattery"] .gauge-label { fill: #6b7280; }
.dark [data-style="evbattery"] .gauge-label { fill: #9ca3af; }

/* 4. CLASSIC GAUGE */
[data-style="classic"] {
    background: #fdf6e3;
    border-radius: 50%;
    box-shadow: inset 0 0 15px rgba(0,0,0,0.2);
}
.dark [data-style="classic"] {
    background: #2d3748;
}
[data-style="classic"] .gauge-arc-bg { display: none; }
[data-style="classic"] .gauge-needle { stroke: #c0392b; stroke-width: 2; }
[data-style="classic"] .gauge-center-pivot { fill: #b58900; }
[data-style="classic"] .gauge-tick { stroke: #657b83; }
[data-style="classic"] .gauge-label { fill: #586e75; font-family: serif; }
.dark [data-style="classic"] .gauge-label { fill: #eee8d5; }

/* === SIZE CONFIGURATIONS === */
.gauge-size-sm { width: 180px; }
.gauge-size-md { width: 220px; }
.gauge-size-lg { width: 260px; }

/* Font sizes theo size */
.gauge-size-sm .digital-value { font-size: 1.5rem; }
.gauge-size-md .digital-value { font-size: 2rem; }
.gauge-size-lg .digital-value { font-size: 2.5rem; }

.gauge-size-sm .digital-unit { font-size: 0.75rem; }
.gauge-size-md .digital-unit { font-size: 0.875rem; }
.gauge-size-lg .digital-unit { font-size: 1rem; }

.gauge-size-sm .gauge-label { font-size: 9px; }
.gauge-size-md .gauge-label { font-size: 11px; }
.gauge-size-lg .gauge-label { font-size: 13px; }

.gauge-size-sm .gauge-title { font-size: 0.875rem; }
.gauge-size-md .gauge-title { font-size: 1rem; }
.gauge-size-lg .gauge-title { font-size: 1.125rem; }

/* ============================================================
   GLOBAL RESPONSIVE FIXES FOR ALL WIDGETS
   ============================================================ */

/* General text scaling for small screens */
@media (max-width: 768px) {
    .chart-card-header h3 {
        font-size: clamp(0.75rem, 3vw, 0.875rem) !important;
    }

    .chart-card-body {
        font-size: clamp(0.75rem, 2.5vw, 0.875rem);
    }
}

@media (max-width: 640px) {
    .chart-card-header h3 {
        font-size: clamp(0.7rem, 2.5vw, 0.875rem) !important;
    }

    .chart-card-body {
        font-size: clamp(0.65rem, 2vw, 0.875rem);
    }

    /* Seven-segment display adjustments */
    .seven-segment-container {
        --digit-width: clamp(40px, 8vw, 60px);
        --digit-height: clamp(80px, 15vw, 120px);
        --segment-thickness: clamp(6px, 1.5vw, 10px);
        --segment-length: clamp(25px, 5vw, 40px);
    }

    /* Temp/Humidity widget adjustments */
    .temp-humidity-widget {
        font-size: clamp(1.8rem, 8vw, 2.8rem);
    }

    .temp-humidity-widget .unit {
        font-size: clamp(0.9rem, 5vw, 1.5rem);
    }

    /* Progress bar adjustments */
    .progress-bar-container {
        height: clamp(20px, 4vw, 25px);
    }

    .progress-bar-fill {
        font-size: clamp(0.65rem, 2vw, 0.75rem);
    }

    /* On-off widget adjustments */
    .on-off-grid.style-round .on-off-widget {
        width: clamp(60px, 12vw, 80px);
        height: clamp(60px, 12vw, 80px);
    }

    .on-off-grid.style-square .on-off-widget {
        width: clamp(60px, 12vw, 80px);
        height: clamp(60px, 12vw, 80px);
    }

    .on-off-widget .on-off-label {
        font-size: clamp(0.6rem, 2vw, 0.75rem);
    }
}

@media (max-width: 480px) {
    .chart-card-header h3 {
        font-size: clamp(0.65rem, 2vw, 0.875rem) !important;
    }

    .chart-card-body {
        font-size: clamp(0.6rem, 1.5vw, 0.875rem);
    }

    /* Seven-segment display micro adjustments */
    .seven-segment-container {
        --digit-width: clamp(35px, 7vw, 50px);
        --digit-height: clamp(70px, 13vw, 100px);
        --segment-thickness: clamp(5px, 1.2vw, 8px);
        --segment-length: clamp(20px, 4vw, 35px);
        padding: 4px;
    }

    /* Temp/Humidity widget micro adjustments */
    .temp-humidity-widget {
        font-size: clamp(1.5rem, 6vw, 2.8rem);
    }

    .temp-humidity-widget .unit {
        font-size: clamp(0.75rem, 4vw, 1.5rem);
    }

    /* Progress bar micro adjustments */
    .progress-bar-container {
        width: 95%;
        height: clamp(18px, 3vw, 25px);
    }

    .progress-bar-fill {
        font-size: clamp(0.6rem, 1.5vw, 0.75rem);
    }

    /* On-off widget micro adjustments */
    .on-off-grid {
        gap: clamp(0.5rem, 2vw, 1rem);
        padding: clamp(0.5rem, 2vw, 1rem);
    }

    .on-off-grid.style-round .on-off-widget {
        width: clamp(50px, 10vw, 80px);
        height: clamp(50px, 10vw, 80px);
    }

    .on-off-grid.style-square .on-off-widget {
        width: clamp(50px, 10vw, 80px);
        height: clamp(50px, 10vw, 80px);
    }

    .on-off-widget .on-off-label {
        font-size: clamp(0.55rem, 1.5vw, 0.75rem);
    }

    /* Gauge value display micro adjustments */
    .gauge-value-display .value {
        font-size: clamp(1.2rem, 5vw, 1.8rem);
    }

    .gauge-value-display .unit {
        font-size: clamp(0.7rem, 3vw, 0.9rem);
    }
}

/* === Candle Chart Widget === */
.candle-chart-widget {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.candle-chart-widget .chart-wrapper {
    flex: 1;
    min-height: 0;
    width: 100%;
    position: relative;
}

.candle-chart-widget .apex-chart-container {
    width: 100% !important;
    height: 100% !important;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.candle-chart-widget .apex-chart-container .apexcharts-canvas {
    width: 100% !important;
    height: 100% !important;
}

.candle-chart-widget .control-panel {
    flex-shrink: 0;
}

/* Ensure widget card body is full height */
.widget-content .candle-chart-widget {
    height: 100%;
}

.grid-stack-item-content .candle-chart-widget {
    height: 100%;
}