:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --ink: #111827;
  --ink-2: #374151;
  --muted: #6b7280;
  --faint: #9ca3af;
  --line: #e5e7eb;
  --line-2: #f0f1f3;
  --accent: #ef5a4c;
  --accent-ink: #c2410c;
  --accent-soft: #fef0ee;
  --teal: #0d9488;
  --teal-soft: #ecfdf8;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.05);
  --shadow: 0 1px 3px rgba(17, 24, 39, 0.06), 0 8px 24px rgba(17, 24, 39, 0.05);
  --side-w: 232px;
}

* {
  box-sizing: border-box;
}
html {
  -webkit-text-size-adjust: 100%;
}
body {
  margin: 0;
  font-family:
    Inter,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    system-ui,
    sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
a {
  color: var(--teal);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
svg {
  flex: 0 0 auto;
}

/* ---------- Sidebar ---------- */
.side {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--side-w);
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  z-index: 30;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px;
}
.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #ff7a6b, var(--accent));
  color: #fff;
  font-weight: 800;
  font-size: 17px;
  box-shadow: 0 4px 12px rgba(239, 90, 76, 0.3);
}
.brand strong {
  display: block;
  font-size: 15px;
  line-height: 1.2;
}
.brand small {
  color: var(--muted);
  font-size: 12px;
}

.tabs {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.tab {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  background: transparent;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--ink-2);
  font: inherit;
  font-weight: 600;
  text-align: left;
  transition:
    background 0.15s,
    color 0.15s;
}
.tab svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--faint);
}
.tab:hover {
  background: var(--surface-2);
}
.tab.active {
  background: var(--accent-soft);
  color: var(--accent-ink);
}
.tab.active svg {
  color: var(--accent);
}

/* ---------- Shell / header ---------- */
.shell {
  margin-left: var(--side-w);
  min-width: 0;
}
.top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px 24px;
  padding: 28px 32px 8px;
}
.top-title {
  flex: 1 1 260px;
}
.eyebrow {
  margin: 0 0 2px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}
h1 {
  margin: 0;
  font-size: 24px;
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 10px;
  flex: 0 1 640px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  color: var(--muted);
  font-size: 12px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.stat span {
  display: block;
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Filters ---------- */
.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  margin: 16px 32px 0;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.filters.hidden {
  display: none;
}
.date-range,
.presets {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.filters label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}
.filters input[type="date"] {
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  background: var(--surface);
  min-height: 34px;
}
.presets .ghost {
  border-radius: 999px;
}

/* ---------- Panels ---------- */
main {
  min-width: 0;
}
.panel {
  display: none;
  padding: 20px 32px 64px;
  overflow-x: auto;
}
.panel.active {
  display: block;
}
.empty {
  color: var(--muted);
  padding: 48px 16px;
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.meta {
  color: var(--muted);
  font-size: 12px;
}
.hint {
  color: var(--muted);
  font-size: 13px;
}

/* ---------- Buttons ---------- */
button.ghost,
a.ghost,
button.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 34px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition:
    background 0.15s,
    border-color 0.15s,
    color 0.15s;
}
button.ghost,
a.ghost {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
}
button.ghost:hover,
a.ghost:hover {
  background: var(--surface-2);
  border-color: #d1d5db;
  text-decoration: none;
}
button.primary {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: #fff;
}
button.primary:hover {
  background: #1f2937;
}
button.danger {
  color: #dc2626;
}
button.danger:hover {
  background: #fef2f2;
  border-color: #fecaca;
}
button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ---------- Toolbars ---------- */
.orders-toolbar,
.buyers-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 0 0 14px;
}
.buyers-toolbar input[type="search"] {
  flex: 1 1 260px;
  max-width: 420px;
  font: inherit;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  background: var(--surface);
  min-height: 36px;
}
#del-all-products,
#del-all-orders,
#del-all-buyers {
  margin-left: auto;
}
.post-filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin: 0 0 14px;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid #fcd5cf;
  border-radius: var(--radius);
}

