/* =========================================================
   Pattaya Smart Sanitary — Vibration MQTT dashboard
   Visual system mirrors the live application as closely as
   reasonable without a build step.
   ========================================================= */
:root {
    --c-primary: #2563eb;
    --c-primary-dark: #1d4ed8;
    --c-text: #0f172a;
    --c-text-soft: #475569;
    --c-muted: #94a3b8;
    --c-border: #e2e8f0;
    --c-bg: #f8fafc;
    --c-surface: #ffffff;
    --c-active-bg: #eff6ff;
    --c-active-bar: #2563eb;

    --status-ok: #14b8a6;
    --status-ok-bg: #ccfbf1;
    --status-ok-fg: #0f766e;

    --status-warn: #f59e0b;
    --status-warn-bg: #fef3c7;
    --status-warn-fg: #b45309;

    --status-danger: #dc2626;
    --status-danger-bg: #fee2e2;
    --status-danger-fg: #b91c1c;

    --status-offline: #94a3b8;
    --status-offline-bg: #f1f5f9;

    --axis-x: #ef4444;
    --axis-y: #10b981;
    --axis-z: #3b82f6;
    --vibration: #7c3aed;

    --sidebar-w: 260px;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow-soft: 0 1px 2px rgba(15,23,42,0.04), 0 1px 3px rgba(15,23,42,0.06);
    --shadow-pop: 0 8px 24px rgba(15,23,42,0.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: "IBM Plex Sans Thai", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--c-text);
    background: var(--c-bg);
    font-size: 14px; line-height: 1.5;
    min-height: 100vh;
}
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }

/* ================== Sidebar ================== */
.sidebar {
    background: var(--c-surface);
    border-right: 1px solid var(--c-border);
    padding: 20px 12px;
    display: flex; flex-direction: column; gap: 6px;
    position: relative;
    transition: width 0.18s ease;
}
.brand { text-align: center; padding: 8px 4px 18px; }
.seal { display: inline-block; margin-bottom: 8px; width: 80px; height: 80px; object-fit: contain; }
.brand-name { font-size: 20px; font-weight: 700; margin: 0; color: var(--c-text); }
.brand-sub { font-size: 11px; color: var(--c-muted); margin-top: 2px; }

.collapse-btn {
    position: absolute;
    right: -12px;
    top: 100px;
    width: 24px; height: 24px;
    border-radius: 50%;
    border: 1px solid var(--c-border);
    background: var(--c-surface);
    cursor: pointer;
    box-shadow: var(--shadow-soft);
    color: var(--c-text-soft);
    display: inline-flex; align-items: center; justify-content: center;
    z-index: 2;
}

.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 12px; }
.nav-item {
    display: grid;
    grid-template-columns: 28px 1fr auto;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    color: var(--c-text);
    text-decoration: none;
    background: none;
    border: none;
    font-family: inherit;
    font-size: 14px;
    text-align: left;
    cursor: pointer;
    width: 100%;
    transition: background 0.12s;
}
.nav-item:hover { background: #f1f5f9; }
.nav-item.active {
    background: var(--c-active-bg);
    color: var(--c-primary);
    font-weight: 500;
    position: relative;
}
.nav-item.active::before {
    content: "";
    position: absolute;
    left: -12px; top: 8px; bottom: 8px;
    width: 3px; background: var(--c-active-bar);
    border-radius: 0 3px 3px 0;
}
.nav-icon { display: inline-flex; align-items: center; justify-content: center; color: var(--c-text-soft); }
.nav-item.active .nav-icon { color: var(--c-primary); }
.nav-icon-sm { font-size: 12px; color: var(--c-muted); }
.nav-ext, .nav-caret {
    color: var(--c-muted); font-size: 12px;
    width: 18px; text-align: center;
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 1px 2px;
}
.nav-ext:hover { border-color: var(--c-border); }

.nav-group { display: flex; flex-direction: column; }
.nav-children { display: flex; flex-direction: column; padding-left: 18px; }
.nav-children[hidden] { display: none; }
.nav-child { padding: 6px 10px; font-size: 13px; }

.nav-footer {
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid var(--c-border);
    display: flex; flex-direction: column; gap: 2px;
}
.nav-item.logout { color: var(--status-danger); }
.nav-item.logout .nav-icon { color: var(--status-danger); }

/* Collapsed sidebar */
.sidebar.collapsed { --sidebar-w: 64px; padding: 20px 6px; }
.sidebar.collapsed .brand-name,
.sidebar.collapsed .brand-sub,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .nav-ext,
.sidebar.collapsed .nav-caret { display: none; }
.app:has(.sidebar.collapsed) { grid-template-columns: 64px 1fr; }
.sidebar.collapsed .seal svg { width: 38px; height: 38px; }
.sidebar.collapsed .collapse-btn { transform: rotate(180deg); }

/* ================== Main ================== */
.main { padding: 18px 28px 60px; min-width: 0; }

.topbar { display: flex; align-items: center; gap: 16px; margin-bottom: 18px; }
.search {
    flex: 0 1 380px;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: 999px;
    padding: 6px 14px;
    display: flex; align-items: center; gap: 8px;
    box-shadow: var(--shadow-soft);
}
.search-icon { color: var(--c-muted); display: inline-flex; }
.search input { border: none; outline: none; flex: 1; font: inherit; background: transparent; }

.title-row {
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    flex-wrap: wrap; margin-bottom: 6px;
}
.page-title { font-size: 22px; font-weight: 700; margin: 0; }
.chips { display:flex; gap:10px; align-items:center; margin-top:4px; }
.sensor-chip {
    display: inline-flex; align-items: center; gap: 8px;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: 999px;
    padding: 4px 12px 4px 6px;
    font-size: 12px;
    color: var(--c-text-soft);
}
.sensor-chip-dot {
    width: 22px; height: 22px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--vibration); color: white;
}

