:root {
  --bg: #0a0a0d;
  --panel: #14141a;
  --panel2: #1c1c24;
  --border: #3a3a46;
  --white: #f5f5f5;
  --accent: #ffd23f;
  --danger: #ff4d5e;
  --soul: #ff0033;
  --text: #e8e8ec;
  --textDim: #9a9aa6;
  font-family: 'Press Start 2P', monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); height: 100%; overflow: hidden; }
body { font-family: 'Segoe UI', 'Hiragino Sans', 'Noto Sans JP', sans-serif; font-size: 14px; }
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ---------- topbar ---------- */
#topbar {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border-bottom: 2px solid var(--border);
  padding: 8px 14px; height: 52px;
}
.brand { font-family: 'Press Start 2P', monospace; font-size: 11px; color: var(--white); white-space: nowrap; line-height: 1.4; }
.brand span { display: block; font-size: 7px; color: var(--accent); margin-top: 2px; }
#patternName {
  background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 6px 10px; border-radius: 4px; width: 180px; font-size: 13px;
}
.modeTabs { display: flex; gap: 6px; margin-left: 8px; }
.modeTab {
  background: var(--panel2); color: var(--textDim); border: 2px solid var(--border);
  padding: 8px 14px; border-radius: 4px; font-size: 12px; font-weight: 700;
}
.modeTab.active { background: var(--soul); color: #fff; border-color: var(--soul); }
.topActions { display: flex; gap: 6px; margin-left: auto; flex-wrap: wrap; }
.tbtn {
  background: var(--panel2); color: var(--text); border: 2px solid var(--border);
  padding: 7px 12px; border-radius: 4px; font-size: 12px; white-space: nowrap;
}
.tbtn:hover { border-color: var(--accent); }
.tbtn:disabled { opacity: 0.4; cursor: default; }
.tbtn.primary { background: var(--soul); border-color: var(--soul); color: #fff; }
.tbtn.danger { background: #3a1418; border-color: var(--danger); color: var(--danger); }

/* ---------- layout ---------- */
#app { height: calc(100vh - 52px); overflow: hidden; }
.view { display: none; height: 100%; }
.view.active { display: grid; }

#editorView {
  grid-template-columns: 240px 1fr 280px;
  grid-template-rows: 1fr 190px;
  gap: 8px; padding: 8px;
}
#boneListPanel { grid-column: 1; grid-row: 1; }
#stagePanel { grid-column: 2; grid-row: 1; }
#keyframePanel { grid-column: 3; grid-row: 1; }
#timelinePanel { grid-column: 1 / span 3; grid-row: 2; }

.panel {
  background: var(--panel); border: 2px solid var(--border); border-radius: 6px;
  padding: 10px; overflow-y: auto; display: flex; flex-direction: column;
}
.panel h2 {
  font-family: 'Press Start 2P', monospace; font-size: 10px; color: var(--accent);
  margin: 6px 0 8px; letter-spacing: 1px;
}
.panel h2:first-child { margin-top: 0; }
.hintSmall {
  display: block; font-family: 'Segoe UI', 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  font-size: 10px; color: var(--textDim); letter-spacing: 0; margin-top: 4px; text-transform: none;
}

/* ---------- bone list ---------- */
.boneList { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.boneRow {
  display: flex; align-items: center; gap: 8px; padding: 7px 8px;
  background: var(--panel2); border: 2px solid var(--border); border-radius: 4px; font-size: 12px;
}
.boneRow.selected { border-color: var(--accent); }
.swatch { width: 12px; height: 12px; border-radius: 3px; border: 1px solid #000; flex-shrink: 0; }
.bname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wide { width: 100%; padding: 9px; }
.addBoneRow { display: flex; gap: 6px; margin-bottom: 6px; }
.addBoneRow select {
  flex: 1; min-width: 0; background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 5px 6px; border-radius: 4px; font-size: 12px;
}
.addBoneRow button {
  background: var(--white); color: #14141a; border: 2px solid var(--white);
  padding: 7px 14px; border-radius: 4px; font-size: 12px; font-weight: 700; white-space: nowrap;
}
.addBoneRow button:hover { opacity: 0.9; }

.settingsGrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 11px; }
.settingsGrid label { display: flex; flex-direction: column; gap: 4px; color: var(--textDim); }
.settingsGrid input {
  background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 5px 6px; border-radius: 4px; font-size: 12px; width: 100%;
}

/* ---------- stage ---------- */
.stageToolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.timeReadout { font-size: 11px; color: var(--textDim); margin-left: auto; }
#stageCanvas {
  background: #000; border: 2px solid var(--border); border-radius: 4px;
  display: block; width: auto; height: auto; max-width: 100%; max-height: min(52vh, 460px);
  align-self: center; touch-action: none;
}
.stageHint { font-size: 11px; color: var(--textDim); margin-top: 8px; line-height: 1.5; }

/* ---------- keyframe panel ---------- */
.boneProps { display: flex; flex-direction: column; gap: 8px; font-size: 11px; margin-bottom: 10px; }
.boneProps.empty { color: var(--textDim); }
.boneProps label { display: flex; flex-direction: column; gap: 4px; color: var(--textDim); }
.boneProps input, .boneProps select {
  background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 5px 6px; border-radius: 4px; font-size: 12px; width: 100%;
}
.boneProps input[type=color] { padding: 2px; height: 30px; }
.propsHint { font-size: 10px; color: var(--textDim); line-height: 1.5; margin: -2px 0 0; }
.boneBtnRow { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.boneBtnRow button { flex: 1; min-width: 70px; }

.kfList { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 8px; }
.kfList.empty { color: var(--textDim); font-size: 11px; }
.kfChip {
  background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 4px 8px; border-radius: 10px; font-size: 10px;
}
.kfChip.selected { border-color: var(--accent); color: var(--accent); }
.kfEditor { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 11px; margin-bottom: 8px; }
.kfEditor.hidden { display: none; }
.kfEditor label { display: flex; flex-direction: column; gap: 4px; color: var(--textDim); }
.kfEditor input {
  background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 5px 6px; border-radius: 4px; font-size: 12px; width: 100%;
}
.kfEditorBtns { grid-column: 1 / span 2; display: flex; gap: 6px; }
.kfEditorBtns button { flex: 1; }
.kfQuickBtns { display: flex; gap: 6px; margin-bottom: 8px; }
.kfQuickBtns button { flex: 1; }

/* ---------- timeline ---------- */
#timelinePanel { overflow: auto; padding: 6px 10px; }
.timelineToolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; position: sticky; left: 0; width: fit-content; }
.timelineToolbar .tbtn { padding: 4px 10px; }
#timelineScroll { position: relative; min-width: 100%; width: max-content; }
#timelineRuler { position: relative; height: 20px; border-bottom: 1px solid var(--border); }
.tick { position: absolute; top: 0; font-size: 9px; color: var(--textDim); border-left: 1px solid var(--border); padding-left: 3px; height: 100%; }
#timelineTracks { position: relative; }
.track {
  position: relative; height: 30px; border-bottom: 1px solid #222;
  background: var(--panel2); margin-top: 2px; border-radius: 3px; cursor: grab; touch-action: none;
}
.track.selected { outline: 1px solid var(--accent); }
#timelineTracks.dragging .track { cursor: grabbing; }
.trackLabel {
  position: sticky; left: 0; display: inline-block; font-size: 9px; color: var(--textDim);
  background: var(--panel2); padding: 2px 6px; border-right: 1px solid var(--border);
  z-index: 1;
}
.kfMarker {
  position: absolute; top: 8px; width: 14px; height: 14px; background: var(--white);
  border: 2px solid #000; transform: translateX(-50%) rotate(45deg); cursor: grab;
}
.kfMarker.selected { background: var(--accent); }
#playhead {
  position: absolute; top: 0; width: 2px; background: var(--soul); pointer-events: none; z-index: 2;
}

/* ---------- play view ---------- */
#playView.active { display: flex; justify-content: center; }
.playWrap { max-width: 700px; width: 100%; padding: 14px; display: flex; flex-direction: column; align-items: center; }
.hpRow { width: 100%; display: flex; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 11px; }
.hpLabel { color: var(--accent); font-family: 'Press Start 2P', monospace; font-size: 10px; }
.hpBarOuter { flex: 1; height: 16px; background: #400; border: 2px solid var(--white); border-radius: 3px; overflow: hidden; }
#hpBarInner { height: 100%; width: 100%; background: linear-gradient(90deg,#ffd23f,#ffb000); transition: width 0.15s; }
#hpText { color: var(--textDim); white-space: nowrap; }
#playTimer { color: var(--textDim); white-space: nowrap; }
#playCanvas {
  background: #000; border: 2px solid var(--white); border-radius: 4px;
  display: block; width: auto; height: auto; max-width: min(100%, 640px); max-height: 52vh;
  touch-action: none;
}

#touchPad { display: none; }

.playControls { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 320px; margin-top: 14px; }
.playControls .primary { background: var(--soul); border-color: var(--soul); color: #fff; }
.playHint { font-size: 11px; color: var(--textDim); margin-top: 10px; text-align: center; }

.playOverlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.85); display: flex; align-items: center; justify-content: center; z-index: 20;
}
.playOverlay.hidden { display: none; }
.playOverlayCard { text-align: center; }
.playOverlayCard h2 { font-family: 'Press Start 2P', monospace; font-size: 24px; margin-bottom: 14px; }
.playOverlayCard h2.win { color: var(--accent); }
.playOverlayCard h2.lose { color: var(--danger); }
.playOverlayCard p { color: var(--textDim); margin-bottom: 20px; font-size: 13px; }
.overlayBtns { display: flex; flex-direction: column; gap: 8px; width: 220px; margin: 0 auto; }
.overlayBtns .primary { background: var(--soul); border-color: var(--soul); color: #fff; }

.hidden { display: none !important; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.75); display: flex; align-items: center; justify-content: center; z-index: 30; }
.modal.hidden { display: none; }
.modalCard {
  background: var(--panel); border: 2px solid var(--border); border-radius: 8px;
  width: min(560px, 92vw); max-height: 80vh; overflow-y: auto; padding: 16px;
}
.modalHead { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modalHead h2 { font-family: 'Press Start 2P', monospace; font-size: 13px; color: var(--accent); }
.libTabs { display: flex; gap: 6px; }
.libTab {
  background: var(--panel2); color: var(--textDim); border: 2px solid var(--border);
  padding: 7px 12px; border-radius: 4px; font-size: 12px;
}
.libTab.active { background: var(--soul); color: #fff; border-color: var(--soul); }
.libList.hidden { display: none; }
.libEmpty { color: var(--textDim); font-size: 12px; }
.libRow {
  display: flex; align-items: center; gap: 8px; padding: 10px; margin-bottom: 8px;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 6px;
}
.libInfo { display: flex; flex-direction: column; gap: 3px; flex: 1; min-width: 0; }
.libInfo b { font-size: 13px; }
.libInfo span { font-size: 11px; color: var(--textDim); }

.helpBody h3 { color: var(--accent); font-size: 13px; margin: 14px 0 6px; }
.helpBody h3:first-child { margin-top: 0; }
.helpBody p { font-size: 12.5px; line-height: 1.7; color: var(--text); }

.publishCard { width: min(420px, 92vw); }
.publishLabel {
  display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--textDim);
  margin: 14px 0;
}
.publishLabel input {
  background: var(--panel2); border: 2px solid var(--border); color: var(--text);
  padding: 8px 10px; border-radius: 4px; font-size: 13px;
}
.libRow.communityRow { align-items: flex-start; }
.libRow .libAuthor { font-size: 10px; color: var(--accent); }

/* ---------- toast ---------- */
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #000; color: #fff; border: 2px solid var(--accent); padding: 10px 18px;
  border-radius: 6px; font-size: 12px; opacity: 0; pointer-events: none; transition: all 0.25s; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- responsive ---------- */
@media (max-width: 980px) {
  html, body { overflow: auto; }
  #app { height: auto; overflow: visible; }
  #editorView {
    grid-template-columns: 1fr; grid-template-rows: auto auto auto auto;
  }
  #boneListPanel { grid-column: 1; grid-row: 2; }
  #stagePanel { grid-column: 1; grid-row: 1; }
  #keyframePanel { grid-column: 1; grid-row: 3; }
  #timelinePanel { grid-column: 1; grid-row: 4; max-height: 220px; }
  #topbar { flex-wrap: wrap; height: auto; }
  #patternName { width: 120px; }
}

@media (max-width: 700px), (pointer: coarse) {
  #touchPad {
    display: grid; grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(2, 56px);
    gap: 4px; margin-top: 10px; justify-content: center;
  }
  .tpBtn {
    background: var(--panel2); border: 2px solid var(--border); color: var(--white);
    border-radius: 8px; font-size: 18px; touch-action: none; user-select: none;
  }
  .tpUp { grid-column: 2; grid-row: 1; }
  .tpLeft { grid-column: 1; grid-row: 2; }
  .tpRight { grid-column: 3; grid-row: 2; }
  .tpDown { grid-column: 2; grid-row: 2; }

  /* 指でも操作しやすいようタップ領域を拡大 */
  .kfMarker { width: 20px; height: 20px; top: 5px; touch-action: none; }
  .track { height: 34px; }
  .trackLabel { font-size: 10px; padding: 5px 8px; }
  .tbtn { padding: 10px 14px; font-size: 13px; }
  .boneBtnRow button, .kfEditorBtns button, .kfQuickBtns button { min-height: 42px; }
  .boneRow { padding: 10px 10px; }
  .addBoneRow button { padding: 10px 14px; }
  .libRow button { min-height: 40px; }
}
