:root {
  --brand: #14532d;
  --brand-dark: #0d3b20;
  --brand-light: #e8f3ec;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { height: 100%; overscroll-behavior-y: contain; }
body {
  margin: 0;
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Tahoma, Arial, sans-serif;
  background: #f4f5f7;
  color: #1f2430;
  -webkit-font-smoothing: antialiased;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}
button, input, select { font-family: inherit; }

/* Login page */
.login-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, var(--safe-top)) max(16px, var(--safe-right)) max(16px, var(--safe-bottom)) max(16px, var(--safe-left));
}
.login-card {
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(20,83,45,0.1);
  padding: 32px 28px;
  width: 100%;
  max-width: 380px;
  animation: fadeUp 0.35s ease both;
}
.login-card h1 {
  font-size: 20px;
  margin: 0 0 20px;
  text-align: center;
  color: var(--brand);
}
.tabs { display: flex; gap: 8px; margin-bottom: 20px; }
.tab {
  flex: 1;
  padding: 12px;
  border: 1px solid #d8dbe0;
  background: #f9fafb;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  min-height: 44px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.tab.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.panel.hidden, .hidden { display: none !important; }
label { display: block; font-size: 13px; margin: 12px 0 4px; color: #4b5563; }
input[type=email], input[type=password], input[type=tel], input[type=text], input[type=number], select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid #d8dbe0;
  border-radius: 10px;
  font-size: 16px;
  min-height: 46px;
  transition: border-color 0.15s, box-shadow 0.15s;
  background: #fff;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(20,83,45,0.12);
}
button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  transition: transform 0.08s ease, opacity 0.15s ease, background 0.15s ease;
}
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.6; cursor: default; transform: none; }
.login-card form button, #phonePanel button {
  width: 100%;
  padding: 13px;
  margin-top: 16px;
  background: var(--brand);
  color: #fff;
  font-weight: 600;
  min-height: 46px;
}
.secondary-btn {
  background: transparent !important;
  color: var(--brand) !important;
  font-weight: 400 !important;
  font-size: 13px;
  margin-top: 8px !important;
  text-decoration: underline;
  min-height: 32px;
}
.error {
  color: #b91c1c;
  background: #fee2e2;
  padding: 10px;
  border-radius: 10px;
  font-size: 13px;
  margin-top: 14px;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes slideIn {
  from { opacity: 0; transform: translateX(12px); }
  to { opacity: 1; transform: translateX(0); }
}
[dir="rtl"] .layout.has-detail .detail { animation: slideInRtl 0.22s ease both; }
[dir="ltr"] .layout.has-detail .detail { animation: slideIn 0.22s ease both; }
@keyframes slideInRtl {
  from { opacity: 0; transform: translateX(-12px); }
  to { opacity: 1; transform: translateX(0); }
}

/* App shell */
.topbar {
  background: var(--brand);
  color: #fff;
  padding: 14px 20px;
  padding-top: max(14px, calc(var(--safe-top) + 8px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: 0 2px 12px rgba(0,0,0,0.12);
}
.topbar h1 { font-size: 16px; margin: 0; flex: 1; padding: 0 10px; }
.topbar button {
  background: rgba(255,255,255,0.15);
  color: #fff;
  padding: 9px 14px;
  min-height: 38px;
}
.topbar button:active { background: rgba(255,255,255,0.28); }
.icon-btn {
  min-width: 42px;
  min-height: 42px;
  padding: 0 !important;
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Side drawer (menu: language, account, password, logout) */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  width: min(300px, 82vw);
  background: #fff;
  z-index: 41;
  padding: 16px;
  padding-top: max(16px, var(--safe-top));
  padding-bottom: max(16px, var(--safe-bottom));
  box-shadow: 0 0 24px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: transform 0.25s ease;
  overflow-y: auto;
}
[dir="rtl"] .drawer { right: 0; transform: translateX(100%); }
[dir="rtl"] .drawer.open { transform: translateX(0); }
[dir="ltr"] .drawer { left: 0; transform: translateX(-100%); }
[dir="ltr"] .drawer.open { transform: translateX(0); }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px; }
.drawer-email { font-size: 13px; color: #6b7280; word-break: break-all; padding-top: 8px; }
.drawer-head .icon-btn { background: #f4f5f7; color: #1f2430; }
.drawer-item {
  text-align: start;
  padding: 12px 14px;
  border-radius: 10px;
  background: #f4f5f7;
  color: #1f2430;
  font-size: 14px;
  min-height: 44px;
}
.drawer-item-danger { background: #fee2e2; color: #991b1b; }
.drawer-msg { font-size: 12px; color: #4b5563; margin: 0; }

.layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 16px;
  padding: 16px;
  padding-bottom: max(16px, var(--safe-bottom));
  align-items: start;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; gap: 0; padding: 12px; }
  .layout > div:first-child { display: block; }
  .layout .detail { display: none; }
  .layout.has-detail > div:first-child { display: none; }
  .layout.has-detail .detail {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
    padding-top: max(16px, var(--safe-top));
    padding-bottom: max(24px, calc(var(--safe-bottom) + 16px));
    border-radius: 0;
    background: #f4f5f7;
  }
}

.card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 1px 6px rgba(0,0,0,0.06);
  padding: 16px;
}
.settings-card { margin-bottom: 12px; }
.settings-row { display: flex; gap: 8px; align-items: flex-end; }
.settings-row > div { flex: 1; }

.back-btn {
  display: none;
  align-items: center;
  gap: 6px;
  background: transparent;
  color: var(--brand);
  font-weight: 600;
  font-size: 14px;
  padding: 8px 0 16px;
  margin: 0;
  min-height: 36px;
}
@media (max-width: 900px) {
  .back-btn { display: inline-flex; }
}

.orders-list-body {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.status-tabs {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 0 0 auto;
  width: 78px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  max-height: min(70vh, 520px);
}
.status-tabs::-webkit-scrollbar { display: none; }
.status-tabs button {
  padding: 10px 4px;
  background: #eef0f2;
  color: #374151;
  font-size: 11px;
  line-height: 1.3;
  white-space: normal;
  min-height: 44px;
  flex-shrink: 0;
  text-align: center;
}
.status-tabs button.active { background: var(--brand); color: #fff; }
.orders-list-scroll {
  flex: 1;
  min-width: 0;
}

.order-row {
  padding: 14px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  min-height: 44px;
}
.order-row:active { background: #f0f7f2; }
.order-row:hover { border-color: var(--brand); }
.order-row.selected { border-color: var(--brand); background: var(--brand-light); }
.order-row .top { display: flex; justify-content: space-between; font-weight: 600; }
.order-row .meta { font-size: 12px; color: #6b7280; margin-top: 4px; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge.new { background: #dbeafe; color: #1e40af; }
.badge.sent_to_supplier { background: #fef3c7; color: #92400e; }
.badge.ready_to_ship { background: #ede9fe; color: #5b21b6; }
.badge.shipped { background: #cffafe; color: #155e75; }
.badge.delivered { background: #d1fae5; color: #065f46; }
.badge.cancelled { background: #fee2e2; color: #991b1b; }

.detail h2 { margin-top: 0; }
.items-table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 14px; }
.items-table th, .items-table td { text-align: right; padding: 10px 8px; border-bottom: 1px solid #eee; }
.status-row { display: flex; gap: 8px; align-items: center; margin: 16px 0; flex-wrap: wrap; }
.status-row select { flex: 1; min-width: 140px; }
.status-row button { padding: 12px 18px; background: var(--brand); color: #fff; min-height: 44px; }
.wa-box { border: 1px dashed #d8dbe0; border-radius: 12px; padding: 14px; margin-top: 16px; }
.wa-row { display: flex; gap: 8px; flex-wrap: wrap; }
.wa-row input { flex: 1; min-width: 160px; }
.wa-row button { padding: 12px 18px; background: #25D366; color: #fff; white-space: nowrap; min-height: 44px; }
.wa-save { display: flex; align-items: center; gap: 6px; margin-top: 8px; font-size: 13px; color: #4b5563; }
.empty-state { color: #9ca3af; text-align: center; padding: 40px 0; }

/* Admin bottom nav (mobile-style tab bar, used on all sizes for consistency) */
.main-tabs {
  display: flex;
  gap: 0;
  padding: 6px max(8px, var(--safe-left)) calc(6px + var(--safe-bottom)) max(8px, var(--safe-right));
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  border-top: 1px solid #e5e7eb;
  box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
  z-index: 25;
}
#appLayout .layout { padding-bottom: 84px; }
.main-tabs button {
  flex: 1;
  padding: 10px 6px;
  background: transparent;
  color: #9ca3af;
  font-weight: 600;
  font-size: 12px;
  border-radius: 10px;
  min-height: 48px;
}
.main-tabs button.active { background: var(--brand-light); color: var(--brand); }

.team-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.team-table th, .team-table td { text-align: right; padding: 12px 8px; border-bottom: 1px solid #eee; }
.team-table select { width: auto; padding: 8px 10px; min-height: 40px; font-size: 14px; }
.role-badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.role-badge.admin { background: #ede9fe; color: #5b21b6; }
.role-badge.employee { background: #d1fae5; color: #065f46; }
.role-badge.supplier { background: #fef3c7; color: #92400e; }
.role-badge.none { background: #f3f4f6; color: #6b7280; }
.acct-status { font-size: 12px; font-weight: 600; }
.acct-status.active { color: #065f46; }
.acct-status.disabled { color: #991b1b; }
.invite-row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.invite-row > div { flex: 1; min-width: 200px; }
.invite-row select { min-width: 120px; }
.invite-msg { font-size: 13px; margin-top: 8px; }
.action-btn { padding: 8px 14px; font-size: 12px; min-height: 36px; }
.action-btn.disable { background: #fee2e2; color: #991b1b; }
.action-btn.enable { background: #d1fae5; color: #065f46; }

.notice {
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: #92400e;
  padding: 14px;
  border-radius: 12px;
  margin: 16px;
  font-size: 14px;
}

@media (max-width: 640px) {
  .card { border-radius: 14px; padding: 14px; }
  .items-table th, .items-table td { padding: 8px 6px; font-size: 13px; }

  /* Team table becomes a stack of cards instead of a wide scrolling table */
  .team-table thead { display: none; }
  .team-table, .team-table tbody, .team-table tr, .team-table td { display: block; width: 100%; }
  .team-table { border: none; }
  .team-table tr {
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    margin-bottom: 10px;
    padding: 10px 12px;
    background: #fff;
  }
  .team-table td {
    border-bottom: none;
    padding: 7px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    text-align: end;
  }
  .team-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #6b7280;
    font-size: 12px;
    text-align: start;
  }
}
