:root {
  --bg: #f6f3ee;
  --panel: #ffffff;
  --panel-2: #fbf8f3;
  --line: #e7dfd4;
  --text: #3a3230;
  --muted: #8c817b;
  --accent: #ff597e;
  --accent-soft: #ffe6ec;
  --accent-2: #7fc8d6;
  --shadow: 0 10px 30px rgba(80, 60, 50, .08);
  --radius: 14px;
  --h: 100vh;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Noto Sans SC", "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", system-ui, -apple-system, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  min-height: var(--h);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 18px;
  background: linear-gradient(180deg, #fff 0%, var(--panel-2) 100%);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 2px 10px rgba(80, 60, 50, .04);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-icon {
  width: 42px; height: 42px; border-radius: 10px; object-fit: cover;
  background: var(--accent-soft); border: 1px solid var(--line);
}
.brand-text { min-width: 0; }
.brand h1 { margin: 0; font-size: 17px; letter-spacing: .3px; white-space: nowrap; }
.brand-sub { margin: 1px 0 0; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  padding: 7px 12px;
  border-radius: 10px;
  font-size: 13px;
  cursor: pointer;
  transition: .15s ease;
  white-space: nowrap;
}
.btn:hover { border-color: #d9cec2; background: #fffdfa; transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; box-shadow: 0 6px 14px rgba(255, 89, 126, .28); }
.btn.primary:hover { background: #ff4a72; border-color: #ff4a72; }
.btn.small { padding: 5px 9px; font-size: 12px; border-radius: 8px; }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }

/* ---------- layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 232px minmax(360px, 1fr) 380px;
  gap: 14px;
  padding: 14px;
  align-items: stretch;
  min-height: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.panel-head, .ctrl-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  padding: 12px 14px 8px;
  border-bottom: 1px dashed var(--line);
}
.panel-head h2, .ctrl-head h2 { margin: 0; font-size: 14px; }
.hint, .muted { color: var(--muted); font-size: 12px; }
.tiny { font-size: 11px; }
.badge { font-size: 11px; color: var(--accent); background: var(--accent-soft); padding: 1px 8px; border-radius: 999px; }

/* ---------- categories ---------- */
.category-list {
  list-style: none; margin: 0; padding: 8px; overflow-y: auto; flex: 1;
  display: flex; flex-direction: column; gap: 4px;
}
.cat {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 8px; border-radius: 10px; cursor: pointer;
  border: 1px solid transparent; background: transparent; text-align: left; width: 100%;
  color: inherit; font: inherit;
}
.cat:hover { background: var(--panel-2); border-color: var(--line); }
.cat.active { background: var(--accent-soft); border-color: #ffc5d3; }
.cat img { width: 34px; height: 34px; object-fit: contain; border-radius: 8px; background: #f2ece4; flex: none; }
.cat .cat-names { display: flex; flex-direction: column; min-width: 0; }
.cat .cat-zh { font-size: 13px; font-weight: 600; }
.cat .cat-jp { font-size: 11px; color: var(--muted); }
.cat .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); margin-left: auto; opacity: 0; flex: none; }
.cat.modified .dot { opacity: 1; }

/* ---------- stage ---------- */
.stage-wrap { display: flex; align-items: center; justify-content: center; min-height: 0; }
.stage-inner { display: flex; flex-direction: column; gap: 10px; align-items: center; width: 100%; height: 100%; justify-content: center; }

.canvas-frame {
  position: relative;
  width: min(100%, 70vh, 700px);
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
  background: #fff;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}
.canvas-frame.checker {
  background-image:
    linear-gradient(45deg, #efe9e1 25%, transparent 25%),
    linear-gradient(-45deg, #efe9e1 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #efe9e1 75%),
    linear-gradient(-45deg, transparent 75%, #efe9e1 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-color: #fff;
}
.canvas-frame canvas { width: 100%; height: 100%; display: block; }
.canvas-frame.can-drag canvas { cursor: grab; }
.canvas-frame.dragging canvas { cursor: grabbing; }

.canvas-loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 10px;
  background: rgba(255, 255, 255, .72); font-size: 13px; color: var(--muted);
  backdrop-filter: blur(2px); transition: opacity .25s ease; pointer-events: none;
}
.canvas-loading.hidden { opacity: 0; }
.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid #f0d9df; border-top-color: var(--accent); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.stage-bar { display: flex; align-items: center; gap: 14px; font-size: 12px; color: var(--muted); flex-wrap: wrap; justify-content: center; }
.kbd-hint { max-width: 460px; text-align: center; }
.check { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }

/* ---------- controls ---------- */
.controls { display: flex; flex-direction: column; overflow-y: auto; overflow-x: hidden; }
.ctrl-block { padding: 12px 14px; border-bottom: 1px dashed var(--line); flex: none; }
.ctrl-block.items { flex: 0 0 auto; min-height: 0; max-height: 38%; display: flex; flex-direction: column; }
.ctrl-block.overview { flex: 0 0 auto; display: flex; flex-direction: column; }

.overview-grid { flex: 0 0 auto; max-height: 17vh; overflow-y: auto; display: flex; flex-wrap: wrap; gap: 6px; align-content: flex-start; padding-right: 2px; }
.ov-cell {
  width: 38px; height: 38px; border-radius: 9px; border: 1px solid var(--line); background: #faf7f2;
  cursor: pointer; padding: 1px; display: flex; align-items: center; justify-content: center; position: relative;
}
.ov-cell img { width: 100%; height: 100%; object-fit: contain; }
.ov-cell:hover { border-color: #e0c9d1; }
.ov-cell.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.ov-cell.empty { color: #c9beb6; font-size: 14px; }
.ov-cell .ov-dot { position: absolute; right: -1px; top: -1px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.ov-cell .ov-name {
  position: absolute; left: 50%; bottom: -22px; transform: translateX(-50%);
  background: rgba(58,50,48,.92); color: #fff; font-size: 11px; padding: 2px 6px; border-radius: 6px;
  white-space: nowrap; opacity: 0; pointer-events: none; transition: .12s; z-index: 5;
}
.ov-cell:hover .ov-name { opacity: 1; }
.ctrl-block:last-child { border-bottom: none; }
.ctrl-title { display: flex; align-items: baseline; justify-content: space-between; font-size: 13px; font-weight: 600; margin-bottom: 8px; }

.item-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  gap: 8px;
  overflow-y: auto;
  padding: 2px 4px 6px 2px;
  align-content: start;
  flex: 0 1 auto;
  min-height: 0;
  max-height: 100%;
}
.item {
  position: relative;
  border: 2px solid var(--line);
  background: #faf7f2;
  border-radius: 10px;
  padding: 2px;
  cursor: pointer;
  aspect-ratio: 1 / 1;
  display: flex; align-items: center; justify-content: center;
  transition: .12s ease;
}
.item img { width: 100%; height: 100%; object-fit: contain; }
.item:hover { border-color: #e0c9d1; transform: translateY(-1px); }
.item.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.item.none { color: var(--muted); font-size: 22px; }

.color-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 30px; height: 30px; border-radius: 9px; cursor: pointer;
  border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); transition: .12s ease;
}
.swatch:hover { transform: scale(1.06); }
.swatch.active { box-shadow: 0 0 0 2px var(--accent); transform: scale(1.06); }

.adjust { display: flex; gap: 18px; align-items: center; }
.ctrl-block.tips { margin-top: auto; border-top: 1px dashed var(--line); border-bottom: none; background: var(--panel-2); }
.ctrl-block.tips p { margin: 0 0 4px; font-size: 11.5px; line-height: 1.6; color: var(--muted); }
.ctrl-block.tips p:last-child { margin-bottom: 0; }
.ctrl-block.tips b { color: var(--text); font-weight: 600; }
.dpad { display: grid; grid-template-columns: repeat(3, 34px); grid-template-rows: repeat(3, 34px); gap: 4px; }
.dbtn {
  border: 1px solid var(--line); background: #fff; border-radius: 9px; cursor: pointer;
  font-size: 12px; color: var(--text); display: flex; align-items: center; justify-content: center;
}
.dbtn:hover:not(:disabled) { background: var(--panel-2); border-color: #d9cec2; }
.dbtn:disabled { opacity: .35; cursor: not-allowed; }
.dbtn.up { grid-area: 1 / 2; }
.dbtn.left { grid-area: 2 / 1; }
.dbtn.reset { grid-area: 2 / 2; color: var(--accent); }
.dbtn.right { grid-area: 2 / 3; }
.dbtn.down { grid-area: 3 / 2; }

.adjust-extra { display: flex; flex-direction: column; gap: 6px; }
.size-row, .rot-row { display: flex; align-items: center; gap: 8px; }

/* ---------- dialogs ---------- */
dialog {
  border: none; border-radius: 16px; padding: 0; box-shadow: 0 24px 60px rgba(60, 40, 40, .25);
  max-width: min(860px, 92vw); width: 100%;
}
dialog::backdrop { background: rgba(58, 50, 48, .42); backdrop-filter: blur(2px); }
.dialog-body { padding: 18px 20px; }
.dialog-body h3 { margin: 0 0 6px; font-size: 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.slot-list { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.slot {
  display: flex; align-items: center; gap: 12px; border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; background: var(--panel-2);
}
.slot .slot-name { font-weight: 600; }
.slot .slot-meta { font-size: 12px; color: var(--muted); }
.slot .spacer { flex: 1; }

.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 10px; }
.info-grid h4 { margin: 0 0 6px; font-size: 13px; color: var(--muted); }
.info-grid pre {
  white-space: pre-wrap; word-break: break-word; font: 12px/1.7 inherit;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px; margin: 0; max-height: 46vh; overflow: auto;
}

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 20px);
  background: rgba(58, 50, 48, .92); color: #fff; padding: 9px 16px; border-radius: 999px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: .22s ease; z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- short viewports ---------- */
@media (max-height: 900px) {
  .ctrl-block.items { max-height: 40%; }
  .overview-grid { max-height: 18vh; }
  .ctrl-block.overview { min-height: 120px; }
  .canvas-frame { width: min(100%, 64vh, 620px); }
  .stage-bar .kbd-hint { display: none; }
}

/* ---------- responsive: narrower desktop / tablet ---------- */
@media (max-width: 1180px) {
  .layout { grid-template-columns: 196px minmax(320px, 1fr) 320px; }
  .item-grid { min-height: 100px; }
}
@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto auto auto; }
  .categories { max-height: 220px; }
  .category-list { flex-direction: row; flex-wrap: wrap; }
  .cat { width: auto; }
  .canvas-frame { width: min(100%, 70vh); }
  .controls { max-height: none; }
  .item-grid { min-height: 160px; }
}

/* ---------- footer & notice ---------- */
.page-footer {
  flex: none;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 7px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel-2);
  font-size: 11.5px; color: var(--muted);
  flex-wrap: wrap;
}
.page-footer b { color: var(--text); font-weight: 600; }
.foot-links { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.linkbtn {
  border: none; background: none; padding: 0; cursor: pointer;
  font: inherit; color: var(--accent); text-decoration: none;
}
.linkbtn:hover { text-decoration: underline; }

dialog.wide { max-width: min(1000px, 94vw); }
.notice-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; margin: 12px 0 4px; }
.notice-grid section { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.notice-grid h4 { margin: 0 0 6px; font-size: 12px; color: var(--muted); letter-spacing: .5px; }
.notice-grid p { margin: 0 0 8px; font-size: 12.5px; line-height: 1.75; }
.notice-grid p:last-child { margin-bottom: 0; }
.notice-grid a { color: var(--accent); }
@media (max-width: 900px) { .notice-grid { grid-template-columns: 1fr; } }
