* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #1b1d26; --bg2: #23262f; --bg3: #2c2f3a; --line: #3a3e4d;
  --fg: #e8e8f0; --fg2: #9aa0b5; --acc: #57b0ff; --acc2: #ff8c57;
  --ok: #6fd66f; --danger: #ff6a6a;
}
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font-family: "Segoe UI", "Hiragino Sans", "Meiryo", sans-serif;
  font-size: 14px; display: flex; flex-direction: column; overflow: hidden;
}
#appHeader {
  display: flex; align-items: center; gap: 24px;
  padding: 8px 16px; background: var(--bg2); border-bottom: 2px solid var(--line);
  flex-shrink: 0;
}
#appHeader h1 { font-size: 18px; letter-spacing: 1px; color: var(--acc); }
#appHeader h1 .sub { font-size: 12px; color: var(--fg2); margin-left: 6px; }
#mainTabs { display: flex; gap: 4px; }
.hdr-actions { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.lang-label { font-size: 13px; color: var(--fg2); }
#langSelect {
  background: var(--bg3); color: var(--fg); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; cursor: pointer; font-size: 13px;
}
.tool-row { display: flex; gap: 6px; }
.tool-row button { flex: 1; }
.tool-row button.active { background: var(--acc2); color: #10121a; border-color: var(--acc2); }
.side-panel input[type=text] {
  background: var(--bg3); color: var(--fg); border: 1px solid var(--line);
  border-radius: 4px; padding: 5px; flex: 1; min-width: 0;
}
.ml-rows { display: flex; flex-direction: column; gap: 4px; }
.ml-row { display: flex; gap: 4px; align-items: center; }
.ml-row input[type=number], .ml-row select {
  background: var(--bg3); color: var(--fg); border: 1px solid var(--line);
  border-radius: 4px; padding: 4px; width: 70px; flex: 1; min-width: 0;
}
.ml-row .del { width: 26px; flex: none; padding: 4px 0; color: var(--danger); }
.tab-btn {
  padding: 8px 18px; background: var(--bg3); color: var(--fg2);
  border: 1px solid var(--line); border-radius: 6px 6px 0 0; cursor: pointer; font-size: 14px;
}
.tab-btn.active { background: var(--acc); color: #10121a; font-weight: bold; }
main { flex: 1; overflow: hidden; display: flex; }
.tab-page { display: none; flex: 1; overflow: auto; padding: 12px; }
.tab-page.active { display: block; }

.step-bar { display: flex; gap: 6px; margin-bottom: 10px; }
.step-btn {
  flex: 1; padding: 8px; background: var(--bg3); color: var(--fg2);
  border: 1px solid var(--line); border-radius: 6px; cursor: pointer; font-size: 13px;
}
.step-btn.active { background: var(--acc2); color: #10121a; font-weight: bold; }
.step-btn.done { border-color: var(--ok); }
.step-page { display: none; }
.step-page.active { display: block; }

.panel-row { display: flex; gap: 14px; align-items: flex-start; }
.canvas-area {
  flex: 1; min-width: 300px; display: flex; flex-direction: column; gap: 8px;
  align-items: center; justify-content: flex-start;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 14px;
  min-height: 420px; overflow: auto; max-height: calc(100vh - 150px);
}
.side-panel {
  width: 300px; flex-shrink: 0; background: var(--bg2);
  border: 1px solid var(--line); border-radius: 8px; padding: 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.side-panel.scroll { max-height: calc(100vh - 160px); overflow-y: auto; }
.side-panel h3 {
  font-size: 12px; color: var(--acc); border-bottom: 1px solid var(--line);
  padding-bottom: 3px; margin-top: 6px; letter-spacing: 1px;
}
.side-panel label { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--fg); }
.side-panel label input[type=range] { flex: 1; }
.side-panel select, .side-panel input[type=number] {
  background: var(--bg3); color: var(--fg); border: 1px solid var(--line);
  border-radius: 4px; padding: 5px; flex: 1;
}
button {
  background: var(--bg3); color: var(--fg); border: 1px solid var(--line);
  border-radius: 6px; padding: 7px 10px; cursor: pointer; font-size: 13px;
}
button:hover { border-color: var(--acc); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
.big-btn { padding: 10px; font-size: 14px; }
button.primary { background: var(--acc); color: #10121a; font-weight: bold; border-color: var(--acc); }
button.primary:hover { background: #7cc4ff; }
.hint { font-size: 11px; color: var(--fg2); line-height: 1.5; }

#dropZone {
  width: 100%; min-height: 380px; border: 2px dashed var(--line); border-radius: 10px;
  display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center;
  color: var(--fg2); cursor: pointer; text-align: center; font-size: 15px;
}
#dropZone.dragover { border-color: var(--acc); background: rgba(87,176,255,0.08); }

canvas.checker {
  background-image:
    linear-gradient(45deg, #333846 25%, transparent 25%, transparent 75%, #333846 75%),
    linear-gradient(45deg, #333846 25%, transparent 25%, transparent 75%, #333846 75%);
  background-size: 16px 16px; background-position: 0 0, 8px 8px; background-color: #262a35;
}
canvas { image-rendering: pixelated; }
#importCanvas, #segCanvas { max-width: 100%; max-height: 62vh; cursor: crosshair; }
#motionPreview { width: 320px; height: 320px; }
#sheetPreview { max-width: 100%; max-height: 70vh; }
#itemCanvas { width: 320px; height: 320px; }

.preview-stack { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.frame-strip { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.frame-strip canvas {
  width: 64px; height: 64px; border: 2px solid var(--line); border-radius: 4px;
  cursor: pointer; background: #262a35;
}
.frame-strip canvas.active { border-color: var(--acc2); }
.play-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

#partList { display: flex; flex-direction: column; gap: 3px; }
.part-row {
  display: flex; align-items: center; gap: 8px; padding: 4px 8px;
  border: 1px solid var(--line); border-radius: 5px; cursor: pointer; font-size: 12px;
}
.part-row.active { border-color: var(--acc2); background: rgba(255,140,87,0.12); }
.part-row .swatch { width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0; }
.part-row .pct { margin-left: auto; color: var(--fg2); font-size: 11px; }

.type-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.type-grid button { padding: 8px 2px; font-size: 11px; }
.type-grid button.active { border-color: var(--acc2); background: rgba(255,140,87,0.15); }

.editor-left { flex: 1; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.tool-bar {
  display: flex; gap: 5px; align-items: center; flex-wrap: wrap;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 8px;
}
.tool-bar .sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.tool-btn { width: 36px; height: 32px; font-size: 15px; }
.tool-btn.active { background: var(--acc); border-color: var(--acc); }
.tool-bar label { font-size: 12px; display: flex; align-items: center; gap: 4px; }
#editorWrap {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px; overflow: auto; max-height: 60vh;
  display: flex; justify-content: safe center; align-items: safe center;
}
#editorCanvas { cursor: crosshair; flex-shrink: 0; display: block; }
.palette-grid { display: flex; flex-wrap: wrap; gap: 4px; }
.palette-grid .pal-cell {
  width: 24px; height: 24px; border-radius: 4px; border: 2px solid var(--line); cursor: pointer;
}
.palette-grid .pal-cell.active { border-color: #fff; }
.palette-grid .pal-cell.trans {
  background-image: linear-gradient(45deg, #555 25%, transparent 25%, transparent 75%, #555 75%);
  background-size: 8px 8px; background-color: #333;
}
#edMini { width: 128px; height: 128px; }
/* 折り返しても高さがきちんと伸びるよう block + inline-block で組む
   （flex-wrap を flex カラム内に置くと折り返し行の高さが親に伝わらず
   下のボタンと重なるため） */
/* flex-shrink:0 が無いと親（flexカラム）が高さを1行に潰し、折り返し行が
   下のボタンへ食い込む */
.pal-swatches { margin: 4px 0; min-height: 16px; line-height: 0; font-size: 0; flex-shrink: 0; }
.pal-swatches .sw {
  display: inline-block; vertical-align: top; width: 16px; height: 16px;
  margin: 0 3px 3px 0; border-radius: 3px; border: 1px solid rgba(0,0,0,0.35);
}
.pal-swatches .empty { line-height: normal; font-size: 12px; color: var(--fg2); }

.help-body { max-width: 860px; margin: 0 auto; line-height: 1.9; }
.help-body h2 { color: var(--acc); margin: 22px 0 8px; font-size: 17px; }
.help-body h3 { color: var(--acc2); margin: 14px 0 6px; font-size: 14px; }
.help-body p, .help-body li { font-size: 13px; color: var(--fg); }
.help-body ul { padding-left: 22px; }
.help-body code { background: var(--bg3); padding: 1px 6px; border-radius: 4px; }
.help-body a { color: var(--acc); }
.help-body ol { padding-left: 22px; }
.help-body ol li { font-size: 13px; }
#aiPrompt {
  width: 100%; height: 340px; background: var(--bg3); color: var(--fg);
  border: 1px solid var(--line); border-radius: 6px; padding: 10px;
  font-family: Consolas, "Courier New", monospace; font-size: 12px;
  white-space: pre; overflow: auto; resize: vertical; margin: 10px 0;
}

/* モーション工房v2: 3面ポーズ編集 + タイムライン */
.ml-panes { display: flex; gap: 10px; width: 100%; justify-content: center; flex-wrap: wrap; }
.ml-pane { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.ml-pane .pane-title { font-size: 11px; color: var(--fg2); letter-spacing: 1px; }
canvas.ml-pose {
  width: 225px; border: 1px solid var(--line); border-radius: 6px; cursor: crosshair;
}
#mlBones { background: #20232c; }
#mlTimeline {
  width: 100%; max-width: 760px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg2); cursor: crosshair; touch-action: none;
}
#mlDetails summary { cursor: pointer; color: var(--fg2); font-size: 12px; padding: 4px 0; }
#mlDetails[open] summary { color: var(--acc); }
.cast-card {
  border: 1px solid var(--line); border-radius: 6px; padding: 6px;
  display: flex; flex-direction: column; gap: 5px; background: var(--bg3);
}
.cast-row { display: flex; gap: 4px; align-items: center; }
.cast-row input[type=text] { width: 80px; flex: 1; min-width: 0; }
.cast-row select { flex: 1; min-width: 0; }
.cast-adj { display: flex; gap: 6px; }
.cast-adj label {
  flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px;
  font-size: 11px; color: var(--fg2);
}
.cast-adj input[type=number] { width: 100%; padding: 3px; }

/* モーダル（目パターン編集など） */
.modal {
  position: fixed; inset: 0; background: rgba(10, 12, 18, 0.72);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal[hidden] { display: none; }
.modal-box {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 10px;
  padding: 18px; display: flex; flex-direction: column; gap: 10px; max-width: 480px;
}
.modal-box h3 { color: var(--acc); font-size: 14px; }
.eye-cols { display: flex; gap: 24px; justify-content: center; }
.eye-col { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.eye-grid { display: grid; gap: 2px; }
.eye-cell {
  width: 26px; height: 26px; border: 1px solid var(--line); border-radius: 3px; cursor: pointer;
  background-image: linear-gradient(45deg, #444 25%, transparent 25%, transparent 75%, #444 75%);
  background-size: 8px 8px; background-color: #2a2d38;
}
.eye-cell.filled { background-image: none; }
.eye-colors button.active { border-color: var(--acc2); background: rgba(255,140,87,0.15); }

/* 言語選択モーダル */
.lang-modal-box { align-items: center; text-align: center; }
.lang-choice { display: flex; gap: 12px; margin: 6px 0; flex-wrap: wrap; justify-content: center; }
.lang-choice button {
  padding: 14px 26px; font-size: 16px; min-width: 130px;
  background: var(--bg3); border: 1px solid var(--acc);
}
.lang-choice button:hover { background: var(--acc); color: #10121a; }
.eye-colors label { display: flex; align-items: center; gap: 4px; font-size: 12px; }

#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg3); border: 1px solid var(--acc); color: var(--fg);
  padding: 10px 22px; border-radius: 8px; opacity: 0; pointer-events: none;
  transition: opacity 0.3s; z-index: 99; font-size: 13px;
}
#toast.show { opacity: 1; }
.chk { user-select: none; }

/* 進捗オーバーレイ（重い処理中に表示。フリーズと処理中の区別用） */
#busy {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(10, 12, 18, 0.66);
  display: flex; align-items: center; justify-content: center;
}
#busy[hidden] { display: none; }
.busy-box {
  background: var(--bg2); border: 1px solid var(--acc);
  border-radius: 10px; padding: 20px 26px; min-width: 260px;
  display: flex; flex-direction: column; gap: 10px; align-items: center;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}
.busy-label { font-size: 14px; color: var(--fg); }
.busy-bar {
  width: 240px; height: 12px; border-radius: 6px; overflow: hidden;
  background: var(--bg3); border: 1px solid var(--line); position: relative;
}
.busy-fill {
  height: 100%; width: 0%; border-radius: 6px;
  background: linear-gradient(90deg, var(--acc), var(--acc2));
  transition: width 0.15s linear;
}
/* 不確定モード: transform アニメでスライド（コンポジタ駆動なので
   メインスレッドがブロックされていても動き続ける＝フリーズと見分けられる） */
.busy-bar.indet .busy-fill {
  width: 40%; transition: none;
  animation: busy-slide 1.1s linear infinite;
}
@keyframes busy-slide {
  from { transform: translateX(-110%); }
  to   { transform: translateX(260%); }
}
.busy-pct { font-size: 12px; color: var(--fg2); min-height: 14px; }

/* モーション工房: ボーン位置編集モード */
#mlBoneEdit.active { background: var(--acc2); color: #10121a; border-color: var(--acc2); }
.ml-pose.bone-edit { cursor: move; box-shadow: 0 0 0 2px var(--acc2); }
