:root {
  color-scheme: light;
  --paper: #FFF7E8;
  --paper-2: #FBEDD3;
  --card: #FFFDF8;
  --ink: #2B211C;
  --ink-2: #6E5F54;
  --line: #E6D9C2;
  --coral: #FF5B3A;
  --coral-d: #D93F20;
  --butter: #FFCF4A;
  --mint: #16976D;
  --mint-bg: #DDF5EA;
  --sky: #CDE9FB;
  --danger: #C8321B;
  --warn: #A66A00;
  --edge: 2px solid var(--ink);
  --lift: 3px 3px 0 var(--ink);
  --radius: 16px;
  --ui: 'Anuphan', 'Sarabun', system-ui, sans-serif;
  --display: 'Mitr', 'Anuphan', system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 400 16px/1.55 var(--ui);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
body.in-editor { overflow: hidden; }
h1, h2, h3 { font-family: var(--display); font-weight: 500; line-height: 1.25; margin: 0; }
p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
button:disabled { cursor: not-allowed; opacity: .38; }
:focus-visible { outline: 3px solid var(--coral); outline-offset: 2px; }
canvas { display: block; }

/* ปุ่ม */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 8px 16px;
  background: var(--card); border: var(--edge); border-radius: 12px; box-shadow: var(--lift);
  font-family: var(--display); font-weight: 400; cursor: pointer;
  transition: transform .08s, box-shadow .08s, background .15s;
}
.btn:hover:not(:disabled) { background: var(--paper-2); }
.btn:active:not(:disabled) { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }
.btn.primary { background: var(--coral); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--coral-d); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; box-shadow: none; border-color: var(--ink-2); }
.btn.ghost:active:not(:disabled) { transform: none; box-shadow: none; }
.btn.small { min-height: 40px; padding: 4px 12px; font-size: 15px; }
.btn.big { min-height: 52px; padding: 10px 22px; font-size: 17px; }
.btn.wide { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; flex: none;
  border-radius: 12px; color: var(--ink);
}
.icon-btn:hover:not(:disabled) { background: var(--paper-2); }
.icon-btn.danger { color: var(--danger); }

.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 18px; }
.row.center { justify-content: center; }
.row.between { justify-content: space-between; }

.pill {
  display: inline-block; padding: 1px 10px; border-radius: 99px; font-size: 13px; font-weight: 500;
  border: 1.5px solid var(--ink); white-space: nowrap;
}
.pill.static { background: var(--sky); }
.pill.animated { background: var(--butter); }
.note { font-size: 14px; color: var(--ink-2); }

