:root {
  --bg: #0b0a14;
  --panel: #17152a;
  --panel-2: #211d3a;
  --line: #322c52;
  --text: #ece9ff;
  --muted: #9a93c4;
  --accent: #a855f7;
  --accent-2: #ec4899;
  --font: "Anuphan", system-ui, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------- topbar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.topbar-spacer { flex: 1; }
#topbar select, #topbar > button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  cursor: pointer;
}
#topbar > button:hover, #topbar select:hover { border-color: var(--accent); }
#topbar .primary {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border: none; font-weight: 700;
}

/* ---------- custom language dropdown (with flag SVGs) ---------- */
.lang-dd { position: relative; }
.lang-btn {
  display: flex; align-items: center; gap: 6px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 9px; font-size: 14px; cursor: pointer;
}
.lang-btn:hover { border-color: var(--accent); }
.lang-flag { width: 21px; height: 14px; border-radius: 2px; overflow: hidden; display: inline-flex; flex: 0 0 auto; }
.lang-flag svg { width: 100%; height: 100%; display: block; }
.lang-btn .caret { font-size: 10px; color: var(--muted); }
.lang-menu {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 60;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 4px; min-width: 130px;
  box-shadow: 0 12px 30px rgba(0,0,0,.5);
}
.lang-menu[hidden] { display: none; }
.lang-menu button {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: none; border: none; color: var(--text);
  padding: 7px 9px; border-radius: 7px; font-size: 14px; cursor: pointer; text-align: left;
}
.lang-menu button:hover { background: var(--panel-2); }
.lang-menu button.active { background: var(--panel-2); }

/* ---------- template panel ---------- */
.tpl-panel {
  display: none;
  position: absolute;
  left: 0; right: 0;
  z-index: 40;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 12px 30px rgba(0,0,0,.45);
  padding: 12px;
  max-height: 60vh;
  overflow: auto;
}
.tpl-panel.open { display: block; }
.display-fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.display-fields label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.display-fields input[type="color"] { width: 44px; height: 28px; border: none; background: none; padding: 0; cursor: pointer; }
.display-fields input[type="range"] { width: 100%; accent-color: var(--accent); }
.tpl-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}
.tpl-grid label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; }
.tpl-grid input {
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--text); border-radius: 6px; padding: 6px 8px; font-size: 13px;
}
.tpl-quads { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.tpl-quad {
  display: flex; align-items: center; gap: 6px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px;
}
.tpl-quad b { font-size: 12px; color: var(--muted); min-width: 52px; }
.tpl-quad input[type="text"], .tpl-quad input:not([type="color"]) {
  flex: 1; background: var(--bg); border: 1px solid var(--line);
  color: var(--text); border-radius: 6px; padding: 5px 7px; font-size: 13px; min-width: 0;
}
.tpl-quad input[type="color"] {
  width: 34px; height: 30px; border: none; background: none; padding: 0; cursor: pointer;
}

/* ---------- stage = frame (title/brand/url อยู่นอกกราฟ พื้นดำ) ---------- */
#stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 14px 18px 34px;
  min-height: 0;
  position: relative;
  background: #000;              /* พื้นหลังนอกกราฟ = ดำ */
  --badge-scale: 1;
}
/* เครดิตมุมซ้ายล่าง (คู่กับ url มุมขวาล่าง) */
.frame-brand {
  position: absolute;
  left: 18px; bottom: 6px;
  display: flex; align-items: center; gap: 7px;
  max-width: 60%;
  z-index: 2;
}
.frame-logo {
  width: 26px; height: 26px; border-radius: 50%;   /* logo วงกลม (ark_logo) */
  object-fit: cover; background: #1a1526; flex: 0 0 auto;
}
.frame-brand-txt { font-weight: 600; font-size: 12.5px; color: rgba(255,255,255,.7); }
.frame-brand-txt a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.frame-title {
  align-self: center;
  width: 100%;
  text-align: center;
  background: transparent;
  border: none;
  outline: none;
  color: var(--title-color, #ffffff);
  font-family: var(--font);
  font-weight: 700;
  font-size: clamp(16px, var(--title-scale, 3.2) * 1cqw, 80px);
  padding: 6px 4px 2px;
}
.frame-title::placeholder { color: rgba(255,255,255,.4); }
.frame-title:focus { border-bottom: 1px dashed var(--accent); }
.frame-url {
  position: absolute;
  right: 18px; bottom: 8px;
  max-width: 65%;
  background: transparent; border: none; outline: none;
  color: rgba(255,255,255,.55);
  font-family: var(--font);
  font-size: 12px; text-align: right;
}
.frame-url:focus { color: rgba(255,255,255,.85); border-bottom: 1px dashed var(--accent); }

.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  margin-top: 6px;
  container-type: size;
  container-name: boardwrap;
}
#stage { container-type: size; container-name: stage; }
#board {
  position: relative;
  width: min(100cqw, 100cqh, 900px);
  aspect-ratio: 1 / 1;
  background: #0f0d1a;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  touch-action: none;   /* กัน browser scroll ตอนลากบนมือถือ */
  container-type: size; /* ให้ badge/ชื่อบนกระดานอิงขนาดกระดาน (cqw) -> ตรงกับ PNG ทุกจอ */
  container-name: board;
}
@supports not (container-type: size) {
  #board { width: min(88vw, calc(100vh - 300px), 820px); }
}