.read-badge {
  display: inline-block;
  margin: 0 5px 0 0;
  font-size: 0.95em;
  line-height: 1;
  cursor: help;
}
td .read-badge { margin: 0 0 0 5px; }
.read-badge.ai { color: #f59e0b; }
.read-badge.fail { color: #dc2626; }
.reread-toggle { color: #b45309; border-color: #fcd34d; }
.reread-toggle.active { background: #fef3c7; }

.sel-all {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-2);
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.sel-box,
.sel-all input {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--accent);
}

/* ---------- Post cards ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition:
    box-shadow 0.2s,
    transform 0.2s;
}
.card:hover {
  box-shadow: var(--shadow);
  transform: translateY(-1px);
}
.card.is-selected {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.card-img {
  aspect-ratio: 4 / 3;
  background: linear-gradient(135deg, #f3f4f6, #e5e7eb);
  overflow: hidden;
}
.card-img img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.card-sel {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
  cursor: pointer;
}
.card .body {
  display: flex;
  flex-direction: column;
  flex: 1;
  padding: 12px 14px 14px;
}
.card-title {
  margin: 0 0 6px;
  font-size: 15px;
  font-weight: 700;
  line-height: 1.35;
  letter-spacing: -0.01em;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: calc(1.35em * 2);
}
.text {
  white-space: pre-line;
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: calc(1.5em * 3);
}
.card .meta {
  margin-top: 10px;
  color: var(--faint);
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: auto;
  padding-top: 12px;
}
.actions > * {
  flex: 1 1 auto;
}

/* ---------- Tables (shared) ---------- */
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
th,
td {
  text-align: left;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line-2);
  vertical-align: top;
}
th {
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--line);
}
tr.is-selected > td {
  background: var(--accent-soft) !important;
}
th.sel-cell,
td.sel-cell {
  width: 36px;
  text-align: center;
  padding-left: 12px;
  padding-right: 4px;
}
td.row-actions {
  width: 1%;
  white-space: nowrap;
  vertical-align: top;
}
td.row-actions > * + * {
  margin-left: 6px;
}
.row-actions:not(td) {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

/* ---------- Orders ---------- */
.orders-table .group-post td {
  background: var(--surface-2);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 14px;
}
.orders-table tbody tr.group-post:first-child td {
  border-top: 0;
}
.orders-table .group-post strong {
  display: block;
  font-size: 15px;
  letter-spacing: -0.01em;
  margin-bottom: 2px;
}
.orders-table .group-post span {
  color: var(--muted);
  font-size: 12px;
}
.group-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.orders-table .buyer-cell {
  vertical-align: top;
  border-right: 1px solid var(--line-2);
  width: 200px;
  min-width: 170px;
}
.orders-table .buyer-cell a {
  font-weight: 600;
}
.orders-table .buyer-cell .dim {
  color: var(--faint);
}
.orders-table .buyer-cell .ghost {
  margin: 8px 6px 0 0;
  min-height: 30px;
  padding: 6px 10px;
  font-size: 12px;
}
.buyer-count {
  margin-top: 6px;
  font-weight: 700;
  font-size: 12px;
  color: var(--ink-2);
}
.orders-table .buyer-end td {
  border-bottom: 1px solid var(--line);
}
.orders-table .order-draft td {
  background: #fffbeb;
}
.orders-table .comment {
  color: var(--muted);
  font-size: 12px;
  max-width: 260px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.orders-table .meta {
  margin-top: 2px;
}
.orders-table input[data-edit-k],
.buyers-table input[data-buyer-k] {
  width: 100%;
  min-width: 90px;
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  background: var(--surface);
}
.orders-table input[data-edit-k="qty"] {
  width: 72px;
  min-width: 72px;
  text-align: center;
}
.orders-table input[data-edit-k="variant"] {
  min-width: 120px;
}
.orders-table input[data-edit-k="comment"] {
  min-width: 140px;
}
input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: #fca99f !important;
  box-shadow: 0 0 0 3px rgba(239, 90, 76, 0.15);
}

/* Order summary per post */
.orders-table .group-summary td {
  background: var(--teal-soft);
  border-bottom: 1px solid var(--line);
  padding: 14px;
}
.sum-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--teal);
  margin-bottom: 10px;
}
.sum-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
}
.sum-type {
  background: var(--surface);
  border: 1px solid #ccefe8;
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}
.sum-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}
.sum-head strong {
  font-size: 13px;
}
.sum-head span {
  font-weight: 800;
  font-size: 16px;
  color: var(--ink);
}
.sum-sizes {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.sum-chip {
  font-size: 12px;
  background: var(--teal-soft);
  border-radius: 999px;
  padding: 2px 8px;
  color: var(--muted);
}
.sum-chip b {
  color: var(--ink);
}

/* ---------- Buyers ---------- */
.buyers-table {
  width: 100%;
}
.buyers-table .fb-url {
  word-break: break-all;
}
.buyers-table td.row-actions {
  min-width: 210px;
}
.invoice-backdrop {
  z-index: 70;
  align-items: flex-start;
  padding-top: min(6vh, 48px);
}
.invoice-editor {
  width: min(900px, 100%);
  max-height: 88vh;
  overflow: auto;
  padding: 24px;
}
.invoice-heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
}
.invoice-heading .hint {
  margin: 5px 0 12px;
}
.invoice-table-wrap {
  margin-top: 16px;
  overflow: auto;
  border: 1px solid #e8edec;
  border-radius: 12px;
  background: #fff;
}
.invoice-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}
.invoice-table th,
.invoice-table td {
  padding: 10px 14px;
  border-bottom: 1px solid #edf0ef;
  vertical-align: middle;
}
.invoice-table th {
  color: var(--muted);
  background: #f8faf9;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-align: left;
  text-transform: uppercase;
}
.invoice-table tbody tr:last-child td {
  border-bottom: 0;
}
.invoice-table tbody tr.is-checked {
  background: #f4fbf9;
}
.invoice-table input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--teal);
  cursor: pointer;
}
.invoice-check-col {
  width: 52px;
  text-align: center !important;
}
.invoice-table th:nth-child(2),
.invoice-table td:nth-child(2) {
  width: 82px;
}
.invoice-title-col {
  width: 30%;
  overflow-wrap: anywhere;
}
.invoice-type-col {
  width: 150px;
  color: var(--ink-2);
  overflow-wrap: anywhere;
}
.invoice-table img,
.invoice-image-placeholder {
  display: block;
  width: 64px;
  height: 64px;
  border-radius: 8px;
  object-fit: cover;
  background: #eef4f2;
}
.invoice-image-placeholder {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 16px;
}
.invoice-qty-col {
  width: 84px;
  font-weight: 600;
}
.invoice-money-col {
  width: 160px;
  text-align: right !important;
  font-weight: 600;
  white-space: nowrap;
}
.invoice-total {
  display: grid;
  gap: 10px;
  margin: 18px 0;
  padding: 16px 18px;
  background: #f1faf8;
  border: 1px solid #d8eee9;
  border-radius: 13px;
}
.invoice-total > div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
}
.invoice-total > div:last-child strong {
  font-size: 19px;
}
.invoice-toolbar {
  align-items: center;
}
.invoice-toolbar .hint {
  margin: 0;
  font-size: 12px;
}
.invoice-preview-wrap {
  margin-top: 16px;
  padding: 12px;
  overflow: auto;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  text-align: center;
}
.invoice-preview-wrap canvas {
  display: block;
  width: min(100%, 560px);
  height: auto;
  margin: 0 auto;
  background: white;
  box-shadow: var(--shadow);
}

