:root {
  --bg: #faf6f0; --surface: #ffffff; --ink: #2b2118; --muted: #7a6a5b; --line: #eadfce;
  --accent: #b5532a; --accent-ink: #ffffff; --accent-soft: #f6e3d6;
  --up: #b3341c; --down: #1f7a4d; --head: #f3ebdf;
  --shadow: 0 1px 2px rgba(60,40,20,.08), 0 6px 20px rgba(60,40,20,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #1b1612; --surface: #26201a; --ink: #f1e9df; --muted: #a8998a; --line: #3a3128;
    --accent: #cdb878; --accent-ink: #1b1612; --accent-soft: #373120;
    --up: #f08a73; --down: #6fcf9a; --head: #2f2720; --shadow: none;
  }
}
/* Selectable themes (picker in the header). "auto" = no data-theme: warm light, dark follows the device. */
[data-theme="strawberry"] {
  --bg: #fff6f7; --surface: #ffffff; --ink: #3a1f26; --muted: #8a6670; --line: #f2d9de;
  --accent: #c93a5e; --accent-ink: #ffffff; --accent-soft: #fde4ea; --up: #b3261e; --down: #1f7a4d; --head: #fbe9ed;
  --shadow: 0 1px 2px rgba(120,30,50,.08), 0 6px 20px rgba(120,30,50,.06);
}
[data-theme="matcha"] {
  --bg: #f3f6ef; --surface: #ffffff; --ink: #1f2a1c; --muted: #667360; --line: #dde5d3;
  --accent: #4d7c3a; --accent-ink: #ffffff; --accent-soft: #e4eed8; --up: #b3341c; --down: #2a6b3f; --head: #e9f0e0;
  --shadow: 0 1px 2px rgba(30,60,20,.08), 0 6px 20px rgba(30,60,20,.06);
}
[data-theme="blueberry"] {
  --bg: #f2f5fb; --surface: #ffffff; --ink: #17233b; --muted: #5f6c85; --line: #d9e1f0;
  --accent: #2f5fb3; --accent-ink: #ffffff; --accent-soft: #e1eafb; --up: #b3341c; --down: #1f7a4d; --head: #e8eefa;
  --shadow: 0 1px 2px rgba(20,40,90,.08), 0 6px 20px rgba(20,40,90,.06);
}
[data-theme="mono"] {
  --bg: #f5f5f4; --surface: #ffffff; --ink: #1c1c1c; --muted: #6b6b6b; --line: #e3e3e1;
  --accent: #1c1c1c; --accent-ink: #ffffff; --accent-soft: #ececea; --up: #b3341c; --down: #1f7a4d; --head: #efefed;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.05);
}
[data-theme="chocolate"] {
  --bg: #1b1612; --surface: #26201a; --ink: #f1e9df; --muted: #a8998a; --line: #3a3128;
  --accent: #cdb878; --accent-ink: #1b1612; --accent-soft: #373120; --up: #f08a73; --down: #6fcf9a; --head: #2f2720; --shadow: none;
}
[data-theme="midnight"] {
  --bg: #0f1620; --surface: #16202e; --ink: #e6edf7; --muted: #8fa0b8; --line: #26354a;
  --accent: #5aa2ff; --accent-ink: #0f1620; --accent-soft: #1c2d45; --up: #ff8a7a; --down: #5fd69c; --head: #1b2738; --shadow: none;
}
select#themeSel { padding: 8px 10px; }
/* Native scrollbars, checkboxes and date/number spinners follow the theme. */
:root { color-scheme: light; }
[data-theme="chocolate"], [data-theme="midnight"] { color-scheme: dark; }
@media (prefers-color-scheme: dark) { :root:not([data-theme]) { color-scheme: dark; } }
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font: 15px/1.45 "Noto Sans Thai", "Segoe UI", system-ui, -apple-system, sans-serif; }
.hidden { display: none !important; }
h1, h2, h3 { margin: 0; }

button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: 10px; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.06); }
button.ghost { background: transparent; }
button.danger { color: var(--up); }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-width: 0; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); padding: 28px; width: min(380px, 100%); display: grid; gap: 14px; }
.card h1 { font-size: 26px; color: var(--accent); }
.sub { margin: -8px 0 6px; color: var(--muted); }
.card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.err { color: var(--up); min-height: 1.3em; margin: 0; font-size: 14px; }

