:root {
  --paper: #f7f5ef;
  --card: #ffffff;
  --ink: #10131d;
  --ink-2: #2a2f3f;
  --muted: #6b6f7b;
  --line: #e7e3d8;
  --soft: #f1eee6;
  --lime: #8CC63F;
  --lime-fresh: #c9ff47;
  --radius: 16px;
  --top-h: 64px;
  --shadow: 0 10px 30px rgba(16, 19, 29, .08);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper); color: var(--ink);
  font: 15px/1.45 "Nunito", "Noto Sans Thai", system-ui, sans-serif;
  overflow: hidden; -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg .fill { fill: currentColor; stroke: none; }

/* ============ header ============ */
.top {
  height: var(--top-h); display: flex; align-items: center; gap: 14px; padding: 0 18px;
  background: var(--paper); border-bottom: 1px solid var(--line); position: relative; z-index: 6;
}
.brand { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.brand img { width: 36px; height: 36px; }
.brand span { display: flex; flex-direction: column; line-height: 1.05; }
.brand small { font-size: 10px; font-weight: 800; letter-spacing: .14em; color: var(--muted); }
.brand b { font-size: 18px; font-weight: 900; }
.charname {
  margin: 0 auto; display: flex; align-items: center; gap: 6px; max-width: 320px; min-width: 0; flex: 1;
  padding: 0 12px; height: 42px; border-radius: 12px; border: 1px solid transparent; cursor: text;
  justify-content: center;
}
.charname:hover { border-color: var(--line); background: var(--card); }
.charname:focus-within { border-color: var(--lime); background: var(--card); box-shadow: 0 0 0 3px rgba(140, 198, 63, .18); }
.charname input {
  border: 0; background: none; font: 800 18px "Nunito", "Noto Sans Thai", sans-serif; text-align: center;
  min-width: 0; width: 100%; outline: none; color: var(--ink);
}
.charname svg { width: 16px; height: 16px; color: var(--muted); }
.actions { display: flex; gap: 8px; position: relative; }
.btn {
  height: 40px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--card);
  font-weight: 800; display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
}
.btn:hover { border-color: var(--ink); }
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.primary:hover { background: var(--ink-2); }
.btn.primary svg { color: var(--lime-fresh); stroke-width: 2.6; }
.btn.only { width: 40px; padding: 0; justify-content: center; }
.menu {
  position: absolute; right: 0; top: 48px; background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: 0 16px 40px rgba(16, 19, 29, .14); padding: 6px; min-width: 260px; z-index: 20;
}
.menu button, .menu-link {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 12px;
  border-radius: 9px; font-weight: 700; text-decoration: none; color: inherit;
}
.menu-link { color: var(--muted); font-size: 14px; }
.menu button:hover, .menu-link:hover { background: var(--soft); }

