:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e2e7ec;
  --border-strong: #cdd5dd;
  --text: #1f2933;
  --text-dim: #5b6776;
  --accent: #0f766e;
  --accent-hover: #0b5f58;
  --accent-soft: #e4f3f1;
  --red: #c93c3c;
  --red-soft: #fdecec;
  --shadow: 0 6px 20px rgba(15, 23, 42, 0.07);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121820;
    --surface: #1b232d;
    --surface-2: #1f2832;
    --border: #2d3844;
    --border-strong: #3b4855;
    --text: #e6ebf0;
    --text-dim: #9eabb8;
    --accent: #14a394;
    --accent-hover: #0f877a;
    --accent-soft: rgba(20, 163, 148, 0.16);
    --red: #ef5d5d;
    --red-soft: rgba(239, 93, 93, 0.14);
    --shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; margin: 0; }
.hidden { display: none !important; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
  min-height: 100vh;
}

button, input, select { font: inherit; color: inherit; }
.muted { color: var(--text-dim); }
.small { font-size: 12px; }
.strong { font-weight: 700; }

.brand { font-size: 22px; font-weight: 800; color: var(--accent); white-space: nowrap; }
.brand span { color: var(--text); font-weight: 600; }

/* ── Buttons & inputs ──────────────────────────────────────────────────── */
.btn {
  height: 38px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); color: #fff; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger:hover:not(:disabled) { filter: brightness(0.92); color: #fff; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; height: 44px; margin-top: 6px; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 8px 0 0; font-weight: 600; }
.icon-btn { background: none; border: 0; font-size: 18px; cursor: pointer; color: var(--text-dim); width: 34px; height: 34px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }

input[type=email], input[type=password], input[type=search], input[type=date], select {
  height: 38px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  padding: 0 10px;
  outline: none;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ── Auth ──────────────────────────────────────────────────────────────── */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.auth-card .brand { font-size: 26px; margin-bottom: 6px; }
.auth-card > .muted { margin-bottom: 18px; }
.auth-card label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 5px; }
.auth-card input { width: 100%; height: 44px; }
.tabs { display: flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; margin-bottom: 4px; }
.tabs button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 8px; font-weight: 600; color: var(--text-dim); cursor: pointer; }
.tabs button.active { background: var(--surface); color: var(--accent); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08); }
.msg { margin-top: 12px; font-size: 13px; min-height: 1em; }
.msg.error { color: var(--red); }
.msg.ok { color: var(--accent); }

