/* Smart Plant - Lab Instrument Panel Design */
/* Palette: warm neutral paper #F4F2EC (light) / #151512 (dark) */
/* Text: #1F1F1B (light) / #E9E7DF (dark) */
/* Accent OK: moss green #4A6B3A (dark: #8DB27A) */
/* Accent Warning: brick red #B5452E (dark: #E0785F) */
/* Max 2 accent colors. 1px lines, 4-6px radius. 4/8px spacing. */
/* Fonts: IBM Plex Sans (text), IBM Plex Mono (numbers) */

:root {
  --bg: #F4F2EC;
  --text: #1F1F1B;
  --text-muted: #6B6863;
  --border: #D8D0C4;
  --panel-bg: #FAF8F3;
  --accent-ok: #4A6B3A;
  --accent-ok-dark: #3D572F;
  --accent-warn: #B5452E;
  --accent-warn-dark: #943826;
  --focus-ring: #4A6B3A;
  --input-bg: #FFFFFF;
  --btn-primary-bg: #1F1F1B;
  --btn-primary-text: #F4F2EC;
  --btn-ghost-bg: transparent;
  --btn-ghost-text: #1F1F1B;
  --seg-active-bg: #1F1F1B;
  --seg-active-text: #F4F2EC;
  --seg-inactive-bg: transparent;
  --seg-inactive-text: #1F1F1B;
  --seg-border: #D8D0C4;
  --error-border: #B5452E;
  --error-bg: #FDF0ED;
  --chart-grid: #D8D0C4;
  --chart-axis: #1F1F1B;
  --chart-line-ok: #4A6B3A;
  --chart-line-warn: #B5452E;
  --chart-threshold: #B5452E;
  --table-header-bg: #F0EDE5;
  --scrollbar-thumb: #C4BCB0;
  --scrollbar-track: #F0EDE5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151512;
    --text: #E9E7DF;
    --text-muted: #9A968F;
    --border: #3A3530;
    --panel-bg: #1C1B18;
    --accent-ok: #8DB27A;
    --accent-ok-dark: #A3C996;
    --accent-warn: #E0785F;
    --accent-warn-dark: #F0967A;
    --focus-ring: #8DB27A;
    --input-bg: #1F1E1B;
    --btn-primary-bg: #F4F2EC;
    --btn-primary-text: #151512;
    --btn-ghost-bg: transparent;
    --btn-ghost-text: #E9E7DF;
    --seg-active-bg: #F4F2EC;
    --seg-active-text: #151512;
    --seg-inactive-bg: transparent;
    --seg-inactive-text: #E9E7DF;
    --seg-border: #3A3530;
    --error-border: #E0785F;
    --error-bg: #3A2520;
    --chart-grid: #3A3530;
    --chart-axis: #E9E7DF;
    --chart-line-ok: #8DB27A;
    --chart-line-warn: #E0785F;
    --chart-threshold: #E0785F;
    --table-header-bg: #1F1E1B;
    --scrollbar-thumb: #4A4540;
    --scrollbar-track: #1F1E1B;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

* {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  margin: 0;
  padding: 0;
  background-color: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;
  font-size: 1rem;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
  font-family: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;
  font-weight: 600;
  line-height: 1.3;
  margin: 0 0 0.5rem;
}

.panel-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 1rem;
}

.sensor-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 500;
  display: block;
  margin-bottom: 0.375rem;
}

.sensor-value {
  font-family: 'IBM Plex Mono', 'SF Mono', 'Fira Code', monospace;
  font-size: 2.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  color: var(--text);
  display: block;
}

