:root, [data-theme="dark"] {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1f2630;
  --border: #2a313c;
  --text: #e6edf3;
  --muted: #8b96a3;
  --accent: #4ea1ff;
  --accent-2: #2563eb;
  --danger: #ef4444;
  --ok: #22c55e;
  --shadow: 0 4px 16px rgba(0,0,0,.4);
  --radius: 12px;
}
[data-theme="light"] {
  --bg: #f6f8fa;
  --panel: #ffffff;
  --panel-2: #eef1f4;
  --border: #d0d7de;
  --text: #1f2328;
  --muted: #6c737e;
  --accent: #0969da;
  --accent-2: #084599;
  --danger: #cf222e;
  --ok: #1a7f37;
  --shadow: 0 4px 16px rgba(0,0,0,.12);
  --radius: 12px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.5 -apple-system,Segoe UI,Roboto,sans-serif; }
[x-cloak] { display: none !important; }
header {
  display: flex; flex-wrap: wrap; align-items: center; gap: 1rem;
  padding: .75rem 1rem; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
header h1 { margin: 0; font-size: 1.15rem; }
nav { display: flex; gap: .5rem; flex-wrap: wrap; margin-left: auto; }
nav a { color: var(--muted); text-decoration: none; padding: .35rem .7rem; border-radius: 8px; }
nav a.active, nav a:hover { color: var(--text); background: var(--panel-2); }
main { padding: 1rem; max-width: 960px; margin: 0 auto; }
section { margin-bottom: 1.5rem; }
h2 { font-size: 1rem; margin: .5rem 0; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.env-badge { font-size: .7rem; padding: .15rem .5rem; border-radius: 999px; background: var(--panel-2); color: var(--muted); margin-left: .5rem; }
.env-development { background: #5a3b1f; color: #ffd28a; }
.env-production { background: #1f3a2a; color: #86efac; }

button, .button {
  display: inline-block; background: var(--accent); color: white; border: 0;
  padding: .55rem .9rem; border-radius: 8px; font: inherit; font-weight: 600;
  cursor: pointer; text-decoration: none;
}
button:hover, .button:hover { background: var(--accent-2); }
button.secondary { background: var(--panel-2); color: var(--text); }
button.danger { background: var(--danger); }
button:disabled { opacity: .4; cursor: not-allowed; }

input, select, textarea {
  width: 100%; padding: .55rem .7rem; background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  font-family: inherit; font-size: 16px; /* 16px keeps iOS Safari from auto-zooming on focus */
}
label { display: block; margin: .5rem 0; font-size: .9rem; color: var(--muted); }
label input, label select, label textarea { margin-top: .25rem; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: .75rem; }
legend { padding: 0 .5rem; color: var(--muted); }
.search { margin-bottom: 1rem; }

.toolbar { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; align-items: center; }
label.inline { display: inline-flex; align-items: center; gap: .35rem; margin: 0; }
label.inline input[type=checkbox] { width: auto; margin: 0; }
.file-btn { background: var(--panel-2); padding: .55rem .9rem; border-radius: 8px; cursor: pointer; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; }
.tile {
  display: flex; flex-direction: column; gap: .25rem; padding: 1rem .75rem;
  background: var(--panel); color: var(--text); text-align: left; min-height: 80px;
  border: 1px solid var(--border); border-radius: var(--radius);
}
.tile:hover { background: var(--panel-2); }
.tile-name { font-weight: 600; }
.tile-qty { color: var(--muted); font-size: .85rem; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center;
  padding: 1rem; z-index: 100;
}
.modal-body { background: var(--panel); padding: 1.25rem; border-radius: var(--radius); max-width: 500px; width: 100%; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow); }
.modal-body .actions { display: flex; gap: .5rem; margin-top: 1rem; }
.stepper { display: flex; align-items: center; gap: .75rem; margin: .35rem 0 .75rem; }
.stepper button { min-width: 3.2rem; font-size: 1.1rem; }
.stepper-val { font-size: 1.5rem; font-weight: 700; min-width: 3rem; text-align: center; }

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); text-align: left; }
th { color: var(--muted); font-weight: 500; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px,1fr)); gap: .6rem; }
.kpis div { background: var(--panel); padding: .75rem; border-radius: var(--radius); }
.kpis .label { display: block; color: var(--muted); font-size: .8rem; }
.kpis .value { display: block; font-size: 1.4rem; font-weight: 600; }