/* ---------- Agent ---------- */
.agent-wrap {
  max-width: 1100px;
}
.agent-wrap > .hint {
  margin: 0 0 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.agent-wrap h2 {
  font-size: 16px;
  margin: 28px 0 10px;
}
.agent-wrap textarea,
.agent-wrap input,
.agent-wrap select {
  width: 100%;
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  background: var(--surface);
}
.agent-wrap textarea {
  resize: vertical;
  line-height: 1.5;
}
.agent-wrap label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-2);
  margin: 16px 0 6px;
}
.agent-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
}
.agent-grid > div {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 16px 16px;
  min-width: 0;
}
.agent-grid .hint {
  margin: 0 0 8px;
  line-height: 1.45;
}
.agent-grid .hint code,
.llm-box code {
  font-size: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  padding: 1px 6px;
  border-radius: 6px;
}
.agent-inline {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
}
.agent-inline input {
  max-width: 180px;
}
.llm-box {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 0 0 8px;
}
.llm-box .agent-inline input,
.llm-box .agent-inline select {
  max-width: 220px;
}
.llm-toggle {
  display: flex !important;
  align-items: center;
  gap: 8px;
  font-size: 14px !important;
  color: var(--ink) !important;
  margin: 0 0 8px !important;
}
.llm-toggle input {
  width: auto;
  accent-color: var(--accent);
}
.agent-out {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 12px;
  white-space: pre-wrap;
  min-height: 80px;
  color: var(--muted);
  overflow: auto;
}
.lesson {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin: 6px 0;
}
.lesson b {
  display: block;
  font-size: 13px;
}
.lesson > div {
  min-width: 0;
  overflow-wrap: anywhere;
}
.teach-table {
  margin: 10px 0;
}
.teach-table table,
.editor table {
  box-shadow: none;
}
.teach-order-lines {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}
.teach-order-line {
  display: grid;
  grid-template-columns: 1fr 90px 70px auto;
  gap: 8px;
  align-items: center;
}
.teach-order-line input {
  width: 100%;
}

