:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f0f3f9;
  --text: #1b2333;
  --muted: #6b7690;
  --border: #e3e8f2;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef0ff;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --ok: #15803d;
  --ok-soft: #e4f6ea;
  --warn: #b45309;
  --warn-soft: #fff3df;
  --info: #0369a1;
  --info-soft: #e3f2fb;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 6px 20px rgba(20, 30, 60, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #171e2e;
    --surface-2: #1f2840;
    --text: #e8ecf6;
    --muted: #93a0bd;
    --border: #2a3550;
    --primary: #818cf8;
    --primary-hover: #a5b0ff;
    --primary-soft: #232b52;
    --danger: #f87171;
    --danger-soft: #3a1f26;
    --ok: #4ade80;
    --ok-soft: #173325;
    --warn: #fbbf24;
    --warn-soft: #3a2d14;
    --info: #38bdf8;
    --info-soft: #14304a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; line-height: 1.25; margin: 0 0 1rem; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .6rem; }
p { margin: .5rem 0 1rem; }
.muted { color: var(--muted); }
.req { color: var(--danger); font-weight: 700; }

/* ---------- navbar ---------- */
.nav {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.nav-inner {
  max-width: 1040px; margin: 0 auto; padding: .6rem 1rem;
  display: flex; align-items: center; gap: .5rem 1.25rem; flex-wrap: wrap;
}
.brand { font-weight: 800; font-size: 1.15rem; color: var(--text); display: flex; align-items: center; gap: .5rem; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--primary), #8b5cf6); color: #fff; font-size: .95rem;
}
.nav-panel { display: flex; align-items: center; gap: .5rem 1.25rem; flex: 1; }
.nav-check, .nav-toggle { display: none; }
.nav-links { display: flex; gap: .25rem; flex: 1; flex-wrap: wrap; }
.nav-links a { color: var(--muted); padding: .4rem .8rem; border-radius: 999px; font-weight: 500; }
.nav-links a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-badge { display: inline-block; margin-left: .4rem; min-width: 1.3rem; padding: .05rem .45rem; border-radius: 999px; background: #dc2626; color: #fff; font-size: .75rem; font-weight: 700; line-height: 1.3; text-align: center; vertical-align: middle; }
.nav-user { display: flex; align-items: center; gap: .6rem; color: var(--muted); font-size: .92rem; }
.nav-user form { margin: 0; }

/* ---------- layout ---------- */
main { max-width: 1040px; margin: 0 auto; padding: 1.5rem 1rem 6rem; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem;
}
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.tile {
  display: block; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem 1.2rem; box-shadow: var(--shadow); transition: transform .12s, border-color .12s;
}
.tile:hover { text-decoration: none; transform: translateY(-2px); border-color: var(--primary); }
.tile .tile-icon { font-size: 1.6rem; display: block; margin-bottom: .35rem; }
.tile .tile-title { font-weight: 700; display: block; }
.tile .tile-sub { color: var(--muted); font-size: .9rem; }

/* ---------- forms ---------- */
label { display: block; margin: 1rem 0 .35rem; font-weight: 600; font-size: .92rem; }
label.check { display: flex; align-items: center; gap: .55rem; font-weight: 500; margin: .55rem 0; cursor: pointer; }
input[type=text], input[type=password], input[type=date], input[type=number], input[type=tel], input[type=search], textarea, select {
  width: 100%; max-width: 480px; padding: .65rem .8rem; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  transition: border-color .12s, box-shadow .12s;
}
textarea { resize: vertical; }
input[type=search] { -webkit-appearance: none; appearance: none; }
input[type=search]::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; height: 14px; width: 14px; cursor: pointer; opacity: .6;
  background: no-repeat center / contain url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' fill='none' stroke='%2393a0bd' stroke-width='1.6' stroke-linecap='round'><path d='M2 2l8 8M10 2l-8 8'/></svg>"); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
input[type=checkbox] { width: 1.15rem; height: 1.15rem; accent-color: var(--primary); }
.form-card { max-width: 560px; }
.form-actions { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-top: 1.25rem; }
.filters { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); align-items: end; }
.filters label { margin: 0 0 .35rem; }
.filters input, .filters select { max-width: none; }

[hidden] { display: none !important; }

