:root {
  --bg: #0f1118; --panel: #181b26; --panel2: #1f2332; --line: #2c3247; --text: #e8eaf0; --muted: #8a90a2;
  --gold: #f2c66d; --gold2: #d9a441; --red: #e5636a; --blue: #5aa9ff; --green: #5fd18b; --auto: #5aa9ff; --manual: #f2a65a;
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: radial-gradient(1200px 600px at 20% -10%, #1d2340 0%, transparent 60%), var(--bg); color: var(--text); min-height: 100vh; font-size: 15px; }
a { color: var(--gold); text-decoration: none; }
a:hover { text-decoration: underline; }
h2 { font-size: 20px; margin: 18px 0 12px; }
input, select, textarea { background: var(--panel2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 7px 10px; font: inherit; min-width: 0; }
input:focus, select:focus { outline: 2px solid var(--gold2); outline-offset: -1px; }
input[type=color] { padding: 2px; width: 44px; height: 34px; }
input.num { width: 80px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.inline { flex-direction: row; align-items: center; gap: 8px; }
label input, label select { color: var(--text); font-size: 15px; }

.btn { background: var(--panel2); border: 1px solid var(--line); color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; font: inherit; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.btn:hover { border-color: var(--gold2); text-decoration: none; }
.btn.primary { background: linear-gradient(180deg, var(--gold), var(--gold2)); color: #1a1408; border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.danger:not(.ghost) { background: var(--red); color: white; border-color: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: wait; }

#header { display: flex; align-items: center; gap: 20px; padding: 10px 20px; background: rgba(15,17,24,.85); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; color: var(--gold); }
.logo { font-size: 22px; }
nav { display: flex; gap: 4px; }
.nav { padding: 7px 14px; border-radius: 8px; color: var(--muted); }
.nav:hover { color: var(--text); text-decoration: none; background: var(--panel2); }
.nav.active { color: #1a1408; background: var(--gold); font-weight: 600; }
.hdr-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.lang { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.lang button { background: transparent; border: 0; color: var(--muted); padding: 5px 10px; cursor: pointer; font: inherit; font-size: 13px; }
.lang button.on { background: var(--panel2); color: var(--gold); font-weight: 700; }
.status { font-size: 13px; color: var(--muted); }
.status.saved { color: var(--green); } .status.error { color: var(--red); }
.banner { background: #3a2f12; color: var(--gold); padding: 6px 20px; font-size: 13px; text-align: center; }
.banner.soft { background: var(--panel); color: var(--muted); }

#main { padding: 20px; max-width: 1400px; margin: 0 auto; }
.page { max-width: 1100px; margin: 0 auto; }
.page.wide { max-width: 1400px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; } .row.between { justify-content: space-between; } .row.end { justify-content: flex-end; } .row.tight { gap: 4px; }
.grow { flex: 1; }
.muted { color: var(--muted); } .small { font-size: 13px; } .nowrap { white-space: nowrap; }
.notice { background: var(--panel); border: 1px dashed var(--line); padding: 14px 16px; border-radius: 10px; margin: 12px 0; color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.dot.big { width: 16px; height: 16px; }
.cls-title { display: flex; align-items: center; gap: 10px; }
.side-title { font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 14px 0 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--panel2); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px 3px 4px; font-size: 13px; margin: 0 6px 6px 0; }
.btnchip { cursor: pointer; color: var(--text); font: inherit; font-size: 13px; }
.btnchip.on { border-color: var(--gold); background: #2b2614; }
.btnchip.auto { border-left: 4px solid var(--auto); } .btnchip.manual { border-left: 4px solid var(--manual); }
.back { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }

/* presets list */
.layout { display: grid; grid-template-columns: 230px 1fr; gap: 20px; }
.side { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px 8px 10px; align-self: start; position: sticky; top: 70px; }
.cls-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text); }
.cls-item:hover { background: var(--panel2); text-decoration: none; }
.cls-item.active { background: var(--panel2); box-shadow: inset 3px 0 0 var(--gold); }
.count { font-size: 12px; color: var(--muted); background: var(--bg); border-radius: 999px; padding: 1px 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-top: 3px solid var(--c); border-radius: 12px; padding: 12px 14px; color: var(--text); display: flex; flex-direction: column; gap: 8px; }
.card:hover { border-color: var(--gold2); text-decoration: none; transform: translateY(-1px); }
.card-top { display: flex; justify-content: space-between; gap: 8px; }
.card-note { color: var(--muted); font-size: 13px; }
.pts { font-size: 13px; color: var(--gold); font-weight: 600; }
.pts.big { font-size: 15px; background: var(--panel2); padding: 5px 12px; border-radius: 8px; border: 1px solid var(--line); }
.pts.full { color: var(--green); }
.icons { display: flex; flex-wrap: wrap; gap: 4px; }

/* skill icons */
.sicon { flex: none; border-radius: 6px; background-repeat: no-repeat; background-color: #0003; }
.sicon.text { display: grid; place-items: center; font-size: 11px; color: var(--muted); background: var(--panel2); }

/* preset editor */
.preset-head { display: flex; flex-direction: column; gap: 12px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 2fr; gap: 12px; }
.tabs { display: flex; align-items: center; gap: 6px; border-bottom: 1px solid var(--line); margin-bottom: 14px; padding-bottom: 8px; flex-wrap: wrap; }
.tabs > button { background: transparent; border: 0; color: var(--muted); padding: 8px 14px; cursor: pointer; font: inherit; border-radius: 8px; }
.tabs > button.on { color: var(--text); background: var(--panel2); box-shadow: inset 0 -2px 0 var(--gold); }
.tools { margin-bottom: 10px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { background: transparent; border: 0; color: var(--muted); padding: 6px 14px; cursor: pointer; font: inherit; }
.seg button.on { background: var(--gold); color: #1a1408; font-weight: 600; }
.tree-layout { display: grid; grid-template-columns: 1fr 260px; gap: 18px; align-items: start; }
.learned { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px 12px 12px; position: sticky; top: 70px; }
.learned .chip { display: flex; margin-right: 0; border-radius: 8px; }

.imgwrap { position: relative; display: inline-block; width: fit-content; justify-self: start; align-self: start; max-width: 100%; line-height: 0; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.rect-ed, .slot, .pal-item, .handle { touch-action: none; }
.imgwrap img { max-width: 100%; display: block; }
.hs { position: absolute; border-radius: 6px; cursor: default; background: rgba(0,0,0,.45); transition: background .12s; }
.imgwrap.editable .hs { cursor: pointer; }
.imgwrap.editable .hs:hover { outline: 2px solid var(--gold); }
.hs.on { background: transparent; box-shadow: 0 0 0 2px var(--gold), 0 0 10px rgba(242,198,109,.6); }
.hs.max { box-shadow: 0 0 0 2px var(--green), 0 0 10px rgba(95,209,139,.6); }
.hs.locked:not(.on) { background: rgba(0,0,0,.7); }
.hs { container-type: size; }
.hs .lv { position: absolute; left: 50%; top: 0; transform: translateX(-50%); min-width: 56%; height: 21%; display: grid; place-items: center; background: #3b4560; color: #cfd6e6; font-size: max(10px, 13cqh); line-height: 1; padding: 0 4px; border-radius: 4px; font-weight: 700; pointer-events: none; white-space: nowrap; }
.imgwrap:not(.tree) .hs .lv { top: auto; bottom: -6px; left: auto; right: -4px; transform: none; min-width: 0; height: auto; padding: 2px 4px; font-size: 11px; background: #000d; }
.hs.on .lv { background: #6a531c; }
.hs.max .lv { background: #1f5a3a; }
.hs.on .lv { color: #ffe3a3; } .hs.max .lv { color: #b8f5cf; }

/* auto/manual */
.auto-layout { display: grid; grid-template-columns: 1fr 300px; gap: 18px; align-items: start; }
.auto-summary { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.sum-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 6px; margin-right: 6px; }
.tag.auto { background: var(--auto); color: #071526; } .tag.manual { background: var(--manual); color: #261507; }
.slot { position: absolute; border-radius: 8px; border: 2px dashed rgba(255,255,255,.25); display: flex; align-items: center; justify-content: center; overflow: hidden; }
.slot.auto { border-color: rgba(90,169,255,.55); } .slot.manual { border-color: rgba(242,166,90,.55); }
.slot.filled { border-style: solid; }
.slot.sel-target { background: rgba(242,198,109,.15); }
.slot .sicon.fill { width: 100% !important; height: 100% !important; border-radius: 6px; }
.slot.drop-hover, .pslot.drop-hover { outline: 3px solid var(--gold); background: rgba(242,198,109,.2); }
.palette { display: flex; flex-wrap: wrap; gap: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.pal-item { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 78px; cursor: grab; touch-action: none; padding: 6px 2px; border-radius: 8px; }
.pal-item small { font-size: 11px; text-align: center; color: var(--muted); line-height: 1.2; }
.pal-item.placed { opacity: .45; }
.pal-item.sel { background: #2b2614; outline: 2px solid var(--gold); }
.drag-ghost { position: fixed; pointer-events: none; z-index: 100; transform: translate(-50%, -50%) scale(1.05); filter: drop-shadow(0 6px 14px #000a); }
.ghost-card { background: var(--panel2); border: 1px solid var(--gold); padding: 6px 12px; border-radius: 8px; }

/* party */
.toolbar { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.room { margin-top: 10px; }
.room-sum { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.room-sum .chip { padding-left: 8px; margin: 0; }
.party-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.party { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px; display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.party-head { display: flex; align-items: center; gap: 8px; padding: 2px 2px 4px; }
.pnum { background: var(--gold); color: #1a1408; font-weight: 700; border-radius: 6px; width: 24px; height: 24px; display: grid; place-items: center; font-size: 13px; flex: none; }
.pname { flex: 1; padding: 4px 8px; font-weight: 600; }
.pslot { display: flex; gap: 6px; align-items: stretch; background: var(--panel2); border-radius: 8px; padding: 5px 6px 5px 0; border-left: 4px solid var(--c); min-height: 52px; }
.pslot:not(.filled) { border-left-color: var(--line); }
.handle { cursor: grab; color: var(--muted); display: grid; place-items: center; padding: 0 2px 0 4px; touch-action: none; user-select: none; }
.pfields { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 0; padding-left: 4px; justify-content: center; }
.pfields input, .pfields select { padding: 3px 6px; font-size: 13px; width: 100%; }
.pfields .row select { flex: 1; }
.cls-tag { color: #111; font-size: 11px; font-weight: 700; padding: 2px 6px; border-radius: 5px; white-space: nowrap; flex: none; }

/* settings */
.tbl { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 12px; overflow: hidden; }
.tbl th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 10px; background: var(--panel2); }
.tbl td { padding: 6px 10px; border-top: 1px solid var(--line); }
.tbl td input:not([type=color]):not(.num) { width: 100%; }
.page > .row { margin-top: 10px; }
.ed-layout { display: grid; grid-template-columns: 1fr 300px; gap: 18px; align-items: start; margin-top: 14px; }
.ed-panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 10px; position: sticky; top: 70px; }
.rect-ed { cursor: crosshair; }
.rect { position: absolute; border: 2px solid rgba(242,198,109,.8); background: rgba(242,198,109,.12); border-radius: 4px; cursor: move; }
.rect.slotrect.auto { border-color: var(--auto); background: rgba(90,169,255,.15); }
.rect.slotrect.manual { border-color: var(--manual); background: rgba(242,166,90,.15); }
.rect.sel { border-color: #fff; box-shadow: 0 0 0 2px var(--gold); z-index: 2; }
.rect.temp { border-style: dashed; pointer-events: none; }
.rect .rl { position: absolute; left: 0; top: 100%; font-size: 10px; line-height: 1.2; background: #000c; color: #fff; padding: 1px 4px; white-space: nowrap; pointer-events: none; border-radius: 0 0 4px 4px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; }
.rect .rh { position: absolute; right: -5px; bottom: -5px; width: 12px; height: 12px; background: #fff; border-radius: 3px; cursor: nwse-resize; }
.hs-list { display: flex; flex-wrap: wrap; max-height: 300px; overflow: auto; }

/* modal & toast */
.modal-bg { position: fixed; inset: 0; background: #0009; display: grid; place-items: center; z-index: 50; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; min-width: 320px; display: flex; flex-direction: column; gap: 12px; }
.modal h3 { margin: 0; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; background: var(--panel2); border: 1px solid var(--line); padding: 10px 18px; border-radius: 10px; z-index: 60; transition: .25s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { border-color: var(--red); color: #ffb3b7; }

@media (max-width: 1100px) { .party-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 860px) {
  .layout, .tree-layout, .auto-layout, .ed-layout { grid-template-columns: 1fr; }
  .side, .learned, .ed-panel { position: static; }
  .grid2 { grid-template-columns: 1fr; }
  #main { padding: 12px; }
}
@media (max-width: 560px) { .party-grid { grid-template-columns: 1fr; } }

/* skill tabs */
.sktabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 12px; }
.sktabs > button, .tabbtn { display: inline-flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--line); color: var(--muted); padding: 7px 12px; border-radius: 8px; cursor: pointer; font: inherit; font-size: 14px; }
.tabbtn:hover { text-decoration: none; color: var(--text); }
.sktabs > button.on, .tabbtn.on { color: var(--text); border-color: var(--gold); background: #2b2614; }
.tabbtn.add { border-style: dashed; }
.sktabs .tp { font-size: 12px; font-weight: 700; color: var(--gold); background: var(--bg); padding: 1px 7px; border-radius: 999px; }
.sktabs button.full .tp { color: var(--green); }
.lgroup { font-size: 12px; color: var(--gold); margin: 10px 0 4px; }
.tabfields { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; align-items: flex-end; }

/* +/- skill buttons (like the in-game skill window) */
.hs .pm { position: absolute; top: 48%; transform: translateY(-50%); width: 24cqw; height: 24cqw; min-width: 22px; min-height: 22px; border-radius: 22%; border: 2px solid #fff; background: linear-gradient(180deg, #6f9cf0, #4a74d6); color: #fff; font: 700 max(14px, 16cqw)/1 system-ui, sans-serif; display: grid; place-items: center; cursor: pointer; padding: 0; box-shadow: 0 2px 6px #0004; z-index: 2; }
.hs .pm.minus { left: -2cqw; } .hs .pm.plus { right: -2cqw; }
.hs .pm:hover:not(:disabled) { filter: brightness(1.15); }
.hs .pm:active:not(:disabled) { transform: translateY(-50%) scale(.92); }
.hs .pm:disabled { opacity: .3; cursor: default; filter: grayscale(1); }
.hs .pm.blocked { opacity: .45; filter: grayscale(.8); }
.imgwrap.editable .hs { cursor: default; }
.imgwrap.editable .hs:hover { outline: none; }
/* party slot: class then preset */
.pfields .row select.csel { flex: 0 0 44%; border-left: 4px solid var(--c); }
.pfields .row select.psel:disabled { opacity: .5; }
.mode-tag { font-size: 12px; font-weight: 700; padding: 3px 10px; border-radius: 999px; }
.mode-tag.admin { background: #2b2614; color: var(--gold); border: 1px solid var(--gold2); }
.mode-tag.viewer { background: var(--panel2); color: var(--muted); border: 1px solid var(--line); }

.sktabs button.over .tp { color: #ffb27a; }

.sum-row.dim { opacity: .55; }

.imgwrap.bar .slot { border-radius: 50%; }
.imgwrap.bar .slot .sicon.fill { border-radius: 50%; }

/* party: view a character's setup */
.pslot .eye { align-self: center; background: transparent; border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px; cursor: pointer; font-size: 14px; color: var(--text); }
.pslot .eye:hover { border-color: var(--gold); }
.pslot.clickable { cursor: pointer; padding-right: 6px; }
.pslot.clickable:hover { background: #2a3048; }
.pname-link { color: var(--gold); }
.setup-modal { width: min(720px, 94vw); max-height: 88vh; overflow: auto; }
.setup h3 { margin: 0 0 4px; font-size: 20px; }
.setup-sec { border-top: 1px solid var(--line); padding-top: 10px; }
.bar-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.bar-item { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 72px; }
.bar-item .sicon { border-radius: 50%; }
.bar-item small { font-size: 11px; text-align: center; color: var(--muted); line-height: 1.2; }
.chips { display: flex; flex-wrap: wrap; }
.notice.small { font-size: 13px; padding: 8px 12px; margin: 0 0 10px; }

/* ---------- tablet & phone ---------- */
.brand { text-decoration: none !important; }
.swipe-hint { display: none; }
button, a, select, input { -webkit-tap-highlight-color: transparent; }
@media (max-width: 1024px) {
  .party-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .layout { grid-template-columns: 190px 1fr; }
}
@media (max-width: 860px) {
  .tree-layout .learned { padding-bottom: 8px; }
  .learned .chip { display: inline-flex; margin: 0 6px 6px 0; }
  .lgroup { width: 100%; }
}
@media (max-width: 700px) {
  body { font-size: 15px; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  /* no backdrop-filter here: it would trap the fixed bottom nav inside the header */
  #header { padding: 8px 12px; gap: 8px; flex-wrap: nowrap; padding-top: max(8px, env(safe-area-inset-top)); backdrop-filter: none; background: var(--bg); }
  .brand { font-size: 15px; gap: 6px; white-space: nowrap; }
  .brand .logo { font-size: 18px; }
  .hdr-right { gap: 6px; }
  .hdr-right .status, .hdr-right .mode-tag { display: none; }
  .hdr-right .btn.small { padding: 6px 9px; }
  /* main navigation becomes a bottom tab bar */
  nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 0;
        background: rgba(15,17,24,.96); backdrop-filter: blur(8px); border-top: 1px solid var(--line); padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
  .nav { text-align: center; padding: 10px 4px; font-size: 14px; }
  .banner { font-size: 12px; padding: 5px 12px; }
  #main { padding: 12px; }
  h2 { font-size: 18px; }
  /* class list becomes a swipeable row */
  .layout { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .side { display: flex; gap: 6px; overflow-x: auto; padding: 6px; position: static; scrollbar-width: none; }
  .side::-webkit-scrollbar { display: none; }
  .side .side-title { display: none; }
  .cls-item { flex: none; padding: 8px 12px; border: 1px solid var(--line); border-radius: 999px; }
  .cls-item.active { box-shadow: none; border-color: var(--gold); }
  .cards { grid-template-columns: minmax(0, 1fr); }
  /* skill tree: keep it big enough to tap, swipe sideways */
  .tree-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -12px; padding: 0 12px; }
  .imgwrap.tree { max-width: none; }
  .imgwrap.tree img { width: 620px; max-width: none; }
  .swipe-hint { display: block; text-align: center; margin-top: 4px; }
  .sktabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .sktabs > button, .tabbtn { flex: none; font-size: 13px; padding: 7px 10px; }
  .tabs > button { padding: 8px 10px; }
  .pts.big { font-size: 13px; }
  .auto-layout, .ed-layout, .tree-layout { grid-template-columns: minmax(0, 1fr); }
  .pal-item { width: 64px; }
  .party-grid { grid-template-columns: minmax(0, 1fr); }
  .toolbar .btn { padding: 7px 10px; }
  .toolbar label.inline { width: 100%; }
  .toolbar label.inline select { flex: 1; }
  .pfields input, .pfields select { font-size: 16px; padding: 6px 8px; } /* 16px stops iOS zooming in */
  input, select, textarea { font-size: 16px; }
  .modal { min-width: 0; width: calc(100vw - 24px); }
  .tbl { display: block; overflow-x: auto; }
}

.auto-layout .palette { margin-top: 4px; }
@media (max-width: 700px) { .imgwrap.bar { display: block; margin: 0 auto; max-width: 360px; } .imgwrap.bar img { width: 100%; } }
