:root {
  --bg: #f4f6f4;
  --panel: #ffffff;
  --panel-soft: #eef3ee;
  --ink: #17211a;
  --muted: #637065;
  --line: #d9e1da;
  --green: #1f8a4c;
  --green-2: #6caf3f;
  --blue: #2477a8;
  --amber: #b7791f;
  --red: #b33a3a;
  --shadow: 0 16px 38px rgba(24, 40, 28, 0.09);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  font: inherit;
}

input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: #142119;
  color: #f5fbf6;
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  gap: 28px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 6px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #2fb164;
  color: #fff;
  font-weight: 800;
}

.brand strong,
.brand span {
  display: block;
}

.brand span {
  color: #aebcaf;
  font-size: 13px;
  margin-top: 2px;
}

.nav {
  display: grid;
  gap: 6px;
}

.nav-item {
  color: #dce8de;
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 11px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  cursor: pointer;
}

.nav-item:hover,
.nav-item.active {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.sidebar-footer {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: #bfcac1;
  font-size: 13px;
  padding: 8px;
}

.sidebar-footer strong,
.sidebar-footer span {
  display: block;
}

.sidebar-footer strong {
  color: #f5fbf6;
}

.connection-footer {
  margin-top: 0;
  justify-content: flex-start;
}

.logout-button {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: #f5fbf6;
  cursor: pointer;
}

.logout-button:hover {
  background: rgba(255, 255, 255, 0.16);
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--amber);
}

.dot.ok {
  background: #3ec66b;
}

.dot.bad {
  background: var(--red);
}

.main {
  min-width: 0;
  padding: 22px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 18px;
}

.topbar h1 {
  margin: 0;
  font-size: 26px;
  letter-spacing: 0;
}

