:root {
  --sand: #F6F1E7;
  --sand-deep: #EDE3CE;
  --white: #FFFDF9;
  --ink: #2B2620;
  --ink-soft: #7A6E5C;
  --teal: #1F5C55;
  --teal-deep: #143F3A;
  --brown: #8A5A34;
  --brick: #C1553A;
  --brick-deep: #A2432E;
  --line: #E3D8C0;
  --shadow: 0 1px 2px rgba(43,38,32,.06), 0 6px 20px rgba(43,38,32,.06);
  --radius: 14px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--sand);
  color: var(--ink);
  font-family: 'Cairo', system-ui, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { font-family: 'Markazi Text', 'Cairo', serif; font-weight: 700; margin: 0; }

button, input, select { font-family: inherit; font-size: 1rem; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(600px 300px at 15% -10%, #E9DEC4 0%, transparent 60%),
    radial-gradient(500px 260px at 110% 10%, #DCE9E4 0%, transparent 55%),
    var(--sand);
}
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 28px;
  text-align: center;
}
.login-mark { margin: 0 auto 8px; width: 112px; height: 112px; }
.login-mark img { display: block; width: 100%; height: 100%; object-fit: contain; }
.login-card h1 { font-size: 1.7rem; color: var(--ink); }
.login-sub { color: var(--ink-soft); margin: 4px 0 22px; font-size: .92rem; }
.login-card input, .login-card select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: var(--sand);
  text-align: center;
  margin-bottom: 12px;
}
.login-card input:focus, .login-card select:focus { outline: 2px solid var(--teal); outline-offset: 1px; }
.login-field { text-align: right; }
.login-field span { display: block; font-size: .8rem; color: var(--ink-soft); margin-bottom: 4px; }
.login-error { color: var(--brick-deep); font-size: .88rem; margin-bottom: 12px; }
.login-card button {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: var(--teal);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
.login-card button:hover { background: var(--teal-deep); }

/* ---------- Layout ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--white);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-title { display: flex; align-items: center; gap: 10px; }
.topbar-title .mark { display: flex; width: 34px; height: 34px; }
.topbar-title .mark img { display: block; width: 100%; height: 100%; object-fit: contain; }
.topbar-title h1 { font-size: 1.35rem; }
.topbar-stats { display: flex; align-items: center; gap: 18px; }
.current-user { color: var(--teal-deep); font-size: .82rem; white-space: nowrap; }
.stat { text-align: center; line-height: 1.2; }
.stat-label { display: block; font-size: .72rem; color: var(--ink-soft); }
.stat-value { display: block; font-weight: 700; font-variant-numeric: tabular-nums; }
.logout-btn {
  color: var(--brick-deep);
  text-decoration: none;
  font-size: .85rem;
  border: 1px solid var(--line);
  padding: 6px 12px;
  border-radius: 8px;
}
.logout-btn:hover { background: var(--sand); }

.low-stock-banner {
  background: #FBEAE2;
  color: var(--brick-deep);
  border-bottom: 1px solid #F0CFC0;
  padding: 9px 20px;
  font-size: .9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  text-align: center;
}
.low-stock-summary { font-weight: 700; }
.low-stock-toggle {
  border: 1px solid #E3B39F;
  border-radius: 999px;
  padding: 3px 10px;
  background: rgba(255,255,255,.45);
  color: var(--brick-deep);
  cursor: pointer;
  font: inherit;
  font-size: .78rem;
}
.low-stock-toggle:hover { background: var(--white); }
.low-stock-details {
  flex-basis: 100%;
  max-width: 980px;
  line-height: 1.7;
  font-size: .8rem;
}

.dashboard-grid {
  max-width: 920px;
  margin: 14px auto 0;
  padding: 0 20px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}
.dashboard-card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  min-width: 0;
}
.dashboard-label { display: block; color: var(--ink-soft); font-size: .8rem; }
.dashboard-card strong {
  display: block;
  margin-top: 3px;
  color: var(--ink);
  font-size: 1.35rem;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}
.dashboard-card small { display: block; color: var(--ink-soft); font-size: .72rem; margin-top: 3px; }
.dashboard-revenue strong { color: var(--teal-deep); }
.dashboard-expenses strong { color: var(--brick-deep); }
.dashboard-net strong { color: var(--teal); }
.dashboard-stock strong { color: var(--brown); }
.dashboard-sold strong { color: var(--teal); }
.dashboard-low-stock strong { color: var(--brick-deep); }

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 20px 0;
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.tab {
  border: none;
  background: none;
  padding: 10px 16px;
  cursor: pointer;
  color: var(--ink-soft);
  font-weight: 600;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab.active { color: var(--teal-deep); border-bottom-color: var(--teal); }

main { max-width: 920px; margin: 0 auto; padding: 24px 20px 60px; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.panel-head h2 { font-size: 1.4rem; }

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 700;
  cursor: pointer;
}
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:hover { background: var(--teal-deep); }
.btn-ghost { background: transparent; border: 1.5px solid var(--line); color: var(--ink); }
.btn-ghost:hover { background: var(--sand); }
.btn-sm { padding: 6px 12px; font-size: .85rem; }
.btn-block { width: 100%; }
.btn-danger { background: none; color: var(--brick-deep); border: none; cursor: pointer; font-size: .82rem; }

/* ---------- Product cards ---------- */
.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.product-card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.product-thumb {
  width: 100%;
  aspect-ratio: 4/3;
  background: var(--sand-deep);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
  overflow: hidden;
}
.product-thumb img { width: 100%; height: 100%; object-fit: cover; }
.product-body { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 10px; flex: 1; }
.product-name-row { display: flex; align-items: center; justify-content: space-between; }
.product-name { font-weight: 700; font-size: 1.05rem; }

.variant-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.variant-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--sand);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px 4px 6px;
  font-size: .82rem;
}
.variant-chip.low { border-color: #E3B39F; background: #FBEAE2; color: var(--brick-deep); }
.swatch { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(0,0,0,.15); flex-shrink: 0; }
.variant-stock { font-weight: 700; font-variant-numeric: tabular-nums; }
.chip-del {
  background: none; border: none; cursor: pointer; color: var(--ink-soft);
  font-size: .9rem; line-height: 1; padding: 0 2px;
}
.chip-del:hover { color: var(--brick-deep); }
.chip-edit {
  background: none; border: none; cursor: pointer; color: var(--teal); font-size: .73rem;
  font-weight: 700; padding: 0 2px;
}
.chip-edit:hover { color: var(--teal-deep); text-decoration: underline; }
.chip-sell {
  background: var(--teal);
  border: none;
  border-radius: 6px;
  color: #fff;
  cursor: pointer;
  font-size: .72rem;
  font-weight: 700;
  padding: 3px 7px;
}
.chip-sell:hover { background: var(--teal-deep); }
.chip-sell:disabled { opacity: .4; cursor: not-allowed; }

.product-actions { display: flex; gap: 8px; margin-top: auto; }

.empty-state { color: var(--ink-soft); text-align: center; padding: 40px 0; }

/* ---------- Forms ---------- */
.card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
form label {
  display: block;
  font-size: .85rem;
  color: var(--ink-soft);
  margin-bottom: 14px;
}
form input, form select, form textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 11px 12px;
  border: 1.5px solid var(--line);
  border-radius: 9px;
  background: var(--sand);
  color: var(--ink);
}
form input:focus, form select:focus, form textarea:focus { outline: 2px solid var(--teal); outline-offset: 1px; }
.sale-form { max-width: 420px; }
.section-hint { color: var(--ink-soft); font-size: .78rem; }
textarea { resize: vertical; min-height: 78px; }
.expenses-layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 16px;
  align-items: start;
}
.expense-form h3 { margin: 0 0 16px; font-family: 'Markazi Text', 'Cairo', serif; font-size: 1.25rem; }
.expenses-list { display: grid; gap: 10px; }
.expense-card {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 13px 15px;
}
.expense-card-top, .expense-card-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.expense-amount { color: var(--brick-deep); font-size: 1.08rem; }
.expense-date { color: var(--ink-soft); font-size: .76rem; }
.expense-motif { margin: 8px 0; line-height: 1.55; }
.expense-card-footer { color: var(--teal-deep); font-size: .8rem; }
.hint { font-size: .82rem; color: var(--ink-soft); margin: -8px 0 14px; }
.form-error {
  background: #FBEAE2; color: var(--brick-deep);
  padding: 8px 12px; border-radius: 8px; font-size: .85rem; margin-bottom: 12px;
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(43,38,32,.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 50;
}
.modal {
  background: var(--white); border-radius: var(--radius);
  padding: 26px 24px; width: 100%; max-width: 440px; position: relative;
  box-shadow: var(--shadow);
}
.modal-sm { max-width: 340px; }
.modal h2 { margin-bottom: 16px; font-size: 1.3rem; }
.modal-close {
  position: absolute; top: 14px; left: 14px;
  background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--ink-soft);
  width: 28px; height: 28px; line-height: 1;
}
.p-preview { width: 100%; max-height: 160px; object-fit: cover; border-radius: 10px; margin: -6px 0 14px; }
.stock-variant-label { color: var(--ink-soft); font-size: .86rem; margin: -8px 0 16px; }

