/* Mobile-first foundation — Granit Sales Analytics ERP */

:root {
  --brand: #667eea;
  --brand-dark: #4c51bf;
  --brand-mid: #5568d3;
  --ok: #047857;
  --ok-bg: #d1fae5;
  --ok-fg: #065f46;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --bad-fg: #991b1b;
  --warn: #92400e;
  --warn-bg: #fef3c7;
  --up: #047857;
  --down: #92400e;
  --text: #333;
  --muted: #888;
  --border: #eee;
  --surface: #fff;
  --bg: #f5f5f5;
  --gap: 0.75rem;
  --radius: 6px;
  --tap: 44px;
  --bar-h: 52px;
  --nav-h: 56px;
  --nav-pad-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  --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);
}

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

h1 {
  font-size: 1.5rem;
  line-height: 1.25;
  margin-bottom: 0.5rem;
}

h2 {
  font-size: 1.15rem;
  line-height: 1.3;
  margin: 1rem 0 0.5rem;
}

/* —— Layout shell —— */
body.has-mobile-shell {
  padding-bottom: var(--nav-pad-bottom);
}

/* Desktop topbar/footer: hidden on phone, shown ≥900px */
.topbar,
footer {
  display: none;
}

.topbar {
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  max-width: 1200px;
  margin: 0 auto;
  padding: calc(0.75rem + var(--safe-top)) 1rem 0;
}

.nav {
  display: flex;
  gap: 1rem;
  font-size: 0.95rem;
}

.nav a {
  color: #334;
  text-decoration: none;
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
}

.nav a.active {
  color: var(--brand-dark);
  font-weight: 600;
  border-bottom: 2px solid var(--brand);
}

.app-bar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: var(--tap) 1fr var(--tap);
  align-items: center;
  gap: 0.35rem;
  height: calc(var(--bar-h) + var(--safe-top));
  padding: var(--safe-top) 0.5rem 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-sizing: border-box;
}

.app-bar-back,
.app-bar-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  color: var(--brand-dark);
  text-decoration: none;
  border-radius: 999px;
}

.app-bar-back:not([href]),
.app-bar-back[href=""] {
  visibility: hidden;
  pointer-events: none;
}

.app-bar-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-bar-avatar {
  background: linear-gradient(135deg, var(--brand) 0%, #764ba2 100%);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  height: var(--nav-pad-bottom);
  padding: 0 0 var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-sizing: border-box;
}

.bottom-nav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  min-height: var(--nav-h);
  padding: 0.25rem 0.15rem;
  color: #556;
  text-decoration: none;
  font-size: 0.68rem;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bottom-nav a svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

.bottom-nav a.active {
  color: var(--brand-dark);
  font-weight: 600;
}

.bottom-nav a span {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.page-back,
.page-h1 {
  display: none;
}

.flash {
  max-width: 1200px;
  margin: 0.75rem auto 0;
  padding: 0 1rem;
}

.flash-item {
  background: var(--surface);
  padding: 0.65rem 1rem;
  margin-bottom: 0.4rem;
  border-left: 4px solid var(--brand);
}

.flash-item.success {
  border-color: var(--ok);
  color: var(--ok);
}

.flash-item.error {
  border-color: var(--bad);
  color: var(--bad);
}

.topbar-right {
  display: inline-flex;
  align-items: center;
  gap: 1rem;
}

.lang-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  color: #555;
  min-height: var(--tap);
}

.lang-switch select {
  padding: 0.35rem 0.5rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: var(--surface);
  font: inherit;
  font-size: 16px;
  min-height: var(--tap);
}

.logout-btn {
  background: none;
  border: 0;
  color: #334;
  font: inherit;
  font-size: 0.875rem;
  cursor: pointer;
  padding: 0.5rem 0.25rem;
  min-height: var(--tap);
  text-decoration: underline;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}

footer {
  text-align: center;
  padding: 2rem 1rem calc(2rem + var(--safe-bottom));
  color: #666;
  font-size: 0.875rem;
}

footer a {
  color: var(--brand-mid);
}

/* More page */
.more-list {
  list-style: none;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  margin: 0 0 1.25rem;
}

.more-list a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 48px;
  padding: 0.65rem 1rem;
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
}

.more-list a:last-child {
  border-bottom: 0;
}

.more-list a svg:first-child {
  width: 22px;
  height: 22px;
  color: var(--brand-dark);
  flex-shrink: 0;
}

.more-list a .more-label {
  flex: 1;
}

