:root {
    --bg-main: #131a26;
    --bg-sidebar: #0f1520;
    --bg-card: #1b2434;
    --bg-card-hover: #212c40;
    --border-color: #283449;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --green: #3bd671;
    --green-bg: rgba(59, 214, 113, 0.14);
    --red: #df484a;
    --red-bg: rgba(223, 72, 74, 0.16);
    --amber: #f59e0b;
    --amber-bg: rgba(245, 158, 11, 0.15);
    --blue: #38bdf8;
    --blue-bg: rgba(56, 189, 248, 0.14);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--bg-main);
    color: var(--text-primary);
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 245px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    flex-shrink: 0;
}

.brand-wrapper {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 2px 4px;
}

.brand-logo-card {
    background: #ffffff;
    border-radius: 8px;
    padding: 8px 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.28);
}

.brand-logo-card svg,
.brand-logo-card img {
    width: 100%;
    height: auto;
    display: block;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 19px;
    font-weight: 700;
    padding: 0 4px;
}

.brand-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--green);
    box-shadow: 0 0 12px var(--green);
}

.nav-menu { display: flex; flex-direction: column; gap: 6px; }

.nav-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: 8px;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border: none;
    background: transparent;
    width: 100%;
    text-align: left;
    transition: 0.15s;
}

.nav-item:hover, .nav-item.active {
    background: var(--bg-card);
    color: var(--text-primary);
}

.nav-item.active { border-left: 3px solid var(--green); }

.badge-count {
    background: #263247;
    color: var(--text-primary);
    padding: 2px 8px;
    border-radius: 99px;
    font-size: 12px;
}

/* Main Content */
.main {
    flex: 1;
    padding: 24px 32px;
    max-width: 1440px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}

.topbar h1 { font-size: 24px; font-weight: 700; }

.topbar-actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

.search-input, .tag-filter-select {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 14px;
}

.search-input { width: 240px; }
.tag-filter-select { min-width: 185px; cursor: pointer; }

.search-input:focus, .tag-filter-select:focus { outline: none; border-color: var(--green); }

.btn {
    padding: 10px 16px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    border: none;
    transition: 0.15s;
}

.btn-primary { background: var(--green); color: #0b131e; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: var(--bg-card); color: var(--text-primary); border: 1px solid var(--border-color); }
.btn-secondary:hover { background: var(--bg-card-hover); }

/* Summary KPI Cards */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.kpi-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 18px 20px;
}

.kpi-label { font-size: 13px; color: var(--text-secondary); margin-bottom: 6px; }
.kpi-value { font-size: 26px; font-weight: 700; }
.kpi-value.up { color: var(--green); }
.kpi-value.down { color: var(--red); }

/* Gateway List */
.gateway-list { display: flex; flex-direction: column; gap: 10px; }

.gateway-row {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 14px 20px;
    display: grid;
    grid-template-columns: 2.2fr 1.5fr 2.3fr 1.1fr auto;
    align-items: center;
    gap: 16px;
    transition: background 0.15s;
}

.gateway-row:hover { background: var(--bg-card-hover); }
.gateway-row.is-down { border-color: rgba(223, 72, 74, 0.55); background: rgba(223, 72, 74, 0.06); }

.gw-info { display: flex; align-items: center; gap: 14px; min-width: 0; }

.status-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}

.status-dot.UP { background: var(--green); box-shadow: 0 0 8px var(--green); }
.status-dot.DOWN { background: var(--red); animation: pulse-red 1.4s infinite; }
.status-dot.PAUSED { background: var(--amber); }

@keyframes pulse-red {
    0% { box-shadow: 0 0 0 0 rgba(223, 72, 74, 0.7); }
    70% { box-shadow: 0 0 0 9px rgba(223, 72, 74, 0); }
    100% { box-shadow: 0 0 0 0 rgba(223, 72, 74, 0); }
}

.gw-title-line {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.gw-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.tag-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 99px;
    font-size: 11px;
    font-weight: 600;
    background: var(--blue-bg);
    color: var(--blue);
    border: 1px solid rgba(56, 189, 248, 0.3);
    white-space: nowrap;
    flex-shrink: 0;
}

.gw-meta { font-size: 12px; color: var(--text-secondary); font-family: monospace; margin-top: 3px; }

/* Live Timer Badge */
.timer-box {
    display: inline-flex;
    flex-direction: column;
    padding: 6px 12px;
    border-radius: 7px;
    font-size: 12px;
}

.timer-box.UP { background: var(--green-bg); color: var(--green); }
.timer-box.DOWN { background: var(--red-bg); color: #ff7b7d; font-weight: 600; }
.timer-box.PAUSED { background: var(--amber-bg); color: var(--amber); }

.timer-clock { font-family: monospace; font-size: 13px; margin-top: 2px; }

/* Uptime Bars + Avanzamento Ciclo 5 Minuti */
.bars-wrap { display: flex; flex-direction: column; gap: 5px; }
.bars-container { display: flex; align-items: center; gap: 2px; height: 26px; }

.ping-bar {
    flex: 1;
    min-width: 2px;
    max-width: 5px;
    height: 24px;
    border-radius: 2px;
    background: #2b374d;
    transition: transform 0.12s, background-color 0.25s;
}

.ping-bar:hover { transform: scaleY(1.22); }
.ping-bar.UP { background: var(--green); }
.ping-bar.DOWN { background: var(--red); }

.ping-bar.latest {
    animation: bar-pop 0.75s ease-out;
}

@keyframes bar-pop {
    0% { transform: scaleY(1.32); filter: brightness(1.45); }
    100% { transform: scaleY(1); filter: brightness(1); }
}

.cycle-track {
    width: 100%;
    height: 3px;
    background: #243044;
    border-radius: 99px;
    overflow: hidden;
}

.cycle-fill {
    height: 100%;
    width: 0%;
    transition: width 0.9s linear;
}

.cycle-fill.UP { background: var(--green); }
.cycle-fill.DOWN { background: var(--red); }
.cycle-fill.PAUSED { background: var(--amber); width: 0% !important; }

.bars-footer {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--text-secondary);
}

.uptime-pill {
    font-weight: 700;
    font-size: 14px;
    text-align: right;
}

.row-actions { display: flex; gap: 6px; }
.icon-btn {
    background: #253146;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
}
.icon-btn:hover { color: var(--text-primary); background: #2f3e57; }
.icon-btn.danger:hover { color: #ff7b7d; border-color: var(--red); }

/* Modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(8, 12, 20, 0.78);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal-backdrop.open { display: flex; }

.modal {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    width: 100%;
    max-width: 460px;
    padding: 24px;
}

.modal h2 { margin-bottom: 18px; font-size: 18px; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 6px; }
.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    border-radius: 7px;
    border: 1px solid var(--border-color);
    background: var(--bg-main);
    color: var(--text-primary);
    font-size: 14px;
}

.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

@media (max-width: 1100px) {
    body { flex-direction: column; }
    .sidebar { width: 100%; flex-direction: row; align-items: center; justify-content: space-between; flex-wrap: wrap; }
    .brand-wrapper { flex-direction: row; align-items: center; }
    .brand-logo-card { max-width: 210px; }
    .gateway-row { grid-template-columns: 1fr; }
    .uptime-pill { text-align: left; }
}