.active-event { background: #1f3a2a; padding: .6rem 1rem; border-radius: var(--radius); margin-bottom: .5rem; }
.no-event { background: #3a2a1f; padding: .6rem 1rem; border-radius: var(--radius); margin-bottom: .5rem; }
.active-event small, .no-event small { color: var(--muted); display: block; }

.toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--ok); color: white; padding: .6rem 1rem; border-radius: 8px; box-shadow: var(--shadow); z-index: 200; }
.err { color: var(--danger); }
.notice { color: var(--text); background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: .6rem .8rem; font-size: .9rem; }
.ok { color: var(--ok); }
.warn { color: #f59e0b; }
.muted { color: var(--muted); }
.badge-display { display: inline-block; padding: .1rem .4rem; border-radius: 4px; background: #1e3a5f; color: #93c5fd; font-size: .8rem; margin-left: .25rem; }
.badge-damaged { display: inline-block; padding: .1rem .4rem; border-radius: 4px; background: #4a1f1f; color: #fca5a5; font-size: .8rem; margin-left: .25rem; }
.tile-qty { display: flex; flex-wrap: wrap; gap: .25rem; align-items: center; color: var(--muted); font-size: .85rem; }
.badge-cart { display: inline-block; padding: .1rem .4rem; border-radius: 4px; background: #1f3a2a; color: #86efac; font-size: .8rem; margin-left: .25rem; font-weight: 600; }
.badge-unavail { display: inline-block; padding: .1rem .4rem; border-radius: 4px; background: #4a1f1f; color: #fca5a5; font-size: .75rem; font-weight: 600; }
tr.row-unavail { opacity: .5; }
tr.row-unavail:hover { opacity: 1; }

.cart-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--panel); border-top: 1px solid var(--border);
  padding: .75rem 1rem; display: flex; gap: 1rem; align-items: center; justify-content: space-between;
  box-shadow: 0 -4px 16px rgba(0,0,0,.3); z-index: 50;
}
.cart-summary { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.deal-tag { display: inline-block; background: var(--accent); color: white; padding: .15rem .5rem; border-radius: 4px; font-size: .75rem; font-weight: 700; }
body.has-cart-open { padding-bottom: 0; }
body main { padding-bottom: 5rem; }

.cart-panel {
  position: fixed; bottom: 0; left: 0; right: 0; top: 0;
  background: rgba(0,0,0,.5); z-index: 60; display: flex; align-items: flex-end; justify-content: center;
  padding: 1rem;
}
.cart-panel-body {
  background: var(--panel); border-radius: var(--radius) var(--radius) 0 0;
  padding: 1.25rem; max-width: 700px; width: 100%; max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow);
}
.cart-table th, .cart-table td { padding: .35rem .4rem; vertical-align: middle; }
.cart-table .warn-row { background: #3a2a1f; }
.cart-totals { display: flex; gap: 1rem; padding: .75rem 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); margin: .75rem 0; flex-wrap: wrap; }
.cart-totals div { display: flex; gap: .5rem; align-items: baseline; }
.cart-totals strong { font-size: 1.2rem; }
.cart-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.cart-table { width: 100%; }
.cart-table input.qty { width: 4rem; }
/* Deal options: radio + a single wrapping text block (not per-word flex items) */
.deal-opt { display: flex; gap: .5rem; align-items: baseline; margin: .4rem 0; }
.deal-opt input[type=radio] { flex: 0 0 auto; width: auto; margin: 0; }
.deal-opt > span { flex: 1 1 auto; min-width: 0; }
.deal-head { margin: 0 0 .25rem; }
.deal-types { margin-top: .5rem; border-top: 1px solid var(--border); padding-top: .4rem; }
.deal-type { margin: .25rem 0; }

.pending-order {
  background: var(--panel); border-left: 3px solid var(--accent);
  padding: .75rem 1rem; border-radius: 8px; margin-bottom: .75rem;
}
.pending-order-head { display: flex; gap: 1rem; flex-wrap: wrap; align-items: baseline; margin-bottom: .35rem; }
.pending-order details { margin: .35rem 0; }
.pending-order details summary { cursor: pointer; color: var(--muted); }
.pending-order ul { margin: .35rem 0; padding-left: 1.25rem; }
.pending-order .actions { margin-top: .5rem; display: flex; gap: .5rem; }

.tile-photo { width: 100%; height: 70px; object-fit: cover; border-radius: 6px; margin-bottom: .25rem; background: var(--panel-2); }
.tile { padding: .5rem; }
.photo-cell { display: inline-block; cursor: pointer; width: 48px; height: 48px; background: var(--panel-2); border-radius: 6px; overflow: hidden; text-align: center; line-height: 48px; color: var(--muted); border: 1px dashed var(--border); }
.photo-cell img { width: 100%; height: 100%; object-fit: cover; }

.undo-bar {
  position: fixed; bottom: 4.5rem; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--danger); border-radius: 8px;
  padding: .55rem 1rem; box-shadow: var(--shadow); display: flex; gap: .5rem; align-items: center; z-index: 55;
}
.cart-preview { font-size: .8rem; margin-left: .5rem; max-width: 30vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.online-indicator {
  position: fixed; top: .5rem; right: .5rem; padding: .2rem .5rem;
  border-radius: 4px; font-size: .75rem; font-weight: 600;
  z-index: 200; pointer-events: none;
}
.online-indicator[data-state="offline"] { background: var(--danger); color: white; }
.online-indicator[data-state="syncing"] { background: #f59e0b; color: white; }
.online-indicator[data-state="online"] { display: none; }

/* ---- Gallery (display) page ---- */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: .75rem;
  margin-bottom: 2rem;
}
.gallery-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0; cursor: pointer; overflow: hidden; text-align: left;
  color: var(--text); display: flex; flex-direction: column;
}
.gallery-card img {
  width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--panel-2);
}
.gallery-caption { padding: .5rem .65rem; }
.gallery-name { font-weight: 600; font-size: .9rem; }

.no-photo-section { border-top: 1px solid var(--border); padding-top: 1rem; }
.no-photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .5rem; }
.no-photo-card {
  background: var(--panel-2); border-radius: 8px; padding: .6rem .8rem;
  border: 1px dashed var(--border); display: flex; flex-direction: column; gap: .15rem;
}