.sensor-value.pump-state {
  font-size: 1.25rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.sensor-unit {
  font-family: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-left: 0.25rem;
  vertical-align: text-bottom;
}

.sensor-value.warn,
.sensor-label.warn {
  color: var(--accent-warn);
}

/* Top Bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  background-color: var(--panel-bg);
  position: sticky;
  top: 0;
  z-index: 100;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.topbar-left {
  flex: 1;
  min-width: 120px;
}

.brand {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.topbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 160px;
  order: 3;
  width: 100%;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

@media (min-width: 640px) {
  .topbar-center {
    order: 2;
    width: auto;
    margin-top: 0;
    padding-top: 0;
    border-top: none;
  }
}

.conn-status {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-family: 'IBM Plex Sans', sans-serif;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--accent-warn);
  flex-shrink: 0;
  transition: background-color 0.2s ease;
}

.conn-dot.online {
  background-color: var(--accent-ok);
}

.conn-text {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.75rem;
}

.topbar-right {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  min-width: 80px;
}

.btn-ghost {
  background: var(--btn-ghost-bg);
  color: var(--btn-ghost-text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5rem 1rem;
  font-family: 'IBM Plex Sans', sans-serif;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.btn-ghost:hover {
  border-color: var(--text);
}

.btn-ghost:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--focus-ring);
  border-color: var(--focus-ring);
}

/* Main Layout */
.main {
  flex: 1;
  padding: 1rem;
  max-width: 100%;
  width: 100%;
}

.panel {
  background-color: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  margin-bottom: 1rem;
}

/* Sensor Panel - 4 cells in one panel */
.sensor-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1px;
  background-color: var(--border);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

@media (min-width: 640px) {
  .sensor-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

.sensor-cell {
  background-color: var(--panel-bg);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  min-height: 100px;
}

.sensor-value-wrap {
  display: flex;
  align-items: flex-end;
  gap: 0.25rem;
  width: 100%;
  justify-content: center;
}

/* Control Panel */
.control-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.seg {
  display: inline-flex;
  background-color: var(--bg);
  border: 1px solid var(--seg-border);
  border-radius: 4px;
  overflow: hidden;
  width: fit-content;
}

.seg-btn {
  background: var(--seg-inactive-bg);
  color: var(--seg-inactive-text);
  border: none;
  padding: 0.5rem 1.25rem;
  font-family: 'IBM Plex Sans', sans-serif;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
  min-width: 80px;
}

.seg-btn:first-child {
  border-right: 1px solid var(--seg-border);
}

.seg-btn[aria-pressed="true"] {
  background: var(--seg-active-bg);
  color: var(--seg-active-text);
}

.seg-btn:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--focus-ring);
}

.btn-primary {
  background: var(--btn-primary-bg);
  color: var(--btn-primary-text);
  border: none;
  border-radius: 4px;
  padding: 0.75rem 1.5rem;
  font-family: 'IBM Plex Sans', sans-serif;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: opacity 0.15s ease, background-color 0.15s ease;
  min-height: 44px;
}

.btn-primary:hover:not(:disabled) {
  opacity: 0.9;
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--focus-ring);
}

.btn-full {
  width: 100%;
}

.hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
  margin: 0;
}

/* Charts */
.chart-panel {
  min-height: 260px;
}

.chart-wrap {
  position: relative;
  width: 100%;
  height: 200px;
}

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

.chart-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-family: 'IBM Plex Sans', sans-serif;
}

/* History Table */
.log-panel {
  overflow: hidden;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.log-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.log-table thead {
  background-color: var(--table-header-bg);
}

.log-table th,
.log-table td {
  padding: 0.5rem 0.75rem;
  text-align: right;
  border-bottom: 1px solid var(--border);
  font-family: 'IBM Plex Mono', monospace;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.log-table th:first-child,
.log-table td:first-child {
  text-align: left;
  font-family: 'IBM Plex Sans', sans-serif;
}

.log-table th {
  font-weight: 600;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
}

.log-table tbody tr:last-child td {
  border-bottom: none;
}

.log-table tbody tr:hover td {
  background-color: var(--bg);
}

.log-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem !important;
  font-family: 'IBM Plex Sans', sans-serif;
  font-size: 0.8rem;
}

/* Login Page */
.login-main {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-panel {
  background-color: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2rem;
  width: 100%;
  max-width: 360px;
}

.login-title {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 1.5rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 0.25rem;
  color: var(--text);
}

.login-subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.25rem;
}

.form-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  margin-bottom: 0.375rem;
  color: var(--text);
}

.form-input {
  width: 100%;
  padding: 0.75rem 1rem;
  font-family: 'IBM Plex Sans', sans-serif;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background-color: var(--input-bg);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-input:focus {
  outline: none;
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus-ring) 25%, transparent);
}

.form-input[aria-invalid="true"] {
  border-color: var(--error-border);
  background-color: var(--error-bg);
}

.form-error {
  font-size: 0.75rem;
  color: var(--accent-warn);
  margin: 0.375rem 0 0;
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.form-error::before {
  content: "";
  width: 3px;
  height: 0.875rem;
  background-color: var(--accent-warn);
  flex-shrink: 0;
}

.btn-primary.btn-full {
  margin-top: 0.5rem;
}

/* Loading state */
.btn-primary.loading {
  position: relative;
  color: transparent;
  pointer-events: none;
}

.btn-primary.loading::after {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  top: 50%;
  left: 50%;
  margin: -9px 0 0 -9px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  color: var(--btn-primary-text);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Focus visible for all interactive elements */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--focus-ring);
  border-radius: 2px;
}

/* Utility */
.hidden {
  display: none !important;
}

[hidden] {
  display: none !important;
}