.topbar p {
  margin: 5px 0 0;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.period-filter {
  min-width: 150px;
  display: grid;
  gap: 3px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 10px;
}

.valid-filter {
  min-height: 56px;
  display: flex;
  align-items: center;
  gap: 9px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 12px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.valid-filter input {
  width: 16px;
  height: 16px;
  accent-color: var(--green);
}

.valid-filter.locked {
  color: var(--muted);
}

.period-filter span {
  color: var(--muted);
  font-size: 12px;
}

.period-filter select {
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
}

.meta {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 12px;
  min-width: 150px;
}

.meta span,
.meta strong {
  display: block;
}

.meta span {
  color: var(--muted);
  font-size: 12px;
}

.meta strong {
  font-size: 13px;
  margin-top: 2px;
}

.icon-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  cursor: pointer;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.kpi-grid.compact {
  grid-template-columns: repeat(4, minmax(150px, 1fr));
}

.kpi,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.kpi {
  padding: 16px;
  min-height: 108px;
}

.kpi.health-ok {
  border-color: rgba(31, 138, 76, 0.35);
  background: #f3fbf6;
}

.kpi.health-bad {
  border-color: rgba(179, 58, 58, 0.45);
  background: #fff4f4;
}

.kpi.health-ok strong {
  color: var(--green);
}

.kpi.health-bad strong {
  color: var(--red);
}

.kpi span,
.kpi small {
  color: var(--muted);
}

.kpi span {
  font-size: 13px;
}

.kpi strong {
  display: block;
  font-size: 24px;
  margin: 8px 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kpi small {
  font-size: 12px;
}

.panel-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}

.panel-grid.four {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.overview-stats {
  margin-top: -2px;
}

.panel {
  min-width: 0;
  overflow: hidden;
}

.panel-header {
  height: 58px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-header h2 {
  margin: 0;
  font-size: 16px;
}

.panel-header span {
  color: var(--muted);
  font-size: 12px;
}

.chart-wrap {
  height: 300px;
  padding: 14px;
  min-width: 0;
  width: 100%;
  overflow: hidden;
}

.chart-wrap.short {
  height: 270px;
}

.chart-wrap canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

.table-wrap {
  overflow: auto;
  max-height: 560px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background: var(--panel-soft);
  z-index: 1;
  color: #26362a;
  font-weight: 700;
}

td {
  color: #2e3c32;
}

[data-icon] {
  width: 18px;
  height: 18px;
  display: inline-block;
  background: currentColor;
  mask-size: contain;
  mask-repeat: no-repeat;
  mask-position: center;
}

[data-icon="gauge"] {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m12 14 4-4'/%3E%3Cpath d='M3.34 19a10 10 0 1 1 17.32 0'/%3E%3C/svg%3E");
}

[data-icon="activity"] {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M22 12h-4l-3 9L9 3l-3 9H2'/%3E%3C/svg%3E");
}

[data-icon="flask"] {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M9 3h6'/%3E%3Cpath d='M10 3v6l-6 10a2 2 0 0 0 1.7 3h12.6a2 2 0 0 0 1.7-3L14 9V3'/%3E%3Cpath d='M7.5 14h9'/%3E%3C/svg%3E");
}

[data-icon="sensor"] {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='7' y='7' width='10' height='10' rx='2'/%3E%3Cpath d='M7 2v2'/%3E%3Cpath d='M12 2v2'/%3E%3Cpath d='M17 2v2'/%3E%3Cpath d='M7 20v2'/%3E%3Cpath d='M12 20v2'/%3E%3Cpath d='M17 20v2'/%3E%3Cpath d='M2 7h2'/%3E%3Cpath d='M2 12h2'/%3E%3Cpath d='M2 17h2'/%3E%3Cpath d='M20 7h2'/%3E%3Cpath d='M20 12h2'/%3E%3Cpath d='M20 17h2'/%3E%3C/svg%3E");
}

[data-icon="refresh"] {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8'/%3E%3Cpath d='M3 3v5h5'/%3E%3Cpath d='M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16'/%3E%3Cpath d='M16 16h5v5'/%3E%3C/svg%3E");
}

[data-icon="logout"] {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4'/%3E%3Cpath d='m16 17 5-5-5-5'/%3E%3Cpath d='M21 12H9'/%3E%3C/svg%3E");
}

.login-page {
  min-height: 100vh;
  background: #142119;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 28px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
}

.login-brand {
  padding: 0;
  margin-bottom: 22px;
}

.login-brand strong {
  color: var(--ink);
}

.login-panel h1 {
  margin: 0;
  font-size: 26px;
}

.login-panel p {
  margin: 8px 0 22px;
  color: var(--muted);
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.login-form input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  color: var(--ink);
}

.login-form input:focus {
  outline: 2px solid rgba(31, 138, 76, 0.22);
  border-color: var(--green);
}

.login-form button {
  border: 0;
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--green);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
}

.login-error {
  border: 1px solid rgba(179, 58, 58, 0.35);
  border-radius: 8px;
  padding: 10px 12px;
  background: #fff4f4;
  color: var(--red);
  font-size: 13px;
}

@media (max-width: 1040px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 10;
    flex-direction: row;
    align-items: center;
    gap: 14px;
    padding: 10px;
  }

  .nav {
    display: flex;
    overflow-x: auto;
  }

  .sidebar-footer {
    display: none;
  }

  .kpi-grid,
  .panel-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 680px) {
  .main {
    padding: 14px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .meta,
  .period-filter,
  .valid-filter {
    flex: 1;
  }
}

/* Responsive overrides for narrow tablets and phones. Keep these last. */
body {
  overflow-x: hidden;
}

.app-shell,
.main,
.panel,
.kpi,
.topbar,
.topbar-actions,
.chart-wrap {
  min-width: 0;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 216px minmax(0, 1fr);
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .kpi-grid,
  .kpi-grid.compact,
  .panel-grid,
  .panel-grid.four {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 900px) {
  .app-shell {
    display: block;
    width: 100%;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 20;
    width: 100%;
    min-width: 0;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .brand {
    flex: 0 0 auto;
    min-width: 0;
    gap: 8px;
    padding: 0;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
  }

  .brand strong {
    font-size: 14px;
    line-height: 1.1;
  }

  .brand span {
    display: none;
  }

  .nav {
    display: flex;
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .nav::-webkit-scrollbar {
    display: none;
  }

  .nav-item {
    flex: 0 0 auto;
    gap: 7px;
    padding: 9px 10px;
    white-space: nowrap;
    font-size: 14px;
  }

  .nav-item [data-icon] {
    width: 16px;
    height: 16px;
  }

  .sidebar-footer {
    display: none;
  }

  .main {
    width: 100%;
    padding: 16px;
    overflow: hidden;
  }
}

@media (max-width: 680px) {
  .main {
    padding: 12px;
  }

  .sidebar {
    display: block;
    padding: 10px;
  }

  .sidebar .brand {
    margin-bottom: 8px;
  }

  .sidebar .nav {
    width: 100%;
  }

  .sidebar .nav-item {
    flex: 1 1 0;
    justify-content: center;
    min-width: 0;
    padding-left: 6px;
    padding-right: 6px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 12px;
  }

  .topbar h1 {
    font-size: 22px;
    line-height: 1.15;
  }

  .topbar p {
    font-size: 14px;
    line-height: 1.35;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 48px;
    gap: 8px;
    width: 100%;
  }

  .period-filter,
  .valid-filter,
  .meta {
    min-width: 0;
    width: 100%;
  }

  .period-filter,
  .valid-filter {
    grid-column: 1 / -1;
  }

  .valid-filter {
    min-height: 48px;
    white-space: normal;
  }

  .meta {
    min-height: 48px;
  }

  .icon-button {
    width: 48px;
    height: 48px;
  }

  .kpi-grid,
  .kpi-grid.compact,
  .panel-grid,
  .panel-grid.four,
  .overview-stats {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
    margin-bottom: 10px;
  }

  .kpi {
    min-height: 92px;
    padding: 14px;
  }

  .kpi strong {
    font-size: 22px;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .panel-header {
    min-height: 54px;
    height: auto;
    padding: 12px 14px;
  }

  .panel-header h2 {
    font-size: 15px;
  }

  .panel-header span {
    text-align: right;
  }

  .chart-wrap,
  .chart-wrap.short {
    height: 235px;
    padding: 10px;
  }

  .table-wrap {
    max-height: 440px;
    -webkit-overflow-scrolling: touch;
  }

  table {
    font-size: 12px;
  }

  th,
  td {
    padding: 8px;
  }

  .login-shell {
    padding: 14px;
  }

  .login-panel {
    padding: 22px;
  }

  .login-panel h1 {
    font-size: 23px;
  }
}

@media (max-width: 430px) {
  .main {
    padding: 10px;
  }

  .topbar h1 {
    font-size: 20px;
  }

  .nav-item {
    gap: 4px;
    padding-left: 4px;
    padding-right: 4px;
    font-size: 13px;
  }

  .nav-item [data-icon] {
    width: 14px;
    height: 14px;
  }

  .nav-item span:last-child {
    max-width: none;
  }

  .chart-wrap,
  .chart-wrap.short {
    height: 220px;
  }

  .kpi strong {
    font-size: 20px;
  }
}
