/* ============================================================
   Mill Pulse · Mobile · Production Control Tower
   ============================================================ */

:root {
  --bg: #f4f6f6;
  --surface: #ffffff;
  --surface-2: #f7f9f9;
  --border: #e2e8e8;
  --border-2: #cdd5d5;
  --text: #0a3d3f;
  --text-2: #475a5b;
  --text-3: #94a3a4;

  --teal: #0d7d80;
  --teal-2: #13a5a8;
  --teal-bg: #e6f2f3;
  --teal-deep: #0a3d3f;
  --green: #10b981;
  --green-bg: #d1fae5;
  --amber: #f59e0b;
  --amber-bg: #fef3c7;
  --red: #ef4444;
  --red-bg: #fee2e2;
  --purple: #7c3aed;

  --shadow-sm: 0 1px 2px rgba(10, 61, 63, 0.04), 0 1px 3px rgba(10, 61, 63, 0.06);
  --shadow-md: 0 4px 14px rgba(10, 61, 63, 0.08);

  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-left: env(safe-area-inset-left);
  --safe-right: env(safe-area-inset-right);

  --appbar-h: 56px;
  --tabbar-h: 44px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { min-height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overflow-x: hidden;
}
button { font: inherit; cursor: pointer; border: 0; background: none; color: inherit; -webkit-tap-highlight-color: transparent; }
a { color: inherit; text-decoration: none; -webkit-tap-highlight-color: transparent; }
ul { list-style: none; }
img, svg { display: block; max-width: 100%; }

/* ============ APP BAR ============ */
.appbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  gap: 8px;
  align-items: center;
  padding: var(--safe-top) max(12px, var(--safe-right)) 0 max(12px, var(--safe-left));
  height: calc(var(--appbar-h) + var(--safe-top));
  background: linear-gradient(135deg, var(--teal-deep) 0%, var(--teal) 100%);
  color: #fff;
}
.appbar-back {
  width: 40px; height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: #fff;
  transition: background 0.15s var(--ease), transform 0.2s var(--ease);
}
.appbar-back:active { background: rgba(255,255,255,0.12); transform: scale(0.94); }
.appbar-back svg { width: 22px; height: 22px; }
.appbar-meta { text-align: center; min-width: 0; }
.appbar-eyebrow {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.8;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  line-height: 1;
}
.live-dot {
  width: 6px; height: 6px;
  background: #4ade80;
  border-radius: 50%;
  box-shadow: 0 0 6px #4ade80;
  animation: livepulse 1.5s infinite;
}
@keyframes livepulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.appbar-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.015em;
  margin-top: 2px;
}
.bell-btn {
  width: 40px; height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: #fff;
  position: relative;
  justify-self: end;
  transition: background 0.15s var(--ease);
}
.bell-btn:active { background: rgba(255,255,255,0.12); }
.bell-btn svg { width: 20px; height: 20px; }
.bell-badge {
  position: absolute;
  top: 6px; right: 6px;
  background: var(--red);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 999px;
  min-width: 16px;
  text-align: center;
  border: 2px solid var(--teal-deep);
}

/* ============ TABS ============ */
.tabs {
  position: sticky;
  top: calc(var(--appbar-h) + var(--safe-top));
  z-index: 49;
  display: flex;
  gap: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 max(12px, var(--safe-left));
  height: var(--tabbar-h);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  position: relative;
  padding: 0 14px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-3);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: color 0.2s var(--ease);
}
.tab.is-active { color: var(--text); font-weight: 600; }
.tab.is-active::after {
  content: '';
  position: absolute;
  left: 8px; right: 8px;
  bottom: 0;
  height: 3px;
  background: var(--teal);
  border-radius: 3px 3px 0 0;
}
.tab-num {
  background: var(--red);
  color: #fff;
  font-family: 'JetBrains Mono', monospace;
  font-size: 9.5px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 999px;
  min-width: 16px;
  text-align: center;
  letter-spacing: 0;
}

