/* ============================================================
   Nile Leaf — theme tokens (dark default + light override)
   Palette: tea green (primary) + Nile Leaf gold (secondary)
   ============================================================ */
:root,
:root[data-theme="dark"] {
  --bg: #0e1930;
  --bg-2: #14223d;
  --bg-glow: #1c2e4d;
  --surface: #1c2e4d;
  --surface-2: #22375a;
  --border: #2a4060;
  --text: #eef2f8;
  --muted: #93a2be;
  --accent: #f0a528;
  --accent-2: #d98c1a;
  --good: #2fd27a;
  --bad: #ff4d4f;
  --warn: #f5a623;
  --high: #ff4d4f;
  --medium: #f5a623;
  --low: #22c55e;
  --icon-bg: rgba(240, 165, 40, 0.12);
  --logo-bg: #f0a528;
  --grid-line: #2a4060;
  --tick: #93a2be;
  --radius: 16px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --card-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  --card-shadow-hover: 0 12px 34px rgba(0, 0, 0, 0.55);
  --glow: 0 0 0 1px color-mix(in srgb, var(--accent) 28%, transparent), 0 8px 28px color-mix(in srgb, var(--accent) 22%, transparent);
  --nav-h: 0px;
}

:root[data-theme="light"] {
  --bg: #f7f9fc;
  --bg-2: #eef2f8;
  --bg-glow: #e3e9f2;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --border: #e3e9f2;
  --text: #14223d;
  --muted: #5b6b85;
  --accent: #f0a528;
  --accent-2: #d98c1a;
  --good: #1a9a5a;
  --bad: #d64545;
  --warn: #d98c1a;
  --high: #d64545;
  --medium: #d98c1a;
  --low: #1a9a5a;
  --icon-bg: rgba(20, 34, 61, 0.06);
  --logo-bg: #f0a528;
  --grid-line: #e3e9f2;
  --tick: #5b6b85;
  --shadow: 0 8px 30px rgba(20, 34, 61, 0.08);
  --card-shadow: 0 6px 20px rgba(20, 34, 61, 0.06);
  --card-shadow-hover: 0 12px 34px rgba(20, 34, 61, 0.1);
  --glow: 0 0 0 1px color-mix(in srgb, var(--accent) 35%, transparent);
  --nav-h: 0px;
}

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

body {
  font-family: "Manrope", "Cairo", system-ui, sans-serif;
  background: radial-gradient(1200px 600px at 80% -10%, var(--bg-glow) 0%, var(--bg) 55%) fixed;
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s ease, color 0.25s ease;
}
:root[data-theme="dark"] body {
  background: var(--bg);
}

.powered { font-style: normal; color: var(--accent); font-weight: 600; }
.powered-footer {
  text-align: center;
  padding: 1rem;
  color: var(--muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
}
.brand-mark {
  width: 44px; height: 44px; border-radius: 12px;
  background: var(--logo-bg); color: #14223d;
  display: flex; align-items: center; justify-content: center;
  font-family: "Space Grotesk", sans-serif; font-weight: 700; font-size: 0.95rem;
}
.pg-ask-row {
  display: flex; gap: 0.5rem; max-width: 560px; margin: 0 auto 1rem; padding: 0 1rem;
}
.pg-ask-row input {
  flex: 1; padding: 0.7rem 1rem; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.pg-ask-row button {
  padding: 0.7rem 1.2rem; border-radius: 12px; border: 0;
  background: var(--accent); color: #14223d; font-weight: 700; cursor: pointer;
}
.pg-transcript { max-width: 560px; margin: 0.5rem auto; max-height: 160px; overflow: auto; font-size: 0.9rem; }
.pg-line { padding: 0.25rem 0; color: var(--muted); }
.pg-line.user { color: var(--text); }

/* ----------------------------- Top bar ----------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 50;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  height: 40px; display: grid; place-items: center;
  background: var(--logo-bg); color: #0a2540; font-weight: 800;
  padding: 0 14px; border-radius: 9px; font-size: 16px; letter-spacing: 0.3px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
}
.brand-text h1 { font-size: 18px; letter-spacing: 0.2px; }
.brand-text span { font-size: 12px; color: var(--muted); }

/* Tabs — desktop pill switcher in the header */
.tabs {
  display: flex; gap: 6px;
  background: var(--surface); padding: 5px; border-radius: 12px;
  border: 1px solid var(--border);
}
.tab {
  display: inline-flex; align-items: center; gap: 8px;
  border: none; background: transparent; color: var(--muted);
  padding: 8px 18px; border-radius: 9px; cursor: pointer;
  font-size: 14px; font-weight: 600; font-family: inherit; transition: all 0.15s;
}
.tabs .tab-icon { display: none; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--accent); color: #fff; }

/* Mobile bottom navigation (body-level, shown only on mobile) */
.bottom-nav { display: none; }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.pill {
  font-size: 12px; font-weight: 600; padding: 6px 12px;
  border-radius: 999px; border: 1px solid var(--border);
}
.pill-muted { color: var(--muted); background: var(--surface); }
.pill-off { color: var(--muted); background: var(--surface); }
.pill-connecting { color: var(--warn); border-color: var(--warn); }
.pill-live { color: var(--good); border-color: var(--good); background: color-mix(in srgb, var(--good) 12%, transparent); }

/* Theme toggle */
.theme-toggle {
  display: inline-grid; place-items: center;
  width: 38px; height: 38px; border-radius: 11px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); cursor: pointer; transition: all 0.15s;
}
.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }
.theme-toggle .icon-moon { display: none; }
.theme-toggle .icon-sun { display: block; }
:root[data-theme="light"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: block; }