.status-line {
    display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
    color: var(--c-text-soft); font-size: 13px; margin-bottom: 16px;
}
.status-pill {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 4px 10px;
    border-radius: 999px;
    border: 1px solid var(--c-border);
    background: var(--c-surface);
    cursor: pointer;
    transition: background 0.12s;
}
.status-pill:hover { background: var(--c-bg); }
.status-pill.ok    { background: var(--status-ok-bg);     color: var(--status-ok-fg);     border-color: transparent; }
.status-pill.err   { background: var(--status-danger-bg); color: var(--status-danger-fg); border-color: transparent; }
.status-pill.warn  { background: var(--status-warn-bg);   color: var(--status-warn-fg);   border-color: transparent; }
.status-pill .dot  { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status-pill.ok .dot { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ── Live data freshness pill ────────────────────────────── */
.live-pill {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 3px 10px 3px 8px;
    border-radius: 999px;
    background: var(--status-ok-bg);
    color: var(--status-ok-fg);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum";
    transition: background 0.2s, color 0.2s;
}
.live-pill .live-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: currentColor;
    animation: live-pulse 0.9s ease-in-out infinite;
}
.live-pill b { font-weight: 600; }
.live-pill .live-sep { opacity: 0.5; }
.live-pill.stale-warn   { background: var(--status-warn-bg);   color: var(--status-warn-fg); }
.live-pill.stale-danger { background: var(--status-danger-bg); color: var(--status-danger-fg); }
.live-pill.stale-warn   .live-dot,
.live-pill.stale-danger .live-dot { animation: none; }
@keyframes live-pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50%      { opacity: 0.35; transform: scale(0.75); }
}

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 500; }
.badge.ok      { background: var(--status-ok-bg);     color: var(--status-ok-fg); }
.badge.warn    { background: var(--status-warn-bg);   color: var(--status-warn-fg); }
.badge.danger  { background: var(--status-danger-bg); color: var(--status-danger-fg); }
.badge.offline { background: var(--status-offline-bg); color: var(--c-text-soft); }

.card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); box-shadow: var(--shadow-soft); }

/* ================== Stat grid ================== */
.stat-grid {
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px;
    margin-bottom: 16px;
}
@media (max-width: 1100px) { .stat-grid { grid-template-columns: repeat(2, 1fr); } }
.stat-card { padding: 14px 16px; border-left: 4px solid var(--c-border); }
.stat-card.ok     { border-left-color: var(--status-ok); }
.stat-card.warn   { border-left-color: var(--status-warn); }
.stat-card.danger { border-left-color: var(--status-danger); }
.stat-head {
    display: flex; align-items: center; justify-content: space-between;
    color: var(--c-text-soft); font-size: 12px; font-weight: 500;
    text-transform: uppercase; letter-spacing: 0.4px;
    margin-bottom: 6px;
}
.stat-big { font-size: 30px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.1; color: var(--c-text); }
.stat-unit { font-size: 14px; color: var(--c-muted); font-weight: 400; margin-left: 4px; }
.stat-sub { color: var(--c-muted); font-size: 12px; margin-top: 4px; }
.axes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 12px; }
.axes .ax-cell {
    background: var(--c-bg);
    border-radius: var(--radius-sm);
    padding: 6px 6px 5px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    border-top: 2px solid;
}
.axes .ax-cell.x { border-top-color: var(--axis-x); }
.axes .ax-cell.y { border-top-color: var(--axis-y); }
.axes .ax-cell.z { border-top-color: var(--axis-z); }
.axes .ax-lbl { color: var(--c-muted); font-size: 10px; font-weight: 600; display: block; }
.axes .ax-val { color: var(--c-text); font-size: 14px; font-weight: 500; margin-top: 1px; }