.gfx { pointer-events: none; }
.quadrant { position: absolute; opacity: .82; transition: none; }
.axis { position: absolute; background: rgba(255,255,255,.55); }
.axis-v { top: 0; width: 2px; height: 100%; transform: translateX(-1px); }
.axis-h { left: 0; height: 2px; width: 100%; transform: translateY(-1px); }

.quad-label {
  position: absolute;
  font-size: clamp(11px, 2.4vw, 15px);
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 4px rgba(0,0,0,.6);
  pointer-events: none;
  max-width: 45%;
}
.axis-label {
  position: absolute;
  font-size: clamp(10px, 2vw, 13px);
  color: rgba(255,255,255,.9);
  text-shadow: 0 1px 3px rgba(0,0,0,.7);
  pointer-events: none;
  white-space: nowrap;
}

.origin-handle {
  position: absolute;
  width: 18px; height: 18px;
  margin: -9px 0 0 -9px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
  cursor: grab;
  z-index: 30;
  touch-action: none;
  pointer-events: auto;
}
.origin-handle.dragging { cursor: grabbing; }

/* ---------- badges ---------- */
.badge {
  position: relative;
  width: auto;
  min-width: 54px;
  max-width: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: grab;
  touch-action: none;
}
.badge img {
  width: 46px; height: 46px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #fff;
  background: #2a2540;
  pointer-events: none;
}
/* ปุ่มแก้ไข badge — แสดงเสมอที่มุมล่างขวาของรูป (หาเจอง่าย) */
.badge-edit {
  position: absolute;
  /* จัดทับมุมล่างขวาของ avatar 46px ที่อยู่กึ่งกลาง badge */
  top: 28px;
  left: calc(50% + 8px);
  width: 20px;
  height: 20px;
  padding: 0;
  border: 1.5px solid #fff;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  box-shadow: 0 2px 6px rgba(0,0,0,.45);
  transition: transform .12s ease, filter .12s ease;
}
.badge-edit:hover,
.badge-edit:focus-visible {
  transform: scale(1.12);
  filter: brightness(1.1);
  outline: none;
}
/* บนกระดาน — มุมล่างขวาของวงกลม (อยู่ในกล่อง badge ไม่โดน board ตัด) */
.badge.placed .badge-edit {
  top: auto;
  bottom: 0;
  left: auto;
  right: 0;
  width: clamp(16px, 2.4cqw, 22px);
  height: clamp(16px, 2.4cqw, 22px);
  font-size: clamp(9px, 1.3cqw, 12px);
}
/* ในการ์ด MBTI */
.badge.in-cell .badge-edit {
  top: calc(4.4cqw * var(--badge-scale, 1) - 1.6cqw);
  left: calc(50% + 0.6cqw);
  right: auto;
  bottom: auto;
  width: clamp(14px, 2cqw, 20px);
  height: clamp(14px, 2cqw, 20px);
  font-size: clamp(8px, 1.2cqw, 11px);
}
.badge.noimg img { display: none; }
.badge.noimg::before {
  content: attr(data-initial);
  width: 46px; height: 46px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  border: 2px solid #fff;
  background: var(--flag, #2a2540);
  color: #fff; font-weight: 700; font-size: 20px;
}
.badge.placed.noimg::before { width: calc(6.6cqw * var(--badge-scale, 1)); height: calc(6.6cqw * var(--badge-scale, 1)); }
.badge-name {
  font-size: 10px;
  line-height: 1.25;
  color: var(--text);
  text-align: center;
  max-width: 104px;
  white-space: normal;
  word-break: break-word;
  text-wrap: balance;   /* เกลี่ยความยาวแต่ละบรรทัดให้ใกล้เคียงกัน ตอนชื่อยาวขึ้นหลายบรรทัด */
  text-shadow: 0 1px 2px rgba(0,0,0,.8);
  pointer-events: none;
}
/* กล่อง badge = ขนาด avatar พอดี, anchor กึ่งกลาง avatar ที่จุด p (ตรงกับ PNG)
   ชื่อลอยใต้ avatar แบบ absolute -> ชื่อยาวหลายบรรทัดไม่ดันตำแหน่ง avatar
   => avatar ทุกตัวอยู่ระดับเดียวกันทั้งบนจอและใน PNG */
.badge.placed {
  position: absolute;
  transform: translate(-50%, -50%);
  z-index: 3;
  display: block;
  min-width: 0;   /* ยกเลิก min-width ของถาด ไม่งั้นบังคับกว้าง 54px -> avatar เป็นวงรี */
  width: calc(6.6cqw * var(--badge-scale, 1));
  height: calc(6.6cqw * var(--badge-scale, 1));
}
.badge.placed img { width: 100%; height: 100%; }
.badge.placed.noimg::before { width: 100%; height: 100%; font-size: 3cqw; }
.badge.placed .badge-name {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  /* กว้างเท่าเนื้อหา แต่ไม่เกิน 12% ของกระดาน (ไม่ถูกบีบด้วยความกว้าง avatar) */
  width: max-content;
  max-width: 12cqw;
  text-align: center;
  /* อิงกระดาน (cqw) ให้เท่ากับ PNG: ฟอนต์ ~1.2% ของกระดาน */
  font-size: clamp(7px, 1.2cqw, 22px);
  margin-top: 0.3cqw;
  line-height: 1.5;
}
.badge.placed .badge-name-txt {
  background: rgba(12, 10, 22, .8);
  padding: 0.15em 0.5em;
  border-radius: 0.4em;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
/* hover ตัวไหน เด้งตัวนั้น + ชื่อขึ้นมาอยู่หน้าสุด (อ่านตัวที่ทับกันได้) */
.badge.placed:hover { z-index: 100; }
.badge.dragging { opacity: .9; cursor: grabbing; z-index: 9999; }

/* ---------- MBTI 16-type grid ---------- */
/* บอร์ดสูงตามเนื้อหา: การ์ดที่ตัวละครเยอะจะยืดสูงขึ้น (ไม่ตัด) แล้วบอร์ดเลื่อนดูได้
   container-type: inline-size — ยังใช้ cqw จัดขนาด badge ได้ แต่ปล่อยให้ "สูง" ตามเนื้อหา
   (ต่างจาก size ที่ตรึงความสูงจนการ์ดล้นแล้วโดน overflow:hidden ตัด เลื่อนไม่ได้) */
#board.grid-mode {
  aspect-ratio: auto;
  width: min(100cqw, 1100px);
  height: auto;
  min-height: min(100cqh, calc(100cqw / 1.6));
  container-type: inline-size;
  overflow: visible;
}
@supports not (container-type: size) {
  #board.grid-mode { width: min(96vw, 1100px); min-height: 60vh; }
}
/* ตอนใช้ตาราง ให้พื้นที่บอร์ดเลื่อนแนวตั้งได้ (การ์ดสูงเกินจอ) และชิดบน */
.grid-stage .board-wrap { align-items: flex-start; overflow-y: auto; overflow-x: hidden; }
.grid-layout {
  display: flex;
  gap: 1.1cqw;
  padding: 1.1cqw;
  box-sizing: border-box;
  min-height: 100%;
}
.grid-col { flex: 1 1 0; display: flex; flex-direction: column; gap: 0.9cqw; min-height: 0; min-width: 0; }
.grid-col-head {
  color: var(--col, #fff);
  font-weight: 700;
  font-size: clamp(11px, 1.55cqw, 22px);
  line-height: 1.15;
  padding: 0 0.2cqw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mbti-card {
  flex: 0 0 auto;            /* สูงตามเนื้อหา — ตัวละครเยอะ = การ์ดยืดสูง (ไม่ตัด) */
  min-height: 9cqw;
  display: flex;
  flex-direction: column;
  gap: 0.4cqw;
  padding: 0.7cqw 0.7cqw 0.5cqw;
  border-radius: 0.9cqw;
  box-sizing: border-box;
  background: color-mix(in srgb, var(--col, #6b7280) 15%, #12101d);
  border: 1px solid color-mix(in srgb, var(--col, #6b7280) 55%, transparent);
}
.mbti-card-type {
  color: color-mix(in srgb, var(--col, #fff) 78%, #ffffff);
  font-weight: 700;
  font-size: clamp(10px, 1.35cqw, 19px);
  line-height: 1;
  letter-spacing: .02em;
  flex: 0 0 auto;
}
.mbti-card-drop {
  flex: 1;
  min-height: 5cqw;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5cqw 0.7cqw;
  justify-content: center;
  align-content: flex-start;
}
/* badge ในการ์ด — รูปวงกลม + ชื่อใต้รูป, ไหลเรียงในการ์ด (อิง cqw ให้ตรงกับ PNG) */
.badge.in-cell { width: auto; min-width: 0; max-width: none; gap: 0.2cqw; }
.badge.in-cell img,
.badge.in-cell.noimg::before {
  width: calc(4.4cqw * var(--badge-scale, 1));
  height: calc(4.4cqw * var(--badge-scale, 1));
  border-width: 2px;
}
.badge.in-cell.noimg::before { font-size: 2.2cqw; }
.badge.in-cell .badge-name {
  font-size: clamp(7px, 1.05cqw, 18px);
  line-height: 1.2;
  max-width: 18cqw;
  color: #fff;
}

/* ---------- tray ---------- */
#tray-wrap {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 6px 10px 10px;
  max-height: 34vh;
  display: flex;
  flex-direction: column;
}
.tray-head {
  display: flex; align-items: center;
  font-size: 12px; color: var(--muted); margin-bottom: 6px; gap: 8px;
  flex-wrap: wrap;
}
.tray-title { font-weight: 600; }
.tray-hint { font-size: 11px; opacity: .8; }
.tray-tools { margin-left: auto; display: flex; gap: 6px; }
.tray-tools button {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 10px; font-size: 12px; cursor: pointer;
}
.tray-tools button:hover { border-color: var(--accent); }
#tray {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 4px;
  overflow-y: auto;
  padding: 4px;
  align-content: flex-start;
}
.tray-error { color: #f87171; font-size: 13px; padding: 8px; }

/* ---------- modal (gimmick) ---------- */
.modal-back {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(3px);
}
.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 26px 28px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
  animation: pop .18s ease;
}
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-title { font-size: 20px; font-weight: 700; margin-bottom: 18px; }
.modal-actions { display: flex; gap: 12px; justify-content: center; }
.modal-actions button {
  border: none; border-radius: 10px; padding: 10px 24px;
  font-size: 15px; font-weight: 700; cursor: pointer;
}
.btn-yes { background: linear-gradient(90deg, var(--accent), var(--accent-2)); color: #fff; }
.btn-no { background: var(--panel-2); color: var(--muted); border: 1px solid var(--line) !important; }
.btn-ok { background: linear-gradient(90deg, var(--accent), var(--accent-2)); color: #fff; }

/* multi-language subtitle in popups */
.modal-sub {
  margin: -6px 0 16px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--muted);
  /* Anuphan ไม่มี glyph CJK -> ใส่ CJK families ก่อน generic sans-serif
     (ถ้าวาง sans-serif ก่อน จะ short-circuit ไม่ถึง family หลัง) */
  font-family: "Anuphan", "Hiragino Sans", "Yu Gothic", "Meiryo",
    "Malgun Gothic", "Microsoft JhengHei", "PingFang TC", "Noto Sans CJK TC",
    system-ui, sans-serif;
}
.modal-sub div { white-space: normal; }

/* ---------- add-character modal ---------- */
.add-char-modal {
  position: relative;
  text-align: left;
  width: min(92vw, 380px);
  padding: 22px;
}
.add-char-modal .modal-title { text-align: center; }
.modal-close {
  position: absolute; top: 12px; right: 14px;
  background: none; border: none; color: var(--muted);
  font-size: 16px; cursor: pointer; line-height: 1;
}
.add-char-modal .tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.add-char-modal .tab {
  flex: 1; background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; font-size: 13px; font-weight: 600; cursor: pointer;
}
.add-char-modal .tab.active {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff; border-color: transparent;
}
.add-char-modal .tab-body { display: flex; flex-direction: column; gap: 10px; }
.add-char-modal .tab-body[hidden] { display: none; }
.drop {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  min-height: 110px;
  border: 2px dashed var(--line); border-radius: 12px;
  cursor: pointer; overflow: hidden; background: var(--panel-2);
}
.drop:hover { border-color: var(--accent); }
.drop input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.drop-text { color: var(--muted); font-size: 13px; }
#ac-preview { display: none; width: 96px; height: 96px; border-radius: 50%; object-fit: cover; }
.add-char-modal input[type="text"], .add-char-modal textarea {
  background: var(--bg); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 9px 11px; font-size: 14px; font-family: var(--font);
  width: 100%;
}
.add-char-modal textarea { min-height: 120px; resize: vertical; }
.add-char-modal .hint { font-size: 11px; color: var(--muted); }
.add-char-modal .hint b { color: var(--text); }
.btn-primary {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff; border: none; border-radius: 10px;
  padding: 10px; font-size: 15px; font-weight: 700; cursor: pointer;
}
.btn-danger {
  background: #3a1520;
  color: #fca5a5;
  border: 1px solid #7f1d1d;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.btn-danger:hover { background: #5b1d2a; border-color: #ef4444; color: #fff; }

/* ---------- edit character modal (crop / rename / delete) ---------- */
.edit-char-modal {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(92vw, 360px);
}
.crop-viewport {
  position: relative;
  width: min(72vw, 260px);
  aspect-ratio: 1;
  margin: 0 auto;
  border-radius: 50%;
  overflow: hidden;
  background: var(--panel-2);
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px var(--flag, var(--line)), 0 8px 24px rgba(0,0,0,.4);
  touch-action: none;
  cursor: grab;
  user-select: none;
}
.crop-viewport:active { cursor: grabbing; }
.crop-viewport.noimg::before {
  content: attr(data-initial);
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 64px; font-weight: 700; color: #fff;
  background: var(--flag, #2a2540);
  z-index: 1;
}
.crop-img {
  position: absolute;
  top: 0; left: 0;
  max-width: none;
  pointer-events: auto;
  cursor: grab;
  will-change: transform;
  user-select: none;
  -webkit-user-drag: none;
}
.crop-img:active { cursor: grabbing; }
.crop-zoom {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}
.crop-hint {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  margin-top: -4px;
}
.crop-file-btn {
  display: block;
  text-align: center;
  background: var(--panel-2);
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 9px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
}
.crop-file-btn:hover { border-color: var(--accent); color: #fff; }
.ec-color-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
}
.ec-color-row input[type="color"] {
  width: 48px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
}
.ec-buttons {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.ec-buttons .btn-danger { flex: 0 0 auto; }
.ec-buttons .btn-primary { flex: 1; }

/* ---------- desktop ---------- */
@media (min-width: 768px) {
  body {
    display: grid;
    grid-template-columns: 1fr 260px;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "header header"
      "main tray";
  }
  #topbar {
    grid-area: header;
    flex-wrap: nowrap;
    gap: 12px;
    padding: 10px 16px;
  }
  .graph-title {
    flex: 1 1 auto;
    min-width: 140px;
    max-width: 480px;
  }
  .actions { flex-shrink: 0; flex-wrap: nowrap; }
  #stage {
    grid-area: main;
    padding: 16px 20px;
  }
  #tray-wrap {
    grid-area: tray;
    max-height: none;
    min-height: 0;
    border-top: none;
    border-left: 1px solid var(--line);
    padding: 10px 12px;
  }
  .tray-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
  }
  #tray {
    flex: 1;
    min-height: 0;
  }
}

@media (min-width: 1100px) {
  body { grid-template-columns: 1fr 300px; }
  #board { width: min(100cqw, 100cqh, 960px); }
}

/* ---------- mobile ---------- */
@media (max-width: 767px) {
  #topbar { padding: 6px 8px; gap: 6px; }
  #stage {
    padding: 4px 6px;
    flex: 1 1 0;
  }
  #board {
    width: min(100cqw, 100cqh);
    max-width: calc(100vw - 12px);
  }
  #tray-wrap {
    max-height: none;
    flex: 0 0 auto;
    padding: 4px 8px 8px;
  }
  .tray-head { margin-bottom: 4px; }
  .tray-hint { display: none; }
  #tray {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 2px;
  }
  #tray .badge { flex: 0 0 auto; }
}

@supports not (container-type: size) {
  @media (max-width: 767px) {
    #board { width: min(calc(100vw - 12px), calc(100dvh - 200px)); }
  }
}

@media (max-width: 640px) {
  .brand-txt { display: none; }
  .graph-title { flex: 1 1 100%; order: 3; }
  .actions button, .actions select { padding: 7px 8px; font-size: 13px; }
}
