:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --border: #e0e4e9;
  --text: #1c2430;
  --muted: #6b7480;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.06);
}

/* Set on <html> by js/portal_settings.js from the Display Mode setting. */
:root[data-theme="dark"] {
  --bg: #10141a;
  --surface: #1a1f28;
  --border: #2b323d;
  --text: #e8ecf1;
  --muted: #8b97a5;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

.app-header h1 {
  font-size: 1.1rem;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-icon {
  width: 56px;
  height: 56px;
  margin: -14px 0 -14px -20px;
  object-fit: cover;
  flex-shrink: 0;
}

.app-main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 20px;
}

.btn {
  display: inline-block;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
}

.btn:hover { background: var(--bg); }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.btn-primary:hover { background: var(--primary-dark); }

.period-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.period-nav {
  container-type: inline-size;
}

.period-tabs {
  display: flex;
  gap: 6px;
  background: var(--bg);
  border-radius: 999px;
  padding: 4px;
}

/* Period tabs collapse into a dropdown when the nav is too narrow for them. */
.period-select {
  display: none;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

/* Message box used by restocker/progress.js's showMessage() in place of alert(). */
.message-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 1000;
}

.message-dialog {
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  max-width: 420px;
  width: 100%;
}

.message-dialog .message-text {
  margin: 0 0 16px;
  line-height: 1.4;
}

.message-dialog .btn {
  width: 100%;
}

.message-dialog h2 {
  margin: 0 0 12px;
  font-size: 1.2rem;
}

.modal-body {
  overflow-y: auto;
  max-height: 60vh;
  margin: 0 0 16px;
}

.settings-field {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin: 0 0 16px;
}

.settings-field legend {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0 6px;
}

.settings-field label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
}

.settings-field select,
.settings-field textarea {
  font-family: inherit;
  font-size: 0.95rem;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

.settings-field textarea {
  width: 100%;
  resize: vertical;
}

/* Side-by-side buttons at the bottom of a .message-dialog (e.g. Cancel/Save) —
   overrides the full-width single-button default those share otherwise. */
.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}

.form-actions .btn {
  width: auto;
  flex: 1;
}

@container (max-width: 440px) {
  .period-tabs {
    display: none;
  }
  .period-select {
    display: block;
  }
}

.period-tabs a {
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}

.period-tabs a.active {
  background: var(--primary);
  color: white;
}

.period-range-label {
  font-weight: 600;
  margin: 0 4px;
}

/* A daily/weekly label switches to mm/dd/yy below 480px — see
   period_range_label_html() in lib/reporting.php. Monthly/quarterly/yearly
   labels have no narrow span at all (nothing to compact), so this never
   hides them. */
.period-label-narrow {
  display: none;
}

@media (max-width: 480px) {
  .period-label-wide {
    display: none;
  }
  .period-label-narrow {
    display: inline;
  }
  .period-nav-label {
    display: none;
  }
}

/* Prev / date range / Next move as one block when the nav wraps. */
.period-pager {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: nowrap;
  white-space: nowrap;
  margin-left: auto;
}

.period-date-form {
  margin-left: auto;
}

.period-date-form input[type="date"] {
  font-family: inherit;
  font-size: 0.85rem;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  background: var(--surface);
}

.summary-totals {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}