/* ---------- Editor modal ---------- */
.editor-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 40px 16px 48px;
  overflow: auto;
  background: rgba(17, 24, 39, 0.45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.editor {
  width: min(1080px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(17, 24, 39, 0.25);
  padding: 22px 24px 20px;
}
.editor h2 {
  margin: 0 0 4px;
  font-size: 19px;
  letter-spacing: -0.01em;
}
.editor .hint {
  margin: 0 0 14px;
}
.editor label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-2);
  margin: 14px 0 6px;
}
.editor textarea,
.editor input,
.editor select {
  width: 100%;
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  background: var(--surface);
}
.editor textarea {
  min-height: 160px;
  resize: vertical;
  line-height: 1.5;
}
.editor-toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 10px 0 0;
}
.editor-table-wrap {
  overflow: auto;
  margin-top: 8px;
}
.editor td {
  vertical-align: middle;
}
#edit-prices input[data-k="minQty"],
#edit-prices input[data-k="maxQty"],
#teach-prices input[data-k="minQty"],
#teach-prices input[data-k="maxQty"] {
  width: 76px;
  min-width: 76px;
  text-align: center;
  padding: 8px 6px;
}
.editor .foot {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line-2);
}
.editor .status {
  color: var(--muted);
  font-size: 13px;
}

/* ---------- Tablet ---------- */
@media (max-width: 1100px) {
  .stats {
    grid-template-columns: repeat(2, minmax(120px, 1fr));
    flex-basis: 360px;
  }
}

