:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #cbd5e1;
  --border-soft: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;
  --warn: #b45309;
  --warn-bg: #fffbeb;
  --warn-border: #fde68a;
  --ok: #15803d;
  --ok-bg: #f0fdf4;
  --ok-border: #bbf7d0;
  --info: #0369a1;
  --info-bg: #f0f9ff;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

*, *::before, *::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

/* Auth Screens */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.08);
}

.auth-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.brand-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.15rem;
}

/* Header & Responsive Mobile-First Layout */
.app-header {
  background: #0f172a;
  color: #f8fafc;
  padding: 10px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  position: sticky;
  top: 0;
  z-index: 40;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.18);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex: 1 1 auto;
  min-width: 0;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-btn {
  background: none;
  border: none;
  color: #ffffff;
  font-size: 1.05rem;
  font-weight: 700;
  padding: 4px 0;
  cursor: pointer;
  white-space: nowrap;
}

.version-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(59, 130, 246, 0.2);
  color: #93c5fd;
  border: 1px solid rgba(147, 197, 253, 0.3);
}

.auth-card .version-badge {
  background: #eff6ff;
  color: var(--primary);
  border-color: #bfdbfe;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.employee-picker select {
  min-height: 38px;
  max-width: 170px;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #1e293b;
  color: #f8fafc;
  padding: 4px 8px;
  font-size: 0.88rem;
}

.lang-switch {
  display: inline-flex;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #334155;
}

.auth-card .lang-switch {
  border-color: var(--border);
}

.lang-btn {
  border: none;
  background: #1e293b;
  color: #cbd5e1;
  padding: 6px 9px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.auth-card .lang-btn {
  background: var(--surface-alt);
  color: var(--muted);
}

.lang-btn.active {
  background: var(--primary);
  color: #ffffff;
}

.header-search {
  position: relative;
  flex: 1 1 320px;
  max-width: 480px;
}

.search-form {
  display: flex;
  gap: 6px;
  width: 100%;
}

.search-form input[type="search"] {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #1e293b;
  color: #f8fafc;
  padding: 6px 10px;
  font-size: 0.92rem;
}

.search-form input::placeholder {
  color: #94a3b8;
}

.search-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.22);
  max-height: 360px;
  overflow-y: auto;
  z-index: 60;
}

.search-hit {
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border-soft);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
  color: var(--text);
}

.search-hit:hover { background: var(--surface-alt); }
.search-hit:last-child { border-bottom: none; }

/* Navigation */
.main-nav {
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
  padding: 6px 12px;
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: thin;
}

.nav-tab {
  border: 1px solid transparent;
  background: none;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  min-height: 38px;
}

.nav-tab:hover {
  background: var(--surface-alt);
  color: var(--text);
}

.nav-tab.active {
  background: #eff6ff;
  color: var(--primary);
  border-color: #bfdbfe;
}

/* Main Content & Cards */
.main-content {
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.view-header h1 {
  margin: 0;
  font-size: 1.35rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.card h2, .card h3 {
  margin: 0 0 12px 0;
  font-size: 1.05rem;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.kpi-label {
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.kpi-value {
  font-size: 1.45rem;
  font-weight: 700;
}

.kpi-value.profit { color: var(--ok); }
.kpi-value.defect { color: var(--danger); }

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
}

/* Forms & Inputs */
.stack-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.88rem;
  font-weight: 600;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.94rem;
  font-family: inherit;
  background: #ffffff;
  color: var(--text);
}

.field textarea {
  min-height: 120px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.inline-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

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

.btn-sm {
  min-height: 32px;
  padding: 4px 10px;
  font-size: 0.82rem;
}

.btn-primary { background: var(--primary); color: #ffffff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-secondary {
  background: var(--surface-alt);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--border-soft); }

.btn-danger { background: var(--danger); color: #ffffff; }
.btn-warn { background: var(--warn); color: #ffffff; }
.btn-ok { background: var(--ok); color: #ffffff; }

.btn-ghost {
  background: transparent;
  color: inherit;
  border-color: rgba(148, 163, 184, 0.35);
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
}

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

th, td {
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
}

th {
  background: var(--surface-alt);
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}

td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Badges & Banners */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 700;
  white-space: nowrap;
}

.badge-ok { background: var(--ok-bg); color: var(--ok); border: 1px solid var(--ok-border); }
.badge-warn { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn-border); }
.badge-danger { background: var(--danger-bg); color: var(--danger); border: 1px solid var(--danger-border); }
.badge-info { background: var(--info-bg); color: var(--info); border: 1px solid #bae6fd; }
.badge-muted { background: var(--surface-alt); color: var(--muted); border: 1px solid var(--border); }

.error-box, .warn-banner, .info-banner, .ok-banner {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 10px;
}

.error-box { background: var(--danger-bg); color: var(--danger); border: 1px solid var(--danger-border); }
.warn-banner { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn-border); }
.info-banner { background: var(--info-bg); color: var(--info); border: 1px solid #bae6fd; }
.ok-banner { background: var(--ok-bg); color: var(--ok); border: 1px solid var(--ok-border); }

.sub-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}

/* Modals & Scanner */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 80;
}

.modal-card {
  width: 100%;
  max-width: 720px;
  max-height: 90vh;
  background: var(--surface);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.3);
}

.scanner-card { max-width: 520px; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
}

.modal-head h3 { margin: 0; }

.modal-body {
  padding: 16px;
  overflow-y: auto;
}

.scanner-viewport {
  width: 100%;
  background: #020617;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 12px;
}

.scanner-viewport video {
  width: 100%;
  max-height: 320px;
  object-fit: cover;
  display: block;
}

.scanner-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
}

/* Toasts */
.toast-container {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  max-width: min(380px, calc(100vw - 32px));
}

.toast {
  padding: 12px 14px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.18);
  background: #0f172a;
  color: #ffffff;
}

.toast-ok { background: #166534; }
.toast-warn { background: #b45309; }
.toast-err { background: #b91c1c; }

/* Mobile Header Spec: Employee in row 1, Global Search in row 2 */
@media (max-width: 768px) {
  .app-header {
    padding: 8px 12px;
    gap: 8px;
  }

  .header-top {
    flex: 1 0 100%;
  }

  .brand-btn {
    font-size: 0.95rem;
  }

  .employee-picker select {
    max-width: 130px;
    font-size: 0.82rem;
  }

  .header-search {
    flex: 1 0 100%;
    max-width: none;
  }

  .hide-xs {
    display: none;
  }

  .main-content {
    padding: 12px;
  }
}
