:root {
    --bg: #f5f3ee;
    --panel: #ffffff;
    --ink: #2b2620;
    --muted: #7d7468;
    --line: #e4ded3;
    --accent: #3f7d4e;
    --accent-ink: #ffffff;
    --warn: #b5562f;
    --changed: #fff3c4;
    --changed-line: #e2b93b;
    --hot: #ffd4c2;
    --code: #f0ece4;
    --radius: 8px;
    font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
    font-size: 14px;
    color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
button {
    border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 6px 12px; cursor: pointer;
    min-height: 32px;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--warn); }
button:disabled { opacity: 0.5; cursor: default; }
button.small { padding: 2px 8px; min-height: 24px; font-size: 12px; }
input[type=number], input[type=text], input[type=password], select, textarea {
    border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; background: #fff; min-height: 30px;
}
input[type=number] { width: 96px; }
input[type=range] { width: 140px; vertical-align: middle; }
textarea { width: 100%; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }

.layout { display: grid; grid-template-columns: 200px 1fr; min-height: 100vh; }
.side { background: #2f3b2c; color: #e9efe4; padding: 16px 10px; position: sticky; top: 0; height: 100vh; overflow: auto; }
.side h1 { font-size: 16px; margin: 4px 8px 16px; }
.side .tab { display: block; width: 100%; text-align: left; background: transparent; border: 0; color: inherit; padding: 9px 10px; border-radius: 6px; }
.side .tab.active { background: rgba(255, 255, 255, 0.14); }
.side .tab:hover { background: rgba(255, 255, 255, 0.08); }
.side .foot { margin: 18px 8px 0; font-size: 12px; color: #b8c4b0; line-height: 1.7; }
.main { padding: 0 24px 60px; min-width: 0; }
.topbar {
    position: sticky; top: 0; z-index: 5; background: var(--bg); display: flex; gap: 10px; align-items: center;
    padding: 14px 0 12px; border-bottom: 1px solid var(--line); margin-bottom: 18px; flex-wrap: wrap;
}
.topbar .title { font-size: 18px; font-weight: 600; margin-right: auto; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--code); font-size: 12px; color: var(--muted); }
.pill.on { background: #dcefd9; color: #2d6a3a; }
.pill.warn { background: var(--hot); color: var(--warn); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; min-width: 0; container-type: inline-size; }
.card h2 { font-size: 15px; margin: 0 0 12px; }
.card h3 { font-size: 14px; margin: 16px 0 8px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 16px; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* generic form */
.fields { display: grid; grid-template-columns: minmax(140px, 360px) minmax(0, 1fr); gap: 6px 14px; align-items: center; min-width: 0; max-width: 900px; }
.field-label { line-height: 1.3; overflow-wrap: anywhere; min-width: 0; }
.field-label small { display: block; color: var(--muted); font-size: 12px; }
.field-ctl { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; min-width: 0; max-width: 100%; }
.field-ctl > input, .field-ctl > select, .field-ctl > textarea { max-width: 100%; }
.changed { background: var(--changed); outline: 1px solid var(--changed-line); border-radius: 6px; }
.hot { animation: hot 1.2s ease-out; }
@keyframes hot { from { background: var(--hot); } to { background: transparent; } }
.arr { display: inline-flex; gap: 4px; flex-wrap: wrap; min-width: 0; max-width: 100%; }
.arr-item { display: flex; flex-direction: column; gap: 3px; color: var(--muted); font-size: 12px; }
.arr input { width: 64px; }
.table2d { border-collapse: collapse; }
.table2d td, .table2d th { padding: 2px 4px; font-weight: normal; color: var(--muted); font-size: 12px; }
.table2d input { width: 64px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); margin: 10px 0; padding: 10px 12px 12px; min-inline-size: 0; }
legend { padding: 0 6px; font-weight: 600; }
.reset { border: 0; background: transparent; color: var(--muted); padding: 0 4px; min-height: 0; }

/* data tables */
.data { border-collapse: collapse; width: 100%; font-size: 13px; }
.data th, .data td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; white-space: nowrap; }
.data th { color: var(--muted); font-weight: 500; background: var(--panel); }
.data td input[type=number] { width: 74px; }
.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
.scroll-x { overflow-x: auto; min-width: 0; max-width: 100%; }
.table-editor { flex: 1 1 100%; }
@container (max-width: 560px) {
    .fields { grid-template-columns: minmax(0, 1fr); gap: 5px; }
    .field-ctl { margin-bottom: 12px; }
}

/* Monster abilities: a compact summary opens an editor with Chinese controls. */
.data .monster-abilities { min-width: 210px; max-width: 300px; white-space: normal; }
.ability-summary { line-height: 1.8; margin-bottom: 4px; }
.ability-summary .pill { margin: 0 4px 3px 0; }
.ability-dialog { width: min(840px, calc(100vw - 24px)); max-height: calc(100dvh - 32px); padding: 0; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: var(--bg); }
.ability-dialog::backdrop { background: #0006; }
.ability-dialog-head, .ability-dialog-foot { display: flex; align-items: center; gap: 12px; padding: 14px 18px; background: var(--panel); position: sticky; z-index: 1; }
.ability-dialog-head { top: 0; border-bottom: 1px solid var(--line); }
.ability-dialog-head h2 { margin: 0; font-size: 18px; flex: 1; }
.ability-dialog-body { padding: 16px; }
.ability-dialog-body > p { margin-top: 0; }
.ability-dialog-foot { bottom: 0; border-top: 1px solid var(--line); justify-content: flex-end; flex-wrap: wrap; }
.ability-dialog .card { margin-bottom: 12px; }
.ability-toggle, .choice { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.ability-toggle { font-weight: 600; }
.ability-fields { margin-top: 12px; }
.ability-fields[hidden] { display: none; }
.monster-choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; width: 100%; }
.bar { height: 8px; background: var(--code); border-radius: 4px; overflow: hidden; min-width: 80px; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.bar.bad > i { background: var(--warn); }
.run-filters { display: flex; align-items: end; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.run-filters label { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; min-width: 0; }
.run-filters select { min-width: 130px; max-width: 100%; font-size: 14px; color: var(--ink); }
.data-state { display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 8px; min-height: 160px; padding: 24px; text-align: center; border: 1px dashed var(--line); border-radius: var(--radius); background: var(--bg); line-height: 1.6; overflow-wrap: anywhere; }

/* level editor */
.lv-layout { display: grid; grid-template-columns: 220px 1fr; gap: 16px; }
.lv-list button { display: block; width: 100%; text-align: left; margin-bottom: 4px; }
.lv-list button.active { border-color: var(--accent); background: #eef6ec; }
.mapgrid { display: inline-grid; gap: 2px; user-select: none; background: var(--line); padding: 2px; border-radius: 6px; }
.mapgrid .cell { width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; font-size: 12px; cursor: pointer; border-radius: 3px; }
.palette { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.palette button { display: flex; gap: 6px; align-items: center; }
.palette button.active { border-color: var(--accent); box-shadow: 0 0 0 2px #cfe5cb; }
.swatch { width: 16px; height: 16px; border-radius: 3px; display: inline-block; }
.wave { border: 1px solid var(--line); border-radius: 6px; padding: 8px; margin-bottom: 8px; }
.wave .row { margin-bottom: 4px; }
.err { color: var(--warn); }
.ok { color: var(--accent); }

/* live */
.feed { font-family: ui-monospace, Consolas, monospace; font-size: 12px; max-height: 260px; overflow: auto; background: var(--code); padding: 8px; border-radius: 6px; }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
.kv div { background: var(--code); border-radius: 6px; padding: 8px; }
.kv b { display: block; font-size: 18px; }

.login { max-width: 360px; margin: 18vh auto; }
.session-dialog { width: min(420px, calc(100vw - 32px)); margin: auto; }
.session-dialog::backdrop { background: #0006; }
.session-dialog input { width: 100%; }
.toast { position: fixed; right: 18px; bottom: 18px; background: #2f3b2c; color: #fff; padding: 10px 14px; border-radius: 8px; z-index: 20; max-width: 420px; }
.toast.err { background: var(--warn); }

@media (max-width: 820px) {
    .layout { grid-template-columns: 1fr; }
    .side { position: static; height: auto; display: flex; flex-wrap: wrap; gap: 4px; }
    .side .tab { width: auto; }
    .main { padding: 0 12px 40px; }
    .lv-layout { grid-template-columns: 1fr; }
}
