:root {
  --bg: #f4f3ef;
  --panel: #ffffff;
  --ink: #1c1b19;
  --muted: #6b6860;
  --line: #e2dfd7;
  --accent: #b4532a;
  --accent-ink: #ffffff;
  --stage: #e9e6de;
  --warn: #9a3412;
  --warn-bg: #fff4ec;
  --ok: #166534;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161513;
    --panel: #201f1c;
    --ink: #ecebe6;
    --muted: #a19d93;
    --line: #34322d;
    --accent: #e07a4a;
    --stage: #121110;
    --warn: #fdba74;
    --warn-bg: #2a1d14;
    --ok: #86efac;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 14px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { width: 34px; height: 34px; border-radius: 8px; display: block; }
.brand h1 .by { color: var(--accent); font-weight: 600; white-space: nowrap; }
.brand h1 { margin: 0; font-size: 17px; }
.brand p { margin: 0; color: var(--muted); font-size: 12px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

button, select, input, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel); padding: 7px 12px; border-radius: 8px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.layout { display: grid; grid-template-columns: 360px 1fr; min-height: calc(100vh - 62px); }
.panel {
  background: var(--panel); border-right: 1px solid var(--line); padding: 8px 14px 40px;
  overflow-y: auto; max-height: calc(100vh - 62px); position: sticky; top: 62px;
}
details { border-bottom: 1px solid var(--line); padding: 8px 0 12px; }
summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); margin-top: 8px; }
label.row { flex-direction: column; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); font-size: 13px; }
label.check.pad { margin-top: 26px; }
input[type=text], input[type=number], select, textarea {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); color: var(--ink);
}
textarea { resize: vertical; }
input[type=color] { width: 100%; height: 34px; padding: 2px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; line-height: 1.45; }
.field { display: flex; gap: 8px; align-items: flex-start; margin-top: 8px; }
.field input[type=checkbox] { margin-top: 10px; }
.field .lbl { padding-top: 7px; flex: 1; }
.field input[type=text], .field textarea { flex: 1; min-width: 0; }
.field .pct { display: flex; align-items: center; gap: 2px; flex: none; font-size: 12px; color: var(--muted); }
.field .pct input { width: 56px; padding: 7px 4px; text-align: right; }
.chiprow { display: flex; align-items: baseline; gap: 8px; }
.chiprow > span { flex: none; width: 74px; font-size: 12px; color: var(--muted); }
.chiprow .chips { flex: 1; }
.blankcta { width: 100%; margin-top: 6px; font-size: 13px; text-align: left; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.chips button { padding: 3px 8px; font-size: 12px; border-radius: 999px; }
.chips button.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.care { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 8px; }
.care label {
  margin: 0; flex-direction: column; align-items: center; gap: 2px; padding: 5px 2px; border: 1px solid var(--line);
  border-radius: 8px; cursor: pointer; color: var(--ink); font-size: 10px; text-align: center;
}
.care label:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.care input { display: none; }
.care svg { width: 26px; height: 26px; }

.workspace { display: flex; flex-direction: column; min-width: 0; }
.toolbar {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button.active { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.tabs button[disabled] { opacity: .4; cursor: not-allowed; }
.tools { display: flex; gap: 10px; align-items: center; }
.tools label { margin: 0; }
.tools select { width: auto; }
.stage {
  flex: 1; min-height: 360px; overflow: auto; display: grid; place-items: center; padding: 24px;
  background-color: var(--stage);
  background-image: radial-gradient(color-mix(in srgb, var(--muted) 30%, transparent) 1px, transparent 1px);
  background-size: 18px 18px;
}
.stage svg { display: block; filter: drop-shadow(0 6px 14px rgba(0,0,0,.18)); }
.info { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 14px 16px; background: var(--panel); border-top: 1px solid var(--line); }
.spec table { border-collapse: collapse; width: 100%; font-size: 13px; }
.spec td { padding: 4px 6px; border-bottom: 1px solid var(--line); }
.spec td:first-child { color: var(--muted); width: 48%; }
.warnings { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.warnings li { padding: 7px 10px; border-radius: 8px; background: var(--warn-bg); color: var(--warn); margin-bottom: 6px; }
.warnings li.ok { background: transparent; color: var(--ok); border: 1px solid var(--line); }

button.danger { color: #b91c1c; }
@media (prefers-color-scheme: dark) { button.danger { color: #fca5a5; } }
button:disabled { opacity: .5; cursor: not-allowed; }

/* Ảnh / logo */
.upl { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 6px; }
label.btn {
  flex-direction: row; justify-content: center; margin: 0; padding: 10px; border-radius: 8px; cursor: pointer;
  border: 1px dashed var(--accent); color: var(--accent); font-size: 13px; font-weight: 600; text-align: center;
}
label.btn:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.imglist { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.thumb {
  width: 56px; height: 56px; padding: 3px; border-radius: 8px; display: grid; place-items: center;
  background: repeating-conic-gradient(#d9d6ce 0 25%, #fff 0 50%) 0 0 / 10px 10px;
}
.thumb.active { outline: 2px solid var(--accent); outline-offset: 1px; }
.thumb img { max-width: 100%; max-height: 100%; display: block; }
.btnrow { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.btnrow button { padding: 6px 10px; font-size: 13px; }
input[type=range] { width: 100%; accent-color: var(--accent); }

.viewer { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.stage svg { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.stage svg [data-img] { cursor: move; }
.stage svg [data-handle=resize] { cursor: nwse-resize; }
.stage svg [data-handle=rotate] { cursor: grab; }
.stage.drop { outline: 3px dashed var(--accent); outline-offset: -10px; }
.imgbar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 8px 12px;
  background: var(--panel); border-top: 1px solid var(--line);
}
.imgbar[hidden] { display: none; }
.imgbar .name { font-size: 12px; color: var(--muted); max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.imgbar label { flex-direction: row; align-items: center; gap: 4px; margin: 0; font-size: 12px; }
.imgbar input { width: 64px; padding: 5px 6px; }
.imgbar button { padding: 5px 9px; font-size: 13px; }

/* Hộp thoại */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--ink);
  width: min(460px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { padding: 16px 18px; display: flex; flex-direction: column; }
dialog h2 { margin: 0 0 4px; font-size: 17px; }
dialog .note { min-height: 1.2em; font-size: 13px; color: var(--accent); margin: 8px 0 0; }
dialog textarea { margin-top: 8px; }
.dlgbtns { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.dlgbtns button[hidden] { display: none; }

/* Mẫu tự động */
button.magic { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.tplcta {
  width: 100%; margin-top: 6px; padding: 10px; text-align: left; font-size: 13px; font-weight: 600; color: var(--accent);
  border: 1px dashed var(--accent); background: color-mix(in srgb, var(--accent) 6%, transparent);
}
dialog.wide { width: min(1040px, calc(100vw - 24px)); }
.tplwrap { display: grid; grid-template-columns: 300px 1fr; gap: 18px; margin-top: 6px; }
.tplstep { margin: 12px 0 0; font-weight: 600; font-size: 13px; }
.tplind { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.tplind button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; font-size: 12px; text-align: center; }
.tplind button span { font-size: 22px; line-height: 1; }
.tplind button.active, #tplSizes button.active {
  border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); font-weight: 600;
}
.tplhead { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.tplhead .btnrow { margin-top: 8px; }
.tplgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; margin-top: 10px; }
.tpl { display: flex; flex-direction: column; gap: 4px; padding: 8px; text-align: left; border-radius: 12px; }
.tpl:hover { box-shadow: 0 4px 14px rgba(0,0,0,.12); }
.tplsvg {
  display: grid; place-items: center; height: 150px; padding: 10px; border-radius: 8px;
  background: var(--stage);
}
.tplsvg svg { max-width: 100%; height: auto; filter: drop-shadow(0 3px 8px rgba(0,0,0,.2)); }
.tplname { font-weight: 600; font-size: 13px; }
.tplmeta { font-size: 11px; color: var(--muted); }
.tplmeta b { color: var(--ok); font-weight: 500; }
.tplmeta em { color: var(--warn); font-style: normal; }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 30px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--panel); padding: 10px 14px; border-radius: 10px; font-size: 13px;
  max-width: min(520px, calc(100vw - 32px)); transition: .25s; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#printArea { display: none; }
#printArea .pcap { font: 9pt system-ui, Arial, sans-serif; color: #444; margin: 3mm 0 0; }
#printArea .pcal { height: 0; border-top: 0.3mm solid #000; margin-top: 4mm; position: relative; }
#printArea .pcal::before, #printArea .pcal::after { content: ""; position: absolute; top: -1.5mm; height: 3mm; border-left: 0.3mm solid #000; }
#printArea .pcal::before { left: 0; } #printArea .pcal::after { right: 0; }
@media print {
  body { background: #fff !important; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block !important; }
  #printArea img { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; min-height: 0; }
  .topbar { position: static; padding: 8px 12px; }
  .brand p { display: none; }
  .actions button { padding: 7px 10px; }
  /* Điện thoại: khung xem dính trên cùng, bảng điều khiển cuộn bên dưới. */
  .workspace { display: contents; }
  .viewer { order: -1; position: sticky; top: 0; z-index: 4; box-shadow: 0 4px 12px rgba(0,0,0,.12); }
  .toolbar { padding: 8px 10px; }
  .tabs { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; }
  .tabs button { white-space: nowrap; padding: 6px 10px; }
  .stage { height: 42vh; height: 42dvh; min-height: 220px; padding: 12px; flex: none; }
  .panel { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); order: 0; }
  .info { grid-template-columns: 1fr; order: 1; }
  input, select, textarea { font-size: 16px; } /* tránh iOS tự phóng to khi chạm ô nhập */
  dialog.wide { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; border-radius: 0; }
  .tplwrap { grid-template-columns: 1fr; }
  .tplind { grid-template-columns: repeat(3, 1fr); }
  .tplgrid { grid-template-columns: 1fr 1fr; }
}