/* ============ CONTENT ============ */
.content {
  padding-bottom: max(24px, var(--safe-bottom));
}
.tab-panel { display: none; }
.tab-panel.is-active { display: block; animation: panelFade 0.25s var(--ease); }
@keyframes panelFade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============ PLANT STRIP ============ */
.plant-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
}
.ps-item {
  text-align: center;
  border-right: 1px solid var(--border);
  padding: 0 6px;
}
.ps-item:last-child { border-right: 0; }
.ps-num {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.ps-lbl {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-3);
  font-weight: 600;
  margin-top: 1px;
}

/* ============ SECTIONS ============ */
.section {
  padding: 18px max(16px, var(--safe-left)) 8px;
}
.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  gap: 10px;
}
.section-head h3 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.015em;
  display: flex;
  align-items: center;
  gap: 6px;
}
.section-head h3 .count {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-3);
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 999px;
}
.updated {
  font-size: 11px;
  color: var(--text-3);
  font-family: 'JetBrains Mono', monospace;
}
.legend {
  display: flex;
  gap: 4px;
  font-size: 10px;
}
.lg {
  padding: 3px 7px;
  border-radius: 999px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-size: 9px;
}
.lg-ok { background: var(--green-bg); color: #047857; }
.lg-warn { background: var(--amber-bg); color: #92400e; }
.lg-bad { background: var(--red-bg); color: #b91c1c; }

/* Segmented buttons */
.seg {
  display: inline-flex;
  background: var(--bg);
  border-radius: 8px;
  padding: 2px;
  border: 1px solid var(--border);
}
.seg-btn {
  padding: 4px 10px;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-2);
  border-radius: 6px;
  transition: all 0.2s var(--ease);
}
.seg-btn.is-on {
  background: var(--text);
  color: #fff;
}

/* ============ KPI GRID ============ */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
}
.kpi-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
  gap: 6px;
}
.kpi-lbl {
  font-size: 11px;
  color: var(--text-3);
  font-weight: 500;
  letter-spacing: 0.005em;
}
.kpi-trend {
  font-size: 10.5px;
  font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
  padding: 1px 5px;
  border-radius: 4px;
}
.kpi-trend.up { background: var(--green-bg); color: #047857; }
.kpi-trend.down { background: var(--red-bg); color: #b91c1c; }
.kpi-val {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.025em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  margin-bottom: 6px;
}
.kpi-val small {
  font-size: 12px;
  color: var(--text-3);
  font-weight: 500;
  margin-left: 1px;
}
.kpi-foot {
  font-size: 10.5px;
  color: var(--text-3);
}
.bar-mini {
  height: 3px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 4px;
}
.bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.6s var(--ease);
}

/* ============ CHART CARD ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
}
.chart-card {
  padding: 14px;
}
.trend-chart { width: 100%; height: 130px; }
.trend-axis {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-3);
  font-family: 'JetBrains Mono', monospace;
  margin-top: 6px;
}

/* ============ DONUT ============ */
.donut-card {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 16px;
  padding: 14px;
  align-items: center;
}
.donut-wrap svg { width: 100%; height: auto; }
.donut-legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.donut-legend li {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  gap: 8px;
  font-size: 12px;
  color: var(--text-2);
  align-items: center;
}
.donut-legend .dot {
  width: 10px; height: 10px;
  border-radius: 50%;
}
.donut-legend b {
  color: var(--text);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ============ FLOOR ZONES ============ */
.zones {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.zone {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
}
.zone.z-warn { border-color: var(--amber); }
.zone.z-bad  { border-color: var(--red); }
.zone-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
}
.zone-icon {
  font-size: 18px;
  color: var(--teal);
  line-height: 1;
}
.zone.z-warn .zone-icon { color: var(--amber); }
.zone.z-bad .zone-icon { color: var(--red); }
.zone-name {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.zone-pct {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.zone-pct small {
  font-size: 11px;
  color: var(--text-3);
  font-weight: 500;
}
.zone-body { display: flex; flex-direction: column; gap: 3px; padding-bottom: 8px; }
.zone-row {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-2);
}
.zone-row b {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.zone-row b.red { color: var(--red); }
.zone-bar {
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin: 2px -2px -2px -2px;
}
.zone-bar div { height: 100%; transition: width 0.6s var(--ease); }

/* ============ LOOMS ============ */
.loom-stats {
  display: flex;
  gap: 10px;
  font-size: 11px;
  font-family: 'JetBrains Mono', monospace;
}
.loom-stats span { display: inline-flex; gap: 3px; }
.loom-stats b { color: var(--text); font-weight: 700; }
.loom-stats .ls-warn b { color: var(--amber); }
.loom-stats .ls-bad b  { color: var(--red); }

.loom-sheds {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.shed {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  box-shadow: var(--shadow-sm);
}
.shed-name {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}
.shed-name span:last-child {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 500;
  font-size: 10.5px;
  color: var(--text-3);
}
.loom-grid {
  display: grid;
  grid-template-columns: repeat(20, 1fr);
  gap: 2px;
}
.loom {
  aspect-ratio: 1;
  background: var(--green);
  border-radius: 2px;
  min-width: 0;
}
.loom.warn { background: var(--amber); }
.loom.bad  { background: var(--red); }
.loom.idle { background: var(--text-3); }

/* ============ ORDERS ============ */
.orders {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.order {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
}
.order-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
  gap: 8px;
}
.order-po {
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
}
.order-cust {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 2px;
}
.status {
  font-size: 10px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  flex-shrink: 0;
}
.s-ok   { background: var(--green-bg); color: #047857; }
.s-warn { background: var(--amber-bg); color: #92400e; }
.s-bad  { background: var(--red-bg); color: #b91c1c; }

.order-stages {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 3px;
  margin-bottom: 8px;
}
.stg {
  font-size: 10px;
  font-weight: 600;
  text-align: center;
  padding: 4px 2px;
  border-radius: 4px;
  background: var(--bg);
  color: var(--text-3);
  letter-spacing: 0.02em;
}
.stg.done { background: #d1f4e0; color: #047857; }
.stg.active { background: var(--text); color: #fff; }

.order-foot {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--text-2);
  margin-bottom: 6px;
}
.order-foot .due {
  font-weight: 600;
  color: var(--text);
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
}

/* ============ ALERTS ============ */
.alerts {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.alert {
  display: grid;
  grid-template-columns: 36px 1fr;
  gap: 12px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  border-left: 4px solid var(--text-3);
  box-shadow: var(--shadow-sm);
}
.alert.a-bad { border-left-color: var(--red); background: linear-gradient(90deg, var(--red-bg) 0%, var(--surface) 35%); }
.alert.a-warn { border-left-color: var(--amber); background: linear-gradient(90deg, var(--amber-bg) 0%, var(--surface) 35%); }
.alert.a-info { border-left-color: var(--teal); background: linear-gradient(90deg, var(--teal-bg) 0%, var(--surface) 35%); }
.a-icon {
  width: 32px; height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.alert.a-bad .a-icon { background: var(--red); color: #fff; border-color: var(--red); }
.alert.a-warn .a-icon { background: var(--amber); color: #fff; border-color: var(--amber); }
.alert.a-info .a-icon { background: var(--teal); color: #fff; border-color: var(--teal); }
.a-title {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 3px;
  line-height: 1.3;
}
.a-meta {
  font-size: 11.5px;
  color: var(--text-2);
  margin-bottom: 4px;
}
.a-action {
  font-size: 11.5px;
  color: var(--text-2);
  font-style: italic;
  background: rgba(255, 255, 255, 0.6);
  padding: 4px 8px;
  border-radius: 6px;
  border-left: 2px solid currentColor;
  display: inline-block;
}

/* Constrain to phone width on desktop */
@media (min-width: 720px) {
  body {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    min-height: 100vh;
    background: var(--bg);
  }
}

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