.variants-editor { margin: 6px 0 18px; }
.variants-editor-head {
  display: flex; align-items: center; justify-content: space-between;
  font-size: .85rem; color: var(--ink-soft); margin-bottom: 8px;
}
.variant-row { display: grid; grid-template-columns: 1fr 1fr 70px 28px; gap: 8px; margin-bottom: 8px; align-items: center; }
.variant-row input {
  padding: 8px 10px; border: 1.5px solid var(--line); border-radius: 8px; background: var(--sand); width: 100%;
}
.variant-row button { background: none; border: none; color: var(--ink-soft); cursor: pointer; font-size: 1.1rem; }

/* ---------- Sales table ---------- */
#sales-table-wrap, #activity-table-wrap { overflow-x: auto; background: var(--white); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.sales-table, .activity-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.sales-table th, .activity-table th {
  text-align: right; padding: 12px 14px; background: var(--sand-deep);
  color: var(--ink-soft); font-weight: 700; font-size: .8rem; white-space: nowrap;
}
.sales-table td, .activity-table td { padding: 11px 14px; border-top: 1px solid var(--line); white-space: nowrap; }
.sales-table td.price { font-weight: 700; font-variant-numeric: tabular-nums; }
.activity-table small { display: block; color: var(--ink-soft); margin-top: 2px; font-size: .78rem; }
.activity-user { font-weight: 700; color: var(--teal-deep); }
.row-action { text-align: left; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--sand); padding: 12px 20px;
  border-radius: 10px; font-size: .9rem; box-shadow: var(--shadow); z-index: 100;
}
.toast.error { background: var(--brick-deep); }

