/* ────────────────────────────────────────────────────────
   Xyrom OS Operator PWA — Mobile-First Styles
   ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0a0f;
  --bg-2: #111118;
  --bg-card: #16161e;
  --border: #1e1e2e;
  --blue: #0078FF;
  --blue-hover: #0066dd;
  --text: #f0f0f8;
  --text-2: #9090a8;
  --red: #ff4444;
  --green: #00cc66;
  --yellow: #ffaa00;
  --orange: #ff6600;
  --header-h: 60px;
  --bottom-nav-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html { height: 100%; -webkit-tap-highlight-color: transparent; }
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Layout ──────────────────────────────────────────────────── */
.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: var(--header-h);
  background: rgba(10,10,15,0.95);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  padding-top: env(safe-area-inset-top, 0px);
}

.header-left { display: flex; align-items: center; gap: 0.75rem; }
.app-logo {
  width: 36px; height: 36px;
  background: var(--blue);
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 900; font-size: 1.1rem; color: #fff; flex-shrink: 0;
}
.header-title { font-weight: 700; font-size: 1rem; line-height: 1.2; }
.header-sub { font-size: 0.72rem; color: var(--text-2); }
.header-right { display: flex; align-items: center; gap: 0.5rem; }

.conn-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--text-2);
  transition: background 0.3s;
}
.conn-dot.online { background: var(--green); }
.conn-dot.offline { background: var(--red); }
.conn-dot.warning { background: var(--yellow); }

.icon-btn {
  background: none; border: none; color: var(--text-2);
  font-size: 1.2rem; cursor: pointer; padding: 0.4rem;
  border-radius: 8px; transition: all 0.15s;
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--bg-card); color: var(--text); }

.app-main {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  bottom: calc(var(--bottom-nav-h) + var(--safe-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 1rem;
  padding-bottom: 80px; /* space for estop FAB */
}

/* ── Bottom Nav ──────────────────────────────────────────────── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--bottom-nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-2);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
}

.nav-item {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 0.2rem;
  background: none; border: none;
  color: var(--text-2);
  cursor: pointer;
  padding: 0.4rem 0.2rem;
  transition: all 0.15s;
  position: relative;
  min-height: 44px;
}
.nav-item.active { color: var(--blue); }
.nav-item:hover { color: var(--text); }
.nav-icon { font-size: 1.3rem; line-height: 1; }
.nav-label { font-size: 0.62rem; font-weight: 600; letter-spacing: 0.02em; }
.nav-badge {
  position: absolute; top: 4px; right: calc(50% - 18px);
  background: var(--red); color: #fff;
  font-size: 0.6rem; font-weight: 700;
  min-width: 16px; height: 16px;
  border-radius: 8px; padding: 0 4px;
  display: flex; align-items: center; justify-content: center;
}

/* ── E-Stop FAB ──────────────────────────────────────────────── */
.estop-fab {
  position: fixed;
  bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 1rem);
  right: 1rem;
  z-index: 200;
  background: var(--red);
  color: #fff;
  font-weight: 900;
  font-size: 0.75rem;
  letter-spacing: 0.1em;
  width: 64px; height: 64px;
  border-radius: 50%;
  border: 3px solid rgba(255,68,68,0.4);
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 20px rgba(255,68,68,0.4);
}
.estop-fab:hover { transform: scale(1.05); box-shadow: 0 6px 28px rgba(255,68,68,0.6); }
.estop-fab.armed {
  animation: estop-pulse 1s infinite;
  border-color: rgba(255,68,68,0.8);
}
@keyframes estop-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,68,68,0.5); }
  50% { box-shadow: 0 0 0 16px rgba(255,68,68,0); }
}

/* ── Cards ───────────────────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  margin-bottom: 0.75rem;
  transition: border-color 0.2s;
}
.card:hover { border-color: rgba(0,120,255,0.3); }
.card-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-2);
  margin-bottom: 0.5rem;
}

/* ── Dashboard ───────────────────────────────────────────────── */
.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}
.metric-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  display: flex; flex-direction: column;
  gap: 0.25rem;
}
.metric-value {
  font-size: 2rem; font-weight: 800;
  line-height: 1; color: var(--text);
}
.metric-value.green { color: var(--green); }
.metric-value.yellow { color: var(--yellow); }
.metric-value.red { color: var(--red); }
.metric-label { font-size: 0.72rem; color: var(--text-2); }

