:root {
  color-scheme: dark;
  --bg: #041916;
  --surface: #08241f;
  --surface-2: #0d3029;
  --surface-3: #123c34;
  --border: #22564b;
  --text: #f2f8f6;
  --muted: #a7bbb6;
  --primary: #b6e431;
  --primary-ink: #173000;
  --teal: #18a7a0;
  --success: #38cf8d;
  --warning: #f0c85a;
  --danger: #ff6b74;
  --shadow: 0 18px 48px rgba(0, 0, 0, .28);
  --radius: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef5f2;
  --surface: #ffffff;
  --surface-2: #e7f0ed;
  --surface-3: #d8e7e2;
  --border: #b7d0c8;
  --text: #102b25;
  --muted: #587069;
  --primary: #8cc817;
  --primary-ink: #132600;
  --teal: #087f7a;
  --success: #138452;
  --warning: #9a6500;
  --danger: #c32d3a;
  --shadow: 0 18px 45px rgba(18, 62, 52, .13);
}

* {
  box-sizing: border-box;
  letter-spacing: 0;
}

html,
body {
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

body {
  min-width: 280px;
}

button,
input,
select {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

.login-view {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
  background:
    linear-gradient(135deg, rgba(24, 167, 160, .13), transparent 42%),
    var(--bg);
}

.login-panel {
  width: min(100%, 420px);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand-logo {
  display: block;
  width: clamp(122px, 22vw, 184px);
  max-height: 58px;
  object-fit: contain;
  object-position: left center;
}

.brand-logo--login {
  width: min(100%, 278px);
  max-height: 112px;
  margin: 0 auto 18px;
  object-position: center;
}

.product-tag,
.eyebrow {
  color: var(--primary);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.login-panel h1 {
  margin: 8px 0 22px;
  text-align: center;
  font-size: 28px;
}

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

.login-form label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.login-form input,
.machine-bar select,
.compact-select {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 12px;
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}

.login-form input:focus,
.machine-bar select:focus,
.compact-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 24%, transparent);
}

.button {
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0 16px;
  font-weight: 800;
}

.button--primary {
  margin-top: 10px;
  background: var(--primary);
  color: var(--primary-ink);
}

.button--secondary {
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text);
}

.button--danger {
  border-color: color-mix(in srgb, var(--danger) 72%, var(--border));
  background: color-mix(in srgb, var(--danger) 18%, var(--surface-2));
  color: var(--danger);
}

.button--small {
  min-height: 36px;
  padding: 0 12px;
  font-size: 13px;
}

.button:disabled {
  opacity: .55;
  cursor: wait;
}

.form-error {
  margin: 4px 0 0;
  color: var(--danger);
  font-size: 14px;
}

.readonly-note {
  margin-top: 18px;
  color: var(--muted);
  text-align: center;
  font-size: 12px;
}

.app-shell {
  min-height: 100dvh;
  padding-bottom: calc(76px + var(--safe-bottom));
}

.topbar {
  position: sticky;
  z-index: 30;
  top: 0;
  min-height: calc(68px + var(--safe-top));
  display: grid;
  grid-template-columns: minmax(112px, 1fr) auto 42px 42px;
  align-items: end;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
}

.topbar__state {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
}

.status-dot {
  width: 9px;
  height: 9px;
  flex: 0 0 9px;
  border-radius: 50%;
  background: var(--muted);
}

.status-dot--live {
  background: var(--success);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--success) 17%, transparent);
}

.status-dot--stale {
  background: var(--warning);
}

.status-dot--offline {
  background: var(--danger);
}

.icon-button {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 19px;
  line-height: 1;
}

