* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: #1e2235;
  color: #eaeaea;
  font-family: 'Segoe UI', sans-serif;
}

/* --- Login --- */
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.login-container {
  background: #252e47;
  padding: 40px;
  border-radius: 8px;
  width: 100%;
  max-width: 400px;
}

h1 {
  font-size: 1.8rem;
  margin-bottom: 8px;
  color: #e94560;
}

h2 {
  font-size: 1.2rem;
  margin-bottom: 24px;
  color: #a0a0b0;
}

input {
  width: 100%;
  padding: 12px;
  margin-bottom: 16px;
  background: #163260;
  border: 1px solid #1e3d70;
  border-radius: 4px;
  color: #eaeaea;
  font-size: 1rem;
}

input::placeholder { color: #6a7a8a; }

button {
  width: 100%;
  padding: 12px;
  background: #e94560;
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 1rem;
  cursor: pointer;
}

button:hover { background: #c73652; }

p {
  color: #a0a0b0;
  margin-bottom: 16px;
  font-size: 0.95rem;
}

/* --- Buttons --- */
.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: opacity 0.2s;
  width: auto;
}

.btn:hover { opacity: 0.85; }

.btn-primary   { background: #00d4ff; color: #1a1a2e; }
.btn-secondary { background: #2a2a4a; color: #e0e0e0; }
.btn-warning   { background: #f0a500; color: #1a1a2e; }
.btn-danger    { background: #e05252; color: #fff; }
.btn-small     { padding: 4px 10px; font-size: 0.8rem; }

/* --- Badges --- */
.badge {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.badge-admin        { background: #4a1a6a; color: #da8fff; }
.badge-site_admin   { background: #6a3a1a; color: #ffc48f; }
.badge-engineer     { background: #1a3a6a; color: #8fbaff; }
.badge-operator     { background: #1a5a3a; color: #8fffc4; }
.badge-maintenance  { background: #1a4a5a; color: #8ff0ff; }
.badge-management   { background: #3a1a5a; color: #d48fff; }
.badge-viewer       { background: #3a3a1a; color: #fffb8f; }
.badge-active       { background: #1a5a3a; color: #8fffc4; }
.badge-locked       { background: #5a3a1a; color: #ffca8f; }
.badge-disabled     { background: #4a1a1a; color: #ff8f8f; }
.badge-warning      { background: #5a4a1a; color: #fffb8f; }

/* --- Alerts --- */
.error {
  background: #4a1a1a;
  color: #ff8f8f;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 0.85rem;
}

.hidden { display: none; }

/* --- Status messages --- */
.status-message {
  padding: 12px 16px;
  border-radius: 6px;
  margin-bottom: 16px;
  font-size: 0.9rem;
}

.status-message.success { background: #1a5a3a; color: #8fffc4; }
.status-message.error   { background: #4a1a1a; color: #ff8f8f; }
.status-message.hidden  { display: none; }

/* --- Table --- */
.table-wrapper { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  background: #252e47;
  border-radius: 8px;
  overflow: hidden;
}

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid #1e3d70;
  font-size: 0.9rem;
}

th {
  background: #163260;
  color: #00d4ff;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
}

tr:last-child td { border-bottom: none; }
tr:hover td { background: #1e2e52; }

/* --- Modals --- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden { display: none; }

.modal-box {
  background: #252e47;
  border: 1px solid #1e3d70;
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-box h3 { margin: 0; color: #00d4ff; }

.modal-box input,
.modal-box select {
  padding: 10px 12px;
  background: #1e2235;
  border: 1px solid #1e3d70;
  border-radius: 6px;
  color: #e0e0e0;
  font-size: 0.9rem;
  width: 100%;
  box-sizing: border-box;
}

.modal-note { font-size: 0.82rem; color: #999; margin: 0; }

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 8px;
}

/* --- Admin page layout --- */
.page-container {
  min-height: 100vh;
  background: #1e2235;
  color: #e0e0e0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 32px;
  background: #252e47;
  border-bottom: 1px solid #1e3d70;
}

.page-header h1 { font-size: 1.4rem; color: #00d4ff; margin: 0; }

.header-actions { display: flex; align-items: center; gap: 16px; }

.page-content { padding: 24px; min-height: calc(100vh - 53px); }

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.section-header h2 { margin: 0; font-size: 1.2rem; color: #00d4ff; }

/* --- Navbar --- */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: #252e47;
  border-bottom: 1px solid #1e3d70;
}

.nav-brand {
  font-size: 1rem;
  font-weight: 600;
  color: #00d4ff;
  white-space: nowrap;
}

.nav-links {
  display: flex;
  gap: 4px;
  flex: 1;
  justify-content: center;
  padding: 0 16px;
}

.nav-link {
  font-size: 0.85rem;
  padding: 6px 14px;
  border-radius: 6px;
  color: #aaa;
  text-decoration: none;
  transition: background 0.2s, color 0.2s;
  white-space: nowrap;
}

.nav-link:hover  { background: #1e2e52; color: #e0e0e0; }
.nav-link.active { background: #163260; color: #00d4ff; }

.nav-user { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.nav-username { font-size: 0.85rem; color: #888; }

/* --- Dashboard layout --- */
.page-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.page-title { font-size: 1.1rem; font-weight: 500; color: #00d4ff; margin: 0; }

/* --- Metric cards --- */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.metric-card { background: #252e47; border-radius: 8px; padding: 14px 16px; }

/* Stall highlight (e.g. steam-peeler "Time Since Last Batch" past the derived threshold).
   Uses an inset ring rather than a border so toggling it doesn't shift layout. */
.metric-card.stall { background: rgba(233, 69, 96, 0.10); box-shadow: inset 0 0 0 1px #e94560; }
.metric-card.stall .metric-value,
.metric-card.stall .metric-unit { color: #e94560; }

/* Setpoint-deviation highlight: live fryer setpoint no longer matches the active recipe target. */
.metric-card.sp-deviation { background: rgba(233, 69, 96, 0.10); box-shadow: inset 0 0 0 1px #e94560; }
.metric-card.sp-deviation .metric-value,
.metric-card.sp-deviation #metric-setpoint-dev { color: #e94560; }

.metric-label {
  font-size: 0.78rem;
  color: #aaa;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.metric-value { font-size: 1.6rem; font-weight: 500; color: #e0e0e0; line-height: 1; }
.metric-unit  { font-size: 0.78rem; color: #999; margin-top: 4px; }

.metric-card--sparkline { padding-bottom: 10px; }
.sparkline-wrap { position: relative; height: 36px; margin-top: 10px; }
.sparkline-wrap canvas { position: absolute; top: 0; left: 0; width: 100% !important; height: 100% !important; }

/* --- Dashboard cards --- */
.dash-card {
  background: #252e47;
  border: 1px solid #1e3d70;
  border-radius: 10px;
  padding: 16px 20px;
  margin-bottom: 20px;
}

.dash-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  font-size: 0.9rem;
  font-weight: 500;
  color: #00d4ff;
}

.dash-card-sub { font-size: 0.78rem; color: #888; font-weight: 400; }

.chart-time-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  /* Sit immediately after the card title, absorbing the free space to the right, so the
     time nav lands in the same place whether or not the header has a trailing control
     (e.g. the admin-only PID Output button on the temperature chart). Keeps the temperature
     and oil-flow chart navs visually aligned for every role. */
  margin-left: 16px;
  margin-right: auto;
}

.chart-wrapper { height: 220px; position: relative; }

/* --- Machine states --- */
.states-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}

.state-card { background: #1e2235; border-radius: 8px; padding: 10px 14px; }
.state-name  { font-size: 0.8rem; color: #aaa; margin-bottom: 6px; text-transform: capitalize; }

.state-badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 4px;
  text-transform: uppercase;
}

.badge-state-running  { background: #1a5a3a; color: #8fffc4; }
.badge-state-stopped  { background: #2a2a4a; color: #aaa; }
.badge-state-fault    { background: #5a1a1a; color: #ff8f8f; }
.badge-state-warning  { background: #5a4a1a; color: #fffb8f; }
.badge-state-disabled { background: #3a3a3a; color: #888; }

/* --- Alarm priority badges --- */
.badge-alarm-critical { background: #5a1a1a; color: #ff8f8f; }
.badge-alarm-warning  { background: #5a4a1a; color: #fffb8f; }
.badge-alarm-info     { background: #1a3a5a; color: #8fbaff; }

/* --- Alarm status badges --- */
.badge-alarm-active   { background: #5a1a1a; color: #ff6464; }
.badge-alarm-cleared  { background: #2a2a4a; color: #888; }

/* --- Alarm source badges --- */
.badge-source-plc    { background: #1a4a3a; color: #8fffc4; }
.badge-source-server { background: #3a2a5a; color: #d48fff; }

.state-time { font-size: 0.75rem; color: #777; margin-top: 4px; }
.state-placeholder { font-size: 0.85rem; color: #555; padding: 12px 0; }
.stale-banner {
  grid-column: 1 / -1;
  background: rgba(233, 69, 96, 0.1);
  border: 1px solid rgba(233, 69, 96, 0.35);
  color: #e94560;
  padding: 7px 12px;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  text-align: center;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.state-card-stale { opacity: 0.45; }
/* Process-alarm history list (dashboard) — compact rows inside a dash-card */
.alarm-tag-history th, .alarm-tag-history td { padding: 6px 10px; font-size: 0.8rem; white-space: nowrap; }

#conn-status {
  display: none;
  padding: 9px 20px;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-align: center;
  text-transform: uppercase;
  margin-bottom: 16px;
}
#conn-status.conn-warn {
  background: rgba(240, 165, 0, 0.12);
  border: 1px solid rgba(240, 165, 0, 0.35);
  color: #f0a500;
}
#conn-status.conn-error {
  background: rgba(233, 69, 96, 0.12);
  border: 1px solid rgba(233, 69, 96, 0.4);
  color: #e94560;
  animation: conn-pulse 2s ease-in-out infinite;
}
@keyframes conn-pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.45; }
}

/* --- Forms (settings pages) --- */
.form-label { display: block; font-size: 0.85rem; color: #bbb; margin-bottom: 6px; }
.alert { padding: 10px 14px; border-radius: 4px; font-size: 0.85rem; }
.alert-success { background: rgba(0,201,125,0.15); border: 1px solid #00c97d; color: #00c97d; }
.alert-error   { background: rgba(233,69,96,0.15);  border: 1px solid #e94560; color: #e94560; }

/* --- Bottom nav (mobile only) --- */
.bottom-nav { display: none; }

/* --- Single media query for all mobile overrides --- */
/* --- Machine States: shift-breakdown rows --- */
.msbreak-row     { margin-bottom: 14px; }
.msbreak-head    { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 0.85rem; margin-bottom: 5px; }
.msbreak-machine strong { color: #eaeaea; }
.msbreak-label   { color: #888; margin-left: 6px; }
.msbreak-metrics { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
.msbreak-meta    { color: #888; font-size: 0.78rem; }
.msbreak-avail   { font-weight: 700; font-size: 1rem; }
.msbreak-bar     { display: flex; height: 16px; border-radius: 3px; overflow: hidden; background: #0f1a30; }

/* --- Machine States: state timeline (Gantt) --- */
.tl-lane         { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.tl-lane-label   { width: 150px; flex: none; font-size: 0.8rem; color: #ccc; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tl-lane-machine { color: #eaeaea; }
.tl-lane-sub     { color: #666; font-size: 0.72rem; display: block; }
.tl-lane-track   { position: relative; flex: 1; height: 22px; background: #0f1a30; border-radius: 3px; overflow: hidden; }
.tl-axis         { display: flex; gap: 10px; margin-top: 6px; }
.tl-axis-spacer  { width: 150px; flex: none; }
.tl-axis-track   { position: relative; flex: 1; height: 14px; font-size: 0.7rem; color: #666; }

@media (max-width: 600px) {
  .navbar { padding: 10px 16px; }
  .nav-links { display: none; }

  .page-content { padding: 12px; padding-bottom: 72px; }

  .chart-time-nav { gap: 6px; }
  .chart-time-nav .dash-card-sub { min-width: 90px; text-align: center; }

  .metrics-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .metric-card  { padding: 10px 12px; }
  .metric-value { font-size: 1.3rem; }
  .metric-label { font-size: 0.7rem; }
  .metric-unit  { font-size: 0.7rem; }

  .dash-card    { padding: 12px 14px; }
  .chart-wrapper { height: 180px; }

  .states-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #252e47;
    border-top: 1px solid #1e3d70;
    z-index: 100;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8px 4px;
    text-decoration: none;
    color: #666;
    gap: 4px;
    transition: color 0.2s;
  }

  .bottom-nav-item.active { color: #00d4ff; }
  .bottom-nav-item:hover  { color: #aaa; }

  .bottom-nav-icon {
    width: 22px;
    height: 22px;
    stroke: currentColor;
  }

  .bottom-nav-label { font-size: 0.68rem; letter-spacing: 0.02em; }

  /* Machine States breakdown: stack name above metrics, give availability % prominence */
  .msbreak-head    { flex-direction: column; align-items: flex-start; gap: 2px; }
  .msbreak-metrics { width: 100%; }
  .msbreak-avail   { font-size: 1.05rem; }

  /* State timeline: full-width lanes with the label above the track */
  .tl-lane        { flex-direction: column; align-items: stretch; gap: 3px; }
  .tl-lane-label  { width: auto; text-align: left; white-space: normal; }
  .tl-lane-sub    { display: inline; margin-left: 6px; }
  /* flex:none so the 22px height survives the column flip (flex:1 would collapse it to 0) */
  .tl-lane-track  { width: 100%; flex: none; }
  .tl-axis-spacer { display: none; }

  /* "More" button reuses .bottom-nav-item; reset native button styling */
  #more-nav-btn { background: none; border: none; font: inherit; cursor: pointer; }

  /* "More" navigation sheet (only reachable from the bottom nav, so mobile-only) */
  .more-sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 109; }
  .more-sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 110;
    background: #252e47; border-top: 1px solid #1e3d70; border-radius: 14px 14px 0 0;
    padding: 6px 12px calc(10px + env(safe-area-inset-bottom));
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.45);
  }
  .more-sheet[hidden], .more-sheet-backdrop[hidden] { display: none; }
  .more-sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: #3a4a66; margin: 4px auto 8px; }
  .more-sheet-item {
    display: flex; align-items: center; gap: 12px; padding: 13px 10px;
    text-decoration: none; color: #cbd5e6; border-radius: 8px; font-size: 0.95rem;
  }
  .more-sheet-item.active { color: #00d4ff; background: #1e2742; }
  .more-sheet-icon { width: 20px; height: 20px; stroke: currentColor; flex: none; }
}

/* ---- V2 plant pages: filter bars, small tables, tabs ---- */
.filter-bar { display:flex; gap:10px; align-items:flex-end; flex-wrap:wrap; padding:4px 0; }
.filter-bar .f { display:flex; flex-direction:column; gap:4px; }
.filter-bar label { color:#aaa; font-size:11px; text-transform:uppercase; letter-spacing:0.05em; }
.filter-bar input, .filter-bar select, .inline-select { background:#0f3460; color:#e0e0e0; border:1px solid #1a4a7a; border-radius:4px;
  padding:5px 8px; font-size:13px; height:32px; box-sizing:border-box; margin-bottom:0; }
.card-title { color:#00d4ff; font-size:0.72rem; text-transform:uppercase; letter-spacing:0.08em; font-weight:600; margin-bottom:10px; }
.card-title .sub { color:#555; font-size:0.7rem; margin-left:8px; text-transform:none; letter-spacing:0; font-weight:400; }
.muted { color:#777; font-size:0.78rem; }
.pager { display:flex; gap:8px; align-items:center; justify-content:flex-end; margin-top:8px; }
.tabs { display:flex; gap:8px; margin-bottom:12px; flex-wrap:wrap; }
table.compact th, table.compact td { padding:6px 8px; font-size:0.8rem; }
td.num, th.num { text-align:right; font-variant-numeric:tabular-nums; }
.pill { font-size:.68rem; padding:2px 7px; border-radius:3px; text-transform:uppercase; letter-spacing:.06em; white-space:nowrap; }
.dot { display:inline-block; width:9px; height:9px; border-radius:50%; margin-right:6px; vertical-align:middle; }
pre.payload { white-space:pre-wrap; word-break:break-all; font-size:.72rem; color:#aaa; margin:0; max-width:520px; }
@media (max-width: 640px) {
  .msbreak-metrics { flex-wrap: wrap; white-space: normal; justify-content: space-between; }
  .dash-card-header { flex-wrap: wrap; }
  .chart-time-nav { flex-wrap: wrap; }
}