/* ---------- shop menu assignment toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem 1rem; margin-bottom: 1rem; }
.toolbar > div { min-width: 0; }
.toolbar .grow { flex: 1 1 240px; }
.toolbar label { margin: 0 0 .35rem; }
.toolbar-buttons { display: flex; gap: .5rem; }
.toolbar-buttons button { padding: .65rem 1rem; font-weight: 600; color: var(--text); }
.toolbar-note { flex-basis: 100%; margin: 0; font-size: .9rem; }

/* ---------- admin: shop-typed items ---------- */
.custom-group { margin-bottom: 1rem; }
.custom-group h2 { margin: 0 0 .25rem; display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.custom-group-meta { margin: 0 0 1rem; }
.custom-actions { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.custom-action { border: 1px solid var(--border); border-radius: 12px; padding: 1rem; background: var(--surface-2); }
.custom-action h3 { margin: 0 0 .25rem; font-size: 1rem; }
.custom-action input, .custom-action select { max-width: none; }
.custom-action label:first-of-type { margin-top: .5rem; }
.custom-action button { margin-top: .75rem; width: 100%; }
.custom-action .row2 { display: grid; gap: .75rem; grid-template-columns: 1fr 1fr; }
.custom-dismiss { margin-top: 1rem; }

/* ---------- trends ---------- */
.section { margin-bottom: 1.25rem; }
.section h2 { margin: 0 0 .75rem; }
.chart-box { position: relative; height: 340px; }
.trend-up { color: var(--ok); font-weight: 600; }
.trend-down { color: var(--danger); font-weight: 600; }

/* ---------- shop order history ---------- */
.history { padding: 0; margin-bottom: 1rem; }
.history > summary {
  cursor: pointer; padding: .9rem 1.1rem; font-weight: 600; list-style: none;
  display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .75rem;
}
.history > summary::-webkit-details-marker { display: none; }
.history > summary::after { content: "▾"; margin-left: auto; color: var(--muted); }
.history[open] > summary::after { content: "▴"; }
.history[open] { padding-bottom: 1rem; }
.history > :not(summary) { margin-left: 1.1rem; margin-right: 1.1rem; }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .6rem; max-height: 22rem; overflow-y: auto; }
.history-list li { border: 1px solid var(--border); border-radius: 10px; padding: .65rem .8rem; background: var(--surface-2); }
.history-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.history-items { margin: .25rem 0; word-break: break-word; }
.history-meta { color: var(--muted); font-size: .85rem; }

/* ---------- buttons ---------- */
button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .6rem 1.1rem; font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  transition: background .12s, border-color .12s, transform .05s;
}
button:hover, .btn:hover { background: var(--surface-2); text-decoration: none; }
button:active, .btn:active { transform: scale(.98); }
button:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary, form button:not(.btn-ghost):not(.step) {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.btn-primary:hover, form button:not(.btn-ghost):not(.step):hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.nav-user button, .btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); padding: .4rem .8rem; font-weight: 500; }
.nav-user button:hover, .btn-ghost:hover { background: var(--surface-2); color: var(--text); }

/* ---------- alerts & badges ---------- */
.err, .ok { border-radius: 12px; padding: .8rem 1rem; margin: 0 0 1rem; }
.err { background: var(--danger-soft); color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); }
ul.err { list-style: none; }
ul.err li + li { margin-top: .25rem; }
.ok { background: var(--ok-soft); color: var(--ok); border: 1px solid color-mix(in srgb, var(--ok) 30%, transparent); }
.badge { display: inline-block; padding: .15rem .65rem; border-radius: 999px; font-size: .82rem; font-weight: 600; white-space: nowrap; }
.badge-new { background: var(--warn-soft); color: var(--warn); }
.badge-confirmed { background: var(--info-soft); color: var(--info); }
.badge-shipped { background: var(--ok-soft); color: var(--ok); }
.badge-on { background: var(--ok-soft); color: var(--ok); }
.badge-off { background: var(--surface-2); color: var(--muted); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; background: transparent; }
th, td { padding: .75rem 1rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--surface-2); font-weight: 600; }
tr:last-child td, tr:last-child th { border-bottom: 0; }
tbody tr:hover td { background: color-mix(in srgb, var(--primary-soft) 45%, transparent); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }@media (max-width: 900px) { th, td { padding: .65rem .7rem; } }
tfoot th { background: var(--surface-2); font-size: 1rem; text-transform: none; letter-spacing: 0; color: var(--text); }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }

/* ---------- order detail ---------- */
.meta { display: grid; gap: .75rem 1.5rem; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 1.25rem; }
.meta dt { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; }
.meta dd { margin: 0; font-weight: 600; word-break: break-word; }