.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center;
  z-index: 300; padding: 1rem;
}
.lightbox-content {
  display: flex; flex-direction: column; align-items: center; gap: .5rem;
  max-width: 95vw; max-height: 95vh;
}
.lightbox-content img {
  max-width: 95vw; max-height: 75vh; object-fit: contain; border-radius: 8px;
  background: var(--panel);
}
.lightbox-meta { text-align: center; color: white; }
.lightbox-meta h2 { margin: 0; }
.lightbox-close, .lightbox-nav {
  position: fixed; top: 1rem; background: rgba(0,0,0,.5); color: white; border: 0;
  width: 3rem; height: 3rem; font-size: 1.6rem; border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.lightbox-close { right: 1rem; }
.lightbox-nav { top: 50%; transform: translateY(-50%); }
.lightbox-prev { left: 1rem; }
.lightbox-next { right: 1rem; }
.lightbox-nav:hover, .lightbox-close:hover { background: rgba(0,0,0,.8); }

.gallery-body main { padding-bottom: 2rem; }

.damaged-section { border-top: 1px solid var(--border); padding-top: 1rem; margin-top: 1rem; }
.damaged-card { opacity: 0.95; border: 1px solid var(--danger); }
.damaged-card .gallery-caption .badge-damaged { display: inline-block; margin-top: .25rem; }

.event-active-row { background: rgba(34, 197, 94, 0.08); }
.event-past-row { color: var(--muted); }
.badge-active { display: inline-block; padding: .15rem .5rem; border-radius: 4px; background: var(--ok); color: white; font-size: .75rem; font-weight: 700; }

.badge-paper { display: inline-block; padding: .1rem .4rem; border-radius: 4px; background: #4a3f1f; color: #fde68a; font-size: .75rem; }
.badge-metals { display: inline-block; padding: .1rem .4rem; border-radius: 4px; background: #1f3a4a; color: #93c5fd; font-size: .75rem; }
.paper-tile { border-color: #4a3f1f; }
.paper-tile:hover { background: #2a2418; }
.product-type-header { margin-top: 1rem; padding-bottom: .5rem; border-bottom: 2px solid var(--accent); color: var(--accent); }
.product-type-row { background: var(--panel); padding: .75rem 1rem; border-radius: 8px; margin-bottom: .75rem; border: 1px solid var(--border); }
.product-type-head { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.pricing-builder { margin-top: .75rem; padding-top: .75rem; border-top: 1px solid var(--border); }
.step-row { display: flex; gap: .5rem; align-items: center; margin: .3rem 0; flex-wrap: wrap; }
.tag-override { display: flex; gap: .5rem; align-items: center; padding: .25rem .5rem; background: var(--panel-2); border-radius: 4px; margin: .2rem 0; }

body.login { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
body.login main { max-width: 360px; width: 100%; padding: 2rem; background: var(--panel); border-radius: var(--radius); }
body.login h1 { text-align: center; }
body.login button { width: 100%; margin-top: .5rem; }

/* Wide-table panes (Items, Bulk move, History): horizontal scroll on small
   screens so dense tables don't squash cells or trigger viewport reflows. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-scroll table { min-width: 760px; }

@media (max-width: 600px) {
  header h1 { font-size: 1rem; width: 100%; }
  nav { width: 100%; overflow-x: auto; }
  .toolbar > * { flex: 1 1 auto; }
  /* Search input dominates the toolbar on a phone — better than 1/6 width. */
  .toolbar > input[type=text], .toolbar > input:not([type]) { flex: 1 1 100%; }

  /* Cart panel: reclaim horizontal room so the table + deal text fit */
  .cart-panel { padding: 0; }
  .cart-panel-body { padding: 1rem .6rem; max-height: 94vh;
    border-radius: var(--radius) var(--radius) 0 0; }
  .cart-table th, .cart-table td { padding: .3rem .25rem; font-size: .85rem; }
  .cart-table input.qty { width: 3rem; }
  .cart-table .danger { padding: .25rem .45rem; }

  /* Dense Items / history tables: tighter padding so more fits per scroll. */
  .table-scroll th, .table-scroll td { padding: .3rem .35rem; font-size: .85rem; }
  .table-scroll button { padding: .3rem .5rem; font-size: .85rem; }
}

/* Pull-to-refresh — installed PWA only (see initPullToRefresh in common.js) */
.pull-refresh {
  position: fixed; top: -46px; left: 50%; z-index: 350;
  width: 34px; height: 34px;
  display: flex; align-items: center; justify-content: center;
  background: var(--panel); border: 1px solid var(--border); border-radius: 50%;
  box-shadow: var(--shadow);
  transform: translateX(-50%);
  transition: transform .25s cubic-bezier(.2,.8,.2,1);
  pointer-events: none;
}
.pull-refresh.dragging { transition: none; }
.pull-refresh .pr-spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
}
.pull-refresh.ready .pr-spinner { border-top-color: var(--ok); }
.pull-refresh.refreshing .pr-spinner { animation: pr-spin .7s linear infinite; }
@keyframes pr-spin { to { transform: rotate(360deg); } }