/* หน้าแรก */
.page { max-width: 1160px; margin: 0 auto; padding: 16px 16px 48px; }
.hero {
  display: grid; grid-template-columns: 1.25fr 1fr; gap: 32px; align-items: center;
  padding: 24px 0 40px;
}
.brand { font: 600 26px var(--display); color: var(--coral); margin-bottom: 28px; display: flex; align-items: baseline; gap: 10px; }
.brand span { font: 400 14px var(--ui); color: var(--ink-2); }
.hero h1 { font-size: clamp(30px, 4.6vw, 50px); font-weight: 600; margin-bottom: 14px; text-wrap: balance; }
.lead { font-size: 18px; color: var(--ink-2); margin-bottom: 24px; max-width: 34em; }
.hero-art { position: relative; height: 300px; }
.peel {
  position: absolute; display: grid; place-items: center;
  background: var(--c); border: 8px solid #fff; border-radius: 50%;
  box-shadow: 0 0 0 2px var(--ink), 5px 6px 0 2px var(--ink);
  font: 600 30px var(--display); color: var(--ink);
}
.p1 { --c: var(--butter); width: 190px; height: 190px; left: 6%; top: 6%; transform: rotate(-9deg); }
.p2 { --c: #FF8FB1; width: 140px; height: 140px; right: 8%; top: 0; transform: rotate(10deg); }
.p3 { --c: #8FE0C2; width: 160px; height: 160px; right: 18%; bottom: 0; transform: rotate(-4deg); }
.section-title { font-size: 22px; margin-bottom: 14px; }
.project-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); }
.project-card {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; padding: 12px;
  background: var(--card); border: var(--edge); border-radius: var(--radius); box-shadow: var(--lift);
  cursor: pointer; transition: transform .12s;
}
.project-card:hover { transform: translateY(-2px) rotate(-.4deg); }
.mini-sheet {
  grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
  background: var(--paper-2); border-radius: 10px; padding: 8px;
}
.mini-sheet canvas { width: 100%; height: auto; }
.project-meta h3 { font-size: 18px; }
.project-meta p { font-size: 14px; color: var(--ink-2); }
.empty-note { border: 2px dashed var(--line); border-radius: var(--radius); padding: 36px 16px; text-align: center; color: var(--ink-2); }
.foot { margin-top: 32px; font-size: 14px; color: var(--ink-2); }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.topbar .brand { margin: 0; }
.topbar .row { flex-wrap: wrap; justify-content: flex-end; }
a.btn { text-decoration: none; color: inherit; }
.design-card { display: grid; gap: 10px; padding: 16px; background: var(--card); border: var(--edge); border-radius: 18px; box-shadow: 5px 5px 0 var(--ink); }
.design-card .field { margin: 0; }
.design-card .field-label { margin: 2px 0 -4px; }
.small-chip { min-height: 34px; padding: 3px 10px; font-size: 13px; }
.seg.three { grid-template-columns: repeat(3, 1fr); }
.pill.emoji { background: #FFC9D9; }
.count-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; font-size: 15px; color: var(--ink-2); }
.copy-row { display: flex; gap: 6px; align-items: flex-start; }
.copy-row textarea { flex: 1; min-width: 0; }
.steps { margin: 10px 0; padding-left: 22px; font-size: 15px; display: grid; gap: 4px; }
.field small { font-size: 12px; }