/* ---------- order form ---------- */
.menu-list { display: grid; gap: .75rem; }
.menu-row {
  display: grid; grid-template-columns: 1fr auto; gap: .5rem 1rem; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .9rem 1rem; box-shadow: var(--shadow);
}
.menu-row.active { border-color: var(--primary); background: var(--primary-soft); }
.menu-name { font-weight: 700; }
.menu-sub { color: var(--muted); font-size: .9rem; }
.stepper { display: inline-flex; align-items: stretch; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface); }
.stepper .step { border: 0; border-radius: 0; padding: 0 .9rem; font-size: 1.2rem; background: var(--surface-2); color: var(--text); min-width: 2.6rem; }
.stepper .step:hover { background: var(--primary-soft); }
.stepper input[type=number] {
  width: 4.2rem; border: 0; border-radius: 0; text-align: center; padding: .55rem .2rem; box-shadow: none; -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.custom-card { margin-top: 1rem; }
.custom-card h2 { margin: 0 0 .25rem; }
.custom-card > p { margin: 0 0 .9rem; font-size: .92rem; }
.custom-row { display: grid; grid-template-columns: 2fr 1fr .8fr auto; gap: .5rem; margin-bottom: .5rem; align-items: center; }
.custom-row input { max-width: none; }
.custom-row .remove-custom { padding: .55rem .8rem; }
.order-fields { margin-top: 1.5rem; }
.order-fields .row { display: grid; gap: 0 1rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.order-fields input, .order-fields textarea { max-width: none; }
.submit-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -6px 20px rgba(20, 30, 60, .1);
}
.submit-bar-inner { max-width: 1040px; margin: 0 auto; padding: .75rem 1rem; display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.total-label { color: var(--muted); font-size: .85rem; line-height: 1.2; }
.total-value { font-size: 1.35rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.submit-bar button { padding: .75rem 1.6rem; font-size: 1.05rem; }

/* ---------- login ---------- */
.auth { min-height: 75vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: 100%; max-width: 400px; }
.auth-card .brand { justify-content: center; font-size: 1.6rem; margin-bottom: 1.25rem; }
.auth-card input { max-width: none; }
.auth-card button { width: 100%; margin-top: 1.25rem; padding: .8rem; font-size: 1.05rem; }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  h1 { font-size: 1.35rem; }
  main { padding: 1rem .85rem 6rem; }
  /* hamburger menu */
  .nav-inner { gap: 0 .75rem; }
  .nav-check { display: block; position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
  .nav-toggle {
    display: flex; align-items: center; justify-content: center;
    margin-left: auto; width: 44px; height: 44px; border-radius: 10px;
    border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  }
  .nav-toggle:hover { background: var(--surface-2); }
  .nav-check:focus-visible + .nav-toggle { outline: 2px solid var(--primary); outline-offset: 2px; }
  .nav-toggle span, .nav-toggle span::before, .nav-toggle span::after {
    display: block; width: 20px; height: 2px; border-radius: 2px; background: var(--text);
    transition: transform .18s, background .18s;
  }
  .nav-toggle span { position: relative; }
  .nav-toggle span::before, .nav-toggle span::after { content: ""; position: absolute; left: 0; }
  .nav-toggle span::before { top: -6px; }
  .nav-toggle span::after { top: 6px; }
  .nav-check:checked + .nav-toggle span { background: transparent; }
  .nav-check:checked + .nav-toggle span::before { transform: translateY(6px) rotate(45deg); }
  .nav-check:checked + .nav-toggle span::after { transform: translateY(-6px) rotate(-45deg); }

  .nav-panel {
    display: none; flex-basis: 100%; flex-direction: column; align-items: stretch;
    gap: .25rem; padding: .5rem 0 .25rem; border-top: 1px solid var(--border); margin-top: .6rem;
  }
  .nav-check:checked ~ .nav-panel { display: flex; }
  .nav-links { flex-direction: column; flex: none; gap: .15rem; }
  .nav-links a { padding: .75rem .9rem; border-radius: 10px; font-size: 1.05rem; }
  .nav-user {
    justify-content: space-between; margin-top: .35rem; padding: .75rem .9rem .35rem;
    border-top: 1px solid var(--border);
  }
  .nav-user .nav-name { font-weight: 600; color: var(--text); }

  .stack thead { display: none; }
  .stack, .stack tbody, .stack tr, .stack td { display: block; width: 100%; }
  .stack tbody tr { padding: .6rem .9rem; border-bottom: 1px solid var(--border); }
  .stack tbody tr:last-child { border-bottom: 0; }
  .stack td { border: 0; padding: .2rem 0; display: flex; justify-content: space-between; gap: 1rem; text-align: right; }
  .stack td::before { content: attr(data-label); color: var(--muted); font-size: .85rem; text-align: left; flex-shrink: 0; }
  .stack tfoot { display: block; }
  .stack tfoot tr { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .75rem .9rem; background: var(--surface-2); }
  .stack tfoot th { display: block; width: auto; padding: 0; border: 0; }
  .history-list { max-height: 15rem; }
  .custom-row { grid-template-columns: 1fr 1fr auto; }
  .custom-row input[name=custom_name] { grid-column: 1 / -1; }
  .custom-row .remove-custom { grid-column: 3; grid-row: 2; }
  .chart-box { height: 260px; }
  .stack td[data-label=""]::before { content: none; }
  .stack td[data-label=""] { justify-content: flex-start; padding-top: .4rem; }
  tbody tr:hover td { background: transparent; }

  .form-card { max-width: none; }
  input[type=text], input[type=password], input[type=date], input[type=number], input[type=tel], input[type=search], textarea, select { max-width: none; font-size: 16px; }
  .submit-bar button { padding: .75rem 1.2rem; }
}