/* account */
.account { position: relative; display: flex; align-items: center; }
.gbtn { min-height: 40px; display: flex; align-items: center; }
.userchip {
  width: 40px; height: 40px; border-radius: 50%; padding: 0; border: 2px solid var(--lime); background: var(--ink);
  color: #fff; font-weight: 900; overflow: hidden; display: flex; align-items: center; justify-content: center;
}
.userchip img { width: 100%; height: 100%; object-fit: cover; }
.userchip img:not([src]), .userchip img[src=""] { display: none; }
.guestchip { height: 32px; padding: 0 12px; border-radius: 999px; border: 1px dashed #c9c4b6; background: none; font-weight: 800; font-size: 13px; color: var(--muted); }
.acc-info { padding: 8px 12px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; display: flex; flex-direction: column; }
.acc-info span { color: var(--muted); font-size: 13px; word-break: break-all; }

/* ============ layout ============ */
.layout { display: flex; height: calc(100% - var(--top-h)); }
.stage {
  position: relative; flex: 1; min-width: 0;
  background: radial-gradient(120% 90% at 50% 35%, #ffffff 0%, #eef2f4 55%, #e3e8ec 100%);
}
.stage canvas { display: block; width: 100%; height: 100%; touch-action: none; outline: none; }
.panel {
  width: 468px; flex: none; display: flex; background: var(--card); border-left: 1px solid var(--line); min-height: 0;
}

/* loading + toast */
.loading {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; color: var(--muted); font-weight: 700; pointer-events: none; transition: opacity .35s; z-index: 3;
}
.loading.done { opacity: 0; }
.spin { width: 34px; height: 34px; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--lime); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: absolute; left: 50%; bottom: 22px; transform: translateX(-50%); background: var(--ink); color: #fff; z-index: 8;
  padding: 11px 18px; border-radius: 999px; font-weight: 700; box-shadow: 0 10px 24px rgba(0, 0, 0, .18);
  max-width: calc(100% - 32px); text-align: center; animation: pop .25s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ============ animation bar (top of the stage) ============ */
.animbar {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); z-index: 4;
  display: flex; align-items: center; gap: 8px; max-width: calc(100% - 24px);
}
.seg {
  display: flex; padding: 4px; gap: 2px; background: rgba(255, 255, 255, .9); border: 1px solid var(--line);
  border-radius: 999px; box-shadow: var(--shadow); backdrop-filter: blur(8px);
}
.seg button { border: 0; background: none; height: 34px; padding: 0 14px; border-radius: 999px; font-weight: 800; font-size: 14px; white-space: nowrap; }
.seg button:hover { background: var(--soft); }
.seg button.on { background: var(--ink); color: #fff; }
.movesbtn, .play {
  height: 42px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line); background: rgba(255, 255, 255, .9);
  font-weight: 800; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; box-shadow: var(--shadow);
}
.movesbtn svg { width: 16px; height: 16px; }
.movesbtn.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.play { background: var(--lime-fresh); border-color: #b6e83c; }
.play:hover { background: var(--lime); }
.moves {
  position: absolute; left: 50%; top: 66px; transform: translateX(-50%); z-index: 9; width: min(420px, calc(100% - 24px));
  background: var(--card); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 20px 50px rgba(16, 19, 29, .16);
  padding: 12px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; animation: pop2 .18s ease-out;
}
@keyframes pop2 { from { opacity: 0; transform: translate(-50%, -6px); } }
.moves h4 { grid-column: 1 / -1; margin: 4px 4px 0; font-size: 12px; color: var(--muted); font-weight: 800; letter-spacing: .04em; }
.moves button {
  border: 1px solid var(--line); background: var(--paper); border-radius: 14px; padding: 10px 6px; font-weight: 800; font-size: 13px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.moves button b { font-size: 24px; line-height: 1.1; }
.moves button:hover { border-color: var(--ink); background: var(--card); }
.moves button.on { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ============ play mode HUD ============ */
.playhud { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
.playhud > * { pointer-events: auto; }
.playhud #btnExit { position: absolute; left: 16px; top: 16px; box-shadow: var(--shadow); }
.playhud .movesplay { position: absolute; right: 16px; top: 16px; box-shadow: var(--shadow); }
.playhud .hint {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); background: rgba(16, 19, 29, .78); color: #fff;
  padding: 8px 16px; border-radius: 999px; font-weight: 700; font-size: 13px; white-space: nowrap;
}
body.playing .moves { top: 64px; left: auto; right: 16px; transform: none; }
.joy, .jumpbtn { display: none; }
@media (pointer: coarse) {
  .playhud .hint { display: none; }
  .joy {
    display: block; position: absolute; left: 24px; bottom: 28px; width: 130px; height: 130px; border-radius: 50%;
    background: rgba(16, 19, 29, .12); border: 2px solid rgba(16, 19, 29, .16); touch-action: none;
  }
  .knob { position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px; border-radius: 50%; background: var(--ink); opacity: .85; }
  .jumpbtn {
    display: block; position: absolute; right: 24px; bottom: 44px; width: 88px; height: 88px; border-radius: 50%;
    border: 0; background: var(--lime-fresh); font-weight: 900; box-shadow: 0 6px 16px rgba(0, 0, 0, .15); touch-action: none;
  }
}
body.playing .panel, body.playing .animbar { display: none; }

/* ============ panel: category rail + drawer ============ */
.rail {
  width: 82px; flex: none; overflow-y: auto; padding: 10px 8px 16px; background: var(--paper);
  border-right: 1px solid var(--line); scrollbar-width: none;
}
.rail::-webkit-scrollbar { display: none; }
.rail h5 { margin: 12px 0 6px; font-size: 10px; font-weight: 800; letter-spacing: .08em; color: #9a9da6; text-align: center; }
.rail h5:first-child { margin-top: 2px; }
.rail button {
  position: relative; width: 100%; border: 0; background: none; border-radius: 14px; padding: 8px 2px 7px;
  display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 12px; font-weight: 800; color: var(--ink-2);
}
.rail button svg { width: 24px; height: 24px; stroke-width: 1.7; }
.rail button:hover { background: var(--soft); }
.rail button.on { background: var(--ink); color: #fff; }
.rail button .dot { position: absolute; top: 7px; right: 14px; width: 7px; height: 7px; border-radius: 50%; background: var(--lime); }
.drawer { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 18px 18px 6px; }
.drawer-head h2 { margin: 0; font-size: 22px; font-weight: 900; line-height: 1.15; }
.drawer-head span { color: var(--muted); font-size: 13px; font-weight: 600; }
.nonebtn {
  height: 34px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--card);
  font-weight: 800; font-size: 13px; color: var(--muted);
}
.nonebtn:hover { border-color: var(--ink); color: var(--ink); }
.nonebtn.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.filters { display: flex; gap: 6px; padding: 8px 18px 4px; overflow-x: auto; scrollbar-width: none; flex: none; }
.filters::-webkit-scrollbar { display: none; }
.filters button {
  flex: none; height: 32px; padding: 0 13px; border-radius: 999px; border: 1px solid var(--line); background: var(--card);
  font-weight: 800; font-size: 13px; color: var(--ink-2);
}
.filters button:hover { border-color: var(--ink); }
.filters button.on { background: var(--soft); border-color: var(--ink); color: var(--ink); }
.colorbar { display: flex; align-items: center; gap: 10px; margin: 8px 18px 2px; padding: 8px 10px; background: var(--paper); border-radius: 14px; flex: none; }
.colorbar > span { font-size: 12px; font-weight: 800; color: var(--muted); }
.colors { display: flex; flex-wrap: wrap; gap: 8px; }
.sw {
  width: 30px; height: 30px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .12); padding: 0;
}
.sw:hover { transform: scale(1.08); }
.sw.on { box-shadow: 0 0 0 2px var(--ink), 0 0 0 5px var(--lime-fresh); }
.items { flex: 1; overflow-y: auto; padding: 10px 18px 28px; min-height: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(86px, 1fr)); gap: 10px; }
.tile {
  position: relative; aspect-ratio: 1; border: 2px solid transparent; border-radius: var(--radius); background: var(--paper);
  padding: 0; overflow: hidden; transition: transform .12s, border-color .12s;
}
.tile:hover { border-color: #d5d1c4; transform: translateY(-1px); }
.tile.on { border-color: var(--ink); background: #fff; box-shadow: 0 0 0 3px var(--lime-fresh); }
.tile .th { position: absolute; inset: 4px; background-repeat: no-repeat; }
.tile .ph { position: absolute; inset: 30%; border-radius: 50%; }
.tile.busy::after {
  content: ""; position: absolute; right: 6px; top: 6px; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--lime); animation: spin .7s linear infinite;
}
.skin-rows { display: flex; flex-direction: column; gap: 14px; }
.skin-row h6 { margin: 0 0 8px; font-size: 13px; font-weight: 800; color: var(--muted); }
.skin-row .colors { gap: 8px; flex-wrap: nowrap; }
.skin-row .sw { width: 34px; height: 34px; flex: none; }

/* ============ first screen ============ */
.intro-ui { display: none; }
body.intro .top, body.intro .panel, body.intro .animbar, body.intro .moves { display: none !important; }
body.intro .layout { height: 100%; }
body.intro .intro-ui {
  display: flex; position: fixed; inset: 0; z-index: 10; align-items: center; padding: 0 7vw; pointer-events: none;
}
body.intro .stage { background: radial-gradient(70% 90% at 72% 45%, #ffffff 0%, #eef2f4 55%, #dfe5e9 100%); }
.intro-card {
  pointer-events: auto; width: min(430px, 100%); background: rgba(255, 255, 255, .94); backdrop-filter: blur(10px);
  border: 1px solid var(--line); border-radius: 26px; box-shadow: 0 30px 70px rgba(16, 19, 29, .12); padding: 36px 34px 30px;
  animation: rise .45s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }
.eyebrow { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 900; letter-spacing: .16em; color: var(--muted); }
.eyebrow span { width: 9px; height: 9px; border-radius: 50%; background: var(--lime); box-shadow: 0 0 0 4px rgba(140, 198, 63, .2); }
.intro-card h1 { margin: 10px 0 8px; font-size: 44px; line-height: 1.02; font-weight: 900; letter-spacing: -.02em; }
.intro-card h2 { margin: 14px 0 6px; font-size: 30px; font-weight: 900; }
.lead { margin: 0 0 24px; color: var(--muted); font-size: 15.5px; }
.glogin { min-height: 44px; display: flex; justify-content: center; }
.or { display: flex; align-items: center; gap: 12px; color: #a3a6ae; font-size: 13px; font-weight: 700; margin: 18px 0; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.guestbtn {
  width: 100%; height: 46px; border-radius: 999px; border: 1.5px solid var(--ink); background: none; font-weight: 800; font-size: 15px;
}
.guestbtn:hover { background: var(--ink); color: #fff; }
.note { margin: 12px 0 0; text-align: center; font-size: 12.5px; color: #9a9da6; }
.back { border: 0; background: none; padding: 0; display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-weight: 800; font-size: 14px; }
.back svg { width: 18px; height: 18px; }
.back:hover { color: var(--ink); }
.bigin {
  width: 100%; height: 56px; border-radius: 16px; border: 1.5px solid var(--line); background: var(--paper); padding: 0 18px;
  font: 800 20px "Nunito", "Noto Sans Thai", sans-serif; color: var(--ink); outline: none;
}
.bigin:focus { border-color: var(--lime); background: #fff; box-shadow: 0 0 0 4px rgba(140, 198, 63, .18); }
.bigin.shake { animation: shake .35s; border-color: #e5484d; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.createbtn {
  margin-top: 14px; width: 100%; height: 54px; border-radius: 999px; border: 0; background: var(--ink); color: #fff;
  font-weight: 900; font-size: 17px; display: flex; align-items: center; justify-content: center; gap: 10px;
}
.createbtn svg { color: var(--lime-fresh); stroke-width: 2.6; }
.createbtn:hover { background: var(--ink-2); }
.who { margin-top: 16px; display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
.who img { width: 22px; height: 22px; border-radius: 50%; }
.intro-foot { position: fixed; left: 7vw; bottom: 18px; font-size: 12px; color: #9a9da6; text-decoration: none; pointer-events: auto; }
.intro-foot:hover { color: var(--ink); }

/* ============ small screens ============ */
@media (max-width: 900px) {
  :root { --top-h: 56px; }
  .top { gap: 8px; padding: 0 12px; }
  .brand span { display: none; }
  .brand img { width: 32px; height: 32px; }
  .charname { padding: 0 8px; }
  .charname input { font-size: 16px; }
  .actions .btn span { display: none; }
  .actions .btn { width: 40px; padding: 0; justify-content: center; }
  .layout { flex-direction: column; }
  .stage { flex: none; height: 46%; }
  .panel { width: auto; flex: 1; flex-direction: column; border-left: 0; border-top: 1px solid var(--line); }
  .rail { width: auto; display: flex; gap: 2px; overflow-x: auto; overflow-y: hidden; padding: 6px 8px; border-right: 0; border-bottom: 1px solid var(--line); flex: none; }
  .rail h5 { display: none; }
  .rail button { width: 62px; flex: none; padding: 6px 2px 5px; font-size: 11px; }
  .rail button svg { width: 22px; height: 22px; }
  .drawer-head { padding: 12px 14px 2px; }
  .drawer-head h2 { font-size: 19px; }
  .filters { padding: 6px 14px 2px; }
  .colorbar { margin: 6px 14px 0; }
  .items { padding: 10px 14px 24px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; }
  .animbar { top: 10px; gap: 6px; }
  .seg button { padding: 0 10px; height: 32px; font-size: 13px; }
  .movesbtn, .play { height: 40px; padding: 0 11px; font-size: 13px; }
  .play span, .movesbtn #movesLabel { display: none; }
  .moves { top: 58px; grid-template-columns: repeat(3, 1fr); }
  body.playing .stage, body.intro .stage { height: 100%; }
  body.intro .intro-ui { align-items: flex-end; padding: 0 12px 12px; }
  .intro-card { padding: 26px 22px 22px; border-radius: 24px; }
  .intro-card h1 { font-size: 36px; }
  .lead { margin-bottom: 18px; font-size: 14.5px; }
  .intro-foot { display: none; }
}
@media (max-width: 420px) {
  .seg button { padding: 0 8px; }
}
