:root {
  --ui-bg: #ffffff;
  --ui-text: #2b2b3a;
  --ui-muted: #6b6b80;
  --ui-line: #ece8f5;
  --accent: #6741d9;
  --accent-soft: #efe9ff;
  --danger: #e03131;
  --stage-bg: #fffdf7;
  --grid: #eee6d3;
  --radius: 14px;
  --font: "Segoe UI", Tahoma, Arial, sans-serif;
  --emoji: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; font-family: var(--font); color: var(--ui-text); background: var(--ui-bg); user-select: none; }
button { font-family: inherit; color: inherit; }
i { font-style: normal; font-family: var(--emoji); }

/* ---------- Thanh công cụ ---------- */
#topbar { position: fixed; inset: 0 0 auto 0; height: 76px; display: flex; align-items: center; gap: 12px; padding: 0 12px; background: var(--ui-bg); border-bottom: 2px solid var(--ui-line); z-index: 20; }
.brand { display: flex; align-items: center; gap: 8px; min-width: 180px; flex: 0 1 260px; }
.logo { width: 40px; height: 40px; }
#docTitle { flex: 1; min-width: 0; font: 700 18px var(--font); border: 2px solid transparent; border-radius: 10px; padding: 6px 8px; background: transparent; color: var(--ui-text); user-select: text; }
#docTitle:hover { border-color: var(--ui-line); }
#docTitle:focus { outline: none; border-color: var(--accent); background: #fff; }
.tools { display: flex; align-items: center; gap: 6px; flex: 1; overflow-x: auto; scrollbar-width: thin; }
.group { display: flex; gap: 2px; padding: 0 6px; border-right: 2px solid var(--ui-line); }
.group:last-child { border-right: 0; }
.tools button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-width: 62px; height: 62px; padding: 4px 6px; border: 0; border-radius: 12px; background: transparent; cursor: pointer; transition: background .15s, transform .1s; }
.tools button i { font-size: 24px; line-height: 1; }
.tools button span { font-size: 12px; font-weight: 600; white-space: nowrap; color: var(--ui-muted); }
.tools button:hover { background: var(--accent-soft); }
.tools button:hover span { color: var(--accent); }
.tools button:active { transform: scale(.94); }
.tools button.hot { background: #e6fcf5; }
.tools button.hot:hover { background: #c3fae8; }
.tools button:disabled { opacity: .35; cursor: default; background: transparent; }
.tools button.on { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); }

@media (max-width: 1180px) { .tools button span { display: none; } .tools button { min-width: 48px; height: 52px; } #topbar { height: 66px; } #stage { top: 66px !important; } }

/* ---------- Vùng vẽ ---------- */
#stage { position: fixed; inset: 76px 0 0 0; overflow: hidden; background-color: var(--stage-bg); background-image: radial-gradient(var(--grid) 1.6px, transparent 1.6px); background-size: 28px 28px; cursor: grab; touch-action: none; outline: none; }
#stage.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#links { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#nodes { position: absolute; left: 0; top: 0; }

.mnode { position: absolute; cursor: pointer; transition: box-shadow .15s, filter .15s; }
.mnode > * { position: absolute; pointer-events: none; }
.mnode.lv0 { box-shadow: 0 6px 18px rgba(0, 0, 0, .18); }
.mnode.lv1 { box-shadow: 0 4px 10px rgba(0, 0, 0, .13); }
.mnode:hover { filter: brightness(1.04); box-shadow: 0 0 0 4px rgba(103, 65, 217, .15), 0 4px 12px rgba(0, 0, 0, .12); }
.mnode.sel { box-shadow: 0 0 0 3px #fff, 0 0 0 6px #4dabf7, 0 6px 16px rgba(0, 0, 0, .2); z-index: 2; }
.mnode.shape-underline.sel, .mnode.shape-underline:hover { box-shadow: none; outline: 3px dashed #4dabf7; outline-offset: 3px; }
.mtext { white-space: pre; text-align: center; }
.memo { font-family: var(--emoji); white-space: nowrap; }
.mnote { font-family: var(--emoji); }
.mimg { border-radius: 8px; object-fit: cover; }
.mnode.dragging { opacity: .9; z-index: 10; box-shadow: 0 12px 28px rgba(0, 0, 0, .3); cursor: grabbing; transition: none; }
.mnode.ghost { opacity: .35; }
.mnode.drop-child { box-shadow: 0 0 0 4px #fff, 0 0 0 8px #40c057 !important; }
.mnode.drop-before::before, .mnode.drop-after::after { content: ""; position: absolute; left: -6px; right: -6px; height: 6px; border-radius: 3px; background: #40c057; }
.mnode.drop-before::before { top: -12px; }
.mnode.drop-after::after { bottom: -12px; }
.mnode.drop-before.horiz::before, .mnode.drop-after.horiz::after { top: -6px; bottom: -6px; left: auto; right: auto; width: 6px; height: auto; }
.mnode.drop-before.horiz::before { left: -12px; }
.mnode.drop-after.horiz::after { right: -12px; }

/* đang sửa chữ */
.mnode.editing { width: max-content !important; height: auto !important; display: flex; align-items: center; justify-content: center; gap: 8px; cursor: text; z-index: 5; box-shadow: 0 0 0 3px #fff, 0 0 0 6px #fab005 !important; }
.mnode.editing > * { position: static !important; }
.mnode.editing .mimg { display: none; }
.mnode.editing .mtext { pointer-events: auto; white-space: pre-wrap; word-break: break-word; min-width: 30px; outline: none; user-select: text; caret-color: currentColor; width: auto !important; }

.mtoggle { position: absolute; width: 22px; height: 22px; margin: -11px 0 0 -11px; border-radius: 50%; background: #fff; border: 2px solid; font: 700 11px/18px var(--font); text-align: center; cursor: pointer; z-index: 3; opacity: 0; transition: opacity .15s, transform .1s; }
.mtoggle.closed, .mtoggle.vis { opacity: 1; }
#nodes:hover .mtoggle { opacity: .9; }
.mtoggle { transform: scale(var(--inv, 1)); }
.mtoggle:hover { transform: scale(calc(var(--inv, 1) * 1.2)); }
.madd { position: absolute; width: 34px; height: 34px; margin: -17px 0 0 -17px; transform: scale(var(--inv, 1)); border-radius: 50%; background: #40c057; color: #fff; font: 700 24px/32px var(--font); text-align: center; cursor: pointer; box-shadow: 0 3px 8px rgba(0, 0, 0, .25); z-index: 4; animation: pop .2s ease-out; }
.madd:hover { background: #2f9e44; transform: scale(calc(var(--inv, 1) * 1.12)); }
@keyframes pop { from { transform: scale(.3); opacity: 0; } }

/* ---------- Thanh phóng to, gợi ý ---------- */
#zoombar { position: fixed; right: 16px; bottom: 16px; display: flex; align-items: center; gap: 4px; padding: 6px; background: var(--ui-bg); border-radius: 999px; box-shadow: 0 4px 16px rgba(0, 0, 0, .15); z-index: 15; }
#zoombar button { border: 0; background: var(--accent-soft); border-radius: 999px; height: 36px; min-width: 36px; padding: 0 12px; cursor: pointer; font: 600 14px var(--font); font-family: var(--font), var(--emoji); }
#zoombar button:hover { background: #ddd1ff; }
#zoomLabel { min-width: 52px; text-align: center; font-weight: 700; font-size: 14px; }
#hint { position: fixed; left: 16px; bottom: 16px; max-width: calc(100% - 420px); padding: 8px 14px; background: rgba(255, 255, 255, .92); border-radius: 999px; box-shadow: 0 3px 12px rgba(0, 0, 0, .1); font-size: 13.5px; color: var(--ui-muted); z-index: 15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hint b { color: var(--accent); background: var(--accent-soft); padding: 1px 7px; border-radius: 6px; font-weight: 700; }
#status { position: fixed; right: 16px; bottom: 70px; font-size: 12px; color: var(--ui-muted); background: rgba(255, 255, 255, .85); padding: 3px 10px; border-radius: 999px; z-index: 15; }
#status:empty { display: none; }

/* ---------- Popover ---------- */
#popover { position: fixed; z-index: 40; background: var(--ui-bg); border-radius: 16px; box-shadow: 0 10px 40px rgba(30, 20, 60, .25); padding: 14px; max-width: min(440px, calc(100vw - 20px)); max-height: calc(100vh - 100px); overflow: auto; animation: pop .15s ease-out; user-select: none; }
.pop-title { font-weight: 800; font-size: 14px; margin: 2px 2px 8px; color: var(--ui-muted); }
.pop-title:not(:first-child) { margin-top: 14px; }
.swatches { display: grid; grid-template-columns: repeat(9, 34px); gap: 7px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 1.5px #d0d0de; cursor: pointer; transition: transform .1s; }
.swatch:hover { transform: scale(1.15); }
.swatch.on { box-shadow: 0 0 0 3px var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 2px solid var(--ui-line); background: #fff; border-radius: 12px; padding: 7px 12px; font: 600 14px var(--font); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.chip:hover { border-color: #c5b6f6; background: var(--accent-soft); }
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.chip i { font-size: 18px; }
.shape-demo { display: inline-block; width: 30px; height: 18px; border: 2.5px solid currentColor; }
.emoji-tabs { display: flex; gap: 4px; margin-bottom: 8px; flex-wrap: wrap; }
.emoji-tabs button { border: 0; background: transparent; font-size: 22px; padding: 4px 6px; border-radius: 10px; cursor: pointer; font-family: var(--emoji); }
.emoji-tabs button.on { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 44px); gap: 4px; }
.emoji-grid button { width: 44px; height: 44px; border: 0; border-radius: 10px; background: transparent; font-size: 28px; cursor: pointer; font-family: var(--emoji); transition: transform .1s; }
.emoji-grid button:hover { background: #fff3bf; transform: scale(1.2); }
.menu { display: flex; flex-direction: column; min-width: 230px; }
.menu button { display: flex; align-items: center; gap: 10px; border: 0; background: transparent; padding: 9px 12px; border-radius: 10px; font: 600 14.5px var(--font); text-align: left; cursor: pointer; }
.menu button i { font-size: 18px; width: 24px; text-align: center; }
.menu button:hover { background: var(--accent-soft); color: var(--accent); }
.menu button.danger:hover { background: #fff0f0; color: var(--danger); }
.menu kbd { margin-left: auto; font: 12px var(--font); color: var(--ui-muted); background: #f3f1f8; padding: 1px 6px; border-radius: 5px; }
.menu hr { border: 0; border-top: 1.5px solid var(--ui-line); margin: 5px 4px; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.card-opt { border: 2px solid var(--ui-line); background: #fff; border-radius: 14px; padding: 10px 6px; cursor: pointer; text-align: center; font: 600 13px var(--font); }
.card-opt:hover { border-color: #c5b6f6; background: var(--accent-soft); }
.card-opt.on { border-color: var(--accent); background: var(--accent-soft); }
.card-opt svg { display: block; margin: 0 auto 6px; }
.theme-dot { width: 100%; height: 44px; border-radius: 10px; margin-bottom: 6px; display: flex; align-items: center; justify-content: center; gap: 4px; }
.theme-dot span { width: 12px; height: 12px; border-radius: 50%; }

/* ---------- Hộp thoại ---------- */
#modal { position: fixed; inset: 0; z-index: 50; background: rgba(35, 25, 70, .45); display: flex; align-items: center; justify-content: center; padding: 20px; animation: fade .15s; }
@keyframes fade { from { opacity: 0; } }
.modal-card { background: var(--ui-bg); border-radius: 24px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); width: min(980px, 100%); max-height: calc(100vh - 40px); display: flex; flex-direction: column; overflow: hidden; animation: pop .2s ease-out; }
.modal-card.small { width: min(520px, 100%); }
.m-head { display: flex; align-items: center; gap: 12px; padding: 18px 22px 10px; }
.m-head h2 { margin: 0; font-size: 22px; flex: 1; }
.m-close { border: 0; background: #f3f1f8; width: 38px; height: 38px; border-radius: 50%; font-size: 18px; cursor: pointer; }
.m-close:hover { background: #ffe3e3; }
.m-body { padding: 8px 22px 18px; overflow: auto; }
.m-foot { display: flex; gap: 10px; justify-content: flex-end; padding: 12px 22px 18px; border-top: 1.5px solid var(--ui-line); flex-wrap: wrap; }
.btn { border: 0; border-radius: 12px; padding: 10px 18px; font: 700 15px var(--font); cursor: pointer; background: #f1eef8; display: inline-flex; align-items: center; gap: 8px; }
.btn:hover { background: #e5def7; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: #5530c2; }
.btn.danger { background: #fff0f0; color: var(--danger); }
.btn i { font-size: 18px; }
textarea.field, input.field { width: 100%; border: 2px solid var(--ui-line); border-radius: 12px; padding: 10px 12px; font: 16px var(--font); resize: vertical; user-select: text; }
textarea.field:focus, input.field:focus { outline: none; border-color: var(--accent); }

.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; }
.tpl { border: 0; border-radius: 18px; padding: 16px 12px 14px; cursor: pointer; text-align: center; transition: transform .12s, box-shadow .12s; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.tpl:hover { transform: translateY(-4px) scale(1.02); box-shadow: 0 10px 22px rgba(0, 0, 0, .14); }
.tpl i { font-size: 42px; line-height: 1.1; }
.tpl b { font-size: 15px; }
.tpl small { font-size: 12px; color: #555; }
.start-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.start-actions .btn { padding: 12px 18px; }
.btn.continue { background: #fff3bf; }
.btn.continue:hover { background: #ffec99; }
.recent { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 16px; }
.recent .chip { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.section-title { font-weight: 800; color: var(--ui-muted); font-size: 14px; margin: 8px 0 10px; }

.hist-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.hist-item { border: 2px solid var(--ui-line); border-radius: 16px; overflow: hidden; display: flex; flex-direction: column; background: #fff; }
.hist-item img, .hist-item .noimg { width: 100%; height: 120px; object-fit: contain; background: #faf8ff; display: flex; align-items: center; justify-content: center; font-size: 40px; border-bottom: 1.5px solid var(--ui-line); }
.hist-item .meta { padding: 8px 10px 4px; }
.hist-item .meta b { display: block; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-item .meta small { color: var(--ui-muted); font-size: 12px; }
.hist-item .acts { display: flex; gap: 6px; padding: 6px 10px 10px; }
.hist-item .acts .btn { padding: 6px 10px; font-size: 13px; flex: 1; justify-content: center; }
.empty { text-align: center; color: var(--ui-muted); padding: 30px; font-size: 15px; }

.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 26px; }
.help-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--ui-line); font-size: 14.5px; }
.help-row kbd { min-width: 92px; text-align: center; font: 700 12.5px var(--font); background: var(--accent-soft); color: var(--accent); padding: 4px 8px; border-radius: 8px; }
.tips { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; margin-bottom: 14px; }
.tip { background: #f8f6ff; border-radius: 14px; padding: 12px 14px; font-size: 14px; display: flex; gap: 10px; align-items: flex-start; }
.tip i { font-size: 28px; }

#toast { position: fixed; left: 50%; top: 92px; transform: translateX(-50%) translateY(-20px); background: #2b2b3a; color: #fff; padding: 10px 20px; border-radius: 999px; font-weight: 600; font-size: 14.5px; opacity: 0; pointer-events: none; transition: all .25s; z-index: 60; box-shadow: 0 6px 20px rgba(0, 0, 0, .25); }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: var(--danger); }
#dropzone { position: fixed; inset: 0; z-index: 70; background: rgba(103, 65, 217, .2); display: flex; align-items: center; justify-content: center; pointer-events: none; }
#dropzone div { background: #fff; padding: 26px 40px; border-radius: 24px; font-size: 22px; font-weight: 800; border: 4px dashed var(--accent); }
[hidden] { display: none !important; }

/* ======================================================================
   Điện thoại & máy tính bảng nhỏ (PWA): bố cục gọn, thanh công cụ dưới cùng
   ====================================================================== */
#mobilebar, .m-only { display: none; }
.lang-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; min-width: 260px; }
.lang-list .chip { justify-content: center; padding: 12px; font-size: 15px; }

@media (max-width: 760px) {
  #topbar { height: calc(56px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 10px 0; gap: 8px; }
  .tools { display: none; }
  .brand { flex: 1; min-width: 0; }
  .logo { width: 32px; height: 32px; }
  #docTitle { font-size: 16px; padding: 6px; }
  .m-only { display: flex; }
  .top-btn { border: 0; background: var(--accent-soft); width: 42px; height: 42px; border-radius: 12px; align-items: center; justify-content: center; font-size: 20px; cursor: pointer; }
  #stage, #stage[style] { top: calc(56px + env(safe-area-inset-top)) !important; bottom: calc(64px + env(safe-area-inset-bottom)); }

  #mobilebar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; height: calc(64px + env(safe-area-inset-bottom)); padding: 4px 4px env(safe-area-inset-bottom); background: var(--ui-bg); border-top: 2px solid var(--ui-line); box-shadow: 0 -4px 14px rgba(0, 0, 0, .06); }
  #mobilebar button { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; border: 0; background: transparent; border-radius: 12px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
  #mobilebar button i { font-size: 22px; line-height: 1; flex: none; }
  #mobilebar button span { font-size: 10.5px; line-height: 1.15; font-weight: 700; color: var(--ui-muted); text-align: center; max-width: 100%; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow-wrap: anywhere; }
  #mobilebar button:active { background: var(--accent-soft); }
  #mobilebar button.hot i { background: #40c057; color: #fff; border-radius: 50%; width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; font-size: 18px; }
  #mobilebar button:disabled { opacity: .35; }

  #hint, #status { display: none; }
  #zoombar { right: 10px; bottom: calc(74px + env(safe-area-inset-bottom)); padding: 4px; }
  #zoombar button { height: 34px; min-width: 34px; padding: 0 8px; }
  #zoombar button[data-cmd="fit"] span { display: none; }
  #zoomLabel { min-width: 44px; font-size: 13px; }
  #toast { top: calc(66px + env(safe-area-inset-top)); max-width: calc(100% - 24px); white-space: normal; text-align: center; border-radius: 16px; }

  /* bảng chọn trượt lên từ dưới */
  #popover.sheet { left: 0 !important; right: 0; top: auto !important; bottom: 0; max-width: none; width: 100%; max-height: 72vh; border-radius: 22px 22px 0 0; padding: 10px 16px calc(16px + env(safe-area-inset-bottom)); animation: sheet-up .2s ease-out; }
  #popover.sheet::before { content: ""; display: block; width: 44px; height: 5px; border-radius: 3px; background: #d9d4e8; margin: 0 auto 10px; }
  @keyframes sheet-up { from { transform: translateY(40%); opacity: .4; } }
  #popover.sheet .swatches { grid-template-columns: repeat(auto-fill, 38px); justify-content: space-between; }
  #popover.sheet .swatch { width: 38px; height: 38px; }
  #popover.sheet .emoji-grid { grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); }
  #popover.sheet .menu { min-width: 0; }
  #popover.sheet .menu button { padding: 12px; font-size: 15.5px; }
  #popover.sheet .menu kbd { display: none; }
  #popover.sheet .lang-list { min-width: 0; }

  /* hộp thoại toàn màn hình */
  #modal { padding: 0; align-items: flex-end; }
  .modal-card, .modal-card.small { width: 100%; max-height: calc(100% - 24px - env(safe-area-inset-top)); border-radius: 22px 22px 0 0; padding-bottom: env(safe-area-inset-bottom); }
  .m-head { padding: 14px 16px 6px; }
  .m-head h2 { font-size: 18px; }
  .m-head .btn { padding: 8px 10px; font-size: 13px; }
  .m-body { padding: 6px 16px 16px; }
  .tpl-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .tpl { padding: 12px 8px; border-radius: 16px; }
  .tpl i { font-size: 34px; }
  .tpl b { font-size: 14px; }
  .hist-list { grid-template-columns: 1fr 1fr; gap: 8px; }
  .hist-item img, .hist-item .noimg { height: 90px; }
  .hist-item .acts .btn { font-size: 12px; padding: 6px; }
  .help-grid { grid-template-columns: 1fr; }
  .desktop-only { display: none !important; }
  .tips { grid-template-columns: 1fr; }
}