/* ── Dashboard ─────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.nav { display: flex; gap: 4px; }
.nav button { border: 0; background: none; padding: 8px 14px; border-radius: 8px; font-weight: 600; color: var(--text-dim); cursor: pointer; }
.nav button:hover { background: var(--surface-2); }
.nav button.active { background: var(--accent-soft); color: var(--accent); }
.account { margin-left: auto; text-align: right; min-width: 0; }
.account-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-email { font-size: 12px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.notice {
  margin: 16px 24px 0;
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
}

.page { padding: 20px 24px 32px; max-width: 1280px; margin: 0 auto; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.filters, .actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.filters label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-dim); }
#orderSearch, #customerSearch { width: 260px; max-width: 100%; }
.selected-info { font-weight: 600; color: var(--accent); }
.summary { font-size: 13px; color: var(--text-dim); margin-bottom: 8px; min-height: 1.2em; }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
}
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
  background: var(--surface-2);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
#ordersBody tr[data-id] { cursor: pointer; }
#ordersBody tr[data-id]:hover { background: var(--surface-2); }
#ordersBody tr.selected { background: var(--accent-soft); }
.num { text-align: right; white-space: nowrap; }
.chk { width: 40px; text-align: center; }
.chk input { width: 16px; height: 16px; cursor: pointer; }
.items-cell { max-width: 360px; color: var(--text-dim); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { text-align: center; color: var(--text-dim); padding: 32px 12px; }

.pager { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 14px; color: var(--text-dim); }

/* ── Menu ──────────────────────────────────────────────────────────────── */
.menu-cat { margin-bottom: 22px; }
.menu-cat h3 { font-size: 16px; margin-bottom: 10px; }
.menu-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.menu-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 10px 12px;
}
.menu-item.inactive { opacity: 0.55; border-left-color: var(--border-strong); }
.star { color: #e0a800; }

/* ── Dialogs ───────────────────────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 20;
}
.dialog {
  width: 100%;
  max-width: 560px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}
.dialog.small { max-width: 400px; }
.dialog h2 { font-size: 18px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.dialog .small + h2, .dialog.small h2 { margin-bottom: 8px; }
.meta { display: grid; gap: 8px; margin-bottom: 14px; }
.meta > div { display: grid; grid-template-columns: 90px 1fr; gap: 8px; }
.items { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.bill { margin-top: 12px; display: grid; gap: 4px; }
.bill-line { display: flex; justify-content: space-between; }
.bill-line.grand { font-size: 17px; font-weight: 800; color: var(--accent); padding: 6px 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); margin: 4px 0; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  box-shadow: var(--shadow);
  z-index: 30;
  max-width: calc(100vw - 32px);
}
.toast.error { background: var(--red); }

/* ── Icons ─────────────────────────────────────────────────────────────── */
.nav svg, .icon-btn svg {
  width: 20px; height: 20px;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.nav button { display: flex; align-items: center; gap: 7px; }
.icon-btn { display: grid; place-items: center; }

/* ── Phones: works like an app (bottom tabs, cards, sheets) ────────────── */
@media (max-width: 760px) {
  body { font-size: 15px; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .topbar {
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
    gap: 8px;
  }
  .topbar .brand { font-size: 19px; }
  .account { margin-left: auto; }
  .account-email { display: none; }
  #signOutBtn { height: 34px; padding: 0 10px; font-size: 13px; }

  /* Bottom tab bar */
  .nav {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 10;
    gap: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 4px 6px calc(4px + env(safe-area-inset-bottom));
    box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.06);
  }
  .nav button {
    flex: 1;
    flex-direction: column;
    gap: 2px;
    padding: 6px 0;
    font-size: 11px;
    border-radius: 10px;
  }
  .nav button svg { width: 24px; height: 24px; }
  .nav button.active { background: none; }
  .nav button.active svg { stroke-width: 2.2; }

  .page { padding: 12px 12px 20px; }
  .notice { margin: 12px 12px 0; }
  .hide-sm { display: none !important; }

  /* Filters stack; search full width, dates side by side */
  .toolbar { gap: 8px; }
  .filters { width: 100%; gap: 8px; }
  #orderSearch, #customerSearch { width: 100%; order: -1; height: 44px; font-size: 16px; }
  .filters label { flex: 1; min-width: 0; font-size: 12px; }
  .filters input[type=date] { flex: 1; min-width: 0; height: 40px; }
  #deviceFilter, #menuDevice { width: 100%; height: 40px; }
  .actions { width: 100%; }
  .actions .btn { flex: 1; height: 42px; }

  /* Ticked orders: action bar floats above the tabs */
  .actions.has-selection {
    position: fixed;
    left: 10px; right: 10px;
    bottom: calc(70px + env(safe-area-inset-bottom));
    z-index: 11;
    width: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 10px;
    box-shadow: 0 10px 30px rgba(15, 23, 42, 0.2);
  }
  .actions.has-selection .selected-info { width: 100%; text-align: center; }

  /* Lists become cards (the bill's item table stays a table) */
  .table-wrap { background: none; border: 0; box-shadow: none; overflow: visible; }
  .table-wrap thead { display: none; }
  .table-wrap tbody tr {
    display: grid;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    margin-bottom: 10px;
    padding: 12px 14px;
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.04);
  }
  .table-wrap td { border: 0 !important; padding: 0; }
  .table-wrap tbody tr td.empty { display: block; }
  .items th, .items td { padding: 8px 6px; }
  /* Room so the floating selection bar never covers the last orders */
  body:has(.actions.has-selection) #page-orders { padding-bottom: 190px; }

  #ordersBody tr[data-id] {
    grid-template-columns: 34px 1fr auto;
    grid-template-areas: "chk num total" "chk date date" "chk cust cust";
    row-gap: 2px;
    align-items: center;
  }
  #ordersBody tr[data-id]:active { transform: scale(0.99); }
  #ordersBody tr.selected { border-color: var(--accent); }
  #ordersBody td:nth-child(1) { grid-area: chk; width: auto; text-align: left; align-self: start; }
  #ordersBody td:nth-child(2) { grid-area: num; font-size: 16px; }
  #ordersBody td:nth-child(3) { grid-area: date; font-size: 13px; color: var(--text-dim); }
  #ordersBody td:nth-child(4) { grid-area: cust; font-size: 13px; }
  #ordersBody td:nth-child(4):has(.muted:only-child) { display: none; }
  #ordersBody td:nth-child(6) { grid-area: total; font-size: 17px; color: var(--accent); }
  .chk input { width: 20px; height: 20px; }

  #customersBody tr { grid-template-columns: 1fr auto; gap: 2px 10px; }
  #customersBody td:nth-child(2) { color: var(--accent); font-weight: 600; }

  .pager .btn { flex: 1; height: 42px; }
  .pager { gap: 10px; }

  .menu-grid { grid-template-columns: 1fr 1fr; gap: 8px; }

  /* Order details: bottom sheet */
  .overlay { place-items: end center; padding: 0; }
  .dialog {
    max-width: none;
    border-radius: 20px 20px 0 0;
    max-height: 92vh;
    padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
    animation: sheet-up 0.22s ease-out;
  }
  .dialog::before {
    content: '';
    display: block;
    width: 40px; height: 4px;
    border-radius: 2px;
    background: var(--border-strong);
    margin: 4px auto 10px;
  }
  .dialog.small { border-radius: 20px 20px 0 0; }
  .dialog-actions .btn { flex: 1; height: 46px; }
  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); }

  /* Sign-in screens fill the phone */
  .auth { padding: calc(16px + env(safe-area-inset-top)) 16px 16px; place-items: start center; }
  .auth-card { border: 0; box-shadow: none; background: transparent; padding: 24px 4px; }
  .auth-card input { font-size: 16px; }   /* stops iPhone zooming into the field */
}

@keyframes sheet-up { from { transform: translateY(60px); } to { transform: none; } }

/* Tap feedback without the grey flash on phones */
button, tr[data-id] { -webkit-tap-highlight-color: transparent; }

/* ── Loading feedback ─────────────────────────────────────────────────── */
.topbar::after {
  content: '';
  position: absolute;
  left: 0; bottom: -1px;
  height: 3px; width: 35%;
  background: var(--accent);
  border-radius: 3px;
  opacity: 0;
  pointer-events: none;
}
body.loading .topbar::after { opacity: 1; animation: load-bar 1s ease-in-out infinite; }
@keyframes load-bar { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
body.loading #refreshBtn svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.skel {
  display: block;
  height: 12px;
  width: 80%;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--border) 25%, var(--surface-2) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
}
.skel.short { width: 45%; margin-top: 8px; }
.skel-card { min-height: 64px; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (max-width: 760px) {
  .table-wrap tbody tr.skel-row { grid-template-columns: 1fr 1fr; gap: 8px; }
  .table-wrap tbody tr.skel-row td:nth-child(n+4) { display: none; }
}