.cmd-bar { display: flex; align-items: center; gap: 8px; }
.cmd-bar input { flex: 1; min-width: 0; min-height: 40px; padding: 6px 12px; background: #fff; border: var(--edge); border-radius: 10px; }
.cmd-label { flex: none; font: 500 15px var(--display); }
.cmd-bar .btn { flex: none; }
.cmd-examples { margin: 8px 0 12px; }
.ed-cmd { grid-area: cmd; padding: 6px 10px; background: var(--paper); border-bottom: 1.5px solid var(--line); }
@media (max-width: 520px) { .cmd-label { display: none; } }

/* หน้าชุดสติกเกอร์ */
.sheet-top { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.sheet-name {
  flex: 1; min-width: 0; font: 500 24px var(--display); background: transparent;
  border: 0; border-bottom: 2px dashed transparent; padding: 2px 4px;
}
.sheet-name:hover, .sheet-name:focus { border-bottom-color: var(--ink-2); outline: none; }
.sheet-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 24px; align-items: start; }
.sheet-tools { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.sort-hint { margin-bottom: 10px; color: var(--coral-d); font-weight: 500; }
.count-select { display: inline-flex; align-items: center; gap: 8px; font-size: 15px; color: var(--ink-2); }
.count-select select { min-height: 40px; padding: 4px 8px; background: var(--card); border: var(--edge); border-radius: 10px; color: var(--ink); }
.tile-grid {
  display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  padding: 14px; background: var(--paper-2); border-radius: 20px;
}
.tile { position: relative; aspect-ratio: 37 / 32; border-radius: 14px; }
.tile-open { display: grid; place-items: center; width: 100%; height: 100%; border-radius: 14px; cursor: pointer; overflow: hidden; }
.tile.empty .tile-open { border: 2px dashed #CDBB9C; color: var(--ink-2); }
.tile.empty .tile-open:hover { border-color: var(--coral); color: var(--coral); background: var(--card); }
.tile.filled .tile-open {
  background-color: #fff;
  background-image: conic-gradient(#F4ECDD 25%, transparent 0 50%, #F4ECDD 0 75%, transparent 0);
  background-size: 18px 18px;
  border: var(--edge); box-shadow: var(--lift); transition: transform .1s;
}
.tile.filled .tile-open:hover { transform: translateY(-2px) rotate(-.6deg); }
.tile canvas { width: 100%; height: 100%; object-fit: contain; }
.tile-num { position: absolute; left: 8px; top: 6px; font: 500 13px var(--display); color: var(--ink-2); pointer-events: none; }
.tile-tag {
  position: absolute; left: 6px; bottom: 6px; padding: 0 7px; border-radius: 6px; font-size: 12px; font-weight: 600;
  background: var(--ink); color: #fff; pointer-events: none;
}
.tile-tag.tabicon { left: auto; right: 6px; background: var(--mint); }
.tile-more {
  position: absolute; right: 2px; top: 2px; width: 38px; height: 38px; display: grid; place-items: center;
  border-radius: 10px; color: var(--ink);
}
.tile-more:hover { background: rgba(43, 33, 28, .1); }
.tile-grid.sorting .tile { touch-action: none; cursor: grab; }
.tile-grid.sorting .tile-open { pointer-events: none; }
.tile.dragging { z-index: 5; opacity: .92; cursor: grabbing; }
.tile.drop-target { outline: 3px solid var(--coral); outline-offset: 2px; }
.sheet-side { display: grid; gap: 14px; position: sticky; top: 14px; }
.card { background: var(--card); border: var(--edge); border-radius: var(--radius); padding: 14px; }
.card h2 { font-size: 18px; margin-bottom: 10px; }
.checklist { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; font-size: 15px; }
.checklist li { display: grid; grid-template-columns: 22px 1fr; gap: 8px; align-items: start; }
.checklist small { display: block; color: var(--ink-2); font-size: 13px; }
.checklist .ok svg { color: var(--mint); }
.checklist .warn svg { color: var(--warn); }
.checklist .todo svg { color: #CDBB9C; }
.cover-row { align-items: flex-end; gap: 18px; margin-top: 10px; }
.cover-row figure { margin: 0; text-align: center; font-size: 13px; color: var(--ink-2); }
.cover-row canvas { background: #fff; border: 1.5px solid var(--line); border-radius: 8px; margin-bottom: 4px; }

/* หน้าแก้ไข */
.editor {
  height: 100dvh; display: grid; grid-template: "top top" auto "cmd cmd" auto "stage panel" minmax(0, 1fr) / minmax(0, 1fr) 360px;
  background: var(--paper);
}
.ed-top {
  grid-area: top; display: flex; align-items: center; gap: 6px; padding: 6px 10px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--card); border-bottom: var(--edge);
}
.ed-title { flex: 1; min-width: 0; line-height: 1.2; }
.ed-title strong { display: block; font: 500 17px var(--display); }
.ed-title span { display: block; font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ed-actions { display: flex; align-items: center; gap: 2px; }
.ed-actions .btn { margin-left: 8px; }
.divider { width: 1.5px; height: 24px; background: var(--line); margin: 0 4px; }
.stage-wrap { grid-area: stage; position: relative; min-height: 0; overflow: hidden; background: var(--paper-2); }
.stage { position: absolute; inset: 0; touch-action: none; cursor: default; }
.floatbar {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); display: flex; gap: 2px; padding: 2px;
  background: var(--card); border: var(--edge); border-radius: 14px; box-shadow: var(--lift);
}
.stage-empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; pointer-events: none; }
.stage-empty-card { pointer-events: auto; text-align: center; max-width: 440px; }
.stage-empty-card h2 { font-size: 24px; margin-bottom: 6px; }
.stage-empty-card p { color: var(--ink-2); margin-bottom: 16px; }
.ed-panel { grid-area: panel; display: flex; flex-direction: row-reverse; min-height: 0; background: var(--card); border-left: var(--edge); }
.panel-body { flex: 1; min-width: 0; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.panel-body h3 { font-size: 15px; margin-top: 6px; color: var(--ink-2); font-weight: 400; }
.tabs { flex: none; width: 74px; display: flex; flex-direction: column; border-right: 1.5px solid var(--line); overflow-y: auto; }
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 62px; padding: 6px 2px; font-size: 12px; line-height: 1.2; color: var(--ink-2); flex: none;
}
.tab:hover { background: var(--paper); }
.tab.on { color: var(--coral-d); background: var(--paper-2); font-weight: 600; }

.seg { display: grid; gap: 6px; }
.seg.two { grid-template-columns: 1fr 1fr; }
.seg.four { grid-template-columns: repeat(4, 1fr); }
.seg button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 44px; padding: 6px 2px; font-size: 13px; line-height: 1.2;
  background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px;
}
.seg button.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 40px; padding: 5px 13px; font-size: 15px;
  background: var(--paper); border: 1.5px solid var(--line); border-radius: 99px;
}
.chip:hover:not(:disabled) { border-color: var(--ink); }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.slider { display: grid; gap: 2px; font-size: 14px; color: var(--ink-2); }
input[type=range] { width: 100%; height: 30px; accent-color: var(--coral); margin: 0; }
.switch { display: flex; align-items: center; gap: 10px; min-height: 36px; cursor: pointer; }
.switch input { width: 22px; height: 22px; accent-color: var(--coral); margin: 0; }
textarea, .field input {
  width: 100%; padding: 10px 12px; background: #fff; border: var(--edge); border-radius: 10px; resize: vertical;
}
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 36px; height: 36px; border-radius: 50%; background: var(--c);
  border: 2px solid var(--ink); position: relative; cursor: pointer;
}
.swatch.on { box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--coral); }
.swatch.custom { background: conic-gradient(#FF5B3A, #FFCF4A, #2FBF8F, #3D9BE9, #8E6BE8, #FF5B3A); overflow: hidden; }
.swatch.custom input { position: absolute; inset: -8px; opacity: 0; cursor: pointer; }
.style-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.style-card { display: grid; gap: 4px; padding: 5px; font-size: 13px; line-height: 1.25; text-align: center; background: var(--paper); border: 2px solid var(--line); border-radius: 12px; }
.style-card canvas { width: 100%; height: auto; border-radius: 8px; background: var(--paper-2); }
.style-card.on { border-color: var(--coral); background: #FFEDE7; font-weight: 600; }
.font-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.font-card { min-height: 44px; padding: 4px 6px; font-size: 16px; background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px; }
.font-card.on { border: 2px solid var(--coral); background: #FFEDE7; }
.shape-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.shape-card { aspect-ratio: 1; padding: 8px; background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px; }
.shape-card:hover { border-color: var(--ink); }
.shape-card svg { width: 100%; height: 100%; fill: var(--coral); }
.layer-list { display: grid; gap: 4px; }
.layer-row { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 4px 10px; text-align: left; background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px; }
.layer-row span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.layer-row.on { border-color: var(--coral); background: #FFEDE7; }
.hint-box { display: grid; gap: 12px; justify-items: start; padding: 8px 0; }

/* หน้าต่างย่อย */
.modal {
  width: min(460px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 20px; color: var(--ink);
  background: var(--card); border: var(--edge); border-radius: 18px; box-shadow: 6px 6px 0 var(--ink);
}
.modal.wide { width: min(640px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(43, 33, 28, .45); }
.modal h2 { font-size: 21px; margin-bottom: 10px; }
.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field > span, .field-label { font-size: 14px; color: var(--ink-2); margin-bottom: 4px; }
.type-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-bottom: 12px; }
.type-card { display: grid; gap: 2px; padding: 12px; text-align: left; background: var(--paper); border: 2px solid var(--line); border-radius: 12px; }
.type-card strong { font: 500 17px var(--display); }
.type-card span { font-size: 13px; color: var(--ink-2); }
.type-card.on { border-color: var(--coral); background: #FFEDE7; }
.menu-list { display: grid; gap: 4px; }
.menu-list button { min-height: 46px; padding: 8px 12px; text-align: left; border-radius: 10px; }
.menu-list button:hover { background: var(--paper-2); }
.danger-text { color: var(--danger); }
.busy { display: flex; align-items: center; gap: 14px; }
.spinner { width: 28px; height: 28px; flex: none; border: 4px solid var(--line); border-top-color: var(--coral); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.chat { display: flex; flex-direction: column; gap: 8px; padding: 14px; min-height: 230px; background: #BFD7E8; border-radius: 12px; margin-bottom: 8px; transition: background .2s; }
.chat.dark { background: #1E2329; }
.bubble.them { align-self: flex-start; padding: 6px 12px; background: #fff; border-radius: 14px; font-size: 15px; }
.chat-sticker { align-self: flex-end; width: 44%; max-width: 200px; }
.chat-sticker canvas { width: 100%; height: auto; }
.chat-tray { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 6px; max-height: 170px; overflow-y: auto; margin-top: 8px; }
.chat-tray button { padding: 3px; background: var(--paper); border-radius: 8px; border: 1.5px solid var(--line); }
.chat-tray canvas { width: 100%; height: auto; }
.file-table { max-height: 260px; overflow-y: auto; border: 1.5px solid var(--line); border-radius: 10px; margin: 10px 0; font-size: 14px; }
.file-row { display: grid; grid-template-columns: 1.2fr 1fr .6fr 1fr; gap: 6px; padding: 5px 10px; border-bottom: 1px solid var(--line); }
.file-row.head { position: sticky; top: 0; background: var(--paper-2); font-weight: 600; }
.file-row:last-child { border-bottom: 0; }
.problem-list { margin: 0 0 10px; padding-left: 20px; color: var(--danger); }

#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 1000; display: grid; gap: 8px; width: min(420px, calc(100vw - 32px)); pointer-events: none; }
.toast { padding: 10px 14px; background: var(--ink); color: #fff; border-radius: 12px; font-size: 15px; }
.toast.warn { background: #6E4A00; }
.toast.bad { background: var(--danger); }

/* จอเล็ก */
@media (max-width: 900px) {
  .floatbar { top: 8px; bottom: auto; }
  .topbar .brand span { display: none; }
  .topbar .btn { white-space: nowrap; }
  .hero { grid-template-columns: 1fr; padding-bottom: 20px; }
  .hero-art { display: none; }
  .brand { margin-bottom: 18px; }
  .sheet-layout { grid-template-columns: minmax(0, 1fr); }
  .sheet-side { position: static; }
  .tile-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; padding: 8px; }
  .editor {
    grid-template: "top" auto "cmd" auto "stage" minmax(150px, 1fr) "panel" auto / minmax(0, 1fr);
  }
  .ed-panel { flex-direction: column; border-left: 0; border-top: var(--edge); }
  .panel-body { max-height: 38dvh; min-height: 150px; padding: 12px 16px; flex: none; }
  .tabs {
    width: auto; flex-direction: row; overflow-x: auto; border-right: 0; border-top: 1.5px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom); scrollbar-width: none;
  }
  .tab { flex: 1 1 0; min-width: 45px; min-height: 56px; font-size: 11px; }
  .style-grid { grid-template-columns: repeat(3, 1fr); }
  .font-grid { grid-template-columns: repeat(2, 1fr); }
  .shape-grid { grid-template-columns: repeat(5, 1fr); }
  .ed-actions .divider, .ed-actions [data-top=prev], .ed-actions [data-top=next] { display: none; }
  .ed-actions .btn span { display: none; }
  .ed-actions .btn { width: 44px; padding: 0; }
  .sheet-name { font-size: 20px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

.qr { width: min(100%, 240px); margin: 0 auto 12px; }
.qr svg { display: block; width: 100%; height: auto; border-radius: 8px; }
.ai-preview { display: block; width: min(100%, 280px); aspect-ratio: 1; object-fit: contain; margin: 0 auto 12px; border-radius: 16px; background: #f4f1ec; }
.busy [data-stop] { margin-left: auto; }