.summary-totals .stat { display: flex; flex-direction: column; }
.summary-totals .stat .label { color: var(--muted); font-size: 0.8rem; }
.summary-totals .stat .value { font-size: 1.4rem; font-weight: 700; }
.summary-totals .stat-commission .value { color: #475569; }
@media (prefers-color-scheme: dark) {
  .summary-totals .stat-commission .value { color: #94a3b8; }
}

.sub-period-nav { display: flex; align-items: center; justify-content: space-between; margin: 4px 0; }
.sub-period-nav-title { font-weight: 600; }
.sub-period-nav label { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; color: var(--muted); }
.sub-period-nav select {
  font-family: inherit;
  font-size: 0.9rem;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

/* Bars are drawn at a fixed pixel width (see sales_bar_chart_html() in
   sales.php), not stretched to the container — this scrolls horizontally
   instead of squeezing bars when there are many buckets. justify-content
   is set twice on purpose: centering an overflowing flex child leaves its
   start-side overflow unreachable by scrolling (a scroll container's
   scrollLeft can't go negative) — "safe center" (the second line) centers
   only when the content fits and left-aligns it otherwise, so every
   bucket stays reachable; browsers that don't support that value ignore
   the invalid declaration and keep the plain "center" above. */
.sales-bar-chart { margin: 16px 0; overflow-x: auto; display: flex; justify-content: center; justify-content: safe center; }
.sales-bar-chart svg { height: 240px; flex-shrink: 0; }
.sales-bar-chart text { fill: var(--text); font-size: 9px; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

.badge-paid { background: #dcfce7; color: #166534; }
.badge-unpaid { background: #fef3c7; color: #92400e; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th, td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

/* Individual Sales: no line between rows of the same sale (see
   group_detail_rows() in sales.php) — its last row keeps the normal
   border, separating it from the next sale's group. */
tr.sale-group-row td {
  border-bottom: none;
}

/* Name + "x {quantity}" share one block beside the product image. */
.product-text {
  display: inline-block;
  vertical-align: middle;
}

.product-qty {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 2px;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.table-icon {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  object-fit: cover;
  vertical-align: middle;
  margin-right: 8px;
}

.sales-icon {
  width: 60px;
  height: 60px;
  border-radius: 8px;
}

.table-icon-placeholder {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  font-size: 1.3rem;
}

.table-wrap { overflow-x: auto; }

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

.empty-state {
  text-align: center;
  padding: 30px 20px;
  color: var(--muted);
}

/* index.php — location overview + machine list */

.location-header {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.location-photo {
  width: 96px;
  height: 96px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--bg);
  flex-shrink: 0;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px 24px;
}

.detail-grid .field { display: flex; flex-direction: column; gap: 2px; }
.detail-grid .label { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; }
.detail-grid .value { font-size: 0.95rem; }

/* One full-width card per row instead of a multi-column grid, since each
   card already packs photo + info + a right-side stock-status column
   (see .machine-stock-status) — mirrors the main app's .card-grid-wide. */
.machine-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

/* Photo+info on the left (.machine-top, fixed width), stock status filling
   the rest on the right — row on wide screens, column on narrow ones (see
   the @media block below) — mirrors the main app's .card-horizontal. */
.machine-card {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.machine-top {
  display: flex;
  flex-direction: row;
  gap: 12px;
  flex: 0 0 320px;
  min-width: 0;
  padding: 14px;
}

.machine-photo {
  width: 56px;
  height: 56px;
  border-radius: 6px;
  flex-shrink: 0;
  background: var(--bg);
  object-fit: cover;
}

.machine-info { min-width: 0; }
.machine-info h3 { margin: 0 0 2px; font-size: 1rem; }

.machine-stock-status {
  flex: 1;
  min-width: 0;
  max-height: 300px;
  overflow-y: auto;
  padding: 14px;
  border-left: 1px solid var(--border);
}

/* --------------------------- Machine stock status --------------------------- */
/* Mirrors www/css/style.css's copy (see www/js/machines.js's stockStatusHtml()). */

.stock-status {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stock-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.stock-item-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stock-item-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.8rem;
}

.stock-item-icon {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border-radius: 4px;
  overflow: hidden;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.stock-item-icon img { width: 100%; height: 100%; object-fit: cover; }
.stock-item-icon-placeholder { font-size: 0.8rem; line-height: 1; }

.stock-item-name {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stock-item-count { color: var(--muted); white-space: nowrap; flex-shrink: 0; }

.stock-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--bg);
  overflow: hidden;
}

.stock-bar-fill { height: 100%; border-radius: 999px; }

.stock-bar-ok { background: var(--success, #16a34a); }
.stock-bar-low { background: var(--warning, #f59e0b); }
.stock-bar-critical { background: var(--danger); }
.stock-bar-unknown { background: var(--border); }

@media (max-width: 640px) {
  .machine-card { flex-direction: column; }
  .machine-top { flex: 0 0 auto; }
  .machine-stock-status { border-left: none; border-top: 1px solid var(--border); }
}

.btn-lg {
  display: block;
  width: 100%;
  padding: 16px;
  font-size: 1.1rem;
  font-weight: 700;
  text-align: center;
  border-radius: 10px;
}

/* Login page */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  width: 100%;
  max-width: 360px;
}

.login-card h1 {
  margin: 0 0 18px;
  font-size: 1.2rem;
  text-align: center;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 14px;
}

.form input {
  font-family: inherit;
  font-size: 0.95rem;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

.form button { width: 100%; padding: 10px; }

.error-box {
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.85rem;
  margin-bottom: 14px;
}

@media (max-width: 640px) {
  .app-main { padding: 14px; }
  th, td { white-space: normal; }
}