/* tabs */
.tabs { display: flex; gap: 4px; padding: 10px 20px 0; border-bottom: 1px solid var(--line); background: var(--surface); }
.tabs button { border: 0; border-bottom: 3px solid transparent; border-radius: 8px 8px 0 0; background: transparent; padding: 10px 16px; font-weight: 600; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.tabs button:hover { border-color: transparent; border-bottom-color: var(--accent); }
td.locked { cursor: default; }
.srcpack { font-size: 13px; }
.srcpack small { display: block; color: var(--muted); font-size: 11px; line-height: 1.2; }
.lock { color: var(--muted); font-size: 12px; margin-left: 4px; }

/* header + controls */
.top { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--surface); position: sticky; top: 0; z-index: 30; flex-wrap: wrap; }
.brand { font-size: 20px; font-weight: 700; color: var(--accent); }
.brand span { font-weight: 400; color: var(--muted); font-size: 14px; margin-left: 6px; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.top-actions button { white-space: nowrap; }
.controls { display: flex; gap: 10px; padding: 14px 20px 8px; flex-wrap: wrap; align-items: center; }
.controls input[type=search] { flex: 1 1 200px; max-width: 320px; }
.check { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; }

/* table */
.tablewrap { margin: 8px 20px; overflow: auto; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); max-height: calc(100vh - 190px); box-shadow: var(--shadow); }
table { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
th, td { padding: 5px 9px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; background: var(--surface); }
thead th { position: sticky; top: 0; background: var(--head); z-index: 3; font-weight: 600; font-size: 13px; line-height: 1.2; }
thead th small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; }
.sticky { position: sticky; left: 0; z-index: 2; text-align: left; border-right: 1px solid var(--line); min-width: 190px; }
thead th.sticky { z-index: 4; }
tr.catrow td { background: var(--accent-soft); color: var(--accent); font-weight: 600; text-align: left; position: sticky; left: 0; padding: 4px 9px; }
tr.inactive td { opacity: .55; }
.name { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.name:hover .nm { color: var(--accent); text-decoration: underline; }
.thumb { width: 32px; height: 32px; border-radius: 8px; object-fit: cover; background: var(--head); flex: none; border: 1px solid var(--line); }
.thumb.ph { display: grid; place-items: center; color: var(--muted); font-size: 15px; }
.nm { font-weight: 600; line-height: 1.25; }
.meta { color: var(--muted); font-size: 11px; line-height: 1.2; }
td.price { cursor: text; }
td.price:hover { background: var(--accent-soft); }
.pv { font-variant-numeric: tabular-nums; font-weight: 600; line-height: 1.2; }
.lb { display: block; color: var(--muted); font-size: 11px; font-weight: 400; line-height: 1.2; }
.chg { display: block; font-size: 11px; line-height: 1.2; font-variant-numeric: tabular-nums; color: var(--muted); }
.chg.up { color: var(--up); } .chg.down { color: var(--down); }
.unit { font-variant-numeric: tabular-nums; }
.unit small { display: block; color: var(--muted); font-size: 11px; line-height: 1.2; }
td.dash { color: var(--muted); }
.cell-edit { display: grid; gap: 4px; }
.cell-edit input { width: 90px; padding: 4px 6px; text-align: right; }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.foot { margin: 8px 22px 24px; color: var(--muted); font-size: 12px; }

/* dialogs */
dialog { border: 1px solid var(--line); border-radius: 18px; padding: 0; background: var(--surface); color: var(--ink); width: min(720px, calc(100vw - 24px)); max-height: calc(100vh - 24px); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(30,20,10,.45); }
.dlg { padding: 22px; display: grid; gap: 16px; }
.dlg h2 { font-size: 20px; }
.dlg h2 small { color: var(--muted); font-weight: 400; font-size: 14px; }
.grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.dlg label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.dlg label.check { display: flex; }
.span2 { grid-column: 1 / -1; }
.imgbox { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.imgbox img, .imgbox .big { width: 150px; height: 150px; border-radius: 14px; object-fit: cover; background: var(--head); border: 1px solid var(--line); display: grid; place-items: center; color: var(--muted); font-size: 40px; }
.imgbtns { display: grid; gap: 8px; }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
td.price.locked:hover { background: var(--surface); }
.utable { width: 100%; border-collapse: collapse; font-size: 14px; }
.utable th, .utable td { text-align: left; white-space: normal; background: transparent; padding: 7px 8px; }
.utable th { background: var(--head); font-size: 12px; }
.ua { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.ua input { min-width: 0; flex: 1 1 160px; }
.ua button { padding: 6px 10px; font-size: 13px; }
.adduser { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 10px; align-items: end; }
.adduser label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
@media (max-width: 640px) { .adduser { grid-template-columns: 1fr; } }
.push-left { margin-right: auto; }
.confirm { display: grid; gap: 8px; padding: 14px; border: 1px solid var(--up); border-radius: 12px; background: var(--head); }
.confirm .errline { min-height: 0; }
button.danger-solid { background: var(--up); color: var(--bg); border-color: var(--up); font-weight: 600; }
.hist { width: 100%; font-size: 14px; }
.hist th, .hist td { padding: 6px 10px; }
.hist-wrap { max-height: 200px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; padding: 12px; background: var(--head); border-radius: 12px; }
.toolbar label { min-width: 110px; }
.pgrid-wrap { max-height: 44vh; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.pgrid input { width: 92px; text-align: right; padding: 5px 7px; }
.pgrid td, .pgrid th { padding: 5px 10px; }
.pgrid td:first-child, .pgrid th:first-child { text-align: left; }
.errline { color: var(--up); min-height: 1.3em; font-size: 14px; margin: 0; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px; z-index: 100; box-shadow: var(--shadow); }

.cattext { position: sticky; left: 12px; }

@media (max-width: 640px) {
  .tabs { padding: 8px 8px 0; }
  .tabs button { flex: 1; padding: 10px 8px; }
  .top { padding: 10px 12px; }
  .top-actions { width: 100%; }
  .top-actions button.primary { flex: 1 1 100%; }
  .top-actions select { flex: 1 1 auto; min-width: 0; }
  .controls { padding: 10px 12px 4px; gap: 8px; }
  .controls input[type=search] { flex: 1 1 100%; max-width: none; }
  .controls select { flex: 1 1 calc(50% - 4px); min-width: 0; }
  .tablewrap { margin: 8px 8px; max-height: calc(100vh - 230px); }
  .sticky { min-width: 140px; }
  .name { gap: 8px; }
  .nm { font-size: 14px; white-space: normal; }
  th, td { padding: 5px 7px; }
  .thumb { width: 28px; height: 28px; }
  .grid2 { grid-template-columns: 1fr; }
  .dlg { padding: 16px; }
}
