:root {
  --bg: #fbf3e6; --surface: #ffffff; --ink: #2a1a12; --muted: #7a6558; --line: #e6d6c0;
  --accent: #e8452c; --accent-ink: #ffffff; --tape: #ffc83d;
  --display: "Kanit", system-ui, sans-serif; --body: "Sarabun", system-ui, sans-serif; --mono: "JetBrains Mono", ui-monospace, monospace;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #1b1410; --surface: #2a1f19; --ink: #f6e9da; --muted: #b69f8c; --line: #43332a; --accent: #ff6a4d; --accent-ink: #1b0b06; --tape: #e6b230; color-scheme: dark; }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.6 var(--body); }
.wrap { max-width: 1100px; margin: 0 auto; padding: 28px 16px 56px; display: grid; gap: 20px; }
h1 { font: 700 clamp(32px, 6vw, 56px)/1.05 var(--display); margin: 0; text-wrap: balance; }
h1 span { background: var(--tape); color: #2a1a12; padding: 0 .2em; margin-left: .1em; display: inline-block; transform: rotate(-2deg); }
h2 { font: 600 18px var(--display); margin: 0 0 10px; }
h2 small { font: 400 13px var(--body); color: var(--muted); margin-left: 6px; }
.tag { color: var(--muted); max-width: 64ch; margin: 10px 0 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px; min-width: 0; }
.notice { background: var(--surface); border: 2px dashed var(--accent); border-radius: 12px; padding: 14px 16px; margin: 0; }
.notice code { font-family: var(--mono); background: var(--bg); padding: 1px 6px; border-radius: 4px; }
.loading .bar { height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; }
.loading .bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.loading p { color: var(--muted); margin: 8px 0 0; }

.drop { border: 2px dashed var(--line); border-radius: 12px; transition: border-color .15s; }
.drop.big { padding: 48px 16px; text-align: center; background: var(--surface); }
.drop.over { border-color: var(--accent); }
.dropTitle { font: 600 24px var(--display); margin: 0; }
.hint2 { color: var(--muted); margin: 4px 0 16px; }

#work { display: grid; grid-template-columns: minmax(0, 340px) minmax(0, 1fr); gap: 20px; align-items: start; }
@media (max-width: 820px) { #work { grid-template-columns: minmax(0, 1fr); } }
.left, .right { display: grid; gap: 16px; min-width: 0; }
.cropper { position: relative; margin: 0 auto; overflow: hidden; border-radius: 6px; touch-action: none; user-select: none; width: max-content; max-width: 100%; }
.cropper canvas { display: block; max-width: 100%; }
.cbox { position: absolute; border: 2px solid var(--tape); box-shadow: 0 0 0 999px rgba(0,0,0,.5); cursor: move; touch-action: none; }
.cbox i { position: absolute; width: 18px; height: 18px; background: var(--tape); border-radius: 3px; touch-action: none; }
.cbox i[data-h=nw] { left: -9px; top: -9px; cursor: nwse-resize; } .cbox i[data-h=se] { right: -9px; bottom: -9px; cursor: nwse-resize; }
.cbox i[data-h=ne] { right: -9px; top: -9px; cursor: nesw-resize; } .cbox i[data-h=sw] { left: -9px; bottom: -9px; cursor: nesw-resize; }

.ctls { display: grid; gap: 12px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; }
.seg span { display: block; text-align: center; padding: 6px; cursor: pointer; font-weight: 600; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); }
.seg label:has(input:focus-visible) { outline: 3px solid var(--tape); outline-offset: -3px; }
.ctl { display: grid; gap: 3px; } .ctl label { font-size: 13px; color: var(--muted); }
select, input[type=search] { font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); width: 100%; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; }
.btn { position: relative; font: 600 15px var(--body); padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; text-align: center; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font: 600 17px var(--display); }
.btn.file { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.btn:disabled { opacity: .5; cursor: default; }
.btn input { position: absolute; width: 1px; height: 1px; opacity: 0; }
button:focus-visible, .btn:focus-within, select:focus-visible, input:focus-visible { outline: 3px solid var(--tape); outline-offset: 2px; }

.counter { font: 500 14px var(--mono); color: var(--muted); margin: 0; }
.mainrow { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 560px) { .mainrow { grid-template-columns: minmax(0, 1fr); } }
.stage { position: relative; }
#outHi { position: absolute; inset: 0; width: 100%; height: 100%; image-rendering: pixelated; border-radius: 6px; background: var(--bg); opacity: 0; transition: opacity .45s; pointer-events: none; }
#outHi.on { opacity: 1; }
#out { width: 100%; aspect-ratio: 1; display: block; image-rendering: pixelated; border-radius: 6px; background: var(--bg); }
.info p { margin: 0; }
.pct b { font: 700 44px/1 var(--display); color: var(--accent); } .pct small { display: block; color: var(--muted); font-size: 13px; }
.th { font: 600 24px/1.3 var(--display); margin-top: 8px !important; } .en { color: var(--muted); font-size: 14px; }
.orig { margin: 12px 0 0; width: 120px; } .orig img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; display: block; background: var(--bg); }
.orig figcaption { font-size: 12px; color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.grid button { display: grid; gap: 2px; padding: 4px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); cursor: pointer; font: 12px/1.3 var(--body); text-align: left; min-width: 0; }
.grid img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 5px; display: block; }
.grid span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grid b { font-family: var(--mono); color: var(--accent); }
.chart { width: 100%; height: 240px; display: block; }
footer { color: var(--muted); font-size: 13px; } footer a { color: inherit; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* Phones/tablets (one column): show the result right after the user's photo, controls below it */
@media (max-width: 820px) {
  #work > .left, #work > .right { display: contents; }
  #work > .left > .card:first-child { order: 1; }
  #work > .right > .counter { order: 2; }
  #work > .right > .card:nth-of-type(1) { order: 3; }
  #work > .left > .ctls { order: 4; }
  #work > .right > .card:nth-of-type(2) { order: 5; }
  #work > .right > .card:nth-of-type(3) { order: 6; }
}