/* ----------------------------- Views ----------------------------- */
.view { display: none; padding: 26px 28px 60px; max-width: 1280px; margin: 0 auto; }
.view.active { display: block; }

.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.section-head h2 { font-size: 22px; font-weight: 700; }
.range-select { display: flex; align-items: center; gap: 8px; }
.range-select label { font-size: 13px; color: var(--muted); }
select {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; font-size: 14px; font-family: inherit; cursor: pointer;
}

/* ----------------------------- KPI grid ----------------------------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
@media (max-width: 1024px) { .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  position: relative;
  overflow: hidden;
  box-shadow: var(--card-shadow);
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}
.kpi-card > * { position: relative; z-index: 1; }
.kpi-card:hover {
  transform: translateY(-2px);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  box-shadow: var(--card-shadow-hover);
}
.kpi-icon {
  position: absolute; top: 14px; right: 14px;
  width: 32px; height: 32px;
  display: grid; place-items: center;
  background: transparent; border: none; box-shadow: none; z-index: 1;
}
.kpi-label { font-size: 12.5px; color: var(--muted); font-weight: 600; padding-right: 38px; }
.kpi-value { font-size: 28px; font-weight: 800; margin-top: 8px; line-height: 1; }
.kpi-unit { font-size: 13px; color: var(--muted); font-weight: 600; margin-left: 4px; }
.kpi-trend { margin-top: 10px; font-size: 12.5px; font-weight: 700; display: inline-flex; align-items: center; gap: 4px; }
.trend-good { color: var(--good); }
.trend-bad { color: var(--bad); }
.trend-flat { color: var(--muted); }

/* ----------------------------- Panels ----------------------------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 22px;
  box-shadow: var(--card-shadow);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 12px; flex-wrap: wrap; }
.panel-head h3 { font-size: 15px; font-weight: 700; }
.tag { font-size: 12px; font-weight: 700; padding: 5px 11px; border-radius: 999px; }
.tag-warn { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

/* Fixed-height box keeps Chart.js from growing in a resize feedback loop. */
.chart-box { position: relative; width: 100%; height: 240px; }
.chart-box canvas { position: absolute; inset: 0; }

/* ----------------------------- Compare panel ----------------------------- */
.compare-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.compare-controls select { padding: 7px 10px; font-size: 13px; }
.cmp-summary { margin-top: 14px; text-align: center; font-size: 14px; color: var(--muted); }

