/* ============================================================================
   T-ERP APP SHELL & COMPONENTS
   Mobile-first. Desktop rules live inside min-width media queries, so the
   phone layout is the baseline rather than an afterthought squeezed in at
   the bottom of the file.
   ============================================================================ */

/* ---- Shell ---------------------------------------------------------------- */

.app-shell {
  display: block;              /* phone: single column, sidebar is a drawer */
  min-height: 100vh;
  min-height: 100dvh;          /* accounts for mobile browser chrome */
}

@media (min-width: 900px) {
  .app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
  }
}

/* ---- Sidebar -------------------------------------------------------------- */

.sidebar {
  background: var(--charcoal);
  color: var(--gray-300);
  padding: var(--sp-5) var(--sp-3);
  padding-bottom: calc(var(--sp-5) + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;

  /* Phone: off-canvas drawer */
  position: fixed;
  top: 0; left: 0;
  width: min(84vw, 300px);
  height: 100vh;
  height: 100dvh;
  transform: translateX(-100%);
  transition: transform 0.24s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 50;
}
.sidebar.open { transform: translateX(0); }

@media (min-width: 900px) {
  .sidebar {
    position: sticky;
    transform: none;
    width: auto;
    z-index: 1;
  }
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
  backdrop-filter: blur(2px);
}
.sidebar-backdrop.open { display: block; }
@media (min-width: 900px) { .sidebar-backdrop.open { display: none; } }

.sidebar__brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 var(--sp-2) var(--sp-5);
  border-bottom: 1px solid var(--charcoal-2);
  margin-bottom: var(--sp-4);
}
.sidebar__brand .mark {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: var(--text-lg);
  letter-spacing: 0.04em;
  color: var(--accent);
}
.sidebar__brand .name {
  font-size: var(--text-xs);
  color: var(--gray-500);
  letter-spacing: 0.02em;
}

.nav-group { margin-bottom: var(--sp-5); }
.nav-group__label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--gray-500);
  padding: 0 var(--sp-2);
  margin-bottom: var(--sp-2);
  font-weight: 600;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 10px var(--sp-2);
  min-height: var(--tap);          /* comfortable to tap */
  border-radius: var(--radius-sm);
  color: var(--gray-300);
  text-decoration: none;
  font-size: var(--text-sm);
  transition: background 0.14s ease, color 0.14s ease;
}
.nav-link:hover { background: var(--charcoal-2); color: #fff; }
.nav-link[aria-current="page"] {
  background: var(--charcoal-2);
  color: #fff;
  box-shadow: inset 2px 0 0 var(--accent);
}
.nav-link .tag {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--gray-500);
  border: 1px solid var(--charcoal-2);
  border-radius: 3px;
  padding: 1px 4px;
}

@media (min-width: 900px) {
  .nav-link { min-height: 0; padding: 7px var(--sp-2); }
}

/* ---- Topbar --------------------------------------------------------------- */

.topbar {
  min-height: var(--topbar-h);
  border-bottom: 1px solid var(--gray-100);
  background: var(--paper);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--gutter);
  padding-top: calc(var(--sp-2) + env(safe-area-inset-top));
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar__search {
  flex: 1;
  min-width: 0;
  max-width: 420px;
  display: none;               /* hidden on phones — space is precious */
  align-items: center;
  gap: var(--sp-2);
  background: var(--gray-50);
  border: 1px solid var(--gray-100);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  color: var(--gray-700);
  font-size: var(--text-sm);
  overflow: hidden;
}
.topbar__search span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (min-width: 720px) { .topbar__search { display: flex; } }

.topbar__search kbd {
  font-family: var(--font-mono);
  font-size: 10px;
  border: 1px solid var(--gray-300);
  border-bottom-width: 2px;
  border-radius: 3px;
  padding: 1px 4px;
  color: var(--gray-700);
  background: var(--paper);
}

.topbar__spacer { flex: 1; }

.topbar__user {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--text-sm);
  min-width: 0;
}
.topbar__user > span:not(.avatar) {
  display: none;              /* name hidden on phone, avatar carries identity */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 160px;
}
@media (min-width: 720px) { .topbar__user > span:not(.avatar) { display: block; } }

.avatar {
  width: 32px; height: 32px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
}