.more-list a svg:last-child {
  width: 18px;
  height: 18px;
  color: #999;
  flex-shrink: 0;
}

.more-profile {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem;
}

.more-profile h2 {
  margin-top: 0;
}

.more-profile .lang-switch {
  display: flex;
  width: 100%;
  margin: 0.75rem 0;
}

.more-profile .lang-switch select {
  flex: 1;
}

.more-profile .logout-btn {
  width: 100%;
  min-height: var(--tap);
  padding: 0.65rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  background: #f8f8fb;
}

/* —— Forms —— */
.select-filter {
  width: 100%;
  padding: 0.5rem;
  margin: 0 0 0.35rem;
  font: inherit;
  font-size: 16px;
  min-height: var(--tap);
}

input,
select,
textarea {
  font-size: 16px;
  line-height: 1.25;
}

input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="file"]),
select,
textarea {
  min-height: var(--tap);
}

input[type="checkbox"],
input[type="radio"] {
  width: 1.15rem;
  height: 1.15rem;
  min-width: 1.15rem;
}

label:has(> input[type="checkbox"]),
label:has(> input[type="radio"]) {
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0;
}

/* —— Buttons —— */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  background: var(--brand);
  color: #fff;
  border: 0;
  padding: 0.55rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  font-size: 1rem;
  line-height: 1.2;
  text-decoration: none;
  min-height: var(--tap);
  box-sizing: border-box;
}

.btn:hover {
  filter: brightness(1.05);
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #eef0ff;
  color: #3f4bb8;
  border: 1px solid #c7ccf5;
  padding: 0.55rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  font-size: 1rem;
  line-height: 1.2;
  text-decoration: none;
  min-height: var(--tap);
  box-sizing: border-box;
}

.btn.btn-secondary {
  background: #eef0ff;
  color: #3f4bb8;
  border: 1px solid #c7ccf5;
}

.btn-danger {
  background: var(--bad);
  color: #fff;
}

.btn.btn-danger {
  background: var(--bad);
  color: #fff;
}

.btn-link {
  display: inline-flex;
  align-items: center;
  background: none;
  border: 0;
  color: var(--brand);
  cursor: pointer;
  padding: 0.5rem 0.35rem;
  font: inherit;
  font-size: 0.95rem;
  text-decoration: underline;
  min-height: var(--tap);
  box-sizing: border-box;
}

/* —— Shared UI —— */
.hint {
  color: #555;
  margin: 0.75rem 0;
}

.help {
  color: #777;
  font-size: 0.85rem;
  margin: 0.2rem 0 0;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

.empty {
  background: var(--surface);
  padding: 1.5rem;
  color: #666;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  margin: 0 0 1rem;
  color: #444;
}

.stats:has(> .stat) {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin: 1.5rem 0;
}

.stat {
  background: var(--surface);
  padding: 1rem;
}

.stat strong {
  display: block;
  font-size: 1.4rem;
  color: var(--brand);
}

.ok {
  color: var(--ok);
}

.bad {
  color: var(--bad);
}

.up {
  color: var(--up);
  font-weight: 600;
}

.down {
  color: var(--down);
  font-weight: 600;
}

/* —— Tables —— */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
}

th,
td {
  text-align: left;
  padding: 0.6rem;
  border-bottom: 1px solid var(--border);
}

th {
  color: #555;
  font-size: 0.85rem;
}

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

tfoot td {
  font-weight: 600;
}

.scroll-x {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  /* Edge fade hints overflow without page-level scroll */
  background:
    linear-gradient(90deg, var(--surface) 30%, transparent),
    linear-gradient(90deg, transparent, var(--surface) 70%) 100% 0,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, 0.12), transparent),
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.12), transparent) 100% 0;
  background-repeat: no-repeat;
  background-size: 24px 100%, 24px 100%, 12px 100%, 12px 100%;
  background-attachment: local, local, scroll, scroll;
}