/* ----------------------------- Pipeline ----------------------------- */
.pipeline-section { margin-bottom: 22px; }
.section-subhead { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.section-subhead h3 {
  font-size: 12px; font-weight: 700; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--muted);
}
.pipeline {
  display: flex; gap: 12px; overflow-x: auto; padding-bottom: 8px;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
}
.stage-chip {
  flex: 0 0 156px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 14px 14px;
  position: relative;
  overflow: hidden;
  box-shadow: var(--card-shadow);
  scroll-snap-align: start;
}
.stage-accent { position: absolute; top: 0; left: 0; right: 0; height: 3px; }
.stage-chip.bottleneck { border-color: var(--bad); box-shadow: 0 0 0 1px var(--bad) inset, var(--card-shadow); }
.stage-name { font-size: 13px; font-weight: 600; color: var(--text); margin-bottom: 10px; min-height: 32px; }
.stage-oee { font-size: 22px; font-weight: 800; }
.stage-meta { font-size: 11px; color: var(--muted); margin-top: 6px; }
.oee-bar { height: 6px; border-radius: 4px; background: var(--border); margin-top: 10px; overflow: hidden; }
.oee-bar > span { display: block; height: 100%; border-radius: 4px; }

/* ----------------------------- Alerts ----------------------------- */
.alerts-feed { display: flex; flex-direction: column; gap: 10px; max-height: 360px; overflow-y: auto; }
.alert-row {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 11px; padding: 12px 14px;
}
.sev-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex-shrink: 0; }
.sev-high { background: var(--high); }
.sev-medium { background: var(--medium); }
.sev-low { background: var(--low); }
.alert-body { flex: 1; min-width: 0; }
.alert-desc { font-size: 13px; font-weight: 600; }
.alert-meta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.alert-sev-label { font-size: 11px; font-weight: 700; text-transform: uppercase; }