.hamburger-btn {
  display: flex;
  width: var(--tap); height: var(--tap);
  margin-left: calc(var(--sp-2) * -1);
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.hamburger-btn:active { background: var(--gray-100); }
.hamburger-btn .bars { display: flex; flex-direction: column; gap: 4px; width: 18px; }
.hamburger-btn .bars i { display: block; height: 2px; background: var(--ink); border-radius: 1px; }
@media (min-width: 900px) { .hamburger-btn { display: none; } }

/* ---- Content -------------------------------------------------------------- */

.content {
  padding: var(--sp-4) var(--gutter);
  padding-bottom: calc(var(--sp-8) * 2 + env(safe-area-inset-bottom));
  max-width: 1400px;
}

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--sp-3);
  flex-wrap: wrap;
  margin-bottom: var(--sp-5);
}
.page-header h1 {
  font-size: var(--text-xl);
  margin: 0;
  letter-spacing: -0.01em;
  line-height: 1.2;
}
.page-header .sub {
  font-size: var(--text-sm);
  color: var(--gray-700);
  margin-top: 2px;
}

/* On phones the primary action goes full-width so it's easy to hit. */
@media (max-width: 560px) {
  .page-header > .btn,
  .page-header > a.btn,
  .page-header > .action-row { width: 100%; }
  .page-header > .action-row .btn { flex: 1; text-align: center; justify-content: center; }
}

/* ---- Panels --------------------------------------------------------------- */

.panel {
  background: var(--paper);
  border: 1px solid var(--gray-100);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  margin-bottom: var(--sp-4);
  overflow: hidden;            /* keeps rounded corners over tables */
}

.panel__header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--gray-100);
  font-weight: 600;
  font-size: var(--text-sm);
  gap: var(--sp-2);
  background: var(--paper);
}

/* Filter bars inside panel headers: stack on phone, inline on desktop. */
.panel__header > select,
.panel__header > input {
  min-height: var(--tap);
  flex: 1 1 140px;
  min-width: 0;
}
@media (min-width: 720px) {
  .panel__header > select,
  .panel__header > input { min-height: 36px; flex: 0 1 auto; }
}

/* ---- KPI cards ------------------------------------------------------------ */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--sp-3);
  margin-bottom: var(--sp-5);
}
.kpi-card {
  background: var(--paper);
  border: 1px solid var(--gray-100);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: var(--sp-4);
}
.kpi-card .label {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--gray-700);
  margin-bottom: var(--sp-2);
}
.kpi-card .value {
  font-size: var(--text-xl);
  font-weight: 600;
  letter-spacing: -0.02em;
}

/* ---- Data tables ----------------------------------------------------------
   On phones a wide table is unusable — horizontal scrolling hides the very
   columns you need. So below 720px each row collapses into a stacked card,
   with the column name printed beside each value. That needs one thing from
   the markup: every <td> carries data-label="Column name". Tables without
   those labels still work (they just scroll), so this degrades safely.      */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

table.data th {
  text-align: left;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--gray-700);
  font-weight: 600;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--gray-200);
  background: var(--gray-50);
  white-space: nowrap;
}

table.data td {
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--gray-100);
  vertical-align: middle;
}

table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr:hover { background: var(--gray-50); }

@media (max-width: 719px) {
  table.data thead { display: none; }
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }

  table.data tr {
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    margin: 0 var(--sp-3) var(--sp-3);
    padding: var(--sp-2) 0;
    background: var(--paper);
    box-shadow: var(--shadow-sm);
  }
  table.data tr:first-child { margin-top: var(--sp-3); }
  table.data tbody tr:hover { background: var(--paper); }

  table.data td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-2) var(--sp-4);
    border-bottom: none;
    text-align: right;
  }

  /* Column name, taken from the markup, shown as the row label. */
  table.data td[data-label]::before {
    content: attr(data-label);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--gray-700);
    font-weight: 600;
    text-align: left;
    flex-shrink: 0;
  }

  /* The first cell reads as the card title. */
  table.data td:first-child {
    font-weight: 600;
    font-size: var(--text-base);
    border-bottom: 1px solid var(--gray-100);
    padding-bottom: var(--sp-3);
    margin-bottom: var(--sp-1);
  }

  /* Action buttons get their own full-width row at the bottom of the card. */
  table.data td .action-row { width: 100%; }
  table.data td .action-row .btn { flex: 1; justify-content: center; }
  table.data td:has(.action-row) { padding-top: var(--sp-3); }
  table.data td:has(.action-row)::before { display: none; }
}