/* ── Robot Cards ─────────────────────────────────────────────── */
.robot-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  margin-bottom: 0.75rem;
  cursor: pointer;
  transition: all 0.2s;
}
.robot-card:hover, .robot-card:active { border-color: var(--blue); transform: translateY(-1px); }
.robot-card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 0.75rem;
}
.robot-id-row { display: flex; align-items: center; gap: 0.6rem; }
.status-dot {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
}
.status-dot.active { background: var(--green); box-shadow: 0 0 6px rgba(0,204,102,0.5); }
.status-dot.warning { background: var(--yellow); box-shadow: 0 0 6px rgba(255,170,0,0.5); }
.status-dot.error { background: var(--red); box-shadow: 0 0 6px rgba(255,68,68,0.5); }
.status-dot.offline { background: var(--text-2); }
.status-dot.idle { background: var(--blue); }

.robot-id { font-weight: 700; font-size: 0.95rem; }
.robot-model { font-size: 0.75rem; color: var(--text-2); }

.health-badge {
  font-size: 0.65rem; font-weight: 700;
  padding: 0.2rem 0.6rem;
  border-radius: 99px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.health-badge.healthy { background: rgba(0,204,102,0.15); color: var(--green); }
.health-badge.warning { background: rgba(255,170,0,0.15); color: var(--yellow); }
.health-badge.critical { background: rgba(255,68,68,0.15); color: var(--red); }
.health-badge.offline { background: rgba(144,144,168,0.15); color: var(--text-2); }

.battery-row { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.5rem; }
.battery-bar-wrap {
  flex: 1; height: 6px; background: var(--border);
  border-radius: 3px; overflow: hidden;
}
.battery-bar {
  height: 100%; border-radius: 3px;
  transition: width 0.3s;
  background: var(--green);
}
.battery-bar.yellow { background: var(--yellow); }
.battery-bar.red { background: var(--red); }
.battery-pct { font-size: 0.72rem; color: var(--text-2); width: 36px; text-align: right; }

.robot-footer {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 0.72rem; color: var(--text-2);
}

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 0.4rem;
  padding: 0.65rem 1.25rem;
  border-radius: 10px;
  font-weight: 600; font-size: 0.875rem;
  cursor: pointer; border: none;
  transition: all 0.15s;
  min-height: 44px;
  text-decoration: none;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-hover); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { background: #cc3333; }
.btn-ghost {
  background: transparent; color: var(--text-2);
  border: 1px solid var(--border);
}
.btn-ghost:hover { border-color: var(--blue); color: var(--text); }
.btn-full { width: 100%; }
.btn-sm { padding: 0.4rem 0.875rem; font-size: 0.8rem; min-height: 36px; }

.actions-row {
  display: flex; gap: 0.5rem; flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

/* ── Task Cards ──────────────────────────────────────────────── */
.task-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  margin-bottom: 0.75rem;
}
.task-header {
  display: flex; align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.task-id { font-size: 0.7rem; color: var(--text-2); margin-bottom: 0.2rem; }
.task-goal { font-size: 0.9rem; font-weight: 600; }
.task-meta { font-size: 0.72rem; color: var(--text-2); margin-top: 0.25rem; }
.state-badge {
  font-size: 0.65rem; font-weight: 700;
  padding: 0.2rem 0.6rem;
  border-radius: 99px;
  text-transform: uppercase;
  white-space: nowrap;
}
.state-badge.running { background: rgba(0,120,255,0.15); color: var(--blue); }
.state-badge.pending { background: rgba(255,170,0,0.15); color: var(--yellow); }
.state-badge.completed { background: rgba(0,204,102,0.15); color: var(--green); }
.state-badge.failed { background: rgba(255,68,68,0.15); color: var(--red); }
.state-badge.cancelled { background: rgba(144,144,168,0.15); color: var(--text-2); }
.state-badge.pending_approval { background: rgba(255,102,0,0.15); color: var(--orange); }

.progress-bar-wrap { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 0.5rem; }
.progress-bar { height: 100%; background: var(--blue); border-radius: 2px; transition: width 0.5s; }

/* ── Alert Items ─────────────────────────────────────────────── */
.alert-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.875rem 1rem;
  margin-bottom: 0.5rem;
  display: flex; align-items: flex-start;
  gap: 0.75rem;
  transition: opacity 0.3s;
}
.alert-item.acked { opacity: 0.5; }
.alert-sev { font-size: 1.2rem; flex-shrink: 0; margin-top: 0.1rem; }
.alert-body { flex: 1; min-width: 0; }
.alert-msg { font-size: 0.875rem; font-weight: 600; }
.alert-sub { font-size: 0.72rem; color: var(--text-2); margin-top: 0.1rem; }
.alert-ack-btn {
  background: none; border: 1px solid var(--border);
  color: var(--text-2); font-size: 0.72rem;
  padding: 0.3rem 0.6rem; border-radius: 7px;
  cursor: pointer; white-space: nowrap;
  transition: all 0.15s;
  min-height: 36px;
}
.alert-ack-btn:hover { border-color: var(--blue); color: var(--blue); }

/* ── Filter Tabs ─────────────────────────────────────────────── */
.filter-tabs {
  display: flex; gap: 0.4rem;
  overflow-x: auto; padding-bottom: 0.25rem;
  margin-bottom: 0.75rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.filter-tabs::-webkit-scrollbar { display: none; }
.filter-tab {
  flex-shrink: 0;
  background: var(--bg-card); border: 1px solid var(--border);
  color: var(--text-2); font-size: 0.78rem; font-weight: 600;
  padding: 0.4rem 0.875rem; border-radius: 99px;
  cursor: pointer; transition: all 0.15s;
  min-height: 36px;
  display: flex; align-items: center;
}
.filter-tab.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.filter-tab:hover:not(.active) { border-color: var(--blue); color: var(--text); }

/* ── Search ──────────────────────────────────────────────────── */
.search-input {
  width: 100%; background: var(--bg-card);
  border: 1px solid var(--border); color: var(--text);
  padding: 0.65rem 1rem;
  border-radius: 10px; font-size: 0.9rem;
  outline: none; margin-bottom: 0.75rem;
  transition: border-color 0.15s;
}
.search-input:focus { border-color: var(--blue); }
.search-input::placeholder { color: var(--text-2); }

/* ── Forms ───────────────────────────────────────────────────── */
.form-group { margin-bottom: 0.875rem; }
.form-label {
  display: block;
  font-size: 0.78rem; font-weight: 600;
  color: var(--text-2);
  margin-bottom: 0.35rem;
}
.form-control {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.65rem 0.875rem;
  border-radius: 10px;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
}
.form-control:focus { border-color: var(--blue); }
.form-control option { background: var(--bg-2); }

textarea.form-control { resize: vertical; min-height: 80px; }

/* ── Joint State Grid ─────────────────────────────────────────── */
.joints-grid {
  display: grid; grid-template-columns: repeat(3,1fr);
  gap: 0.5rem; margin-top: 0.5rem;
}
.joint-item { background: var(--bg-2); border-radius: 8px; padding: 0.5rem; }
.joint-name { font-size: 0.65rem; color: var(--text-2); margin-bottom: 0.25rem; }
.joint-value { font-size: 0.85rem; font-weight: 700; }
.joint-bar-wrap { height: 3px; background: var(--border); border-radius: 2px; margin-top: 0.25rem; }
.joint-bar { height: 100%; background: var(--blue); border-radius: 2px; }

/* ── Modal ───────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 500;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 1.5rem;
}
.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 2rem 1.5rem;
  max-width: 360px; width: 100%;
  text-align: center;
}
.modal-icon { font-size: 3rem; margin-bottom: 0.75rem; }
.modal h2 { font-size: 1.25rem; font-weight: 800; margin-bottom: 0.5rem; }
.modal p { font-size: 0.875rem; color: var(--text-2); margin-bottom: 1.5rem; line-height: 1.6; }
.modal .btn { margin-bottom: 0.5rem; }

/* ── QR Scanner ──────────────────────────────────────────────── */
.qr-overlay {
  position: fixed; inset: 0; z-index: 400;
  background: #000;
  display: flex; flex-direction: column;
}
.qr-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem;
  background: rgba(0,0,0,0.7);
}
.qr-header h3 { font-size: 1rem; font-weight: 700; }
.qr-video { width: 100%; flex: 1; object-fit: cover; }
.qr-frame {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 220px; height: 220px;
  border: 3px solid var(--blue);
  border-radius: 16px;
  box-shadow: 0 0 0 4000px rgba(0,0,0,0.5);
}
.qr-hint {
  padding: 1.5rem;
  text-align: center;
  color: var(--text-2);
  font-size: 0.875rem;
  background: rgba(0,0,0,0.8);
}
.qr-manual { padding: 0 1.5rem 1.5rem; background: rgba(0,0,0,0.8); }

/* ── Install Banner ──────────────────────────────────────────── */
.install-banner {
  position: fixed;
  bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 0.5rem);
  left: 0.75rem; right: 0.75rem;
  z-index: 150;
  background: var(--bg-2);
  border: 1px solid var(--blue);
  border-radius: 14px;
  padding: 0.875rem 1rem;
  display: flex; align-items: center; gap: 0.75rem;
  box-shadow: 0 8px 32px rgba(0,120,255,0.2);
}
.install-text {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
}
.install-text strong { font-size: 0.875rem; }
.install-text span { font-size: 0.75rem; color: var(--text-2); }

