:root {
  --bg: #f5f8f8;
  --surface: #ffffff;
  --ink: #182b30;
  --muted: #5d7178;
  --line: #dfe7e8;
  --accent: #14a19c;
  --accent-dark: #0c7c78;
  --accent-wash: #def4f2;
  --navy: #1f3640;
  --navy-light: #2c4a55;
  --navy-line: rgba(255, 255, 255, 0.12);
  --danger: #c0392b;
  --danger-wash: #fbe7e4;
  --success: #2f8f5b;
  --success-wash: #e3f3ea;
  --warning: #d98e2b;
  --warning-wash: #fbeed9;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(24, 43, 48, 0.06), 0 4px 12px rgba(24, 43, 48, 0.04);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  font-size: inherit;
}

table { font-variant-numeric: tabular-nums; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 20px;
  height: 56px;
  background: var(--navy);
  border-bottom: 1px solid var(--navy-line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.01em;
  white-space: nowrap;
  color: #ffffff;
}

.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(20, 161, 156, 0.35);
}

.tabs {
  display: flex;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
}

.tab {
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.65);
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
}

.tab:hover { background: var(--navy-light); color: #ffffff; }

.tab.active {
  background: rgba(20, 161, 156, 0.22);
  color: #6fe0d9;
}

.conn-status {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
  white-space: nowrap;
}
.conn-status.ok::before { content: "● "; color: #4fd6a8; }
.conn-status.err::before { content: "● "; color: #ff8a7a; }

.topbar-meta {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}

/* ---------- Layout ---------- */
.view { display: none; padding: 18px 20px 60px; max-width: 1180px; margin: 0 auto; }
.view.active { display: block; }

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

.view-header h1 {
  font-size: 20px;
  margin: 0;
  letter-spacing: -0.01em;
}

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

input[type="text"], input[type="number"], select, textarea {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

.search-input { min-width: 220px; flex: 1; }

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn:hover { border-color: var(--accent); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { border-color: var(--danger); background: var(--danger-wash); }
.btn-sm { padding: 5px 9px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Table ---------- */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

table { width: 100%; border-collapse: collapse; }

th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
th:hover { color: var(--ink); }

td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg); }

.cell-actions { display: flex; gap: 6px; justify-content: flex-end; white-space: nowrap; }

.stock-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
}
.stock-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.stock-dot.ok { background: var(--success); }
.stock-dot.low { background: var(--warning); box-shadow: 0 0 0 3px var(--warning-wash); }
.stock-dot.out { background: var(--danger); box-shadow: 0 0 0 3px var(--danger-wash); }

.muted { color: var(--muted); }
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}
.empty-state h3 { color: var(--ink); margin-bottom: 6px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(28,27,24,0.4);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }

.modal {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}
.modal-header h2 { font-size: 16px; margin: 0; }
.modal-close { background: none; border: none; cursor: pointer; color: var(--muted); font-size: 20px; line-height: 1; }

.modal-body { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 600; color: var(--muted); }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.modal-footer {
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.segmented { display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.segmented button {
  flex: 1; border: none; background: var(--surface); padding: 9px; cursor: pointer; font-weight: 700; color: var(--muted);
}
.segmented button.active.in { background: var(--success-wash); color: var(--success); }
.segmented button.active.out { background: var(--danger-wash); color: var(--danger); }

/* ---------- Reports ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat-card .label { font-size: 12px; color: var(--muted); font-weight: 600; }
.stat-card .value { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.stat-card.accent .value { color: var(--warning); }
.stat-card.danger .value { color: var(--danger); }

.section-title {
  font-size: 14px;
  font-weight: 700;
  margin: 24px 0 10px;
  color: var(--ink);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s ease;
  z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .topbar {
    flex-wrap: wrap;
    height: auto;
    min-height: 56px;
    padding: 8px 12px;
    row-gap: 8px;
  }
  .brand { order: 1; }
  .brand span.brand-text { display: none; }
  .topbar-meta {
    order: 2;
    margin-left: auto;
    gap: 8px;
  }
  .conn-status { display: none; } /* shown in full on desktop; phone width is too tight for it */
  .user-badge { max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabs {
    order: 3;
    flex: 1 1 100%;
    width: 100%;
  }
  .view { padding: 14px 12px 60px; }
  th, td { padding: 9px 10px; font-size: 13.5px; }
  .table-wrap { overflow-x: auto; }
  table { min-width: 640px; }
  .field-row { flex-direction: column; }
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Auth screen ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--navy);
  padding: 20px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 4px solid var(--accent);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
  padding: 28px;
  width: 100%;
  max-width: 360px;
}
.auth-brand {
  justify-content: center;
  margin-bottom: 22px;
  font-size: 18px;
  color: var(--ink);
}
.auth-card h2 {
  font-size: 17px;
  margin: 0 0 16px;
  text-align: center;
}
.auth-card .field { margin-bottom: 12px; }
.auth-card input { width: 100%; }
.auth-btn { width: 100%; justify-content: center; margin-top: 4px; }
.auth-switch { text-align: center; font-size: 13px; color: var(--muted); margin-top: 14px; }
.auth-switch a { color: var(--accent-dark); font-weight: 600; text-decoration: none; }
.auth-switch a:hover { text-decoration: underline; }
.auth-note { font-size: 12px; text-align: center; margin-top: 10px; line-height: 1.5; }
.auth-error { color: var(--danger); font-size: 13px; text-align: center; margin-top: 10px; min-height: 1px; }

/* ---------- User badge / users table ---------- */
.user-badge {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.85);
  white-space: nowrap;
  font-weight: 600;
}
.perm-cell { text-align: center; }
.perm-cell input[type="checkbox"] { width: 17px; height: 17px; cursor: pointer; }
.owner-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent-dark);
  background: var(--accent-wash);
  padding: 2px 8px;
  border-radius: 10px;
}
.price-hidden { color: var(--muted); font-style: italic; }

/* ---------- Product search combobox (stock move modal) ---------- */
.combobox-field { position: relative; }
.combobox-results {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 8px 8px;
  max-height: 220px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: var(--shadow);
}
.combobox-results.open { display: block; }
.combobox-item {
  padding: 9px 12px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 1px solid var(--line);
}
.combobox-item:last-child { border-bottom: none; }
.combobox-item:hover, .combobox-item.active { background: var(--accent-wash); }
.combobox-item .cb-sku { color: var(--muted); font-size: 12px; }
.combobox-empty { padding: 10px 12px; color: var(--muted); font-size: 13px; }
