:root {
  --red: #D71920;
  --red-dark: #A8121A;
  --red-soft: #FDECEC;
  --gold: #C9A227;
  --gold-soft: #FBF4DC;
  --bg: #FFF8F2;
  --surface: #FFFFFF;
  --text: #2A2321;
  --muted: #7A6E6A;
  --line: #EADFD8;
  --ok: #1E8E3E;
  --warn: #B26A00;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(80, 30, 20, .08);
  --topbar-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  font-family: 'Anuphan', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.55;
}
button, input, select, textarea { font: inherit; color: inherit; }
button, a, label, input[type=range] { touch-action: manipulation; }
img { max-width: 100%; display: block; }
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
.muted { color: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 10px;
  min-height: var(--topbar-h);
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--red); color: #fff;
  box-shadow: var(--shadow);
}
.admin-body .topbar { justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo {
  display: grid; place-items: center; flex: none;
  width: 40px; height: 40px; border-radius: 10px;
  background: #fff; color: var(--red); font-weight: 800; letter-spacing: .5px;
}
.brand b { display: block; font-size: 17px; line-height: 1.2; }
.brand small { display: block; font-size: 13px; opacity: .85; }
.top-title { min-width: 0; }
.top-title b {
  display: block; font-size: 16px; line-height: 1.25;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.top-title small { display: block; font-size: 12px; opacity: .85; }
.icon-btn {
  display: grid; place-items: center; flex: none;
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: rgba(255, 255, 255, .16); color: #fff; font-size: 20px; cursor: pointer;
}
.icon-btn:active { background: rgba(255, 255, 255, .3); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 12px; border: 1px solid transparent;
  background: var(--surface); color: var(--text);
  font-weight: 600; text-decoration: none; white-space: nowrap;
  cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--red); color: #fff; }
.btn.primary:hover:not(:disabled), .btn.primary:active:not(:disabled) { background: var(--red-dark); }
.btn.primary:disabled { background: #C9B8B3; opacity: 1; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover:not(:disabled) { background: var(--red-soft); }
.topbar .btn.ghost { color: #fff; border-color: rgba(255, 255, 255, .5); }
.topbar .btn.ghost:hover { background: rgba(255, 255, 255, .14); }
.btn.danger { background: transparent; border-color: var(--line); color: var(--red); }
.btn.danger:hover { background: var(--red-soft); }
.btn.sm { min-height: 36px; padding: 6px 10px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn.lg { min-height: 52px; font-size: 17px; }

/* ---------- Generic ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.panel-head h2 { font-size: 18px; }
.checker {
  background-color: #fff;
  background-image: repeating-conic-gradient(#EDE6E1 0 25%, #fff 0 50%);
  background-size: 16px 16px;
}
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap;
  background: var(--red-soft); color: var(--red-dark);
}
.badge.booth { background: var(--gold-soft); color: #7A5F00; }
.badge.off { background: #EEE; color: #666; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 8px; border-radius: 999px; font-size: 12px; white-space: nowrap;
  border: 1px solid var(--line); background: #fff; color: var(--muted);
}
.help { font-size: 14px; color: var(--muted); margin: 8px 0 0; }
.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 14px; font-weight: 600; }
.field input[type=text], .field textarea, .field input:not([type]) {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.field textarea { min-height: 76px; resize: vertical; }
.field input:focus, .field textarea:focus, .xywh input:focus {
  outline: 2px solid rgba(215, 25, 32, .25); border-color: var(--red);
}
.empty {
  text-align: center; padding: 32px 16px; color: var(--muted);
  border: 2px dashed var(--line); border-radius: var(--radius);
}
.warn { font-size: 13px; color: var(--warn); background: #FFF4E0; border-radius: 8px; padding: 6px 10px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; top: calc(var(--topbar-h) + 10px); transform: translateX(-50%);
  z-index: 100; display: grid; gap: 8px; width: min(92vw, 420px); pointer-events: none;
}
.toast {
  padding: 10px 14px; border-radius: 12px; background: #2A2321; color: #fff; font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2); transition: opacity .3s, transform .3s;
}
.toast.success { background: var(--ok); }
.toast.error { background: var(--red-dark); }
.toast.out { opacity: 0; transform: translateY(-8px); }

/* =======================================================
   Employee page (mobile-first)
   ======================================================= */
.container { max-width: 1100px; margin: 0 auto; padding: 12px 16px calc(24px + var(--safe-b)); }
.hero { padding: 6px 0 2px; }
.hero h1 { font-size: clamp(21px, 5.6vw, 32px); color: var(--red-dark); }
.steps { display: flex; gap: 6px; margin: 10px 0 0; padding: 0; list-style: none; overflow-x: auto; }
.steps li {
  display: flex; align-items: center; gap: 6px; flex: none;
  padding: 4px 10px 4px 4px; border-radius: 999px; background: #fff; border: 1px solid var(--line); font-size: 13px;
}
.steps li span {
  display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--red); color: #fff; font-weight: 700; font-size: 12px;
}
.privacy { font-size: 13px; color: var(--muted); margin: 8px 0 0; }

/* filter chips เลื่อนแนวนอนได้บนจอแคบ */
.filters {
  display: flex; gap: 8px; margin: 14px -16px 12px; padding: 0 16px;
  overflow-x: auto; scrollbar-width: none;
}
.filters::-webkit-scrollbar { display: none; }
.filter {
  flex: none; min-height: 38px; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: #fff; cursor: pointer; font-weight: 600; font-size: 14px;
}
.filter.on { background: var(--red); border-color: var(--red); color: #fff; }

/* แกลเลอรีกรอบ: มือถือ 2 คอลัมน์, การ์ดสูงเท่ากันทุกใบ */
.gallery { display: grid; gap: 10px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 640px) { .gallery { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; } }
@media (min-width: 960px) { .gallery { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.frame-card {
  display: flex; flex-direction: column; text-align: left; padding: 0; overflow: hidden; min-width: 0;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); cursor: pointer; transition: transform .15s, border-color .15s;
}
.frame-card:active { transform: scale(.98); border-color: var(--red); }
@media (hover: hover) { .frame-card:hover { transform: translateY(-2px); border-color: var(--red); } }
/* กล่องตัวอย่างสัดส่วนคงที่ — กรอบสูง/กว้างแค่ไหนก็ย่อให้พอดีกล่อง */
.frame-card .thumb {
  aspect-ratio: 3 / 4; display: flex; align-items: center; justify-content: center;
  padding: 10px; overflow: hidden;
}
.frame-card .thumb img {
  width: 100%; height: 100%; object-fit: contain;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, .15));
}
.frame-card-body { flex: 1; padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 5px; border-top: 1px solid var(--line); }
.frame-card-body b {
  font-size: 14px; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.frame-card-body .desc {
  margin: 0; font-size: 12px; line-height: 1.4; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.orients { display: flex; gap: 4px; flex-wrap: wrap; }

/* ---------- หน้าใส่รูป: พอดี 1 จอ ไม่ต้องเลื่อน ---------- */
body.editing { overflow: hidden; overscroll-behavior: none; }
.edit-screen {
  display: flex; flex-direction: column;
  height: calc(100vh - var(--topbar-h) - env(safe-area-inset-top, 0px));
  height: calc(100dvh - var(--topbar-h) - env(safe-area-inset-top, 0px));
}
.preview-wrap {
  position: relative; flex: 1 1 0; min-height: 0;
  display: grid; place-items: center;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.preview-wrap canvas { border-radius: 4px; box-shadow: 0 4px 18px rgba(0, 0, 0, .14); }
.busy {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 10px;
  background: rgba(255, 248, 242, .8); font-weight: 600;
}
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--red);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.panel-col {
  flex: none; display: flex; flex-direction: column; gap: 10px;
  padding: 12px 16px calc(12px + var(--safe-b));
  background: #fff; border-top: 1px solid var(--line);
  border-radius: 20px 20px 0 0; box-shadow: 0 -4px 16px rgba(80, 30, 20, .07);
}
.slot-hint { display: grid; gap: 2px; font-size: 14px; }
.slot-hint b { margin-right: 6px; }
.slot-hint .warn { margin-top: 4px; }

.slot-tray { display: flex; gap: 10px; justify-content: center; }
.tile {
  position: relative; height: 64px; max-width: 30%; padding: 0; overflow: hidden; cursor: pointer;
  border: 2px dashed #D9C9C2; border-radius: 10px; background: #FAF5F2;
}
.tile.filled { border-style: solid; border-color: var(--line); }
.tile.active { border: 3px solid var(--gold); }
.tile img { width: 100%; height: 100%; object-fit: cover; }
.tile .plus { position: absolute; inset: 0; display: grid; place-items: center; font-size: 24px; color: #B9A9A2; }
.tile-num {
  position: absolute; left: 3px; top: 3px; display: grid; place-items: center;
  width: 18px; height: 18px; border-radius: 50%; background: var(--red); color: #fff; font-size: 11px; font-weight: 700;
}
.tile-warn {
  position: absolute; right: 3px; top: 3px; display: grid; place-items: center;
  width: 18px; height: 18px; border-radius: 50%; background: var(--warn); color: #fff; font-size: 12px; font-weight: 700;
}

.slot-tools { display: grid; gap: 8px; }
.zoom-row { display: flex; align-items: center; gap: 10px; color: var(--muted); font-weight: 700; }
.zoom-row input { flex: 1; height: 28px; accent-color: var(--red); }
.tool-btns { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.tool-btns .btn { padding: 6px 4px; }

/* แท็บ ใส่รูป / ฟิลเตอร์ */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; background: #F5EEEA; border-radius: 12px; }
.tab {
  min-height: 38px; border: 0; border-radius: 9px; background: transparent;
  font-weight: 600; font-size: 14px; color: var(--muted); cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tab.on { background: #fff; color: var(--red-dark); box-shadow: 0 1px 4px rgba(80, 30, 20, .12); }
.tab small { font-weight: 500; }
.pane { display: flex; flex-direction: column; gap: 10px; }

/* แถบเลือกฟิลเตอร์ เลื่อนแนวนอน */
.look-strip {
  display: flex; gap: 10px; overflow-x: auto; padding: 14px 16px 4px; margin: 0 -16px;
  scroll-snap-type: x proximity; scrollbar-width: none;
}
.look-strip::-webkit-scrollbar { display: none; }
.look {
  position: relative; flex: none; width: 72px; padding: 0; border: 0; background: none;
  display: grid; gap: 4px; justify-items: center; cursor: pointer; scroll-snap-align: start;
}
.look img {
  width: 72px; height: 72px; object-fit: cover; border-radius: 12px;
  border: 3px solid transparent; transition: border-color .15s;
}
.look.on img { border-color: var(--red); }
.look span { font-size: 11px; line-height: 1.2; text-align: center; color: var(--muted); }
.look.on span { color: var(--red-dark); font-weight: 700; }
.look-group {
  position: absolute; top: -12px; left: 0; padding: 0 6px; border-radius: 6px;
  background: #2A2321; color: #fff !important; font-size: 10px !important; font-weight: 700;
}
.look-empty { padding: 18px 0; color: var(--muted); font-size: 14px; text-align: center; width: 100%; }
@media (min-width: 900px) {
  .look-strip { flex-wrap: wrap; overflow: visible; margin: 0; padding: 14px 0 0; row-gap: 20px; }
}

/* จอกว้าง: preview ซ้าย แผงควบคุมขวา */
@media (min-width: 900px) {
  .edit-screen { flex-direction: row; }
  .panel-col {
    width: 380px; border-top: 0; border-left: 1px solid var(--line); border-radius: 0;
    padding: 20px; overflow: auto; box-shadow: none;
  }
  #btnDownload { margin-top: auto; }
}
/* จอเตี้ยมาก (เช่น iPhone SE) ลดพื้นที่แผงควบคุม */
@media (max-height: 640px) and (max-width: 899px) {
  .panel-col { gap: 8px; padding-top: 10px; }
  .tile { height: 52px; }
  .slot-hint .muted { display: none; }
}

/* ---------- Result sheet ---------- */
.modal {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px;
  background: rgba(30, 15, 10, .6);
}
.modal-box {
  background: #fff; border-radius: var(--radius); padding: 16px; width: min(100%, 440px);
  max-height: 92vh; overflow: auto; display: grid; gap: 10px;
}
.modal-box img {
  max-height: 46vh; margin: 0 auto; border-radius: 6px; box-shadow: 0 4px 14px rgba(0, 0, 0, .15);
  -webkit-touch-callout: default; user-select: auto;
}
.result-list { display: grid; gap: 10px; }
.result-list.two { grid-template-columns: 1fr 1fr; align-items: start; }
.result-list figure { margin: 0; display: grid; gap: 6px; justify-items: center; text-align: center; }
.result-list.two img { max-height: 34vh; }
.result-list figcaption { font-size: 13px; font-weight: 600; color: var(--muted); }
.result-help { margin: 0; font-size: 14px; text-align: center; }
.sheet-grip { display: none; }
@media (max-width: 640px) {
  .modal.sheet { place-items: end stretch; padding: 0; }
  .modal.sheet .modal-box {
    width: 100%; border-radius: 20px 20px 0 0; padding: 8px 16px calc(16px + var(--safe-b));
    animation: sheet-up .25s ease-out;
  }
  .sheet-grip { display: block; width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 4px; }
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }

.page-foot { margin-top: 28px; text-align: center; }
.page-foot a { font-size: 13px; color: var(--muted); text-decoration: none; padding: 8px 12px; }

/* =======================================================
   Admin page
   ======================================================= */
.top-actions { display: flex; gap: 6px; }
.login-screen {
  display: grid; place-items: center; padding: 24px 16px;
  min-height: calc(100vh - var(--topbar-h)); min-height: calc(100dvh - var(--topbar-h));
}
.login-box { width: min(100%, 380px); display: grid; gap: 12px; justify-items: center; text-align: center; padding: 28px 22px; }
.login-box .logo { width: 56px; height: 56px; font-size: 20px; background: var(--red); color: #fff; }
.login-box p { margin: 0; }
.login-box input {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; font-size: 16px;
}
.login-box input:focus { outline: 2px solid rgba(215, 25, 32, .25); border-color: var(--red); }
.login-err { color: var(--red-dark); font-size: 14px; font-weight: 600; }
.admin { display: grid; gap: 16px; padding: 16px; max-width: 1500px; margin: 0 auto; }
@media (min-width: 1000px) { .admin { grid-template-columns: 340px minmax(0, 1fr); align-items: start; } }

.frame-list { display: grid; gap: 10px; }
.admin-frame {
  display: grid; grid-template-columns: 64px 1fr; gap: 6px 10px; align-items: center;
  padding: 8px; border: 1px solid var(--line); border-radius: 12px;
}
.admin-frame.current { border-color: var(--red); background: var(--red-soft); }
.admin-frame .thumb { width: 64px; height: 64px; border-radius: 8px; display: grid; place-items: center; padding: 4px; grid-row: span 2; }
.admin-frame .thumb img { max-height: 100%; object-fit: contain; }
.admin-frame .info { min-width: 0; }
.admin-frame .info b { display: block; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-frame .meta { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; font-size: 13px; }
.admin-frame .actions { display: flex; gap: 4px; flex-wrap: wrap; }
.list-tools { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }

.editor-grid { display: grid; gap: 16px; }
@media (min-width: 1250px) { .editor-grid { grid-template-columns: minmax(0, 1fr) 360px; align-items: start; } }

.dropzone {
  display: grid; place-items: center; text-align: center; gap: 6px;
  min-height: 280px; padding: 24px; cursor: pointer;
  border: 2px dashed var(--line); border-radius: var(--radius); background: #FFFCFA;
}
.dropzone.over { border-color: var(--red); background: var(--red-soft); }
.dropzone b { font-size: 17px; }
.stage { display: grid; place-items: center; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius); background: #F7F2EF; }
.stage canvas { touch-action: none; }
.stage-tools { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }

.type-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.type-pick label {
  display: grid; gap: 2px; padding: 10px; border: 2px solid var(--line); border-radius: 10px; cursor: pointer; font-size: 14px;
}
.type-pick label:has(input:checked) { border-color: var(--red); background: var(--red-soft); }
.type-pick input { position: absolute; opacity: 0; pointer-events: none; }
.type-pick b { font-size: 15px; }
.check { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; font-size: 14px; }
.check input { accent-color: var(--red); width: 18px; height: 18px; }

.slots-head { display: flex; justify-content: space-between; align-items: center; margin: 4px 0 8px; }
.slots-head h3 { font-size: 16px; }
.count.ok { color: var(--ok); font-weight: 700; }
.count.bad { color: var(--warn); font-weight: 700; }
.slot-list { display: grid; gap: 8px; margin-bottom: 14px; }
.slot-item { border: 2px solid var(--line); border-radius: 12px; padding: 10px; cursor: pointer; }
.slot-item.sel { border-color: var(--gold); background: #FFFDF5; }
.slot-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.slot-head .num {
  display: grid; place-items: center; flex: none; width: 24px; height: 24px; border-radius: 50%;
  background: var(--red); color: #fff; font-weight: 700; font-size: 13px;
}
.slot-head .dims { font-size: 13px; margin-left: auto; }
.xywh { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 8px; }
.xywh label { display: grid; gap: 2px; font-size: 12px; color: var(--muted); }
.xywh input { width: 100%; padding: 5px 6px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.field.sm { margin: 0; }
.field.sm > span { font-size: 12px; font-weight: 500; color: var(--muted); }
.field.sm input { padding: 6px 10px; font-size: 14px; }
.form-actions { display: flex; gap: 8px; }
.form-actions .btn { flex: 1; }