@media (max-width: 640px) {
  .topbar { padding: 10px 12px; }
  .topbar-title h1 { font-size: 1.1rem; }
  .topbar-stats { gap: 12px; }
  .current-user { display: none; }
  .low-stock-banner { padding: 8px 12px; gap: 8px; font-size: .82rem; }
  .low-stock-details { font-size: .76rem; }
  .dashboard-grid { margin: 10px 0 0; padding: 0 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .dashboard-card { padding: 11px 12px; border-radius: 11px; }
  .dashboard-label { font-size: .75rem; }
  .dashboard-card strong { font-size: 1.15rem; }
  .dashboard-card small { font-size: .68rem; }
  main { padding: 18px 12px 92px; }
  .tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 0;
    padding: 0;
    border-top: 1px solid var(--line);
    border-bottom: none;
    box-shadow: 0 -4px 18px rgba(43,38,32,.08);
  }
  .tab {
    min-width: 0;
    min-height: 58px;
    padding: 8px 3px 9px;
    margin: 0;
    border-bottom: none;
    border-top: 2px solid transparent;
    font-size: .78rem;
    line-height: 1.25;
    white-space: normal;
  }
  .tab.active { border-top-color: var(--teal); border-bottom: none; }
  .products-grid { grid-template-columns: 1fr; gap: 12px; }
  .product-thumb { aspect-ratio: 16 / 7; }
  .variant-chips { display: grid; gap: 6px; }
  .variant-chip { width: 100%; min-height: 42px; border-radius: 10px; padding: 6px 9px; }
  .variant-chip .variant-stock { margin-inline-start: auto; }
  .chip-sell, .chip-edit { min-height: 30px; }
  .variant-row { grid-template-columns: 1fr 1fr 56px 24px; }
  .expenses-layout { grid-template-columns: 1fr; gap: 14px; }

  #sales-table-wrap, #activity-table-wrap { overflow: visible; background: transparent; border: none; box-shadow: none; }
  .sales-table, .activity-table,
  .sales-table tbody, .activity-table tbody { display: block; width: 100%; }
  .sales-table thead, .activity-table thead { display: none; }
  .sales-table tr, .activity-table tr {
    display: block;
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
    margin-bottom: 10px;
    padding: 10px 12px;
  }
  .sales-table td, .activity-table td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    padding: 4px 0;
    border: none;
    white-space: normal;
    text-align: left;
  }
  .sales-table td::before, .activity-table td::before {
    content: attr(data-label);
    color: var(--ink-soft);
    font-size: .76rem;
    flex: 0 0 auto;
  }
  .sales-table td.row-action::before { content: none; }
  .sales-table .row-action { justify-content: flex-start; padding-top: 8px; }
  .activity-table td { align-items: flex-start; }
  .activity-table td:last-child { display: block; }
  .activity-table td:last-child::before { display: block; margin-bottom: 2px; }
  .activity-table small { margin-top: 3px; }
}