/* ----------------------------- Machines ----------------------------- */
.machine-summary { display: flex; flex-direction: column; gap: 14px; }
.machine-stats { display: flex; gap: 12px; }
.mstat { flex: 1; background: var(--surface-2); border: 1px solid var(--border); border-radius: 11px; padding: 14px; text-align: center; }
.mstat .n { font-size: 24px; font-weight: 800; }
.mstat .l { font-size: 12px; color: var(--muted); margin-top: 4px; }
.machine-list { display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow-y: auto; }
.machine-row { display: flex; justify-content: space-between; align-items: center; font-size: 13px; padding: 8px 12px; background: var(--surface-2); border-radius: 9px; border: 1px solid var(--border); }
.status-badge { font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px; }
.status-operational { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.status-maint { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }

/* ----------------------------- Batch card ----------------------------- */
.batch-card { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.batch-field { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.batch-field .k { font-size: 11px; color: var(--muted); }
.batch-field .v { font-size: 15px; font-weight: 700; margin-top: 4px; }

/* ----------------------------- Playground (voice only) ----------------------------- */
.playground { max-width: 1000px; }
.pg-stage {
  min-height: calc(100vh - 230px);
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  padding-top: 8px;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 130px);
}

.pg-orb-wrap { display: flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.pg-orb {
  position: relative; width: 62px; height: 62px; border-radius: 50%;
  border: none; cursor: pointer; background: transparent; padding: 0;
  display: grid; place-items: center; flex: 0 0 auto;
  --lvl: 0;
}
.pg-orb-core {
  position: relative; z-index: 2;
  width: 56px; height: 56px; border-radius: 50%;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 45%, transparent);
  transform: scale(calc(1 + var(--lvl) * 0.12));
  transition: transform 0.08s linear;
}
.pg-orb-ring {
  position: absolute; inset: 0; border-radius: 50%; z-index: 1;
  background: radial-gradient(circle, color-mix(in srgb, var(--accent) 35%, transparent) 0%, color-mix(in srgb, var(--accent-2) 10%, transparent) 60%, transparent 72%);
  transform: scale(calc(1 + var(--lvl) * 0.4));
  opacity: calc(0.1 + var(--lvl) * 0.6);
  transition: transform 0.08s linear, opacity 0.08s linear;
}
.pg-orb:hover .pg-orb-core { transform: scale(1.05); }
.pg-orb.live { cursor: default; }
.pg-orb.live .pg-orb-ring { animation: orbBreath 3s ease-in-out infinite; }
@keyframes orbBreath { 0%,100% { opacity: 0.4; } 50% { opacity: 0.7; } }
.pg-orb.connecting .pg-orb-core { background: var(--surface-2); opacity: 0.85; }

.pg-state-label { margin-top: auto; font-size: 14px; color: var(--muted); font-weight: 500; min-height: 20px; text-align: center; }

.pg-response { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.pg-widget { width: 100%; max-width: 760px; animation: pop 0.3s ease; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.pg-big-kpi { text-align: center; }
.pg-big-kpi .label { font-size: 17px; color: var(--muted); font-weight: 600; }
.pg-big-kpi .value { font-size: 72px; font-weight: 800; line-height: 1; margin: 10px 0; background: linear-gradient(135deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.pg-big-kpi .unit { font-size: 24px; -webkit-text-fill-color: var(--muted); }

.pg-controls {
  position: sticky;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 30px);
  margin-top: 24px;
  display: flex; gap: 12px; align-items: center; justify-content: center;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 8px 12px; box-shadow: var(--shadow);
  width: fit-content; max-width: 100%; margin-left: auto; margin-right: auto;
  z-index: 70;
}
:root[data-theme="dark"] .pg-controls {
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 90px);
}
.ctrl-btn[hidden] { display: none; }
.ctrl-btn {
  display: inline-flex; align-items: center; gap: 9px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font-family: inherit; font-size: 14px; font-weight: 600;
  border-radius: 999px; padding: 10px 18px; cursor: pointer; transition: all 0.15s;
}
.ctrl-btn:hover { border-color: var(--accent); }
.ctrl-btn.mic-on { color: var(--text); }
.ctrl-btn.mic-off { color: var(--bad); border-color: var(--bad); background: color-mix(in srgb, var(--bad) 8%, transparent); }
.ctrl-btn.end { color: var(--bad); }
.ctrl-btn.end:hover { background: var(--bad); color: #fff; border-color: var(--bad); }

.mic-bars { display: inline-flex; align-items: center; gap: 3px; height: 18px; --mic: 0; }
.mic-bars i { width: 3px; border-radius: 3px; background: currentColor; height: 5px; transition: height 0.08s linear; }
.ctrl-btn.mic-on .mic-bars i:nth-child(1) { height: calc(5px + var(--mic) * 13px); }
.ctrl-btn.mic-on .mic-bars i:nth-child(2) { height: calc(7px + var(--mic) * 11px); }
.ctrl-btn.mic-on .mic-bars i:nth-child(3) { height: calc(5px + var(--mic) * 13px); }
.ctrl-btn.mic-off .mic-bars i { height: 5px; opacity: 0.6; }

/* ----------------------------- Toast ----------------------------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 12px 20px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: all 0.25s; z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--bad); color: var(--bad); }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

/* ============================================================
   Mobile (<= 720px) — bottom tab nav + tightened layout
   ============================================================ */
@media (max-width: 720px) {
  :root { --nav-h: 64px; }

  .topbar { flex-wrap: wrap; gap: 12px; padding: 12px 16px; }
  .brand { flex: 1 1 auto; min-width: 0; }
  .brand-mark { height: 32px; font-size: 14px; padding: 0 11px; }
  .brand-text h1 { font-size: 16px; }
  .brand-text span { font-size: 11px; }

  .topbar .tabs { display: none; }

  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 60;
    gap: 8px;
    padding: 8px 16px calc(8px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    background: color-mix(in srgb, var(--bg) 90%, transparent);
    backdrop-filter: blur(14px);
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.18);
  }
  .bottom-nav .tab {
    flex: 1; flex-direction: column; gap: 4px; padding: 8px 8px;
    border-radius: 14px; font-size: 12px; background: transparent; color: var(--muted);
  }
  .bottom-nav .tab-icon { display: block; }
  .bottom-nav .tab.active {
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    color: var(--accent);
  }
  .bottom-nav .tab.active .tab-icon { color: var(--accent); }

  .topbar-right { order: 2; }

  .view { padding: 18px 16px calc(var(--nav-h) + 32px); }

  .section-head { flex-direction: column; align-items: flex-start; gap: 12px; }
  .section-head h2 { font-size: 19px; }
  .range-select { width: 100%; }
  .range-select select { flex: 1; }

  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
  .kpi-card { padding: 15px; }
  .kpi-value { font-size: 24px; }
  .kpi-icon { width: 30px; height: 30px; top: 13px; right: 13px; }

  .panel { padding: 16px; }
  .compare-controls { width: 100%; }
  .compare-controls select { flex: 1 1 40%; }

  .machine-stats { flex-wrap: wrap; }
  .mstat { flex: 1 1 calc(50% - 12px); }

  .pg-stage { min-height: calc(100vh - 210px); padding-top: 8px; }
}

@media (max-width: 380px) {
  .kpi-value { font-size: 22px; }
}