/* —— .table-cards: <table> on desktop, stacked cards on phone ——
   Markup:
     <table class="table-cards table-cards--compact">
       <thead>…</thead>
       <tbody>
         <tr>
           <td data-label="…" class="tc-primary"><a href="…">Name</a></td>
           <td data-label="…" class="tc-meta">…</td>
           <td data-label="…" class="num tc-qty">…</td>
           <td data-label="…" class="num tc-amount">…</td>
           <td class="tc-action"><a href="…">Order items</a></td>
         </tr>
       </tbody>
       <tfoot>…</tfoot>
     </table>
   data-label must match the <th> text (same {% trans %}).
   --compact: name left, amount/qty right; meta muted.
   Stretched link (phone): card `position:relative`; `.tc-primary a::after
   { position:absolute; inset:0 }` makes the whole card tappable; hide `.tc-action`
   on phone, show it again ≥900px. If the row has no name link, wrap the name
   in the same href as the action (e.g. receipts_query). The in-flow link is
   min 44×44 so tap metrics see the hit box, not only the ::after overlay.
   A real button in the row (Remove / Add) uses `.tc-action.tc-action--keep`:
   it stays visible on the phone, sits above the overlay (z-index), and is not
   a second copy of the card link.
*/
.table-cards {
  width: 100%;
  border-collapse: collapse;
  background: transparent;
}

.table-cards thead {
  display: none;
}

.table-cards tbody,
.table-cards tfoot {
  display: block;
}

.table-cards tbody tr,
.table-cards tfoot tr {
  display: block;
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.65rem;
  margin: 0 0 0.45rem;
}

.table-cards tfoot tr {
  font-weight: 600;
  position: sticky;
  bottom: calc(var(--nav-pad-bottom) + 0.25rem);
  z-index: 5;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
}

.table-cards td {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.35rem 0;
  border: 0;
  text-align: left;
  word-break: break-word;
}

.table-cards td::before {
  content: attr(data-label);
  flex: 0 1 auto;
  color: var(--muted);
  font-size: 0.85rem;
  padding-right: 0.5rem;
}

.table-cards td:not([data-label])::before,
.table-cards td[data-label=""]::before {
  content: none;
}

.table-cards td.num {
  font-variant-numeric: tabular-nums;
}

.table-cards td.tc-action {
  display: block;
  padding-top: 0.5rem;
}

.table-cards td.tc-action::before {
  content: none;
}

.table-cards td.tc-action a,
.table-cards td.tc-action .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0.5rem 0.85rem;
  box-sizing: border-box;
}

/* Compact: title + meta left, amount/qty right */
.table-cards--compact tbody tr,
.table-cards--compact tfoot tr {
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 0.75rem;
  row-gap: 0.15rem;
  align-items: start;
}

.table-cards--compact td {
  display: block;
  padding: 0;
}

.table-cards--compact td::before {
  content: none;
}

.table-cards--compact .tc-primary {
  grid-column: 1;
  grid-row: 1;
  font-weight: 600;
  line-height: 1.35;
}

.table-cards--compact .tc-primary a {
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  min-width: var(--tap);
  box-sizing: border-box;
}

.table-cards--compact .tc-id {
  grid-column: 1;
  grid-row: 2;
}

/* Whole card is the hit target; in-flow min-height keeps ≥44px for metrics. */
.table-cards--compact .tc-primary a::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
}

.table-cards--compact .tc-primary a:hover,
.table-cards--compact .tc-primary a:focus-visible {
  color: var(--brand-dark);
  text-decoration: underline;
}

.table-cards--compact .tc-meta,
.table-cards--compact .tc-secondary {
  grid-column: 1;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.35;
}

.table-cards--compact .tc-amount {
  grid-column: 2;
  grid-row: 1;
  text-align: right;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.35;
}

.table-cards--compact .tc-qty {
  grid-column: 2;
  grid-row: 2;
  text-align: right;
  font-size: 0.85rem;
  color: #555;
  font-variant-numeric: tabular-nums;
}

.table-cards--compact .tc-stock {
  grid-column: 2;
  grid-row: 3;
  text-align: right;
  font-size: 0.85rem;
  color: #555;
  font-variant-numeric: tabular-nums;
}

.table-cards--compact .tc-stock--empty {
  display: none;
}

.table-cards--compact .tc-action {
  display: none;
}

/* Button that is not the card's navigation link (Remove, Add). */
.table-cards--compact .tc-action--keep {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  grid-column: 1 / -1;
  position: relative;
  z-index: 2;
  margin-top: 0.35rem;
}

.table-cards--compact .tc-action--keep .btn,
.table-cards--compact .tc-action--keep button {
  position: relative;
  z-index: 2;
}

.table-cards--compact tfoot .tc-primary {
  font-weight: 700;
}