/* ---- Buttons -------------------------------------------------------------- */

.btn {
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  font-weight: 500;
  padding: 10px var(--sp-4);
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--gray-300);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: border-color 0.14s ease, background 0.14s ease, opacity 0.14s ease;
  -webkit-tap-highlight-color: transparent;
}
@media (min-width: 900px) {
  .btn { min-height: 36px; padding: 8px var(--sp-4); }
}

.btn:hover { border-color: var(--gray-700); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:disabled:hover { border-color: var(--gray-300); }

.btn.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn.primary:hover { background: #000; }
.btn.accent { background: var(--accent); color: var(--ink); border-color: var(--accent); font-weight: 600; }
.btn.accent:hover { filter: brightness(0.96); }
.btn.danger { color: var(--bad); border-color: var(--gray-300); background: var(--paper); }
.btn.danger:hover { background: var(--bad-soft); border-color: var(--bad); }
.btn[data-active="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }

.btn.sm {
  padding: 6px 10px;
  font-size: var(--text-xs);
  min-height: 36px;
}
@media (min-width: 900px) { .btn.sm { min-height: 30px; } }

.action-row { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---- Forms ---------------------------------------------------------------- */

.field { display: flex; flex-direction: column; gap: 5px; }
.field label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--gray-700);
  letter-spacing: 0.02em;
}

.field input, .field select, .field textarea,
input[type="text"], input[type="number"], input[type="date"],
input[type="email"], input[type="password"], select, textarea {
  width: 100%;
  font-family: var(--font-ui);
  /* 16px prevents iOS Safari from zooming when a field gets focus. */
  font-size: 16px;
  padding: 10px 12px;
  min-height: var(--tap);
  border: 1px solid var(--gray-300);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
}
@media (min-width: 900px) {
  .field input, .field select, .field textarea,
  input[type="text"], input[type="number"], input[type="date"],
  input[type="email"], input[type="password"], select, textarea {
    font-size: var(--text-sm);
    min-height: 36px;
    padding: 8px 10px;
  }
}

.field input:focus, .field select:focus, .field textarea:focus,
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(20, 21, 26, 0.06);
}

input[type="checkbox"], input[type="radio"] {
  width: 20px; height: 20px;
  min-height: 0;
  accent-color: var(--ink);
  cursor: pointer;
  flex-shrink: 0;
}

textarea { min-height: 90px; resize: vertical; }

/* ---- Modals --------------------------------------------------------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 21, 26, 0.55);
  display: flex;
  align-items: flex-end;        /* phone: sheet slides up from the bottom */
  justify-content: center;
  z-index: 60;
  overscroll-behavior: contain;
}
@media (min-width: 720px) {
  .modal-backdrop {
    align-items: flex-start;
    padding: var(--sp-8) var(--sp-4);
    overflow-y: auto;
  }
}

.modal-card {
  background: var(--paper);
  width: 100%;
  max-width: 680px;
  max-height: 92vh;
  max-height: 92dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow-lg);
  padding-bottom: env(safe-area-inset-bottom);
  animation: sheet-up 0.22s cubic-bezier(0.4, 0, 0.2, 1);
}
@media (min-width: 720px) {
  .modal-card { border-radius: var(--radius-lg); max-height: none; animation: none; }
}
@keyframes sheet-up { from { transform: translateY(14px); opacity: 0.6; } to { transform: none; opacity: 1; } }

.modal-card .panel__header {
  position: sticky;
  top: 0;
  z-index: 1;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  color: var(--gray-700);
  line-height: 1;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
}
.modal-close:hover { color: var(--ink); background: var(--gray-100); }

/* Sticky footer for modal actions so Save is always reachable. */
.modal-actions {
  position: sticky;
  bottom: 0;
  background: var(--paper);
  border-top: 1px solid var(--gray-100);
  padding: var(--sp-3) var(--sp-4);
  display: flex;
  gap: var(--sp-2);
  flex-wrap: wrap;
  align-items: center;
}
.modal-actions .btn { flex: 1; }
@media (min-width: 720px) { .modal-actions .btn { flex: 0 0 auto; } }