.machine-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px 12px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.machine-bar label {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.machine-controller {
  align-self: center;
  max-width: 34vw;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.content {
  width: min(100%, 1280px);
  margin: 0 auto;
  padding: 18px 14px 28px;
}

.view-panel {
  min-width: 0;
}

.section-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.section-heading h2 {
  margin: 3px 0 0;
  font-size: clamp(22px, 5vw, 30px);
}

.freshness,
.count-badge {
  display: inline-flex;
  min-height: 30px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0 10px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.freshness--live {
  border-color: color-mix(in srgb, var(--success) 58%, var(--border));
  color: var(--success);
}

.freshness--stale {
  color: var(--warning);
}

.freshness--offline {
  color: var(--danger);
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.kpi-card {
  min-width: 0;
  min-height: 92px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.kpi-card__label,
.metric-card__label {
  overflow-wrap: anywhere;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.kpi-card__value {
  min-width: 0;
  overflow: hidden;
  font-size: clamp(20px, 6vw, 30px);
  font-weight: 780;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kpi-card__unit {
  margin-left: 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.metric-sections {
  display: grid;
  gap: 14px;
  margin-top: 16px;
}

.metric-group,
.chart-panel {
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.metric-group h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
}

.metric-card {
  min-width: 0;
  padding: 10px;
  border: 1px solid color-mix(in srgb, var(--border) 80%, transparent);
  border-radius: 6px;
  background: var(--surface-2);
}

.metric-card__value {
  display: block;
  min-width: 0;
  margin-top: 6px;
  overflow: hidden;
  font-size: 17px;
  font-weight: 760;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.compact-select {
  width: auto;
  min-width: 94px;
  min-height: 38px;
}

.chart-frame {
  position: relative;
  width: 100%;
  min-height: 300px;
  aspect-ratio: 16 / 10;
}

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

.chart-legend {
  min-height: 30px;
  display: flex;
  flex-wrap: wrap;
  gap: 7px 12px;
  margin-bottom: 8px;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.legend-color {
  width: 10px;
  height: 10px;
  border-radius: 3px;
}

.event-list,
.session-list {
  display: grid;
  gap: 8px;
}

.event-card,
.session-card {
  min-width: 0;
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.event-card {
  display: grid;
  grid-template-columns: 4px minmax(0, 1fr);
  gap: 11px;
}

.event-severity {
  border-radius: 999px;
  background: var(--warning);
}

.event-severity--alarm {
  background: var(--danger);
}

.event-card h3,
.session-card h3 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 15px;
}

.event-meta,
.session-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-top: 7px;
  color: var(--muted);
  font-size: 12px;
}

.session-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.download-link {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 12px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
  text-decoration: none;
}

.subheading {
  margin: 22px 0 10px;
  font-size: 17px;
}

.empty-state {
  padding: 24px 12px;
  color: var(--muted);
  text-align: center;
}

.bottom-nav {
  position: fixed;
  z-index: 40;
  right: 0;
  bottom: 0;
  left: 0;
  min-height: calc(68px + var(--safe-bottom));
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  padding: 5px 5px var(--safe-bottom);
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  backdrop-filter: blur(12px);
}

.nav-button {
  min-width: 0;
  min-height: 58px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 3px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
}

.nav-button--active {
  background: color-mix(in srgb, var(--primary) 14%, transparent);
  color: var(--primary);
}

.nav-symbol {
  min-width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  font-size: 18px;
  font-weight: 900;
}

.console-connection,
.console-note {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
}

.control-panel {
  margin: 0 0 20px;
  padding: 16px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: 6px;
  background: var(--surface-2);
}

.control-panel__heading {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 14px;
}

.control-panel__heading h3,
.control-card h4 {
  margin: 4px 0 0;
}

.control-status {
  flex: 0 0 auto;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.control-status[data-state="ready"] {
  border-color: color-mix(in srgb, var(--success) 55%, var(--border));
  color: var(--success);
}

.control-status[data-state="busy"] {
  border-color: color-mix(in srgb, var(--warning) 55%, var(--border));
  color: var(--warning);
}

.control-status[data-state="offline"],
.control-status[data-state="error"] {
  border-color: color-mix(in srgb, var(--danger) 55%, var(--border));
  color: var(--danger);
}

.control-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
}

.control-card {
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.control-card label,
.control-card p,
.setpoint-readout span {
  color: var(--muted);
  font-size: 12px;
}

.control-card label {
  display: block;
  margin: 14px 0 6px;
}

.control-card .compact-select {
  width: 100%;
}

.control-actions {
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr 1fr;
  margin-top: 10px;
}

.setpoint-readout {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 12px 0;
}

.setpoint-readout strong {
  color: var(--primary);
  font-size: 24px;
}

.setpoint-stepper {
  min-height: 48px;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  gap: 8px;
  align-items: center;
}

.setpoint-stepper > span {
  overflow-wrap: anywhere;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.step-button {
  width: 48px;
  height: 48px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-3);
  color: var(--text);
  font-size: 24px;
  font-weight: 800;
}

.step-button:disabled,
.control-card .button:disabled {
  opacity: .45;
}

.control-result {
  min-height: 20px;
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.control-result[data-state="verified"] {
  color: var(--success);
}

.control-result[data-state="error"],
.control-result[data-state="unconfirmed"] {
  color: var(--warning);
}

.console-values {
  overflow: hidden;
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
}

.console-row {
  min-height: 44px;
  display: grid;
  grid-template-columns: minmax(78px, 0.65fr) minmax(120px, 1.5fr) minmax(80px, 0.8fr);
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

.console-row:last-child {
  border-bottom: 0;
}

.console-row__group {
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}

.console-row__label {
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 12px;
}

.console-row__value {
  color: var(--primary);
  font-size: 13px;
  font-weight: 800;
  text-align: right;
}

.console-note {
  margin-top: 12px;
}

.command-dialog {
  width: min(92vw, 500px);
  max-height: min(86dvh, 640px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.command-dialog::backdrop {
  background: rgb(0 0 0 / 72%);
}

.command-dialog__content {
  padding: 20px;
}

.command-dialog__content h2 {
  margin: 6px 0 12px;
}

.command-dialog__content > p {
  color: var(--muted);
  line-height: 1.55;
}

.command-confirmation {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  margin: 18px 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  font-size: 13px;
  line-height: 1.45;
}

.command-confirmation input {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--primary);
}

.command-dialog__actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.toast {
  position: fixed;
  z-index: 100;
  right: 14px;
  bottom: calc(82px + var(--safe-bottom));
  left: 14px;
  max-width: 520px;
  margin: auto;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-3);
  box-shadow: var(--shadow);
  color: var(--text);
  font-size: 13px;
}

@media (min-width: 700px) {
  .app-shell {
    padding-bottom: 0;
  }

  .topbar {
    grid-template-columns: 1fr auto 42px 42px;
    padding-inline: 24px;
  }

  .machine-bar {
    grid-template-columns: minmax(280px, 560px) auto;
    padding-inline: 24px;
  }

  .content {
    padding: 24px 24px 100px;
  }

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

  .metric-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .bottom-nav {
    right: 24px;
    bottom: 18px;
    left: 24px;
    width: min(620px, calc(100% - 48px));
    min-height: 62px;
    margin: auto;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
  }
}

@media (min-width: 1040px) {
  .metric-sections {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

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

  .chart-frame {
    min-height: 430px;
    aspect-ratio: 16 / 7;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition: none !important;
    animation: none !important;
  }
}