/* ── Toast ───────────────────────────────────────────────────── */
.toast-container {
  position: fixed; bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 0.5rem);
  left: 0.75rem; right: 0.75rem;
  z-index: 600;
  pointer-events: none;
}
.toast {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem 1rem;
  margin-top: 0.5rem;
  font-size: 0.875rem;
  pointer-events: auto;
  animation: toast-in 0.3s ease;
}
.toast.warn { border-color: var(--yellow); }
.toast.error { border-color: var(--red); }
.toast.success { border-color: var(--green); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Section header ──────────────────────────────────────────── */
.view-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 0.875rem;
}
.view-title { font-size: 1.1rem; font-weight: 800; }

/* ── Settings ────────────────────────────────────────────────── */
.settings-section { margin-bottom: 1.5rem; }
.settings-label {
  font-size: 0.7rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--text-2); margin-bottom: 0.75rem;
}
.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.875rem 0;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: none; }
.settings-row-label { font-size: 0.9rem; }
.settings-row-sub { font-size: 0.75rem; color: var(--text-2); margin-top: 0.1rem; }

/* Toggle switch */
.toggle {
  position: relative; width: 48px; height: 26px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0;
  background: var(--border);
  border-radius: 13px; cursor: pointer;
  transition: background 0.2s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 20px; height: 20px;
  left: 3px; top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}