/* ---- Status indicators ---------------------------------------------------- */

.stock-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: baseline;
  flex-shrink: 0;
}
.stock-dot.normal   { background: var(--good); }
.stock-dot.low      { background: var(--accent); }
.stock-dot.critical { background: #E07B39; }
.stock-dot.out      { background: var(--bad); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: var(--text-xs);
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 20px;
  background: var(--gray-100);
  color: var(--gray-700);
}
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--accent-soft); color: var(--accent-ink); }
.badge.bad  { background: var(--bad-soft); color: var(--bad); }

.empty-state {
  padding: var(--sp-8) var(--sp-4);
  text-align: center;
  color: var(--gray-700);
  font-size: var(--text-sm);
}

/* ---- POS ------------------------------------------------------------------ */

.pos-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--sp-4);
}
@media (min-width: 900px) {
  .pos-layout {
    grid-template-columns: 1fr 380px;
    height: calc(100vh - var(--topbar-h) - var(--sp-8));
  }
}

.pos-search { margin-bottom: var(--sp-3); }
.pos-search input { font-size: 16px; padding: 12px 14px; }

.pos-results {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: var(--sp-2);
  overflow-y: auto;
  align-content: start;
}

.pos-tile {
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-md);
  padding: var(--sp-3);
  background: var(--paper);
  cursor: pointer;
  min-height: 88px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
  -webkit-tap-highlight-color: transparent;
}
.pos-tile:hover { border-color: var(--ink); box-shadow: var(--shadow-md); }
.pos-tile:active { transform: scale(0.98); }
.pos-tile .name { font-weight: 600; font-size: var(--text-sm); line-height: 1.3; }
.pos-tile .meta { font-size: var(--text-xs); color: var(--gray-700); }
.pos-tile .price {
  font-family: var(--font-mono);
  font-weight: 600;
  margin-top: auto;
  font-size: var(--text-base);
}

.pos-cart {
  background: var(--paper);
  border: 1px solid var(--gray-100);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: 70vh;
}
@media (min-width: 900px) { .pos-cart { max-height: none; } }

.cart-items { flex: 1; overflow-y: auto; }

.cart-line {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--gray-100);
  font-size: var(--text-sm);
}

.qty-btn {
  width: 34px; height: 34px;
  border: 1px solid var(--gray-300);
  background: var(--paper);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.qty-btn:hover { border-color: var(--ink); }

.cart-footer {
  border-top: 1px solid var(--gray-200);
  padding: var(--sp-4);
  padding-bottom: calc(var(--sp-4) + env(safe-area-inset-bottom));
  background: var(--gray-50);
}
.cart-total {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: var(--text-lg);
  font-weight: 700;
  margin-bottom: var(--sp-3);
}
.cart-total span:last-child { font-family: var(--font-mono); }

/* ---- Auth (login) ---------------------------------------------------------
   Standalone screen — no shell, no sidebar. Centred card on desktop, full
   width with breathing room on a phone. */

.auth-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-5) var(--gutter);
  padding-top: calc(var(--sp-5) + env(safe-area-inset-top));
  padding-bottom: calc(var(--sp-5) + env(safe-area-inset-bottom));
  background: var(--gray-50);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--paper);
  border: 1px solid var(--gray-100);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: var(--sp-6) var(--sp-5);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}

.auth-card .mark {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: var(--text-xl);
  letter-spacing: 0.04em;
  color: var(--ink);
  display: block;
}

/* The tagline sits directly under the wordmark, so kill the gap between them. */
.auth-card .mark + .sub { margin-top: -14px; }

.auth-card .btn { width: 100%; }

/* ---- Inline form errors ---------------------------------------------------
   Hidden until a message is written into it, so it takes no space when the
   form is clean. */

.form-error {
  display: none;
  background: var(--bad-soft);
  border: 1px solid rgba(179, 67, 46, 0.25);
  color: var(--bad);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: var(--text-sm);
  line-height: 1.4;
}
.form-error:not(:empty) { display: block; }

/* ---- Print ---------------------------------------------------------------- */

@media print {
  .sidebar, .topbar, .sidebar-backdrop, .btn, .action-row, .modal-backdrop { display: none !important; }
  .app-shell { display: block; }
  .content { padding: 0; max-width: none; }
  .panel { border: none; box-shadow: none; }
}
