:root {
  --bg: #f5f5f4; --fg: #1c1917; --muted: #6b7280; --card: #ffffff;
  --line: #d6d3d1; --accent: #1f2937; --accent-fg: #ffffff; --ok: #166534; --err: #b91c1c;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111315; --fg: #e7e5e4; --muted: #9ca3af; --card: #1b1e21;
    --line: #3a3f45; --accent: #e7e5e4; --accent-fg: #111315; --ok: #86efac; --err: #fca5a5;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: clamp(15px, 0.85rem + 0.3vw, 19px)/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh; min-height: 100dvh; display: flex; justify-content: center;
}
main {
  width: 100%; max-width: 1480px; min-width: 0;
  padding: clamp(16px, 3vw, 48px);
  padding-left: max(clamp(16px, 3vw, 48px), env(safe-area-inset-left));
  padding-right: max(clamp(16px, 3vw, 48px), env(safe-area-inset-right));
  padding-bottom: max(clamp(16px, 3vw, 48px), env(safe-area-inset-bottom));
}
h1 { margin: 0; font-size: clamp(1.6rem, 1.1rem + 1.8vw, 2.6rem); line-height: 1.2; }
.sub { margin: 4px 0 clamp(16px, 2.5vw, 32px); color: var(--muted); }
.tools { display: grid; grid-template-columns: minmax(0, 1fr); gap: clamp(16px, 2.5vw, 32px); align-items: start; }
.tool { min-width: 0; }
@media (min-width: 900px) {
  .tools { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); }
  /* khi đang chỉnh ảnh, thẻ hình chiếm toàn bề ngang */
  .tools:has(#imged:not([hidden])) { grid-template-columns: minmax(0, 1fr); }
}
.tool > .card:first-child, .tool > .drop:first-child { margin-top: 0; }
.drop {
  border: 2px dashed var(--line); border-radius: 14px; background: var(--card);
  padding: clamp(32px, 6vw, 72px) 16px; text-align: center; cursor: pointer; outline: none;
  transition: border-color .15s, background .15s;
}
.drop:hover, .drop:focus-visible, .drop.over { border-color: var(--accent); }
.drop.over { background: color-mix(in srgb, var(--card) 85%, var(--accent)); }
.drop-title { font-size: 1.1rem; font-weight: 600; }
.drop-or { margin: 8px 0; color: var(--muted); }
.formats { margin-top: 16px; font-size: .85rem; color: var(--muted); }
.btn {
  display: inline-block; border: 1px solid var(--accent); background: transparent; color: var(--accent);
  padding: 10px 20px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none;
}
.btn.primary { background: var(--accent); color: var(--accent-fg); }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; font: inherit; cursor: pointer; margin-top: 12px; display: block; }
.card { margin-top: 16px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: clamp(16px, 2.5vw, 28px); text-align: center; }
.card[hidden], [hidden] { display: none !important; }
.fname { font-weight: 600; word-break: break-word; }
.bar { margin: 14px 0 8px; height: 10px; background: var(--line); border-radius: 6px; overflow: hidden; }
#barfill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.stage { color: var(--muted); font-size: .95rem; margin-bottom: 12px; word-break: break-word; }
.ok { font-weight: 700; color: var(--ok); margin-bottom: 4px; }
.error .ok { color: var(--err); }
footer { margin-top: clamp(24px, 4vw, 48px); text-align: center; color: var(--muted); font-size: .85rem; }
.badge { display: inline-block; padding: 2px 10px; border: 1px solid var(--line); border-radius: 999px; }
.badge.ready { color: var(--ok); border-color: var(--ok); }
.imgprep { margin-top: 0; container-type: inline-size; }
.imgprep .stage { margin: 4px 0 12px; }
.imgprep .btn:disabled { opacity: .6; cursor: default; }
.preview { display: block; margin: 16px auto 8px; max-width: 100%; max-height: min(70vh, 720px); height: auto; border: 1px solid var(--line); background: #fff; }
.err { color: var(--err); margin-top: 12px; }

.btn.sm { padding: 6px 12px; font-size: .9rem; }
.editor { text-align: left; }
.ed-stage { display: flex; justify-content: center; background: repeating-conic-gradient(var(--line) 0 25%, transparent 0 50%) 0 0 / 16px 16px; border-radius: 8px; padding: 8px; }
#edcanvas { max-width: 100%; touch-action: none; }
.ed-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 12px 0; }
.ed-row .link { margin: 0; display: inline; }
.ed-field { display: block; margin: 12px 0; font-size: .95rem; }
.ed-field output { float: right; color: var(--muted); }
.ed-field select, .ed-field input[type=range] { display: block; width: 100%; margin-top: 6px; font: inherit; }
#edresult { width: min(100%, 264px); max-height: none; margin-left: 0; }
.ed-main, .ed-side { min-width: 0; }
.ed-main .stage { margin-bottom: 0; }
@container (min-width: 720px) {
  .editor { display: grid; grid-template-columns: minmax(0, 1fr) minmax(260px, 340px); gap: clamp(16px, 2.5cqw, 32px); align-items: start; }
  .ed-side .ed-row:first-child { margin-top: 0; }
}
@media (max-width: 420px) {
  .ed-row .btn.sm { flex: 1 1 auto; text-align: center; }
  .btn { padding: 12px 18px; }
}
@media (max-width: 899px) { #edresult { margin-left: auto; margin-right: auto; } }
.ed-check { display: flex; gap: 8px; align-items: flex-start; margin: 12px 0; font-size: .95rem; }
.ed-check input { margin-top: .3em; }