/* —— Auth (shared bits; layout stays in auth_base extra) —— */
body.auth-body {
  min-height: 100vh;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: #1f2937;
  background:
    radial-gradient(ellipse 80% 60% at 10% 0%, rgba(102, 126, 234, 0.35), transparent 55%),
    radial-gradient(ellipse 70% 50% at 90% 100%, rgba(118, 75, 162, 0.28), transparent 50%),
    linear-gradient(160deg, #f3f4f8 0%, #e8eaf6 45%, #f5f5f5 100%);
}

/* —— Breakpoints (min-width only) —— */
@media (min-width: 600px) {
  .container {
    padding: 1.5rem;
  }

  .topbar {
    padding: calc(1rem + var(--safe-top)) 1.5rem 0;
  }

  .flash {
    padding: 0 1.5rem;
  }

  h1 {
    font-size: 1.75rem;
  }
}

@media (min-width: 900px) {
  body.has-mobile-shell {
    padding-bottom: 0;
  }

  .app-bar,
  .bottom-nav {
    display: none;
  }

  .topbar {
    display: flex;
    padding: calc(1rem + var(--safe-top)) 2rem 0;
  }

  footer {
    display: block;
  }

  .page-back {
    display: block;
  }

  .page-h1 {
    display: block;
  }

  .container {
    padding: 2rem;
  }

  .flash {
    padding: 0 2rem;
  }

  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.25rem;
  }

  /* Desktop density: restore compact fields/buttons */
  .select-filter,
  .lang-switch,
  .lang-switch select,
  .logout-btn,
  input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="file"]),
  select,
  textarea,
  .btn,
  .btn-secondary,
  .btn-link,
  label:has(> input[type="checkbox"]),
  label:has(> input[type="radio"]) {
    min-height: unset;
  }

  .btn,
  .btn-secondary {
    padding: 0.4rem 0.85rem;
  }

  .btn-link {
    padding: 0.2rem 0.15rem;
  }

  .select-filter {
    padding: 0.35rem 0.5rem;
  }

  input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="file"]),
  select,
  textarea {
    font-size: inherit;
  }

  /* .table-cards → real table on desktop */
  .table-cards {
    display: table;
    background: var(--surface);
  }

  .table-cards thead {
    display: table-header-group;
  }

  .table-cards tbody,
  .table-cards tfoot {
    display: table-row-group;
  }

  .table-cards tbody tr,
  .table-cards tfoot tr,
  .table-cards--compact tbody tr,
  .table-cards--compact tfoot tr {
    display: table-row;
    background: transparent;
    border: 0;
    border-radius: 0;
    padding: 0;
    margin: 0;
    box-shadow: none;
    position: static;
  }

  .table-cards td,
  .table-cards--compact td {
    display: table-cell;
    padding: 0.6rem;
    border-bottom: 1px solid var(--border);
  }

  .table-cards td::before,
  .table-cards--compact td::before {
    content: none;
  }

  .table-cards td.num,
  .table-cards--compact td.num,
  .table-cards--compact .tc-amount,
  .table-cards--compact .tc-qty,
  .table-cards--compact .tc-stock {
    text-align: right;
    font-weight: inherit;
    font-size: inherit;
    color: inherit;
    grid-column: auto;
    grid-row: auto;
  }

  .table-cards--compact .tc-primary,
  .table-cards--compact .tc-meta,
  .table-cards--compact .tc-id,
  .table-cards--compact .tc-secondary,
  .table-cards--compact .tc-action,
  .table-cards--compact .tc-action--keep {
    grid-column: auto;
    grid-row: auto;
  }

  .table-cards--compact .tc-primary {
    font-weight: inherit;
  }

  .table-cards--compact .tc-meta,
  .table-cards--compact .tc-secondary {
    color: inherit;
    font-size: inherit;
  }

  .table-cards--compact .tc-stock--empty {
    display: table-cell;
  }

  .table-cards--compact .tc-primary a {
    color: var(--brand);
    text-decoration: underline;
  }

  .table-cards--compact .tc-primary a::after {
    content: none;
  }

  .table-cards--compact .tc-primary a {
    min-width: unset;
    min-height: unset;
  }

  .table-cards td.tc-action,
  .table-cards--compact .tc-action,
  .table-cards--compact .tc-action--keep {
    display: table-cell;
    padding: 0.6rem;
    margin-top: 0;
    position: static;
  }

  .table-cards td.tc-action a,
  .table-cards td.tc-action .btn {
    min-height: unset;
    min-width: unset;
    padding: 0.2rem 0.15rem;
  }

  .table-cards tfoot td {
    font-weight: 600;
  }
}

@media (min-width: 1200px) {
  .container {
    padding: 2rem;
  }
}