/* ---------- Mobile ---------- */
@media (max-width: 900px) {
  body {
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
  }

  /* Sidebar → bottom tab bar */
  .side {
    inset: auto 0 0 0;
    width: auto;
    height: calc(60px + env(safe-area-inset-bottom));
    padding: 0 6px env(safe-area-inset-bottom);
    border-right: 0;
    border-top: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }
  .brand {
    display: none;
  }
  .tabs {
    flex-direction: row;
    height: 60px;
    gap: 0;
  }
  .tab {
    flex: 1;
    flex-direction: column;
    justify-content: center;
    gap: 3px;
    padding: 6px 2px;
    border-radius: 0;
    font-size: 11px;
    text-align: center;
  }
  .tab svg {
    width: 22px;
    height: 22px;
  }
  .tab:hover {
    background: transparent;
  }
  .tab.active {
    background: transparent;
  }

  .shell {
    margin-left: 0;
  }
  /* Safari iOS < 17 tính sai chiều cao grid nằm trong flex dọc → ô thống kê bị kéo dài, đè lên bộ lọc */
  .top {
    display: block;
    padding: 18px 14px 4px;
  }
  h1 {
    font-size: 20px;
  }
  .stats {
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: auto;
    align-items: start;
    gap: 8px;
    margin-top: 12px;
  }
  .stat {
    padding: 10px 12px;
  }
  .stat span {
    font-size: 18px;
  }

  .filters {
    margin: 12px 14px 0;
    padding: 10px;
    gap: 8px;
  }
  .date-range {
    width: 100%;
  }
  .date-range label {
    flex: 1 1 0;
    min-width: 0;
  }
  .filters input[type="date"] {
    flex: 1;
    min-width: 0;
    width: 100%;
    display: block;
    -webkit-appearance: none;
    appearance: none;
  }
  .filters input[type="date"]::-webkit-date-and-time-value {
    text-align: left;
  }
  /* Safari iOS tự phóng to trang khi chạm ô nhập chữ < 16px */
  input:not([type="checkbox"]):not([type="radio"]),
  select,
  textarea {
    font-size: 16px;
  }
  .presets {
    width: 100%;
  }
  .presets .ghost {
    flex: 1 1 auto;
  }

  .panel {
    padding: 12px 14px 32px;
  }
  #del-all-products,
  #del-all-orders,
  #del-all-buyers {
    margin-left: 0;
  }
  .buyers-toolbar input[type="search"] {
    max-width: none;
    flex-basis: 100%;
  }

  .grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .card .body {
    padding: 10px 10px 12px;
  }
  .card-title {
    font-size: 14px;
  }
  .text {
    font-size: 12px;
  }
  .card .meta {
    font-size: 11px;
  }
  .actions .ghost,
  .actions .primary {
    padding: 7px 8px;
    font-size: 12px;
  }

  /* Tables → cards */
  .orders-table,
  .orders-table tbody,
  .orders-table tr,
  .orders-table td,
  .buyers-table,
  .buyers-table tbody,
  .buyers-table tr,
  .buyers-table td {
    display: block;
  }
  .orders-table,
  .buyers-table {
    background: transparent;
    border: 0;
    box-shadow: none;
    border-radius: 0;
    overflow: visible;
  }
  .orders-table thead,
  .buyers-table thead {
    display: none;
  }

  .orders-table tr.group-post td {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    margin-top: 20px;
    background: var(--surface);
  }
  .orders-table tbody tr.group-post:first-child td {
    margin-top: 0;
    border-top: 1px solid var(--line);
  }
  .orders-table .group-post span {
    display: block;
    margin-top: 2px;
  }

  .orders-table tr[data-order-row],
  .orders-table tr.order-draft,
  .buyers-table tr[data-buyer-row] {
    position: relative;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px 10px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 12px 12px 42px;
    margin-top: 8px;
    box-shadow: var(--shadow-sm);
  }
  .buyers-table tr[data-buyer-row] {
    grid-template-columns: 1fr 1fr;
  }
  .orders-table tr[data-order-row] td,
  .orders-table tr.order-draft td,
  .buyers-table td {
    border: 0;
    padding: 0;
    min-width: 0;
    background: transparent;
  }
  .orders-table td[data-label]::before,
  .buyers-table td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--faint);
    margin-bottom: 2px;
  }
  .orders-table td.sel-cell,
  .buyers-table td.sel-cell {
    position: absolute;
    top: 14px;
    left: 14px;
    width: auto;
    padding: 0;
  }
  .hide-mobile {
    display: none !important;
  }

  .orders-table td.buyer-cell {
    grid-column: 1 / -1;
    background: var(--surface-2) !important;
    border: 0;
    border-radius: var(--radius-sm);
    padding: 10px !important;
    width: auto;
    min-width: 0;
  }
  .orders-table td[data-label="Giá"],
  .orders-table td.comment,
  .orders-table td[data-label="Ghi chú"],
  .buyers-table td[data-label="Facebook"],
  .buyers-table td[data-label="Địa chỉ"],
  .buyers-table td[data-label="Ghi chú"] {
    grid-column: 1 / -1;
  }
  .orders-table .comment {
    max-width: none;
  }

  .orders-table td.row-actions,
  .buyers-table td.row-actions {
    grid-column: 1 / -1;
    display: flex;
    width: auto;
    gap: 8px;
  }
  td.row-actions > * + * {
    margin-left: 0;
  }
  .orders-table td.row-actions button,
  .buyers-table td.row-actions button {
    flex: 1;
    min-height: 38px;
  }
  .orders-table input[data-edit-k],
  .orders-table input[data-edit-k="qty"],
  .orders-table input[data-edit-k="variant"],
  .orders-table input[data-edit-k="comment"] {
    min-width: 0;
    width: 100%;
  }

  .orders-table tr.group-summary td {
    border: 1px solid #ccefe8;
    border-radius: var(--radius);
    margin-top: 8px;
  }
  .sum-grid {
    grid-template-columns: 1fr 1fr;
  }

  .agent-grid {
    grid-template-columns: 1fr;
  }
  .teach-order-line {
    grid-template-columns: 1fr 70px 56px auto;
  }
  .editor-backdrop {
    padding: 0;
  }
  .editor {
    border-radius: 0;
    min-height: 100%;
    padding: 18px 14px;
    border: 0;
  }
  .invoice-backdrop {
    align-items: stretch;
    padding: 0;
  }
  .invoice-editor {
    width: 100%;
    min-height: 100dvh;
    max-height: 100dvh;
    padding: max(20px, env(safe-area-inset-top)) 14px calc(18px + env(safe-area-inset-bottom));
  }
  .invoice-table th,
  .invoice-table td {
    padding: 8px;
  }
  .invoice-check-col {
    width: 42px;
  }
  .invoice-table {
    min-width: 620px;
  }
  .invoice-table img,
  .invoice-image-placeholder {
    width: 54px;
    height: 54px;
  }
  .invoice-qty-col {
    width: 52px;
  }
  .invoice-money-col {
    width: 100px;
    font-size: 13px;
  }
  .invoice-heading {
    gap: 8px;
  }
  .invoice-heading h2 {
    font-size: 18px;
  }
  .invoice-toolbar {
    align-items: flex-start;
  }
  .invoice-toolbar [data-invoice-copy] {
    width: 100%;
    min-height: 46px;
  }
  .editor-table-wrap table {
    min-width: 720px;
  }
}

@media (max-width: 380px) {
  .grid {
    grid-template-columns: 1fr;
  }
}