.toggle input:checked + .toggle-slider { background: var(--blue); }
.toggle input:checked + .toggle-slider::before { transform: translateX(22px); }

/* Range input */
.range-input {
  -webkit-appearance: none;
  width: 100%; height: 4px;
  background: var(--border);
  border-radius: 2px; outline: none;
  margin-top: 0.5rem;
}
.range-input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--blue);
  cursor: pointer;
}

/* ── Detail panel ────────────────────────────────────────────── */
.detail-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  margin-bottom: 0.75rem;
}
.detail-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.875rem 1rem;
  border-bottom: 1px solid var(--border);
}
.detail-body { padding: 1rem; }
.detail-row {
  display: flex; justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.875rem;
}
.detail-row:last-child { border: none; }
.detail-key { color: var(--text-2); }
.detail-val { font-weight: 600; text-align: right; }

/* ── Activity feed ───────────────────────────────────────────── */
.activity-item {
  display: flex; align-items: flex-start; gap: 0.6rem;
  padding: 0.6rem 0; border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
}
.activity-item:last-child { border: none; }
.activity-icon { font-size: 0.9rem; flex-shrink: 0; margin-top: 0.1rem; }
.activity-text { flex: 1; color: var(--text-2); line-height: 1.5; }
.activity-time { font-size: 0.7rem; color: var(--text-2); white-space: nowrap; }

/* ── Empty states ────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-2);
}
.empty-icon { font-size: 3rem; margin-bottom: 0.75rem; opacity: 0.5; }
.empty-title { font-size: 1rem; font-weight: 700; color: var(--text); margin-bottom: 0.35rem; }
.empty-sub { font-size: 0.82rem; }

/* ── Pose grid ────────────────────────────────────────────────── */
.pose-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 0.5rem; margin-top: 0.5rem; }
.pose-item { background: var(--bg-2); border-radius: 8px; padding: 0.5rem; text-align: center; }
.pose-label { font-size: 0.65rem; color: var(--text-2); margin-bottom: 0.2rem; }
.pose-value { font-size: 0.9rem; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ── Tablet & Desktop ─────────────────────────────────────────── */
@media (min-width: 768px) {
  .app-main { padding: 1.25rem; }
  .metrics-grid { grid-template-columns: repeat(4, 1fr); }
  .bottom-nav { display: none; }

  /* Sidebar nav for tablet+ */
  .sidebar-nav {
    display: flex !important;
    position: fixed;
    left: 0; top: var(--header-h);
    bottom: 0;
    width: 200px;
    background: var(--bg-2);
    border-right: 1px solid var(--border);
    flex-direction: column;
    padding: 1rem 0;
    z-index: 90;
  }
  .sidebar-nav-item {
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0.75rem 1.25rem;
    color: var(--text-2);
    cursor: pointer; transition: all 0.15s;
    font-size: 0.875rem; font-weight: 600;
    border: none; background: none;
    width: 100%; text-align: left;
  }
  .sidebar-nav-item.active { color: var(--text); background: rgba(0,120,255,0.1); }
  .sidebar-nav-item:hover { color: var(--text); background: var(--bg-card); }
  .sidebar-icon { font-size: 1.1rem; }

  .app-main {
    left: 200px;
    bottom: 0;
  }
  .estop-fab { bottom: 1.5rem; right: 1.5rem; width: 72px; height: 72px; font-size: 0.8rem; }
}

@media (min-width: 1024px) {
  .app-main { padding: 1.5rem 2rem; }
  .content-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 1rem;
  }
}