/* ================== Chart ================== */
.section { margin-top: 20px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-title { font-size: 16px; font-weight: 700; margin: 0; }
.legend-axes { display: flex; gap: 12px; color: var(--c-text-soft); font-size: 12px; }
.legend-axes .item { display: inline-flex; align-items: center; gap: 5px; }
.legend-axes .swatch { width: 12px; height: 3px; border-radius: 2px; }
.swatch.x { background: var(--axis-x); }
.swatch.y { background: var(--axis-y); }
.swatch.z { background: var(--axis-z); }

.chart-card { padding: 18px 8px 8px; overflow-x: auto; }
.chart { width: 100%; height: 280px; display: block; min-width: 600px; }
.chart .grid-line { stroke: var(--c-border); stroke-dasharray: 2 4; stroke-width: 0.5; }
.chart .axis-text { fill: var(--c-muted); font-size: 11px; }
.chart .axis-label-y { fill: var(--c-muted); font-size: 10px; }
.chart .threshold-warn   { stroke: var(--status-warn);   stroke-dasharray: 4 4; stroke-width: 1; opacity: 0.7; }
.chart .threshold-danger { stroke: var(--status-danger); stroke-dasharray: 4 4; stroke-width: 1; opacity: 0.7; }
.chart .line-x { stroke: var(--axis-x); fill: none; stroke-width: 1.8; }
.chart .line-y { stroke: var(--axis-y); fill: none; stroke-width: 1.8; }
.chart .line-z { stroke: var(--axis-z); fill: none; stroke-width: 1.8; }

/* ================== Faults ================== */
.faults-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
@media (max-width: 900px) { .faults-grid { grid-template-columns: 1fr; } }
.fault-card { padding: 14px 16px; }
.fault-head { font-size: 13px; font-weight: 700; margin-bottom: 8px; }
.fault-axis { display: flex; align-items: center; gap: 6px; margin: 6px 0 4px; }
.axis-tag {
    width: 22px; height: 22px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    color: white; font-size: 11px; font-weight: 700;
}
.axis-tag.x { background: var(--axis-x); }
.axis-tag.y { background: var(--axis-y); }
.axis-tag.z { background: var(--axis-z); }
.fault-list {
    color: var(--c-text-soft); font-size: 12px;
    list-style: none; padding: 0; margin: 0 0 6px;
    display: flex; flex-direction: column; gap: 4px;
}
.fault-list li { display: flex; align-items: flex-start; gap: 6px; }
.fault-list li::before { content: "⚠"; color: var(--status-warn); flex-shrink: 0; margin-top: 1px; }
.fault-list li.danger::before { color: var(--status-danger); content: "●"; }
.fault-list .empty { color: var(--status-ok); display: flex; align-items: center; gap: 6px; }
.fault-list .empty::before { content: "✓"; color: var(--status-ok); flex-shrink: 0; }

/* ================== Footer ================== */
.sensor-info {
    margin-top: 24px;
    padding: 14px 16px;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    font-size: 13px;
    color: var(--c-text-soft);
}
.sensor-info .label { color: var(--c-muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.4px; margin-bottom: 2px; }
.sensor-info .value { color: var(--c-text); font-weight: 500; font-variant-numeric: tabular-nums; }

/* ================== Rail ================== */
.rail {
    position: fixed; top: 24px; right: 16px;
    display: flex; flex-direction: column; gap: 10px; z-index: 50;
}
.rail-btn {
    width: 40px; height: 40px; border-radius: 50%;
    background: var(--c-surface); border: 1px solid var(--c-border);
    box-shadow: var(--shadow-soft); cursor: pointer; color: var(--c-text-soft);
    display: inline-flex; align-items: center; justify-content: center;
    transition: color 0.12s, border-color 0.12s;
}
.rail-btn:hover { color: var(--c-primary); border-color: var(--c-primary); }

/* ================== Modal ================== */
.modal-backdrop {
    position: fixed; inset: 0; background: rgba(15,23,42,0.45);
    z-index: 100; display: flex; align-items: center; justify-content: center;
    padding: 16px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
    background: var(--c-surface); border-radius: 12px;
    box-shadow: var(--shadow-pop);
    width: 100%; max-width: 480px;
    overflow: hidden;
}
.modal-head {
    padding: 14px 18px;
    border-bottom: 1px solid var(--c-border);
    display: flex; align-items: center; justify-content: space-between;
}
.modal-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.modal-close {
    background: none; border: none; font-size: 22px; line-height: 1;
    cursor: pointer; color: var(--c-muted); padding: 0 4px;
}
.modal-close:hover { color: var(--c-text); }
.modal-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot {
    padding: 12px 18px;
    border-top: 1px solid var(--c-border);
    display: flex; justify-content: flex-end; gap: 8px;
    background: var(--c-bg);
}
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--c-text-soft); }
.field span { font-weight: 500; color: var(--c-text); }
.field input {
    padding: 8px 10px;
    border: 1px solid var(--c-border);
    border-radius: 6px;
    font: inherit;
    background: var(--c-surface);
    color: var(--c-text);
}
.field input:focus { outline: none; border-color: var(--c-primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
.field small { color: var(--c-muted); font-size: 11px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.brokers-quick {
    display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
    font-size: 12px; color: var(--c-muted);
}
.brokers-quick button {
    background: var(--c-bg);
    border: 1px solid var(--c-border);
    border-radius: 999px;
    padding: 3px 10px;
    font: inherit; font-size: 12px;
    cursor: pointer; color: var(--c-text);
}
.brokers-quick button:hover { background: var(--c-active-bg); border-color: var(--c-primary); color: var(--c-primary); }

.btn-primary {
    background: var(--c-primary);
    color: white; border: none;
    padding: 8px 16px; border-radius: 6px;
    font: inherit; font-weight: 500; cursor: pointer;
}
.btn-primary:hover { background: var(--c-primary-dark); }
.btn-secondary {
    background: var(--c-surface);
    color: var(--c-text-soft);
    border: 1px solid var(--c-border);
    padding: 8px 16px; border-radius: 6px;
    font: inherit; cursor: pointer;
}
.btn-secondary:hover { background: var(--c-bg); }

code { background: var(--c-bg); padding: 1px 4px; border-radius: 3px; font-family: "SF Mono", Menlo, monospace; font-size: 0.9em; }

/* ============ Sensor over-temperature alarm banner ============ */
.sensor-alarm {
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 20px;
    font-size: 14px;
    font-family: inherit;
    color: #ffffff;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.sensor-alarm.hidden { display: none; }
.sensor-alarm-icon {
    font-size: 26px;
    flex-shrink: 0;
}
.sensor-alarm-body {
    flex: 1;
    line-height: 1.35;
}
.sensor-alarm-title {
    font-weight: 700;
    font-size: 15px;
    margin-bottom: 2px;
}
.sensor-alarm-sub {
    font-size: 12.5px;
    opacity: 0.92;
}
.sensor-alarm-ack {
    background: rgba(255,255,255,0.18);
    border: 1px solid rgba(255,255,255,0.45);
    color: #ffffff;
    padding: 7px 16px;
    border-radius: 6px;
    font-weight: 600;
    font-family: inherit;
    font-size: 12.5px;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s;
}
.sensor-alarm-ack:hover {
    background: rgba(255,255,255,0.3);
}
.sensor-alarm.warn {
    background: linear-gradient(90deg, #b45309 0%, #d97706 50%, #f59e0b 100%);
}
.sensor-alarm.critical {
    background: linear-gradient(90deg, #7f1d1d 0%, #b91c1c 50%, #dc2626 100%);
    animation: alarm-pulse 1.1s ease-in-out infinite;
}
.sensor-alarm.critical.muted {
    animation: none;
    opacity: 0.88;
}
@keyframes alarm-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.0), 0 4px 20px rgba(0,0,0,0.3); }
    50%      { box-shadow: 0 0 0 6px rgba(220, 38, 38, 0.35), 0 4px 28px rgba(220,38,38,0.6); }
}
